TestByLengthWithAliasType tests that alias types with length validations can generate examples correctly. Previously, this would panic because the code checked a.Type.Kind() instead of the underlying type's kind.
(t *testing.T)
| 91 | // can generate examples correctly. Previously, this would panic because the |
| 92 | // code checked a.Type.Kind() instead of the underlying type's kind. |
| 93 | func TestByLengthWithAliasType(t *testing.T) { |
| 94 | r := expr.NewRandom("test") |
| 95 | |
| 96 | // Create an alias type based on String with length validation |
| 97 | // We need to use the DSL package properly |
| 98 | root := expr.RunDSL(t, testdata.AliasLengthValidationDSL) |
| 99 | |
| 100 | aliasType := root.UserType("ValidatedString") |
| 101 | att := aliasType.Attribute() |
| 102 | |
| 103 | // This should not panic and should generate a string example |
| 104 | // The key test is that byLength handles alias types correctly by unaliasing |
| 105 | // before checking the kind. Previously this would panic with: |
| 106 | // "invalid type for length validation: ValidatedString" |
| 107 | example := att.Example(r) |
| 108 | str, ok := example.(string) |
| 109 | if !ok { |
| 110 | t.Fatalf("Expected string example, got %T", example) |
| 111 | } |
| 112 | // Verify it's a valid non-empty string (exact length may vary due to randomness) |
| 113 | if len(str) == 0 { |
| 114 | t.Error("Expected non-empty string example") |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | // TestByLengthWithAliasArray tests that alias array types with length |
| 119 | // validations generate examples correctly. |