| 9 | ) |
| 10 | |
| 11 | func TestFormat(t *testing.T) { |
| 12 | cases := map[string]struct { |
| 13 | Format expr.ValidationFormat |
| 14 | }{ |
| 15 | "date": {expr.FormatDate}, |
| 16 | "date-time": {expr.FormatDateTime}, |
| 17 | "uuid": {expr.FormatUUID}, |
| 18 | "email": {expr.FormatEmail}, |
| 19 | "hostname": {expr.FormatHostname}, |
| 20 | "ipv4": {expr.FormatIPv4}, |
| 21 | "ipv6": {expr.FormatIPv6}, |
| 22 | "ip": {expr.FormatIP}, |
| 23 | "uri": {expr.FormatURI}, |
| 24 | "mac": {expr.FormatMAC}, |
| 25 | "cidr": {expr.FormatCIDR}, |
| 26 | "regexp": {expr.FormatRegexp}, |
| 27 | "json": {expr.FormatJSON}, |
| 28 | "rfc1123": {expr.FormatRFC1123}, |
| 29 | } |
| 30 | |
| 31 | for k, tc := range cases { |
| 32 | eval.Context = &eval.DSLContext{} |
| 33 | expr := &expr.AttributeExpr{} |
| 34 | eval.Execute(func() { Format(tc.Format) }, expr) |
| 35 | if eval.Context.Errors != nil { |
| 36 | t.Errorf("%s: Format failed unexpectedly with %s", k, eval.Context.Errors) |
| 37 | } |
| 38 | if expr.Validation == nil { |
| 39 | t.Errorf("%s: Format not initialized Validation in %+v", k, expr) |
| 40 | } else if expr.Validation.Format != tc.Format { |
| 41 | t.Errorf("%s: Format not set on %+v, expected %s, got %+v", k, expr, tc.Format, expr.Validation.Format) |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | func TestRequired(t *testing.T) { |
| 47 | att := &expr.AttributeExpr{ |