| 12 | ) |
| 13 | |
| 14 | func TestByPattern(t *testing.T) { |
| 15 | cases := []struct { |
| 16 | Name string |
| 17 | Pattern string |
| 18 | ExpectedMaxLen int |
| 19 | }{ |
| 20 | {"not-a-regexp", "foo", 3}, |
| 21 | {"max-len", "foo[a-z]+", 9}, |
| 22 | {"max-len-2", "^/api/example/[0-9]+$", 19}, |
| 23 | } |
| 24 | r := expr.NewRandom("test") |
| 25 | for _, k := range cases { |
| 26 | t.Run(k.Name, func(t *testing.T) { |
| 27 | val := &expr.ValidationExpr{Pattern: k.Pattern} |
| 28 | att := expr.AttributeExpr{Validation: val} |
| 29 | |
| 30 | example := att.Example(r).(string) |
| 31 | |
| 32 | if match, _ := regexp.MatchString(k.Pattern, example); !match { |
| 33 | t.Errorf("got %s, expected a match for %s", example, k.Pattern) |
| 34 | } |
| 35 | if utf8.RuneCountInString(example) > k.ExpectedMaxLen { |
| 36 | t.Errorf("got %s (len %d) exceeded expected len of %d", example, len(example), k.ExpectedMaxLen) |
| 37 | } |
| 38 | }) |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | func TestByFormatUUID(t *testing.T) { |
| 43 | val := &expr.ValidationExpr{Format: expr.FormatUUID} |