Esempio n. 1
0
func TestCanUseDefFromImport(t *testing.T) {
	assert := assert.New(t)
	ds := datas.NewDataStore(chunks.NewMemoryStore())

	dir, err := ioutil.TempDir("", "")
	assert.NoError(err)
	defer os.RemoveAll(dir)

	byPathNomDL := filepath.Join(dir, "filedep.noms")
	err = ioutil.WriteFile(byPathNomDL, []byte("struct FromFile{i:Int8}"), 0600)
	assert.NoError(err)

	r1 := strings.NewReader(`
		struct A {
			B: B
		}
		struct B {
			X: Int64
		}`)
	pkg1 := pkg.ParseNomDL("test1", r1, dir, ds)
	pkgRef1 := ds.WriteValue(pkg1.Package).TargetRef()

	r2 := strings.NewReader(fmt.Sprintf(`
		alias Other = import "%s"
		struct C {
			C: Map<Int64, Other.A>
		}
		`, pkgRef1))
	pkg2 := pkg.ParseNomDL("test2", r2, dir, ds)
	gen2 := newCodeGen(nil, "test2", "go", map[string]bool{}, depsMap{pkg1.Ref(): pkg1.Package}, pkg2)

	assert.True(gen2.canUseDef(pkg2.Types()[0], gen2.pkg.Package))
}
Esempio n. 2
0
func parsePackageFile(packageName string, in string, pkgDS dataset.Dataset) pkg.Parsed {
	inFile, err := os.Open(in)
	d.Chk.NoError(err)
	defer inFile.Close()

	return pkg.ParseNomDL(packageName, inFile, filepath.Dir(in), pkgDS.Store())
}
Esempio n. 3
0
func assertOutput(inPath, lang, goldenPath string, t *testing.T) {
	assert := assert.New(t)
	emptyDS := datas.NewDataStore(chunks.NewMemoryStore()) // Will be DataStore containing imports

	depsDir, err := ioutil.TempDir("", "")
	assert.NoError(err)
	defer os.RemoveAll(depsDir)

	inFile, err := os.Open(inPath)
	assert.NoError(err)
	defer inFile.Close()

	goldenFile, err := os.Open(goldenPath)
	assert.NoError(err)
	defer goldenFile.Close()
	goldenBytes, err := ioutil.ReadAll(goldenFile)
	d.Chk.NoError(err)

	var buf bytes.Buffer
	pkg := pkg.ParseNomDL("gen", inFile, filepath.Dir(inPath), emptyDS)
	written := map[string]bool{}
	gen := newCodeGen(&buf, getBareFileName(inPath), lang, written, depsMap{}, pkg)
	gen.WritePackage()

	bs := buf.Bytes()
	if lang == "go" {
		bs, err = imports.Process("", bs, nil)
		d.Chk.NoError(err)
	}

	assert.Equal(string(goldenBytes), string(bs), "%s did not generate the same string", inPath)
}
Esempio n. 4
0
func TestCanUseDef(t *testing.T) {
	assert := assert.New(t)
	emptyDS := datas.NewDataStore(chunks.NewMemoryStore())

	depsDir, err := ioutil.TempDir("", "")
	assert.NoError(err)
	defer os.RemoveAll(depsDir)

	assertCanUseDef := func(s string, using, named bool) {
		pkg := pkg.ParseNomDL("fakefile", bytes.NewBufferString(s), "", emptyDS)
		gen := newCodeGen(nil, "fakefile", "go", map[string]bool{}, depsMap{}, pkg)
		for _, t := range pkg.UsingDeclarations {
			assert.Equal(using, gen.canUseDef(t, gen.pkg.Package))
		}
		for _, t := range pkg.Types() {
			assert.Equal(named, gen.canUseDef(t, gen.pkg.Package))
		}
	}

	good := `
		using List<Int8>
		using Set<Int8>
		using Map<Int8, Int8>
		using Map<Int8, Set<Int8>>
		using Map<Int8, Map<Int8, Int8>>

		struct Simple {
			x: Int8
		}
		using Set<Simple>
		using Map<Simple, Int8>
		using Map<Simple, Simple>
		`
	assertCanUseDef(good, true, true)

	good = `
		struct Tree {
		  children: List<Tree>
		}
		`
	assertCanUseDef(good, true, true)

	bad := `
		struct WithList {
			x: List<Int8>
		}
		using Set<WithList>
		using Map<WithList, Int8>

		struct WithSet {
			x: Set<Int8>
		}
		using Set<WithSet>
		using Map<WithSet, Int8>

		struct WithMap {
			x: Map<Int8, Int8>
		}
		using Set<WithMap>
		using Map<WithMap, Int8>
		`
	assertCanUseDef(bad, false, true)

	bad = `
		struct Commit {
			value: Value
			parents: Set<Commit>
		}
		`
	assertCanUseDef(bad, false, false)

	bad = `
		Set<Set<Int8>>
		Set<Map<Int8, Int8>>
		Set<List<Int8>>
		Map<Set<Int8>, Int8>
		Map<Map<Int8, Int8>, Int8>
		Map<List<Int8>, Int8>
		`

	for _, line := range strings.Split(bad, "\n") {
		if strings.TrimSpace(line) == "" {
			continue
		}
		assertCanUseDef(fmt.Sprintf("using %s", line), false, false)
		assertCanUseDef(fmt.Sprintf("struct S { x: %s }", line), false, false)
	}
}