|
1 |
| -package schema |
| 1 | +// +build go1.7 |
| 2 | + |
| 3 | +package schema_test |
2 | 4 |
|
3 | 5 | import (
|
4 | 6 | "testing"
|
5 | 7 |
|
6 |
| - "github.com/stretchr/testify/assert" |
| 8 | + "github.com/rs/rest-layer/schema" |
7 | 9 | )
|
8 | 10 |
|
9 | 11 | func TestDictValidatorCompile(t *testing.T) {
|
10 |
| - v := &Dict{KeysValidator: &String{}, ValuesValidator: &String{}} |
11 |
| - err := v.Compile() |
12 |
| - assert.NoError(t, err) |
13 |
| - v = &Dict{ |
14 |
| - KeysValidator: &String{Regexp: "[invalid re"}, |
| 12 | + testCases := []compilerTestCase{ |
| 13 | + { |
| 14 | + Name: "KeysValidator=&String{},ValuesValidator=&String{}", |
| 15 | + Compiler: &schema.Dict{ |
| 16 | + KeysValidator: &schema.String{}, |
| 17 | + ValuesValidator: &schema.String{}, |
| 18 | + }, |
| 19 | + }, |
| 20 | + { |
| 21 | + Name: "KeysValidator=&String{Regexp:invalid}", |
| 22 | + Compiler: &schema.Dict{KeysValidator: &schema.String{Regexp: "[invalid re"}}, |
| 23 | + Error: "invalid regexp: error parsing regexp: missing closing ]: `[invalid re`", |
| 24 | + }, |
| 25 | + { |
| 26 | + Name: "ValuesValidator=&String{Regexp:invalid}", |
| 27 | + Compiler: &schema.Dict{ValuesValidator: &schema.String{Regexp: "[invalid re"}}, |
| 28 | + Error: "invalid regexp: error parsing regexp: missing closing ]: `[invalid re`", |
| 29 | + }, |
15 | 30 | }
|
16 |
| - err = v.Compile() |
17 |
| - assert.EqualError(t, err, "invalid regexp: error parsing regexp: missing closing ]: `[invalid re`") |
18 |
| - v = &Dict{ |
19 |
| - ValuesValidator: &String{Regexp: "[invalid re"}, |
| 31 | + for i := range testCases { |
| 32 | + testCases[i].Run(t) |
20 | 33 | }
|
21 |
| - err = v.Compile() |
22 |
| - assert.EqualError(t, err, "invalid regexp: error parsing regexp: missing closing ]: `[invalid re`") |
23 | 34 | }
|
24 | 35 |
|
25 | 36 | func TestDictValidator(t *testing.T) {
|
26 |
| - v, err := Dict{KeysValidator: &String{}}.Validate(map[string]interface{}{"foo": true, "bar": false}) |
27 |
| - assert.NoError(t, err) |
28 |
| - assert.Equal(t, map[string]interface{}{"foo": true, "bar": false}, v) |
29 |
| - v, err = Dict{KeysValidator: &String{MinLen: 3}}.Validate(map[string]interface{}{"foo": true, "ba": false}) |
30 |
| - assert.EqualError(t, err, "invalid key `ba': is shorter than 3") |
31 |
| - assert.Equal(t, nil, v) |
32 |
| - v, err = Dict{ValuesValidator: &Bool{}}.Validate(map[string]interface{}{"foo": true, "bar": false}) |
33 |
| - assert.NoError(t, err) |
34 |
| - assert.Equal(t, map[string]interface{}{"foo": true, "bar": false}, v) |
35 |
| - v, err = Dict{ValuesValidator: &Bool{}}.Validate(map[string]interface{}{"foo": true, "bar": "value"}) |
36 |
| - assert.EqualError(t, err, "invalid value for key `bar': not a Boolean") |
37 |
| - assert.Equal(t, nil, v) |
38 |
| - v, err = Dict{ValuesValidator: &String{}}.Validate("value") |
39 |
| - assert.EqualError(t, err, "not a dict") |
40 |
| - assert.Equal(t, nil, v) |
41 |
| - v, err = Dict{ValuesValidator: &String{}}.Validate([]interface{}{"value"}) |
42 |
| - assert.EqualError(t, err, "not a dict") |
43 |
| - assert.Equal(t, nil, v) |
44 |
| - |
| 37 | + testCases := []fieldValidatorTestCase{ |
| 38 | + { |
| 39 | + Name: `KeysValidator=&String{},Validate(map[string]interface{}{"foo":true,"bar":false})`, |
| 40 | + Validator: &schema.Dict{KeysValidator: &schema.String{}}, |
| 41 | + Input: map[string]interface{}{"foo": true, "bar": false}, |
| 42 | + Expect: map[string]interface{}{"foo": true, "bar": false}, |
| 43 | + }, |
| 44 | + { |
| 45 | + Name: `KeysValidator=&String{MinLen:3},Validate(map[string]interface{}{"foo":true,"bar":false})`, |
| 46 | + Validator: &schema.Dict{KeysValidator: &schema.String{MinLen: 3}}, |
| 47 | + Input: map[string]interface{}{"foo": true, "bar": false}, |
| 48 | + Expect: map[string]interface{}{"foo": true, "bar": false}, |
| 49 | + }, |
| 50 | + { |
| 51 | + Name: `KeysValidator=&String{MinLen:3},Validate(map[string]interface{}{"foo":true,"ba":false})`, |
| 52 | + Validator: &schema.Dict{KeysValidator: &schema.String{MinLen: 3}}, |
| 53 | + Input: map[string]interface{}{"foo": true, "ba": false}, |
| 54 | + Error: "invalid key `ba': is shorter than 3", |
| 55 | + }, |
| 56 | + { |
| 57 | + Name: `ValuesValidator=&Bool{},Validate(map[string]interface{}{"foo":true,"bar":false})`, |
| 58 | + Validator: &schema.Dict{ValuesValidator: &schema.Bool{}}, |
| 59 | + Input: map[string]interface{}{"foo": true, "bar": false}, |
| 60 | + Expect: map[string]interface{}{"foo": true, "bar": false}, |
| 61 | + }, |
| 62 | + { |
| 63 | + Name: `ValuesValidator=&Bool{},Validate(map[string]interface{}{"foo":true,"bar":"value"})`, |
| 64 | + Validator: &schema.Dict{ValuesValidator: &schema.Bool{}}, |
| 65 | + Input: map[string]interface{}{"foo": true, "bar": "value"}, |
| 66 | + Error: "invalid value for key `bar': not a Boolean", |
| 67 | + }, |
| 68 | + { |
| 69 | + Name: `ValuesValidator=&String{},Validate("value")`, |
| 70 | + Validator: &schema.Dict{ValuesValidator: &schema.String{}}, |
| 71 | + Input: "value", |
| 72 | + Error: "not a dict", |
| 73 | + }, |
| 74 | + { |
| 75 | + Name: `MinLen=2,Validate(map[string]interface{}{"foo":true,"bar":false})`, |
| 76 | + Validator: &schema.Dict{MinLen: 2}, |
| 77 | + Input: map[string]interface{}{"foo": true, "bar": "value"}, |
| 78 | + Expect: map[string]interface{}{"foo": true, "bar": "value"}, |
| 79 | + }, |
| 80 | + { |
| 81 | + Name: `MinLen=3,Validate(map[string]interface{}{"foo":true,"bar":false})`, |
| 82 | + Validator: &schema.Dict{MinLen: 3}, |
| 83 | + Input: map[string]interface{}{"foo": true, "bar": "value"}, |
| 84 | + Error: "has fewer properties than 3", |
| 85 | + }, |
| 86 | + { |
| 87 | + Name: `MaxLen=2,Validate(map[string]interface{}{"foo":true,"bar":false})`, |
| 88 | + Validator: &schema.Dict{MaxLen: 3}, |
| 89 | + Input: map[string]interface{}{"foo": true, "bar": "value"}, |
| 90 | + Expect: map[string]interface{}{"foo": true, "bar": "value"}, |
| 91 | + }, |
| 92 | + { |
| 93 | + Name: `MaxLen=1,Validate(map[string]interface{}{"foo":true,"bar":false})`, |
| 94 | + Validator: &schema.Dict{MaxLen: 1}, |
| 95 | + Input: map[string]interface{}{"foo": true, "bar": "value"}, |
| 96 | + Error: "has more properties than 1", |
| 97 | + }, |
| 98 | + } |
| 99 | + for i := range testCases { |
| 100 | + testCases[i].Run(t) |
| 101 | + } |
45 | 102 | }
|
0 commit comments