1
2
3
4
5
6
7 package assert
8
9 import (
10 "bytes"
11 "fmt"
12 "reflect"
13 "runtime"
14 "testing"
15 )
16
17 func TestCompare(t *testing.T) {
18 type customInt int
19 type customInt8 int8
20 type customInt16 int16
21 type customInt32 int32
22 type customInt64 int64
23 type customUInt uint
24 type customUInt8 uint8
25 type customUInt16 uint16
26 type customUInt32 uint32
27 type customUInt64 uint64
28 type customFloat32 float32
29 type customFloat64 float64
30 type customString string
31 for _, currCase := range []struct {
32 less interface{}
33 greater interface{}
34 cType string
35 }{
36 {less: customString("a"), greater: customString("b"), cType: "string"},
37 {less: "a", greater: "b", cType: "string"},
38 {less: customInt(1), greater: customInt(2), cType: "int"},
39 {less: int(1), greater: int(2), cType: "int"},
40 {less: customInt8(1), greater: customInt8(2), cType: "int8"},
41 {less: int8(1), greater: int8(2), cType: "int8"},
42 {less: customInt16(1), greater: customInt16(2), cType: "int16"},
43 {less: int16(1), greater: int16(2), cType: "int16"},
44 {less: customInt32(1), greater: customInt32(2), cType: "int32"},
45 {less: int32(1), greater: int32(2), cType: "int32"},
46 {less: customInt64(1), greater: customInt64(2), cType: "int64"},
47 {less: int64(1), greater: int64(2), cType: "int64"},
48 {less: customUInt(1), greater: customUInt(2), cType: "uint"},
49 {less: uint8(1), greater: uint8(2), cType: "uint8"},
50 {less: customUInt8(1), greater: customUInt8(2), cType: "uint8"},
51 {less: uint16(1), greater: uint16(2), cType: "uint16"},
52 {less: customUInt16(1), greater: customUInt16(2), cType: "uint16"},
53 {less: uint32(1), greater: uint32(2), cType: "uint32"},
54 {less: customUInt32(1), greater: customUInt32(2), cType: "uint32"},
55 {less: uint64(1), greater: uint64(2), cType: "uint64"},
56 {less: customUInt64(1), greater: customUInt64(2), cType: "uint64"},
57 {less: float32(1.23), greater: float32(2.34), cType: "float32"},
58 {less: customFloat32(1.23), greater: customFloat32(2.23), cType: "float32"},
59 {less: float64(1.23), greater: float64(2.34), cType: "float64"},
60 {less: customFloat64(1.23), greater: customFloat64(2.34), cType: "float64"},
61 } {
62 resLess, isComparable := compare(currCase.less, currCase.greater, reflect.ValueOf(currCase.less).Kind())
63 if !isComparable {
64 t.Error("object should be comparable for type " + currCase.cType)
65 }
66
67 if resLess != compareLess {
68 t.Errorf("object less (%v) should be less than greater (%v) for type "+currCase.cType,
69 currCase.less, currCase.greater)
70 }
71
72 resGreater, isComparable := compare(currCase.greater, currCase.less, reflect.ValueOf(currCase.less).Kind())
73 if !isComparable {
74 t.Error("object are comparable for type " + currCase.cType)
75 }
76
77 if resGreater != compareGreater {
78 t.Errorf("object greater should be greater than less for type " + currCase.cType)
79 }
80
81 resEqual, isComparable := compare(currCase.less, currCase.less, reflect.ValueOf(currCase.less).Kind())
82 if !isComparable {
83 t.Error("object are comparable for type " + currCase.cType)
84 }
85
86 if resEqual != 0 {
87 t.Errorf("objects should be equal for type " + currCase.cType)
88 }
89 }
90 }
91
92 type outputT struct {
93 buf *bytes.Buffer
94 helpers map[string]struct{}
95 }
96
97
98 func (t *outputT) Errorf(format string, args ...interface{}) {
99 s := fmt.Sprintf(format, args...)
100 t.buf.WriteString(s)
101 }
102
103 func (t *outputT) Helper() {
104 if t.helpers == nil {
105 t.helpers = make(map[string]struct{})
106 }
107 t.helpers[callerName(1)] = struct{}{}
108 }
109
110
111
112 func callerName(skip int) string {
113
114 var pc [1]uintptr
115 n := runtime.Callers(skip+2, pc[:])
116 if n == 0 {
117 panic("testing: zero callers found")
118 }
119 frames := runtime.CallersFrames(pc[:n])
120 frame, _ := frames.Next()
121 return frame.Function
122 }
123
124 func TestGreater(t *testing.T) {
125 mockT := new(testing.T)
126
127 if !Greater(mockT, 2, 1) {
128 t.Error("Greater should return true")
129 }
130
131 if Greater(mockT, 1, 1) {
132 t.Error("Greater should return false")
133 }
134
135 if Greater(mockT, 1, 2) {
136 t.Error("Greater should return false")
137 }
138
139
140 for _, currCase := range []struct {
141 less interface{}
142 greater interface{}
143 msg string
144 }{
145 {less: "a", greater: "b", msg: `"a" is not greater than "b"`},
146 {less: int(1), greater: int(2), msg: `"1" is not greater than "2"`},
147 {less: int8(1), greater: int8(2), msg: `"1" is not greater than "2"`},
148 {less: int16(1), greater: int16(2), msg: `"1" is not greater than "2"`},
149 {less: int32(1), greater: int32(2), msg: `"1" is not greater than "2"`},
150 {less: int64(1), greater: int64(2), msg: `"1" is not greater than "2"`},
151 {less: uint8(1), greater: uint8(2), msg: `"1" is not greater than "2"`},
152 {less: uint16(1), greater: uint16(2), msg: `"1" is not greater than "2"`},
153 {less: uint32(1), greater: uint32(2), msg: `"1" is not greater than "2"`},
154 {less: uint64(1), greater: uint64(2), msg: `"1" is not greater than "2"`},
155 {less: float32(1.23), greater: float32(2.34), msg: `"1.23" is not greater than "2.34"`},
156 {less: float64(1.23), greater: float64(2.34), msg: `"1.23" is not greater than "2.34"`},
157 } {
158 out := &outputT{buf: bytes.NewBuffer(nil)}
159 False(t, Greater(out, currCase.less, currCase.greater))
160 Contains(t, out.buf.String(), currCase.msg)
161 Contains(t, out.helpers, "go.mongodb.org/mongo-driver/internal/assert.Greater")
162 }
163 }
164
165 func TestGreaterOrEqual(t *testing.T) {
166 mockT := new(testing.T)
167
168 if !GreaterOrEqual(mockT, 2, 1) {
169 t.Error("GreaterOrEqual should return true")
170 }
171
172 if !GreaterOrEqual(mockT, 1, 1) {
173 t.Error("GreaterOrEqual should return true")
174 }
175
176 if GreaterOrEqual(mockT, 1, 2) {
177 t.Error("GreaterOrEqual should return false")
178 }
179
180
181 for _, currCase := range []struct {
182 less interface{}
183 greater interface{}
184 msg string
185 }{
186 {less: "a", greater: "b", msg: `"a" is not greater than or equal to "b"`},
187 {less: int(1), greater: int(2), msg: `"1" is not greater than or equal to "2"`},
188 {less: int8(1), greater: int8(2), msg: `"1" is not greater than or equal to "2"`},
189 {less: int16(1), greater: int16(2), msg: `"1" is not greater than or equal to "2"`},
190 {less: int32(1), greater: int32(2), msg: `"1" is not greater than or equal to "2"`},
191 {less: int64(1), greater: int64(2), msg: `"1" is not greater than or equal to "2"`},
192 {less: uint8(1), greater: uint8(2), msg: `"1" is not greater than or equal to "2"`},
193 {less: uint16(1), greater: uint16(2), msg: `"1" is not greater than or equal to "2"`},
194 {less: uint32(1), greater: uint32(2), msg: `"1" is not greater than or equal to "2"`},
195 {less: uint64(1), greater: uint64(2), msg: `"1" is not greater than or equal to "2"`},
196 {less: float32(1.23), greater: float32(2.34), msg: `"1.23" is not greater than or equal to "2.34"`},
197 {less: float64(1.23), greater: float64(2.34), msg: `"1.23" is not greater than or equal to "2.34"`},
198 } {
199 out := &outputT{buf: bytes.NewBuffer(nil)}
200 False(t, GreaterOrEqual(out, currCase.less, currCase.greater))
201 Contains(t, out.buf.String(), currCase.msg)
202 Contains(t, out.helpers, "go.mongodb.org/mongo-driver/internal/assert.GreaterOrEqual")
203 }
204 }
205
206 func TestLess(t *testing.T) {
207 mockT := new(testing.T)
208
209 if !Less(mockT, 1, 2) {
210 t.Error("Less should return true")
211 }
212
213 if Less(mockT, 1, 1) {
214 t.Error("Less should return false")
215 }
216
217 if Less(mockT, 2, 1) {
218 t.Error("Less should return false")
219 }
220
221
222 for _, currCase := range []struct {
223 less interface{}
224 greater interface{}
225 msg string
226 }{
227 {less: "a", greater: "b", msg: `"b" is not less than "a"`},
228 {less: int(1), greater: int(2), msg: `"2" is not less than "1"`},
229 {less: int8(1), greater: int8(2), msg: `"2" is not less than "1"`},
230 {less: int16(1), greater: int16(2), msg: `"2" is not less than "1"`},
231 {less: int32(1), greater: int32(2), msg: `"2" is not less than "1"`},
232 {less: int64(1), greater: int64(2), msg: `"2" is not less than "1"`},
233 {less: uint8(1), greater: uint8(2), msg: `"2" is not less than "1"`},
234 {less: uint16(1), greater: uint16(2), msg: `"2" is not less than "1"`},
235 {less: uint32(1), greater: uint32(2), msg: `"2" is not less than "1"`},
236 {less: uint64(1), greater: uint64(2), msg: `"2" is not less than "1"`},
237 {less: float32(1.23), greater: float32(2.34), msg: `"2.34" is not less than "1.23"`},
238 {less: float64(1.23), greater: float64(2.34), msg: `"2.34" is not less than "1.23"`},
239 } {
240 out := &outputT{buf: bytes.NewBuffer(nil)}
241 False(t, Less(out, currCase.greater, currCase.less))
242 Contains(t, out.buf.String(), currCase.msg)
243 Contains(t, out.helpers, "go.mongodb.org/mongo-driver/internal/assert.Less")
244 }
245 }
246
247 func TestLessOrEqual(t *testing.T) {
248 mockT := new(testing.T)
249
250 if !LessOrEqual(mockT, 1, 2) {
251 t.Error("LessOrEqual should return true")
252 }
253
254 if !LessOrEqual(mockT, 1, 1) {
255 t.Error("LessOrEqual should return true")
256 }
257
258 if LessOrEqual(mockT, 2, 1) {
259 t.Error("LessOrEqual should return false")
260 }
261
262
263 for _, currCase := range []struct {
264 less interface{}
265 greater interface{}
266 msg string
267 }{
268 {less: "a", greater: "b", msg: `"b" is not less than or equal to "a"`},
269 {less: int(1), greater: int(2), msg: `"2" is not less than or equal to "1"`},
270 {less: int8(1), greater: int8(2), msg: `"2" is not less than or equal to "1"`},
271 {less: int16(1), greater: int16(2), msg: `"2" is not less than or equal to "1"`},
272 {less: int32(1), greater: int32(2), msg: `"2" is not less than or equal to "1"`},
273 {less: int64(1), greater: int64(2), msg: `"2" is not less than or equal to "1"`},
274 {less: uint8(1), greater: uint8(2), msg: `"2" is not less than or equal to "1"`},
275 {less: uint16(1), greater: uint16(2), msg: `"2" is not less than or equal to "1"`},
276 {less: uint32(1), greater: uint32(2), msg: `"2" is not less than or equal to "1"`},
277 {less: uint64(1), greater: uint64(2), msg: `"2" is not less than or equal to "1"`},
278 {less: float32(1.23), greater: float32(2.34), msg: `"2.34" is not less than or equal to "1.23"`},
279 {less: float64(1.23), greater: float64(2.34), msg: `"2.34" is not less than or equal to "1.23"`},
280 } {
281 out := &outputT{buf: bytes.NewBuffer(nil)}
282 False(t, LessOrEqual(out, currCase.greater, currCase.less))
283 Contains(t, out.buf.String(), currCase.msg)
284 Contains(t, out.helpers, "go.mongodb.org/mongo-driver/internal/assert.LessOrEqual")
285 }
286 }
287
288 func TestPositive(t *testing.T) {
289 mockT := new(testing.T)
290
291 if !Positive(mockT, 1) {
292 t.Error("Positive should return true")
293 }
294
295 if !Positive(mockT, 1.23) {
296 t.Error("Positive should return true")
297 }
298
299 if Positive(mockT, -1) {
300 t.Error("Positive should return false")
301 }
302
303 if Positive(mockT, -1.23) {
304 t.Error("Positive should return false")
305 }
306
307
308 for _, currCase := range []struct {
309 e interface{}
310 msg string
311 }{
312 {e: int(-1), msg: `"-1" is not positive`},
313 {e: int8(-1), msg: `"-1" is not positive`},
314 {e: int16(-1), msg: `"-1" is not positive`},
315 {e: int32(-1), msg: `"-1" is not positive`},
316 {e: int64(-1), msg: `"-1" is not positive`},
317 {e: float32(-1.23), msg: `"-1.23" is not positive`},
318 {e: float64(-1.23), msg: `"-1.23" is not positive`},
319 } {
320 out := &outputT{buf: bytes.NewBuffer(nil)}
321 False(t, Positive(out, currCase.e))
322 Contains(t, out.buf.String(), currCase.msg)
323 Contains(t, out.helpers, "go.mongodb.org/mongo-driver/internal/assert.Positive")
324 }
325 }
326
327 func TestNegative(t *testing.T) {
328 mockT := new(testing.T)
329
330 if !Negative(mockT, -1) {
331 t.Error("Negative should return true")
332 }
333
334 if !Negative(mockT, -1.23) {
335 t.Error("Negative should return true")
336 }
337
338 if Negative(mockT, 1) {
339 t.Error("Negative should return false")
340 }
341
342 if Negative(mockT, 1.23) {
343 t.Error("Negative should return false")
344 }
345
346
347 for _, currCase := range []struct {
348 e interface{}
349 msg string
350 }{
351 {e: int(1), msg: `"1" is not negative`},
352 {e: int8(1), msg: `"1" is not negative`},
353 {e: int16(1), msg: `"1" is not negative`},
354 {e: int32(1), msg: `"1" is not negative`},
355 {e: int64(1), msg: `"1" is not negative`},
356 {e: float32(1.23), msg: `"1.23" is not negative`},
357 {e: float64(1.23), msg: `"1.23" is not negative`},
358 } {
359 out := &outputT{buf: bytes.NewBuffer(nil)}
360 False(t, Negative(out, currCase.e))
361 Contains(t, out.buf.String(), currCase.msg)
362 Contains(t, out.helpers, "go.mongodb.org/mongo-driver/internal/assert.Negative")
363 }
364 }
365
366 func Test_compareTwoValuesDifferentValuesTypes(t *testing.T) {
367 mockT := new(testing.T)
368
369 for _, currCase := range []struct {
370 v1 interface{}
371 v2 interface{}
372 compareResult bool
373 }{
374 {v1: 123, v2: "abc"},
375 {v1: "abc", v2: 123456},
376 {v1: float64(12), v2: "123"},
377 {v1: "float(12)", v2: float64(1)},
378 } {
379 compareResult := compareTwoValues(mockT, currCase.v1, currCase.v2, []CompareType{compareLess, compareEqual, compareGreater}, "testFailMessage")
380 False(t, compareResult)
381 }
382 }
383
384 func Test_compareTwoValuesNotComparableValues(t *testing.T) {
385 mockT := new(testing.T)
386
387 type CompareStruct struct {
388 }
389
390 for _, currCase := range []struct {
391 v1 interface{}
392 v2 interface{}
393 }{
394 {v1: CompareStruct{}, v2: CompareStruct{}},
395 {v1: map[string]int{}, v2: map[string]int{}},
396 {v1: make([]int, 5), v2: make([]int, 5)},
397 } {
398 compareResult := compareTwoValues(mockT, currCase.v1, currCase.v2, []CompareType{compareLess, compareEqual, compareGreater}, "testFailMessage")
399 False(t, compareResult)
400 }
401 }
402
403 func Test_compareTwoValuesCorrectCompareResult(t *testing.T) {
404 mockT := new(testing.T)
405
406 for _, currCase := range []struct {
407 v1 interface{}
408 v2 interface{}
409 compareTypes []CompareType
410 }{
411 {v1: 1, v2: 2, compareTypes: []CompareType{compareLess}},
412 {v1: 1, v2: 2, compareTypes: []CompareType{compareLess, compareEqual}},
413 {v1: 2, v2: 2, compareTypes: []CompareType{compareGreater, compareEqual}},
414 {v1: 2, v2: 2, compareTypes: []CompareType{compareEqual}},
415 {v1: 2, v2: 1, compareTypes: []CompareType{compareEqual, compareGreater}},
416 {v1: 2, v2: 1, compareTypes: []CompareType{compareGreater}},
417 } {
418 compareResult := compareTwoValues(mockT, currCase.v1, currCase.v2, currCase.compareTypes, "testFailMessage")
419 True(t, compareResult)
420 }
421 }
422
423 func Test_containsValue(t *testing.T) {
424 for _, currCase := range []struct {
425 values []CompareType
426 value CompareType
427 result bool
428 }{
429 {values: []CompareType{compareGreater}, value: compareGreater, result: true},
430 {values: []CompareType{compareGreater, compareLess}, value: compareGreater, result: true},
431 {values: []CompareType{compareGreater, compareLess}, value: compareLess, result: true},
432 {values: []CompareType{compareGreater, compareLess}, value: compareEqual, result: false},
433 } {
434 compareResult := containsValue(currCase.values, currCase.value)
435 Equal(t, currCase.result, compareResult)
436 }
437 }
438
439 func TestComparingMsgAndArgsForwarding(t *testing.T) {
440 msgAndArgs := []interface{}{"format %s %x", "this", 0xc001}
441 expectedOutput := "format this c001\n"
442 funcs := []func(t TestingT){
443 func(t TestingT) { Greater(t, 1, 2, msgAndArgs...) },
444 func(t TestingT) { GreaterOrEqual(t, 1, 2, msgAndArgs...) },
445 func(t TestingT) { Less(t, 2, 1, msgAndArgs...) },
446 func(t TestingT) { LessOrEqual(t, 2, 1, msgAndArgs...) },
447 func(t TestingT) { Positive(t, 0, msgAndArgs...) },
448 func(t TestingT) { Negative(t, 0, msgAndArgs...) },
449 }
450 for _, f := range funcs {
451 out := &outputT{buf: bytes.NewBuffer(nil)}
452 f(out)
453 Contains(t, out.buf.String(), expectedOutput)
454 }
455 }
456
View as plain text