MCPcopy Create free account
hub / github.com/goadesign/goa / TestCompatible

Function TestCompatible

codegen/service/convert_test.go:137–190  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

135 }
136}
137func TestCompatible(t *testing.T) {
138 cases := []struct {
139 Name string
140 From expr.DataType
141 To any
142 ExpectedErr string
143 }{
144 {"bool", expr.Boolean, false, ""},
145 {"int", expr.Int, 0, ""},
146 {"int32", expr.Int32, int32(0), ""},
147 {"int64", expr.Int64, int64(0), ""},
148 {"uint", expr.UInt, uint(0), ""},
149 {"uint32", expr.UInt32, uint32(0), ""},
150 {"uint64", expr.UInt64, uint64(0), ""},
151 {"float32", expr.Float32, float32(0.0), ""},
152 {"float64", expr.Float64, 0.0, ""},
153 {"string", expr.String, "", ""},
154 {"bytes", expr.Bytes, []byte{}, ""},
155 {"array", dsl.ArrayOf(expr.String), []string{}, ""},
156 {"map", dsl.MapOf(expr.String, expr.String), map[string]string{}, ""},
157 {"map-interface", dsl.MapOf(expr.String, expr.Any), map[string]any{}, ""},
158 {"object", obj, objT{}, ""},
159 {"object-mapped", objMapped, objT{}, ""},
160 {"object-ignored", objIgnored, objT{}, ""},
161 {"object-extra", objIgnored, objExtraT{}, ""},
162 {"object-recursive", objRecursive(), objRecursiveT{}, ""},
163 {"array-object", dsl.ArrayOf(obj), []objT{{}}, ""},
164
165 {"invalid-primitive", expr.String, 0, "types don't match: type of <value> is int but type of corresponding attribute is string"},
166 {"invalid-int", expr.Int, 0.0, "types don't match: type of <value> is float64 but type of corresponding attribute is int"},
167 {"invalid-float32", expr.Float32, 0, "types don't match: type of <value> is int but type of corresponding attribute is float32"},
168 {"invalid-array", dsl.ArrayOf(expr.String), []int{0}, "types don't match: type of <value>[0] is int but type of corresponding attribute is string"},
169 {"invalid-map-key", dsl.MapOf(expr.String, expr.String), map[int]string{0: ""}, "types don't match: type of <value>.key is int but type of corresponding attribute is string"},
170 {"invalid-map-val", dsl.MapOf(expr.String, expr.String), map[string]int{"": 0}, "types don't match: type of <value>.value is int but type of corresponding attribute is string"},
171 {"invalid-obj", obj, "", "types don't match: <value> is a string, expected a struct"},
172 {"invalid-obj-2", obj, objT2{}, "types don't match: type of <value>.Bar is string but type of corresponding attribute is int"},
173 {"invalid-obj-3", obj, objT3{}, "types don't match: type of <value>.Goo is int but type of corresponding attribute is float32"},
174 {"invalid-obj-4", obj, objT4{}, "types don't match: type of <value>.Goo2 is float32 but type of corresponding attribute is uint"},
175 {"invalid-obj-5", obj, objT5{}, "types don't match: could not find field \"Baz\" of external type \"objT5\" matching attribute \"Baz\" of type \"objT\""},
176 {"invalid-array-object", dsl.ArrayOf(obj), []objT2{{}}, "types don't match: type of <value>[0].Bar is string but type of corresponding attribute is int"},
177 }
178 for _, c := range cases {
179 t.Run(c.Name, func(t *testing.T) {
180 err := compatible(c.From, reflect.TypeOf(c.To))
181 if c.ExpectedErr == "" {
182 assert.NoError(t, err)
183 }
184 if c.ExpectedErr != "" {
185 assert.Error(t, err)
186 assert.Equal(t, c.ExpectedErr, err.Error())
187 }
188 })
189 }
190}
191
192// Test fixtures
193var obj = &expr.UserTypeExpr{

Callers

nothing calls this directly

Calls 6

ArrayOfFunction · 0.92
MapOfFunction · 0.92
objRecursiveFunction · 0.85
compatibleFunction · 0.85
RunMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected