-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathjson_test.go
78 lines (71 loc) · 1.78 KB
/
json_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package govalidator
import (
"testing"
"github.com/stretchr/testify/assert"
)
func Test_IsJSON(t *testing.T) {
tests := []struct {
name string
field string
value string
isPassed bool
message string
expectedMsg string
}{
{
name: "test {\"menu\": {\"id\": \"1\", \"value\": \"file\"}} is a valid JSON input",
field: "input",
value: "{\"menu\": {\"id\": \"1\", \"value\": \"file\"}}",
isPassed: true,
message: "",
expectedMsg: "",
},
{
name: "test empty string won't pass the JSON validation rule",
field: "input",
value: "",
isPassed: false,
message: "",
expectedMsg: "input should be a valid JSON",
},
{
name: "test empty space string won't pass the JSON validation rule",
field: "input",
value: " ",
isPassed: false,
message: "",
expectedMsg: "input should be a valid JSON",
},
{
name: "test `Reza` won't pass the JSON validation rule",
field: "input",
value: "Reza",
isPassed: false,
message: "",
expectedMsg: "input should be a valid JSON",
},
{
name: "test `{\"example\":2:]}}` won't pass the JSON validation rule",
field: "input",
value: "{\"example\":2:]}}",
isPassed: false,
message: "input should be a valid JSON",
expectedMsg: "input should be a valid JSON",
},
}
for _, test := range tests {
v := New()
v.IsJSON(test.value, test.field, test.message)
assert.Equal(t, test.isPassed, v.IsPassed(), test.name)
if !test.isPassed {
assert.Equalf(
t,
test.expectedMsg,
v.Errors()[test.field],
"test case %q failed, expected: %s, got: %s",
test.expectedMsg,
v.Errors()[test.field],
)
}
}
}