-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcomparisons_test.go
185 lines (137 loc) · 3.73 KB
/
comparisons_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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package checker
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
type comparison struct {
Int int
Uint uint
Float float64
String string
TimeStr string
Time time.Time
}
func TestComparison(t *testing.T) {
cChecker := NewChecker()
layout := "2006-01-02"
startDate, _ := time.Parse(layout, "2020-12-12")
endDate, _ := time.Parse(layout, "2020-12-13")
// equal rules
intEqRule := EqInt("Int", 10)
cChecker.Add(intEqRule, "invalid Int")
uintEqRule := EqUint("Uint", 58)
cChecker.Add(uintEqRule, "invalid Uint")
floatRule := EqFloat("Float", 3.14)
cChecker.Add(floatRule, "invalid Float")
strRule := EqStr("String", "string")
cChecker.Add(strRule, "invalid String")
timeStrRule := EqTimeStr("TimeStr", layout, startDate)
cChecker.Add(timeStrRule, "invalid TimeStr")
timeRule := EqTime("Time", startDate)
cChecker.Add(timeRule, "invalid Time")
// not equal rules
intNeRule := NeInt("Int", 20)
cChecker.Add(intNeRule, "invalid Int")
uintNeRule := NeUint("Uint", 158)
cChecker.Add(uintNeRule, "invalid Uint")
floatNeRule := NeFloat("Float", 6.28)
cChecker.Add(floatNeRule, "invalid Float")
strNeRule := NeStr("String", "string12")
cChecker.Add(strNeRule, "invalid String")
timeNeStrRule := NeTimeStr("TimeStr", layout, endDate)
cChecker.Add(timeNeStrRule, "invalid TimeStr")
timeNeRule := NeTime("Time", endDate)
cChecker.Add(timeNeRule, "invalid Time")
// range rules
intRangeRule := RangeInt("Int", 1, 20)
cChecker.Add(intRangeRule, "invalid Int")
uintRangeRule := RangeUint("Uint", 10, 258)
cChecker.Add(uintRangeRule, "invalid Uint")
floatRangeRule := RangeFloat("Float", 3.14, 6.28)
cChecker.Add(floatRangeRule, "invalid Float")
timeRangeStrRule := RangeTimeStr("TimeStr", layout, startDate, endDate)
cChecker.Add(timeRangeStrRule, "invalid TimeStr")
timeRangeRule := RangeTime("Time", startDate, endDate)
cChecker.Add(timeRangeRule, "invalid Time")
comp := comparison{
Int: 10,
Uint: 58,
Float: 3.14,
String: "string",
TimeStr: "2020-12-12",
Time: startDate,
}
isValid, _, _ := cChecker.Check(comp)
assert.Equal(t, true, isValid)
}
type comp2 struct {
InnerInt *innerInt
}
func TestComparisonComparable(t *testing.T) {
cChecker := NewChecker()
equivalent := innerInt{
Val: 100,
}
inequivalent := innerInt{
Val: 120,
}
ge := innerInt{
Val: 80,
}
le := innerInt{
Val: 180,
}
eqRule := EqComp("InnerInt", equivalent)
cChecker.Add(eqRule, "invalid InnerInt[eq]")
neRule := NeComp("InnerInt", inequivalent)
cChecker.Add(neRule, "invalid InnerInt[ne]")
rangeRule := RangeComp("InnerInt", ge, le)
cChecker.Add(rangeRule, "invalid InnerInt[range]")
param := comp2{
InnerInt: &innerInt{Val: 100},
//InnerInt: nil,
}
isValid, _, _ := cChecker.Check(param)
assert.Equal(t, true, isValid)
}
func TestMapRuleSimple(t *testing.T) {
mChecker := NewChecker()
keyRangeRule := RangeInt("", 1, 10)
valueEnumRule := InInt("", 8, 9, 10)
mapRule := Map("", keyRangeRule, valueEnumRule)
mChecker.Add(mapRule, "invalid map")
m := map[int]int{
1: 8,
2: 9,
3: 10,
}
isValid, _, _ := mChecker.Check(m)
assert.Equal(t, true, isValid)
}
type keyStruct struct {
Key int
}
type valueStruct struct {
Value int
}
type mapStruct struct {
Map map[keyStruct]valueStruct
}
func TestMapRuleStruct(t *testing.T) {
mChecker := NewChecker()
keyRangeRule := RangeInt("Key", 1, 10)
valueEnumRule := InInt("Value", 8, 9, 10)
mapRule := Map("Map", keyRangeRule, valueEnumRule)
mChecker.Add(mapRule, "invalid map")
kvMap := make(map[keyStruct]valueStruct)
keys := []keyStruct{{1}, {2}, {3}}
for _, key := range keys {
kvMap[key] = valueStruct{Value: 9}
}
m := mapStruct{
kvMap,
}
isValid, _, _ := mChecker.Check(m)
assert.Equal(t, true, isValid)
}