TestAliasTypeInArrayAndMap tests that alias types work correctly when nested in arrays and maps, ensuring the recursion guard doesn't interfere.
(t *testing.T)
| 159 | // TestAliasTypeInArrayAndMap tests that alias types work correctly when |
| 160 | // nested in arrays and maps, ensuring the recursion guard doesn't interfere. |
| 161 | func 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 | } |
nothing calls this directly
no test coverage detected