func TestGenerateDeps(t *testing.T) { assert := assert.New(t) ds := datas.NewDataStore(chunks.NewMemoryStore()) dir, err := ioutil.TempDir("", "codegen_test_") assert.NoError(err) defer os.RemoveAll(dir) leaf1 := types.NewPackage([]types.Type{types.MakeEnumType("e1", "a", "b")}, []ref.Ref{}) leaf1Ref := ds.WriteValue(leaf1).TargetRef() leaf2 := types.NewPackage([]types.Type{types.MakePrimitiveType(types.BoolKind)}, []ref.Ref{}) leaf2Ref := ds.WriteValue(leaf2).TargetRef() depender := types.NewPackage([]types.Type{}, []ref.Ref{leaf1Ref}) dependerRef := ds.WriteValue(depender).TargetRef() top := types.NewPackage([]types.Type{}, []ref.Ref{leaf2Ref, dependerRef}) types.RegisterPackage(&top) localPkgs := refSet{top.Ref(): true} generateDepCode(filepath.Base(dir), dir, map[string]bool{}, top, localPkgs, ds) leaf1Path := filepath.Join(dir, code.ToTag(leaf1.Ref())+".go") leaf2Path := filepath.Join(dir, code.ToTag(leaf2.Ref())+".go") leaf3Path := filepath.Join(dir, code.ToTag(depender.Ref())+".go") _, err = os.Stat(leaf1Path) assert.NoError(err) _, err = os.Stat(leaf2Path) assert.NoError(err) _, err = os.Stat(leaf3Path) assert.NoError(err) }
func TestSkipDuplicateTypes(t *testing.T) { assert := assert.New(t) dir, err := ioutil.TempDir("", "codegen_test_") assert.NoError(err) defer os.RemoveAll(dir) leaf1 := types.NewPackage([]types.Type{ types.MakeEnumType("E1", "a", "b"), types.MakeStructType("S1", []types.Field{ types.Field{"f", types.MakeCompoundType(types.ListKind, types.MakePrimitiveType(types.Uint16Kind)), false}, types.Field{"e", types.MakeType(ref.Ref{}, 0), false}, }, types.Choices{}), }, []ref.Ref{}) leaf2 := types.NewPackage([]types.Type{ types.MakeStructType("S2", []types.Field{ types.Field{"f", types.MakeCompoundType(types.ListKind, types.MakePrimitiveType(types.Uint16Kind)), false}, }, types.Choices{}), }, []ref.Ref{}) written := map[string]bool{} tag1 := code.ToTag(leaf1.Ref()) leaf1Path := filepath.Join(dir, tag1+".go") generateAndEmit(tag1, leaf1Path, written, depsMap{}, pkg.Parsed{Package: leaf1, Name: "p"}) tag2 := code.ToTag(leaf2.Ref()) leaf2Path := filepath.Join(dir, tag2+".go") generateAndEmit(tag2, leaf2Path, written, depsMap{}, pkg.Parsed{Package: leaf2, Name: "p"}) code, err := ioutil.ReadFile(leaf2Path) assert.NoError(err) assert.NotContains(string(code), "type ListOfUint16") }
func generateDepCode(packageName, outDir string, written map[string]bool, p types.Package, localPkgs refSet, vr types.ValueReader) depsMap { deps := depsMap{} for _, r := range p.Dependencies() { p := vr.ReadValue(r).(types.Package) pDeps := generateDepCode(packageName, outDir, written, p, localPkgs, vr) tag := code.ToTag(p.Ref()) parsed := pkg.Parsed{Package: p, Name: packageName} if !localPkgs[parsed.Ref()] { generateAndEmit(tag, filepath.Join(outDir, tag+"."+*outputLang), written, pDeps, parsed) localPkgs[parsed.Ref()] = true } for depRef, dep := range pDeps { deps[depRef] = dep } deps[r] = p } return deps }