-
Notifications
You must be signed in to change notification settings - Fork 12
/
checker_test.go
168 lines (152 loc) · 3.12 KB
/
checker_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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package checker
import (
"testing"
"github.com/stretchr/testify/assert"
)
type profile struct {
// Info is pointer filed
Info *basicInfo
Companies []company
}
type basicInfo struct {
// 1 <= len <= 20
Name string
// 18 <= age <= 80
Age int
// 1<= len <= 64
Email string
}
type company struct {
// frontend,backend
Position string
// frontend: html,css,javascript
// backend: C,Cpp,Java,Golang
// SkillStack 'length is between [1,3]
Skills []string
}
func getPassedProfile() profile {
companies := []company{
{
Position: "frontend",
Skills: []string{"html", "css"},
},
{
Position: "backend",
Skills: []string{"C", "Golang"},
},
}
info := basicInfo{Name: "liang", Age: 24, Email: "[email protected]"}
return profile{
Info: &info,
Companies: companies,
}
}
func getFailedProfile() profile {
companies := []company{
{
Position: "frontend",
Skills: []string{"Golang", "css"},
},
{
Position: "backend",
Skills: []string{"C", "Golang"},
},
}
info := basicInfo{Name: "liang", Age: 24, Email: "[email protected]"}
return profile{
Info: &info,
Companies: companies,
}
}
func getProfileChecker() Checker {
profileChecker := NewChecker()
rule :=
And(
Length("Info.Name", 1, 20),
RangeInt("Info.Age", 18, 80),
Array("Companies",
And(
Length("Skills", 1, 3),
Or(
And(
EqStr("Position", "frontend"),
Array("Skills",
InStr("", "html", "css", "javascript"),
),
),
And(
EqStr("Position", "backend"),
Array("Skills",
InStr("", "C", "CPP", "Java", "Golang"),
),
)),
)),
)
profileChecker.Add(rule, "invalid companies")
return profileChecker
}
func TestProfileCheckerPassed(t *testing.T) {
profile := getPassedProfile()
profileChecker := getProfileChecker()
isValid, _, _ := profileChecker.Check(profile)
assert.Equal(t, true, isValid)
}
func TestProfileCheckerFailed(t *testing.T) {
profile := getFailedProfile()
profileChecker := getProfileChecker()
isValid, _, _ := profileChecker.Check(profile)
assert.Equal(t, false, isValid, "error failed checker")
}
type Item struct {
Info typeInfo
Email string
}
type typeStr string
type typeInfo struct {
Type typeStr
Range []string
Unit string
Granularity string
}
func TestField(t *testing.T) {
items := []Item{
{
Email: "[email protected]",
Info: typeInfo{
Type: "range",
Range: []string{"2020-01-01", "2021-01-01"},
},
},
{
Email: "[email protected]",
Info: typeInfo{
Type: "last",
Granularity: "day",
Unit: "7",
},
},
}
rule := And(
Email("Email"),
Field("Info",
Or(
And(
EqStr("Type", "range"),
Length("Range", 2, 2),
Array("Range", Time("", "2006-01-02")),
),
And(
EqStr("Type", "last"),
InStr("Granularity", "day", "week", "month"),
Number("Unit"),
),
),
),
)
itemChecker := NewChecker()
itemChecker.Add(rule, "wrong item")
for _, item := range items {
isValid, _, errMsg := itemChecker.Check(item)
assert.Equal(t, true, isValid, errMsg)
}
}