forked from zpsean/go4api
/
assertion.go
254 lines (204 loc) · 6.2 KB
/
assertion.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
* go4api - an api testing tool written in Go
* Created by: Ping Zhu 2018
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
*/
package assertion
import (
// "fmt"
// "os"
"reflect"
"strings"
// "encoding/json"
// "strconv"
"regexp"
)
//
func CallAssertion (name string, params ... interface{}) bool {
if !ValidateCallName(name) {
return false
}
if ifBothNil(params) {
return true
} else if !ValidateCallParams(name, params) {
var r bool
n := strings.ToLower(name)
if strings.Contains(n, "not") {
r = true
} else {
r = false
}
return r
} else {
f := reflect.ValueOf(assertionMapping[name].AssertionFunc)
in := make([]reflect.Value, len(params))
for k, param := range params {
in[k] = reflect.ValueOf(param)
}
result := f.Call(in)
return result[0].Interface().(bool)
}
}
// for both String and Numeric
func Equals (actualValue interface{}, expValue interface{}) bool {
switch reflect.TypeOf(actualValue).Kind().String() {
case "int", "float64": {
actualValueConverted, expValueConverted := convertIntToFloat64 (actualValue, expValue)
if actualValueConverted == expValueConverted {
return true
} else {
return false
}
}
case "string": {
if actualValue.(string) == expValue.(string) {
return true
} else {
return false
}
}
case "bool": {
if actualValue.(bool) == expValue.(bool) {
return true
} else {
return false
}
}
default:
if reflect.DeepEqual(actualValue, expValue) {
return true
} else {
return false
}
}
}
func convertIntToFloat64 (actualValue interface{}, expValue interface{}) (float64, float64) {
var actualValueConverted float64
var expValueConverted float64
if reflect.TypeOf(actualValue).String() == "int" {
actualValueConverted = float64(actualValue.(int))
} else if reflect.TypeOf(actualValue).String() == "int64" {
actualValueConverted = float64(actualValue.(int64))
} else {
actualValueConverted = actualValue.(float64)
}
if reflect.TypeOf(expValue).String() == "int" {
expValueConverted = float64(expValue.(int))
} else if reflect.TypeOf(expValue).String() == "int64" {
expValueConverted = float64(expValue.(int64))
} else {
expValueConverted = expValue.(float64)
}
return actualValueConverted, expValueConverted
}
func NotEquals (actualValue interface{}, expValue interface{}) bool {
return !reflect.DeepEqual(actualValue, expValue)
}
// string
func Contains (actualValue interface{}, expValue interface{}) bool {
if strings.Contains(actualValue.(string), expValue.(string)) {
return true
} else {
return false
}
}
func StartsWith (actualValue interface{}, expValue interface{}) bool {
if strings.HasPrefix(actualValue.(string), expValue.(string)) {
return true
} else {
return false
}
}
func EndsWith (actualValue interface{}, expValue interface{}) bool {
if strings.HasSuffix(actualValue.(string), expValue.(string)) {
return true
} else {
return false
}
}
// float
func Less (actualValue interface{}, expValue interface{}) bool {
actualValueConverted, expValueConverted := convertIntToFloat64 (actualValue, expValue)
if actualValueConverted < expValueConverted {
return true
} else {
return false
}
}
func LessOrEquals (actualValue interface{}, expValue interface{}) bool {
actualValueConverted, expValueConverted := convertIntToFloat64 (actualValue, expValue)
if actualValueConverted <= expValueConverted {
return true
} else {
return false
}
}
func Greater (actualValue interface{}, expValue interface{}) bool {
actualValueConverted, expValueConverted := convertIntToFloat64 (actualValue, expValue)
if actualValueConverted > expValueConverted {
return true
} else {
return false
}
}
func GreaterOrEquals (actualValue interface{}, expValue interface{}) bool {
actualValueConverted, expValueConverted := convertIntToFloat64 (actualValue, expValue)
if actualValueConverted >= expValueConverted {
return true
} else {
return false
}
}
// In, NotIn, Has, NotHas, for item in item slice
func In (actualValue interface{}, expValue interface{}) bool {
var ifIn bool
for _, value := range reflect.ValueOf(expValue).Interface().([]interface{}) {
if CallAssertion("Equals", actualValue, value) {
ifIn = true
break
}
}
return ifIn
}
func NotIn (actualValue interface{}, expValue interface{}) bool {
return !In(actualValue, expValue)
}
func Has (actualValue interface{}, expValue interface{}) bool {
var ifHas bool
for _, value := range reflect.ValueOf(actualValue).Interface().([]interface{}) {
if CallAssertion("Equals", value, expValue) {
ifHas = true
break
}
}
return ifHas
}
func NotHas (actualValue interface{}, expValue interface{}) bool {
return !Has(actualValue, expValue)
}
// for regrex
func Match (actualValue interface{}, expPattern interface{}) bool {
reg := regexp.MustCompile(expPattern.(string))
resSlice := reg.FindAllString(actualValue.(string), -1)
if resSlice != nil {
return true
} else {
return false
}
}
// special cases for map:
// map has key
func HasMapKey (actualValue interface{}, expValue interface{}) {
}
func NotHasMapKey (actualValue interface{}, expValue interface{}) {
}
// has key but value is null
func IsNull (actualValue interface{}, expValue interface{}) {
}
// has key but value is not null
func IsNotNull (actualValue interface{}, expValue interface{}) {
}