-
-
Notifications
You must be signed in to change notification settings - Fork 584
Expand file tree
/
Copy pathserver_encode_test.go
More file actions
140 lines (127 loc) · 8.82 KB
/
server_encode_test.go
File metadata and controls
140 lines (127 loc) · 8.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package codegen
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"goa.design/goa/v3/codegen"
"goa.design/goa/v3/expr"
"goa.design/goa/v3/http/codegen/testdata"
)
func TestEncode(t *testing.T) {
cases := []struct {
Name string
DSL func()
Code string
}{
{"header-bool", testdata.ResultHeaderBoolDSL, testdata.ResultHeaderBoolEncodeCode},
{"header-int", testdata.ResultHeaderIntDSL, testdata.ResultHeaderIntEncodeCode},
{"header-int32", testdata.ResultHeaderInt32DSL, testdata.ResultHeaderInt32EncodeCode},
{"header-int64", testdata.ResultHeaderInt64DSL, testdata.ResultHeaderInt64EncodeCode},
{"header-uint", testdata.ResultHeaderUIntDSL, testdata.ResultHeaderUIntEncodeCode},
{"header-uint32", testdata.ResultHeaderUInt32DSL, testdata.ResultHeaderUInt32EncodeCode},
{"header-uint64", testdata.ResultHeaderUInt64DSL, testdata.ResultHeaderUInt64EncodeCode},
{"header-float32", testdata.ResultHeaderFloat32DSL, testdata.ResultHeaderFloat32EncodeCode},
{"header-float64", testdata.ResultHeaderFloat64DSL, testdata.ResultHeaderFloat64EncodeCode},
{"header-string", testdata.ResultHeaderStringDSL, testdata.ResultHeaderStringEncodeCode},
{"header-bytes", testdata.ResultHeaderBytesDSL, testdata.ResultHeaderBytesEncodeCode},
{"header-any", testdata.ResultHeaderAnyDSL, testdata.ResultHeaderAnyEncodeCode},
{"header-array-bool", testdata.ResultHeaderArrayBoolDSL, testdata.ResultHeaderArrayBoolEncodeCode},
{"header-array-int", testdata.ResultHeaderArrayIntDSL, testdata.ResultHeaderArrayIntEncodeCode},
{"header-array-int32", testdata.ResultHeaderArrayInt32DSL, testdata.ResultHeaderArrayInt32EncodeCode},
{"header-array-int64", testdata.ResultHeaderArrayInt64DSL, testdata.ResultHeaderArrayInt64EncodeCode},
{"header-array-uint", testdata.ResultHeaderArrayUIntDSL, testdata.ResultHeaderArrayUIntEncodeCode},
{"header-array-uint32", testdata.ResultHeaderArrayUInt32DSL, testdata.ResultHeaderArrayUInt32EncodeCode},
{"header-array-uint64", testdata.ResultHeaderArrayUInt64DSL, testdata.ResultHeaderArrayUInt64EncodeCode},
{"header-array-float32", testdata.ResultHeaderArrayFloat32DSL, testdata.ResultHeaderArrayFloat32EncodeCode},
{"header-array-float64", testdata.ResultHeaderArrayFloat64DSL, testdata.ResultHeaderArrayFloat64EncodeCode},
{"header-array-string", testdata.ResultHeaderArrayStringDSL, testdata.ResultHeaderArrayStringEncodeCode},
{"header-array-bytes", testdata.ResultHeaderArrayBytesDSL, testdata.ResultHeaderArrayBytesEncodeCode},
{"header-array-any", testdata.ResultHeaderArrayAnyDSL, testdata.ResultHeaderArrayAnyEncodeCode},
{"header-bool-default", testdata.ResultHeaderBoolDefaultDSL, testdata.ResultHeaderBoolDefaultEncodeCode},
{"header-bool-required-default", testdata.ResultHeaderBoolRequiredDefaultDSL, testdata.ResultHeaderBoolRequiredDefaultEncodeCode},
{"header-string-default", testdata.ResultHeaderStringDefaultDSL, testdata.ResultHeaderStringDefaultEncodeCode},
{"header-string-required-default", testdata.ResultHeaderStringRequiredDefaultDSL, testdata.ResultHeaderStringRequiredDefaultEncodeCode},
{"header-array-bool-default", testdata.ResultHeaderArrayBoolDefaultDSL, testdata.ResultHeaderArrayBoolDefaultEncodeCode},
{"header-array-bool-required-default", testdata.ResultHeaderArrayBoolRequiredDefaultDSL, testdata.ResultHeaderArrayBoolRequiredDefaultEncodeCode},
{"header-array-string-default", testdata.ResultHeaderArrayStringDefaultDSL, testdata.ResultHeaderArrayStringDefaultEncodeCode},
{"header-array-string-required-default", testdata.ResultHeaderArrayStringRequiredDefaultDSL, testdata.ResultHeaderArrayStringRequiredDefaultEncodeCode},
{"body-string", testdata.ResultBodyStringDSL, testdata.ResultBodyStringEncodeCode},
{"body-object", testdata.ResultBodyObjectDSL, testdata.ResultBodyObjectEncodeCode},
{"body-user", testdata.ResultBodyUserDSL, testdata.ResultBodyUserEncodeCode},
{"body-union", testdata.ResultBodyUnionDSL, testdata.ResultBodyUnionEncodeCode},
{"body-result-multiple-views", testdata.ResultBodyMultipleViewsDSL, testdata.ResultBodyMultipleViewsEncodeCode},
{"body-result-collection-multiple-views", testdata.ResultBodyCollectionDSL, testdata.ResultBodyCollectionMultipleViewsEncodeCode},
{"body-result-collection-explicit-view", testdata.ResultBodyCollectionExplicitViewDSL, testdata.ResultBodyCollectionExplicitViewEncodeCode},
{"empty-body-result-multiple-views", testdata.EmptyBodyResultMultipleViewsDSL, testdata.EmptyBodyResultMultipleViewsEncodeCode},
{"body-array-string", testdata.ResultBodyArrayStringDSL, testdata.ResultBodyArrayStringEncodeCode},
{"body-array-user", testdata.ResultBodyArrayUserDSL, testdata.ResultBodyArrayUserEncodeCode},
{"body-primitive-string", testdata.ResultBodyPrimitiveStringDSL, testdata.ResultBodyPrimitiveStringEncodeCode},
{"body-primitive-bool", testdata.ResultBodyPrimitiveBoolDSL, testdata.ResultBodyPrimitiveBoolEncodeCode},
{"body-primitive-any", testdata.ResultBodyPrimitiveAnyDSL, testdata.ResultBodyPrimitiveAnyEncodeCode},
{"body-primitive-array-string", testdata.ResultBodyPrimitiveArrayStringDSL, testdata.ResultBodyPrimitiveArrayStringEncodeCode},
{"body-primitive-array-bool", testdata.ResultBodyPrimitiveArrayBoolDSL, testdata.ResultBodyPrimitiveArrayBoolEncodeCode},
{"body-primitive-array-user", testdata.ResultBodyPrimitiveArrayUserDSL, testdata.ResultBodyPrimitiveArrayUserEncodeCode},
{"body-inline-object", testdata.ResultBodyInlineObjectDSL, testdata.ResultBodyInlineObjectEncodeCode},
{"body-header-object", testdata.ResultBodyHeaderObjectDSL, testdata.ResultBodyHeaderObjectEncodeCode},
{"body-header-user", testdata.ResultBodyHeaderUserDSL, testdata.ResultBodyHeaderUserEncodeCode},
{"explicit-body-primitive-result-multiple-views", testdata.ExplicitBodyPrimitiveResultMultipleViewsDSL, testdata.ExplicitBodyPrimitiveResultMultipleViewsEncodeCode},
{"explicit-body-user-result-multiple-views", testdata.ExplicitBodyUserResultMultipleViewsDSL, testdata.ExplicitBodyUserResultMultipleViewsEncodeCode},
{"explicit-body-result-collection", testdata.ExplicitBodyResultCollectionDSL, testdata.ExplicitBodyResultCollectionEncodeCode},
{"explicit-content-type-result", testdata.ExplicitContentTypeResultDSL, testdata.ExplicitContentTypeResultEncodeCode},
{"explicit-content-type-response", testdata.ExplicitContentTypeResponseDSL, testdata.ExplicitContentTypeResponseEncodeCode},
{"tag-string", testdata.ResultTagStringDSL, testdata.ResultTagStringEncodeCode},
{"tag-string-required", testdata.ResultTagStringRequiredDSL, testdata.ResultTagStringRequiredEncodeCode},
{"tag-result-multiple-views", testdata.ResultMultipleViewsTagDSL, testdata.ResultMultipleViewsTagEncodeCode},
{"empty-server-response", testdata.EmptyServerResponseDSL, testdata.EmptyServerResponseEncodeCode},
{"empty-server-response-with-tags", testdata.EmptyServerResponseWithTagsDSL, testdata.EmptyServerResponseWithTagsEncodeCode},
{"skip-response-body-encode-decode", testdata.ResponseEncoderSkipResponseBodyEncodeDecodeDSL, testdata.ResponseEncoderSkipResponseBodyEncodeDecodeCode},
{"result-with-custom-pkg-type", testdata.ResultWithCustomPkgTypeDSL, testdata.ResultWithCustomPkgTypeEncodeCode},
{"result-with-embedded-custom-pkg-type", testdata.EmbeddedCustomPkgTypeDSL, testdata.ResultWithEmbeddedCustomPkgTypeEncodeCode},
}
for _, c := range cases {
t.Run(c.Name, func(t *testing.T) {
RunHTTPDSL(t, c.DSL)
fs := ServerFiles("", expr.Root)
require.Len(t, fs, 2)
sections := fs[1].SectionTemplates
require.Greater(t, len(sections), 1)
code := codegen.SectionCode(t, sections[1])
assert.Equal(t, c.Code, code)
})
}
}
func TestEncodeMarshallingAndUnmarshalling(t *testing.T) {
cases := []struct {
Name string
DSL func()
Code []string
SectionsOffset int
}{
{"embedded-custom-pkg-type", testdata.EmbeddedCustomPkgTypeDSL, []string{
testdata.EmbeddedCustomPkgTypeUnmarshalCode,
testdata.EmbeddedCustomPkgTypeMarshalCode}, 3},
{"array-alias-extended", testdata.ArrayAliasExtendedDSL, []string{
testdata.ArrayAliasExtendedUnmarshalCode,
testdata.ArrayAliasExtendedMarshalCode}, 3},
{"extension-with-alias", testdata.ExtensionWithAliasDSL, []string{
testdata.ExtensionWithAliasUnmarshalExtensionCode,
testdata.ExtensionWithAliasUnmarshalBarCode,
testdata.ExtensionWithAliasMarshalResultCode,
testdata.ExtensionWithAliasMarshalExtensionCode,
testdata.ExtensionWithAliasMarshalBarCode}, 4},
}
for _, c := range cases {
t.Run(c.Name, func(t *testing.T) {
RunHTTPDSL(t, c.DSL)
fs := ServerFiles("", expr.Root)
require.Len(t, fs, 2)
sections := fs[1].SectionTemplates
totalSectionsExpected := c.SectionsOffset + len(c.Code)
require.Len(t, sections, totalSectionsExpected)
for i := 0; i < len(c.Code); i++ {
code := codegen.SectionCode(t, sections[c.SectionsOffset+i])
assert.Equal(t, c.Code[i], code)
}
})
}
}