// Verify that our example data in the demo package is correct and
// that we won't break the existing test code with updated outputs.
// This test has two phases.  The first generates the code we want
// to emit and the second performs a complete type-checking of the
// demo package to make sure that any changes to the generated
// code will compile.
Verify that our example data in the demo package is correct and
that we won't break the existing test code with updated outputs.
This test has two phases.  The first generates the code we want
to emit and the second performs a complete type-checking of the
demo package to make sure that any changes to the generated
code will compile.
// Verify that our example data in the demo package is correct and // that we won ' t break the existing test code with updated outputs . // This test has two phases . The first generates the code we want // to emit and the second performs a complete type - checking of the // demo package to make sure that any changes to the generated // code will compile .
func TestExampleData(t *testing.T) {
	for name, cfg := range configs {
		t.Run(name, func(t *testing.T) {
			a := assert.New(t)
			outputs := make(map[string][]byte)
			newGeneration := func() (*generation, string, error) {
				g, err := newGenerationForTesting(cfg, outputs)
				if err != nil {
					return nil, "", err
				}
				if cfg.union == "" {
					return g, cfg.typeNames[0], nil
				}
				return g, cfg.union, nil
			}
			g, prefix, err := newGeneration()
			if !a.NoError(err) {
				return
			}
			if !a.NoError(g.Execute()) {
				for k, v := range outputs {
					t.Logf("%s\n%s\n\n\n", k, string(v))
				}
				return
			}
			expectTarget := true
			v := g.visitation
			a.Equal(prefix, v.Root.String(), "wrong intfname")
			switch name {
			case "single":
				a.Len(v.Types, 16)
				v.checkStructInfo(a, "ContainerType", "ByRef", "ByRefPtr", "ByRefSlice", "ByRefPtrSlice",
					"ByVal", "ByValPtr", "ByValSlice", "ByValPtrSlice", "Container", "AnotherTarget",
					"AnotherTargetPtr", "EmbedsTarget", "EmbedsTargetPtr", "TargetSlice",
					"InterfacePtrSlice", "NamedTargets")
			case "unionReachable":
				a.Len(v.Types, 22)
				v.checkStructInfo(a, "ContainerType", "ByRef", "ByRefPtr", "ByRefSlice", "ByRefPtrSlice",
					"ByVal", "ByValPtr", "ByValSlice", "ByValPtrSlice", "Container", "AnotherTarget",
					"AnotherTargetPtr", "EmbedsTarget", "EmbedsTargetPtr", "TargetSlice",
					"InterfacePtrSlice", "NamedTargets", "UnionableType", "ReachableType")
				v.checkStructInfo(a, "ReachableType")
				a.Equal(cfg.union, v.Root.Union)
			case "union":
				a.Len(v.Types, 20)
				v.checkStructInfo(a, "ContainerType", "ByRef", "ByRefPtr", "ByRefSlice", "ByRefPtrSlice",
					"ByVal", "ByValPtr", "ByValSlice", "ByValPtrSlice", "Container", "AnotherTarget",
					"AnotherTargetPtr", "EmbedsTarget", "EmbedsTargetPtr", "TargetSlice", "InterfacePtrSlice",
					"NamedTargets", "UnionableType")
				v.checkStructInfo(a, "UnionableType")
				a.Equal(cfg.union, v.Root.Union)
			case "structUnion":
				a.Len(v.Types, 11)
				v.checkStructInfo(a, "ContainerType", "ByRef", "ByRefPtr", "ByRefSlice", "ByRefPtrSlice",
					"ByVal", "ByValPtr", "ByValSlice", "ByValPtrSlice", "Container")
				a.Equal(cfg.union, v.Root.Union)
				expectTarget = false
			case "structUnionReachable":
				a.Len(v.Types, 21)
				v.checkStructInfo(a, "ContainerType", "ByRef", "ByRefPtr", "ByRefSlice", "ByRefPtrSlice",
					"ByVal", "ByValPtr", "ByValSlice", "ByValPtrSlice", "Container", "AnotherTarget",
					"AnotherTargetPtr", "EmbedsTarget", "EmbedsTargetPtr", "TargetSlice",
					"InterfacePtrSlice", "NamedTargets", "UnionableType", "ReachableType")
				v.checkStructInfo(a, "ReachableType")
				a.Equal(cfg.union, v.Root.Union)
				expectTarget = false
			default:
				a.Fail("unknown test configuration", name)
			}
			v.checkStructInfo(a, "ByValType")
			v.checkStructInfo(a, "ByRefType")
			if expectTarget {
				v.checkVisitableInterface(a, "Target")
				v.checkVisitableInterface(a, "EmbedsTarget")
			}
			cfg := g.packageConfig()
			cfg.Mode = packages.LoadAllSyntax
			cfg.Overlay = outputs
			pkgs, err := packages.Load(cfg, ".")
			if a.NoError(err) {
				for _, pkg := range pkgs {
					a.Nil(pkg.Errors)
				}
			}
		})
	}
}
func TestExampleData ( t * testing . T ) { for name , cfg := range configs { t . Run ( name , func ( t * testing . T ) { a := assert . New ( t ) outputs := make ( map [ string ] [ ] byte ) newGeneration := func ( ) ( * generation , string , error ) { g , err := newGenerationForTesting ( cfg , outputs ) if err != nil { return nil , " " , err } if cfg . union == " " { return g , cfg . typeNames [ 0 ] , nil } return g , cfg . union , nil } g , prefix , err := newGeneration ( ) if ! a . NoError ( err ) { return } if ! a . NoError ( g . Execute ( ) ) { for k , v := range outputs { t . Logf ( " \n \n \n \n " , k , string ( v ) ) } return } expectTarget := true v := g . visitation a . Equal ( prefix , v . Root . String ( ) , " " ) switch name { case " " : a . Len ( v . Types , 16 ) v . checkStructInfo ( a , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " ) case " " : a . Len ( v . Types , 22 ) v . checkStructInfo ( a , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " ) v . checkStructInfo ( a , " " ) a . Equal ( cfg . union , v . Root . Union ) case " " : a . Len ( v . Types , 20 ) v . checkStructInfo ( a , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " ) v . checkStructInfo ( a , " " ) a . Equal ( cfg . union , v . Root . Union ) case " " : a . Len ( v . Types , 11 ) v . checkStructInfo ( a , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " ) a . Equal ( cfg . union , v . Root . Union ) expectTarget = false case " " : a . Len ( v . Types , 21 ) v . checkStructInfo ( a , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " ) v . checkStructInfo ( a , " " ) a . Equal ( cfg . union , v . Root . Union ) expectTarget = false default : a . Fail ( " " , name ) } v . checkStructInfo ( a , " " ) v . checkStructInfo ( a , " " ) if expectTarget { v . checkVisitableInterface ( a , " " ) v . checkVisitableInterface ( a , " " ) } cfg := g . packageConfig ( ) cfg . Mode = packages . LoadAllSyntax cfg . Overlay = outputs pkgs , err := packages . Load ( cfg , " " ) if a . NoError ( err ) { for _ , pkg := range pkgs { a . Nil ( pkg . Errors ) } } } ) } }
Verify that our example data in the demo package is correct and
that we won't break the existing test code with updated outputs.
Verify that our example data in the demo package is correct and that we won ' t break the existing test code with updated outputs .