-
Notifications
You must be signed in to change notification settings - Fork 4
/
assert.go
332 lines (267 loc) · 7.97 KB
/
assert.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package strutils
import (
"runtime"
"strings"
"testing"
)
// Assert is Methods for helping testing the strutils pkg.
type Assert struct {
plib *StringProc
}
// NewAssert Creates and returns a String processing methods's pointer.
func NewAssert() *Assert {
obj := &Assert{}
obj.plib = NewStringProc()
return obj
}
//a.printMsg is equivalent to t.Errorf include other information for easy debug
func (a *Assert) printMsg(t *testing.T, v1 interface{}, v2 interface{}, msgfmt string, args ...interface{}) {
outf := t.Errorf
out := t.Error
if UNITTESTMODE { //assertion methods test on go test
outf = t.Logf
out = t.Log
t.Log("*** Don't Worry error below, Just Testing ***")
}
funcn, file, line, _ := runtime.Caller(2)
out(strings.Repeat("-", 120))
outf("+ %v:%v\n", file, line)
outf("+ %+v\n", runtime.FuncForPC(funcn).Name())
out(strings.Repeat("-", 120))
outf(msgfmt, args...)
outf("- value1 : %+v\n", v1)
if v2 != nil {
outf("- value2 : %+v\n", v2)
}
out(strings.Repeat("-", 120))
}
/*
//TODO: will remove below, numericTypeUpCase better than below
//isCompareableNum asserts the specified objects are can compareble
func (a *Assert) isComparableNum(t *testing.T, v1 interface{}, v2 interface{}) bool {
if !reflect.ValueOf(v1).IsValid() || !reflect.ValueOf(v2).IsValid() {
a.printMsg(t, v1, v2, "Invalid Value")
return false
}
refv1 := reflect.TypeOf(v1)
refv2 := reflect.TypeOf(v2)
if refv1.Comparable() != refv2.Comparable() {
a.printMsg(t, v1, v2, "Not Comparable")
return false
}
refv1k := refv1.Kind()
refv2k := refv2.Kind()
//int ~ int64 (0x2 ~ 0x6)
//uint ~ uint64 (0x7 ~ 0xc)
//float ~ float64 (0xd ~ 0xe)
if (refv1k >= 0x2 && refv1k <= 0xe) && (refv2k >= 0x2 && refv2k <= 0xe) {
return true
}
a.printMsg(t, v1, v2, "Different Type v1.(%+v) != v2(%+v)", refv1k, refv2k)
return false
}
*/
//numericTypeUpCase converts the any numeric type to upsize type of that
func (a *Assert) numericTypeUpCase(val interface{}) (int64, uint64, float64, bool) {
var tmpint int64
var tmpuint uint64
var tmpfloat float64
switch val.(type) {
case int:
tmpint = int64(val.(int))
case int8:
tmpint = int64(val.(int8))
case int16:
tmpint = int64(val.(int16))
case int32:
tmpint = int64(val.(int32))
case int64:
tmpint = val.(int64)
case uint:
tmpuint = uint64(val.(uint))
case uint8:
tmpuint = uint64(val.(uint8))
case uint16:
tmpuint = uint64(val.(uint16))
case uint32:
tmpuint = uint64(val.(uint32))
case uint64:
tmpuint = val.(uint64)
case float32:
tmpfloat = float64(val.(float32))
case float64:
tmpfloat = val.(float64)
default:
return 0, 0, 0, false
}
return tmpint, tmpuint, tmpfloat, true
}
//AssertLog formats its arguments using default formatting, analogous to t.Log
func (a *Assert) AssertLog(t *testing.T, err error, msgfmt string, args ...interface{}) {
if err != nil {
t.Logf("Error : %v", err)
}
if len(args) > 0 {
t.Logf(msgfmt, args...)
} else {
t.Log(msgfmt)
}
}
//AssertEquals asserts that two objects are equal.
func (a *Assert) AssertEquals(t *testing.T, v1 interface{}, v2 interface{}, msgfmt string, args ...interface{}) {
_, err := a.plib.AnyCompare(v1, v2)
if err != nil {
a.printMsg(t, v1, v2, err.Error())
}
}
//AssertNotEquals asserts that two objects are not equal.
func (a *Assert) AssertNotEquals(t *testing.T, v1 interface{}, v2 interface{}, msgfmt string, args ...interface{}) {
_, err := a.plib.AnyCompare(v1, v2)
if err == nil && v1 == v2 {
a.printMsg(t, v1, v2, msgfmt, args...)
}
}
//AssertFalse asserts that the specified value is false.
func (a *Assert) AssertFalse(t *testing.T, v1 bool, msgfmt string, args ...interface{}) {
if v1 {
a.printMsg(t, v1, nil, msgfmt, args...)
}
}
//AssertTrue asserts that the specified value is true.
func (a *Assert) AssertTrue(t *testing.T, v1 bool, msgfmt string, args ...interface{}) {
if !v1 {
a.printMsg(t, v1, nil, msgfmt, args...)
}
}
//AssertNil asserts that the specified value is nil.
func (a *Assert) AssertNil(t *testing.T, v1 interface{}, msgfmt string, args ...interface{}) {
if v1 != nil {
a.printMsg(t, v1, nil, msgfmt, args...)
}
}
//AssertNotNil asserts that the specified value isn't nil.
func (a *Assert) AssertNotNil(t *testing.T, v1 interface{}, msgfmt string, args ...interface{}) {
if v1 == nil {
a.printMsg(t, v1, nil, msgfmt, args...)
}
}
//AssertLessThan asserts that the specified value are v1 less than v2
func (a *Assert) AssertLessThan(t *testing.T, v1 interface{}, v2 interface{}, msgfmt string, args ...interface{}) {
/*
if !a.isComparableNum(t, v1, v2) {
return
}
*/
tmpv1int, tmpv1uint, tmpv1float, ok := a.numericTypeUpCase(v1)
if !ok {
a.printMsg(t, v1, v2, "Required value 1 must be a numeric (int,uint,float with bit (8~64)")
return
}
tmpv2int, tmpv2uint, tmpv2float, ok := a.numericTypeUpCase(v2)
if !ok {
a.printMsg(t, v1, v2, "Required value 2 must be a numeric (int,uint,float with bit (8~64)")
return
}
var retval bool
switch v1.(type) {
case int, int8, int16, int32, int64:
retval = (tmpv1int < tmpv2int)
case uint, uint8, uint16, uint32, uint64:
retval = (tmpv1uint < tmpv2uint)
case float32, float64:
retval = (tmpv1float < tmpv2float)
}
if !retval {
a.printMsg(t, v1, v2, msgfmt, args...)
}
}
//AssertLessThanEqualTo asserts that the specified value are v1 less than v2 or equal to
func (a *Assert) AssertLessThanEqualTo(t *testing.T, v1 interface{}, v2 interface{}, msgfmt string, args ...interface{}) {
/*
if !a.isComparableNum(t, v1, v2) {
return
}
*/
tmpv1int, tmpv1uint, tmpv1float, ok := a.numericTypeUpCase(v1)
if !ok {
a.printMsg(t, v1, v2, "Required value 1 must be a numeric (int,uint,float with bit (8~64)")
return
}
tmpv2int, tmpv2uint, tmpv2float, ok := a.numericTypeUpCase(v2)
if !ok {
a.printMsg(t, v1, v2, "Required value 2 must be a numeric (int,uint,float with bit (8~64)")
return
}
var retval bool
switch v1.(type) {
case int, int8, int16, int32, int64:
retval = (tmpv1int <= tmpv2int)
case uint, uint8, uint16, uint32, uint64:
retval = (tmpv1uint <= tmpv2uint)
case float32, float64:
retval = (tmpv1float <= tmpv2float)
}
if !retval {
a.printMsg(t, v1, v2, msgfmt, args...)
}
}
//AssertGreaterThan nsserts that the specified value are v1 greater than v2
func (a *Assert) AssertGreaterThan(t *testing.T, v1 interface{}, v2 interface{}, msgfmt string, args ...interface{}) {
/*
if !a.isComparableNum(t, v1, v2) {
return
}
*/
tmpv1int, tmpv1uint, tmpv1float, ok := a.numericTypeUpCase(v1)
if !ok {
a.printMsg(t, v1, v2, "Required value 1 must be a numeric (int,uint,float with bit (8~64)")
return
}
tmpv2int, tmpv2uint, tmpv2float, ok := a.numericTypeUpCase(v2)
if !ok {
a.printMsg(t, v1, v2, "Required value 2 must be a numeric (int,uint,float with bit (8~64)")
return
}
retval := false
switch v1.(type) {
case int, int8, int16, int32, int64:
retval = (tmpv1int > tmpv2int)
case uint, uint8, uint16, uint32, uint64:
retval = (tmpv1uint > tmpv2uint)
case float32, float64:
retval = (tmpv1float > tmpv2float)
}
if !retval {
a.printMsg(t, v1, v2, msgfmt, args...)
}
}
//AssertGreaterThanEqualTo asserts that the specified value are v1 greater than v2 or equal to
func (a *Assert) AssertGreaterThanEqualTo(t *testing.T, v1 interface{}, v2 interface{}, msgfmt string, args ...interface{}) {
/*
if !a.isComparableNum(t, v1, v2) {
return
}
*/
tmpv1int, tmpv1uint, tmpv1float, ok := a.numericTypeUpCase(v1)
if !ok {
a.printMsg(t, v1, v2, "Required value 1 must be a numeric (int,uint,float with bit (8~64)")
return
}
tmpv2int, tmpv2uint, tmpv2float, ok := a.numericTypeUpCase(v2)
if !ok {
a.printMsg(t, v1, v2, "Required value 2 must be a numeric (int,uint,float with bit (8~64)")
return
}
retval := false
switch v1.(type) {
case int, int8, int16, int32, int64:
retval = (tmpv1int >= tmpv2int)
case uint, uint8, uint16, uint32, uint64:
retval = (tmpv1uint >= tmpv2uint)
case float32, float64:
retval = (tmpv1float >= tmpv2float)
}
if !retval {
a.printMsg(t, v1, v2, msgfmt, args...)
}
}