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

Function TestAliasTypeInArrayAndMap

codegen/validation_test.go:161–209  ·  view source on GitHub ↗

TestAliasTypeInArrayAndMap tests that alias types work correctly when nested in arrays and maps, ensuring the recursion guard doesn't interfere.

(t *testing.T)

Source from the content-addressed store, hash-verified

159// TestAliasTypeInArrayAndMap tests that alias types work correctly when
160// nested in arrays and maps, ensuring the recursion guard doesn't interfere.
161func TestAliasTypeInArrayAndMap(t *testing.T) {
162 root := RunDSL(t, testdata.ValidationTypesDSL)
163 scope := NewNameScope()
164
165 var (
166 alias = root.UserType("Alias")
167 )
168
169 // Create a type with alias in array
170 arrayWithAlias := &expr.AttributeExpr{
171 Type: &expr.Array{
172 ElemType: &expr.AttributeExpr{Type: alias},
173 },
174 }
175
176 // Create a type with alias in map
177 mapWithAlias := &expr.AttributeExpr{
178 Type: &expr.Map{
179 KeyType: &expr.AttributeExpr{Type: expr.String},
180 ElemType: &expr.AttributeExpr{Type: alias},
181 },
182 }
183
184 cases := []struct {
185 Name string
186 Att *expr.AttributeExpr
187 Target string
188 }{
189 {"alias-in-array", arrayWithAlias, "target"},
190 {"alias-in-map", mapWithAlias, "target"},
191 }
192
193 for _, c := range cases {
194 t.Run(c.Name, func(t *testing.T) {
195 ctx := NewAttributeContext(false, false, false, "", scope)
196 code := ValidationCode(c.Att, nil, ctx, true, false, false, c.Target)
197 code = FormatTestCode(t, "package foo\nfunc Validate() (err error){\n"+code+"}")
198
199 // Verify validation code is generated
200 if code == "" {
201 t.Error("Expected validation code to be generated")
202 }
203 // Verify it contains validation for the alias type
204 if !strings.Contains(code, "ValidatePattern") && !strings.Contains(code, "InvalidLengthError") {
205 t.Error("Expected validation code to contain pattern or length validation for alias type")
206 }
207 })
208 }
209}

Callers

nothing calls this directly

Calls 8

NewNameScopeFunction · 0.85
NewAttributeContextFunction · 0.85
ValidationCodeFunction · 0.85
FormatTestCodeFunction · 0.85
UserTypeMethod · 0.80
RunDSLFunction · 0.70
RunMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected