| 14 | ) |
| 15 | |
| 16 | func TestViews(t *testing.T) { |
| 17 | cases := []struct { |
| 18 | Name string |
| 19 | DSL func() |
| 20 | Code string |
| 21 | }{ |
| 22 | {"result-with-multiple-views", testdata.ResultWithMultipleViewsDSL, testdata.ResultWithMultipleViewsCode}, |
| 23 | {"result-collection-multiple-views", testdata.ResultCollectionMultipleViewsDSL, testdata.ResultCollectionMultipleViewsCode}, |
| 24 | {"result-with-user-type", testdata.ResultWithUserTypeDSL, testdata.ResultWithUserTypeCode}, |
| 25 | {"result-with-result-type", testdata.ResultWithResultTypeDSL, testdata.ResultWithResultTypeCode}, |
| 26 | {"result-with-recursive-result-type", testdata.ResultWithRecursiveResultTypeDSL, testdata.ResultWithRecursiveResultTypeCode}, |
| 27 | {"result-type-with-custom-fields", testdata.ResultWithCustomFieldsDSL, testdata.ResultWithCustomFieldsCode}, |
| 28 | {"result-with-recursive-collection-of-result-type", testdata.ResultWithRecursiveCollectionOfResultTypeDSL, testdata.ResultWithRecursiveCollectionOfResultTypeCode}, |
| 29 | {"result-with-multiple-methods", testdata.ResultWithMultipleMethodsDSL, testdata.ResultWithMultipleMethodsCode}, |
| 30 | {"result-with-enum-type", testdata.ResultWithEnumTypeDSL, testdata.ResultWithEnumType}, |
| 31 | {"result-with-pkg-path", testdata.ResultWithPkgPathDSL, testdata.ResultWithPkgPathCode}, |
| 32 | {"result-with-oneof-in-result-type", testdata.ResultWithOneOfInResultTypeDSL, testdata.ResultWithOneOfInResultTypeCode}, |
| 33 | } |
| 34 | for _, c := range cases { |
| 35 | t.Run(c.Name, func(t *testing.T) { |
| 36 | root := codegen.RunDSL(t, c.DSL) |
| 37 | services := NewServicesData(root) |
| 38 | require.Len(t, root.Services, 1) |
| 39 | fs := ViewsFile("goa.design/goa/example", root.Services[0], services) |
| 40 | require.NotNil(t, fs) |
| 41 | buf := new(bytes.Buffer) |
| 42 | for _, s := range fs.SectionTemplates[1:] { |
| 43 | require.NoError(t, s.Write(buf)) |
| 44 | } |
| 45 | bs, err := format.Source(buf.Bytes()) |
| 46 | require.NoError(t, err, buf.String()) |
| 47 | code := string(bs) |
| 48 | code = strings.ReplaceAll(code, "\r\n", "\n") |
| 49 | assert.Equal(t, c.Code, code) |
| 50 | }) |
| 51 | } |
| 52 | } |