| 85 | } |
| 86 | |
| 87 | func TestStructPkgPath(t *testing.T) { |
| 88 | fooPath := filepath.Join("gen", "foo", "foo.go") |
| 89 | recursiveFooPath := filepath.Join("gen", "foo", "recursive_foo.go") |
| 90 | barPath := filepath.Join("gen", "bar", "bar.go") |
| 91 | bazPath := filepath.Join("gen", "baz", "baz.go") |
| 92 | cases := []struct { |
| 93 | Name string |
| 94 | DSL func() |
| 95 | TypeFiles []string |
| 96 | }{ |
| 97 | {"none", testdata.SingleMethodDSL, nil}, |
| 98 | {"single", testdata.PkgPathDSL, []string{fooPath}}, |
| 99 | {"array", testdata.PkgPathArrayDSL, []string{fooPath}}, |
| 100 | {"recursive", testdata.PkgPathRecursiveDSL, []string{fooPath, recursiveFooPath}}, |
| 101 | {"multiple", testdata.PkgPathMultipleDSL, []string{barPath, bazPath}}, |
| 102 | {"nopkg", testdata.PkgPathNoDirDSL, nil}, |
| 103 | {"dupes", testdata.PkgPathDupeDSL, []string{fooPath}}, |
| 104 | {"payload_attribute", testdata.PkgPathPayloadAttributeDSL, []string{fooPath}}, |
| 105 | } |
| 106 | for _, c := range cases { |
| 107 | t.Run(c.Name, func(t *testing.T) { |
| 108 | userTypePkgs := make(map[string][]string) |
| 109 | root := codegen.RunDSL(t, c.DSL) |
| 110 | services := NewServicesData(root) |
| 111 | files := Files("goa.design/goa/example", root.Services[0], services, userTypePkgs) |
| 112 | |
| 113 | // Check file count |
| 114 | expectedFiles := len(c.TypeFiles) + 1 |
| 115 | require.Len(t, files, expectedFiles, "unexpected number of files") |
| 116 | |
| 117 | // First file is always the service file |
| 118 | buf := new(bytes.Buffer) |
| 119 | for _, s := range files[0].SectionTemplates[1:] { |
| 120 | require.NoError(t, s.Write(buf)) |
| 121 | } |
| 122 | bs, err := format.Source(buf.Bytes()) |
| 123 | require.NoError(t, err) |
| 124 | testutil.AssertGo(t, "testdata/golden/pkg_path_"+c.Name+"_service.go.golden", string(bs)) |
| 125 | |
| 126 | // Type files |
| 127 | for i, typeFile := range c.TypeFiles { |
| 128 | buf := new(bytes.Buffer) |
| 129 | for _, s := range files[i+1].SectionTemplates[1:] { |
| 130 | require.NoError(t, s.Write(buf)) |
| 131 | } |
| 132 | bs, err := format.Source(buf.Bytes()) |
| 133 | require.NoError(t, err) |
| 134 | goldenName := filepath.Base(typeFile) |
| 135 | testutil.AssertGo(t, "testdata/golden/pkg_path_"+c.Name+"_"+goldenName+".golden", string(bs)) |
| 136 | } |
| 137 | |
| 138 | // For dupes case, test the second service |
| 139 | if c.Name == "dupes" && len(root.Services) > 1 { |
| 140 | files = Files("goa.design/goa/example", root.Services[1], services, userTypePkgs) |
| 141 | require.Len(t, files, 1) |
| 142 | buf := new(bytes.Buffer) |
| 143 | for _, s := range files[0].SectionTemplates[1:] { |
| 144 | require.NoError(t, s.Write(buf)) |