-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcomposite_test.go
108 lines (94 loc) · 2.29 KB
/
composite_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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package checker
import (
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
func TestArrayInner(t *testing.T) {
type twoDimArr struct {
Arr struct {
Arr2 [][]int
}
}
arrayS := twoDimArr{}
arrayS.Arr.Arr2 = [][]int{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
}
prompt := "元素不能等于1"
rule := Field("Arr",
Array("Arr2",
Array("", NeInt("", 1)).Prompt(prompt),
),
)
ch := NewChecker()
ch.Add(rule, "")
isValid, errPrompt, errMsg := ch.Check(arrayS)
assert.Equal(t, false, isValid, "")
assert.Equal(t, prompt, errPrompt, "wrong errPrompt")
msg := "[NeInt]:Arr.Arr2[0][0] should not be 1,actual is 1"
assert.Equal(t, msg, errMsg, "wrong errMsg")
}
func TestMap(t *testing.T) {
type MapStruct struct {
InnerMap struct {
Map map[int]string
}
}
mapStruct := MapStruct{}
mapStruct.InnerMap.Map = map[int]string{
0: "orange",
1: "apple",
2: "watermelon",
}
rule := Field("InnerMap", And(
Length("Map", 0, 5).Prompt("长度必须在[2,5]"),
Map("Map",
RangeInt("", 0, 2).Prompt("key范围必须在[0,2]"),
InStr("", "orange", "apple", "watermelon").Prompt("错误的水果"),
),
))
ch := NewChecker()
ch.Add(rule, "")
isValid, errPrompt, _ := ch.Check(mapStruct)
assert.Equal(t, true, isValid, errPrompt)
mapStruct.InnerMap.Map = map[int]string{
3: "watermelon",
}
prompt := "key范围必须在[0,2]"
msg := "[RangeInt]:InnerMap.Map[3] should be between 0 and 2,actual is 3"
isValid, errPrompt, errMsg := ch.Check(mapStruct)
assert.Equal(t, false, isValid, errPrompt)
assert.Equal(t, prompt, errPrompt, "wrong errPrompt")
assert.Equal(t, msg, errMsg, "wrong errMsg")
mapStruct.InnerMap.Map = map[int]string{
2: "pineapple",
}
isValid, errPrompt, errMsg = ch.Check(mapStruct)
prompt = "错误的水果"
msg = "[InStr]:InnerMap.Map[2] 's value should be in [orange apple watermelon],actual is pineapple"
assert.Equal(t, prompt, errPrompt, "wrong errPrompt")
assert.Equal(t, msg, errMsg, "wrong errMsg")
}
func TestGetKeyStr(t *testing.T) {
cases := []interface{}{
1.2,
1,
uint(3),
"abc",
}
expecteds := []string{
"1.2",
"1",
"3",
"abc",
}
for i := 0; i < len(cases); i++ {
aCase := cases[i]
expected := expecteds[i]
value := reflect.ValueOf(aCase)
res := getKeyStr(value)
assert.Equal(t, expected, res)
}
}