1 package require
2
3 import (
4 "encoding/json"
5 "errors"
6 "testing"
7 "time"
8 )
9
10
11 type AssertionTesterInterface interface {
12 TestMethod()
13 }
14
15
16 type AssertionTesterConformingObject struct {
17 }
18
19 func (a *AssertionTesterConformingObject) TestMethod() {
20 }
21
22
23 type AssertionTesterNonConformingObject struct {
24 }
25
26 type MockT struct {
27 Failed bool
28 }
29
30 func (t *MockT) FailNow() {
31 t.Failed = true
32 }
33
34 func (t *MockT) Errorf(format string, args ...interface{}) {
35 _, _ = format, args
36 }
37
38 func TestImplements(t *testing.T) {
39
40 Implements(t, (*AssertionTesterInterface)(nil), new(AssertionTesterConformingObject))
41
42 mockT := new(MockT)
43 Implements(mockT, (*AssertionTesterInterface)(nil), new(AssertionTesterNonConformingObject))
44 if !mockT.Failed {
45 t.Error("Check should fail")
46 }
47 }
48
49 func TestIsType(t *testing.T) {
50
51 IsType(t, new(AssertionTesterConformingObject), new(AssertionTesterConformingObject))
52
53 mockT := new(MockT)
54 IsType(mockT, new(AssertionTesterConformingObject), new(AssertionTesterNonConformingObject))
55 if !mockT.Failed {
56 t.Error("Check should fail")
57 }
58 }
59
60 func TestEqual(t *testing.T) {
61
62 Equal(t, 1, 1)
63
64 mockT := new(MockT)
65 Equal(mockT, 1, 2)
66 if !mockT.Failed {
67 t.Error("Check should fail")
68 }
69
70 }
71
72 func TestNotEqual(t *testing.T) {
73
74 NotEqual(t, 1, 2)
75 mockT := new(MockT)
76 NotEqual(mockT, 2, 2)
77 if !mockT.Failed {
78 t.Error("Check should fail")
79 }
80 }
81
82 func TestExactly(t *testing.T) {
83
84 a := float32(1)
85 b := float32(1)
86 c := float64(1)
87
88 Exactly(t, a, b)
89
90 mockT := new(MockT)
91 Exactly(mockT, a, c)
92 if !mockT.Failed {
93 t.Error("Check should fail")
94 }
95 }
96
97 func TestNotNil(t *testing.T) {
98
99 NotNil(t, new(AssertionTesterConformingObject))
100
101 mockT := new(MockT)
102 NotNil(mockT, nil)
103 if !mockT.Failed {
104 t.Error("Check should fail")
105 }
106 }
107
108 func TestNil(t *testing.T) {
109
110 Nil(t, nil)
111
112 mockT := new(MockT)
113 Nil(mockT, new(AssertionTesterConformingObject))
114 if !mockT.Failed {
115 t.Error("Check should fail")
116 }
117 }
118
119 func TestTrue(t *testing.T) {
120
121 True(t, true)
122
123 mockT := new(MockT)
124 True(mockT, false)
125 if !mockT.Failed {
126 t.Error("Check should fail")
127 }
128 }
129
130 func TestFalse(t *testing.T) {
131
132 False(t, false)
133
134 mockT := new(MockT)
135 False(mockT, true)
136 if !mockT.Failed {
137 t.Error("Check should fail")
138 }
139 }
140
141 func TestContains(t *testing.T) {
142
143 Contains(t, "Hello World", "Hello")
144
145 mockT := new(MockT)
146 Contains(mockT, "Hello World", "Salut")
147 if !mockT.Failed {
148 t.Error("Check should fail")
149 }
150 }
151
152 func TestNotContains(t *testing.T) {
153
154 NotContains(t, "Hello World", "Hello!")
155
156 mockT := new(MockT)
157 NotContains(mockT, "Hello World", "Hello")
158 if !mockT.Failed {
159 t.Error("Check should fail")
160 }
161 }
162
163 func TestPanics(t *testing.T) {
164
165 Panics(t, func() {
166 panic("Panic!")
167 })
168
169 mockT := new(MockT)
170 Panics(mockT, func() {})
171 if !mockT.Failed {
172 t.Error("Check should fail")
173 }
174 }
175
176 func TestNotPanics(t *testing.T) {
177
178 NotPanics(t, func() {})
179
180 mockT := new(MockT)
181 NotPanics(mockT, func() {
182 panic("Panic!")
183 })
184 if !mockT.Failed {
185 t.Error("Check should fail")
186 }
187 }
188
189 func TestNoError(t *testing.T) {
190
191 NoError(t, nil)
192
193 mockT := new(MockT)
194 NoError(mockT, errors.New("some error"))
195 if !mockT.Failed {
196 t.Error("Check should fail")
197 }
198 }
199
200 func TestError(t *testing.T) {
201
202 Error(t, errors.New("some error"))
203
204 mockT := new(MockT)
205 Error(mockT, nil)
206 if !mockT.Failed {
207 t.Error("Check should fail")
208 }
209 }
210
211 func TestErrorContains(t *testing.T) {
212
213 ErrorContains(t, errors.New("some error: another error"), "some error")
214
215 mockT := new(MockT)
216 ErrorContains(mockT, errors.New("some error"), "different error")
217 if !mockT.Failed {
218 t.Error("Check should fail")
219 }
220 }
221
222 func TestEqualError(t *testing.T) {
223
224 EqualError(t, errors.New("some error"), "some error")
225
226 mockT := new(MockT)
227 EqualError(mockT, errors.New("some error"), "Not some error")
228 if !mockT.Failed {
229 t.Error("Check should fail")
230 }
231 }
232
233 func TestEmpty(t *testing.T) {
234
235 Empty(t, "")
236
237 mockT := new(MockT)
238 Empty(mockT, "x")
239 if !mockT.Failed {
240 t.Error("Check should fail")
241 }
242 }
243
244 func TestNotEmpty(t *testing.T) {
245
246 NotEmpty(t, "x")
247
248 mockT := new(MockT)
249 NotEmpty(mockT, "")
250 if !mockT.Failed {
251 t.Error("Check should fail")
252 }
253 }
254
255 func TestWithinDuration(t *testing.T) {
256
257 a := time.Now()
258 b := a.Add(10 * time.Second)
259
260 WithinDuration(t, a, b, 15*time.Second)
261
262 mockT := new(MockT)
263 WithinDuration(mockT, a, b, 5*time.Second)
264 if !mockT.Failed {
265 t.Error("Check should fail")
266 }
267 }
268
269 func TestInDelta(t *testing.T) {
270
271 InDelta(t, 1.001, 1, 0.01)
272
273 mockT := new(MockT)
274 InDelta(mockT, 1, 2, 0.5)
275 if !mockT.Failed {
276 t.Error("Check should fail")
277 }
278 }
279
280 func TestZero(t *testing.T) {
281
282 Zero(t, "")
283
284 mockT := new(MockT)
285 Zero(mockT, "x")
286 if !mockT.Failed {
287 t.Error("Check should fail")
288 }
289 }
290
291 func TestNotZero(t *testing.T) {
292
293 NotZero(t, "x")
294
295 mockT := new(MockT)
296 NotZero(mockT, "")
297 if !mockT.Failed {
298 t.Error("Check should fail")
299 }
300 }
301
302 func TestJSONEq_EqualSONString(t *testing.T) {
303 mockT := new(MockT)
304 JSONEq(mockT, `{"hello": "world", "foo": "bar"}`, `{"hello": "world", "foo": "bar"}`)
305 if mockT.Failed {
306 t.Error("Check should pass")
307 }
308 }
309
310 func TestJSONEq_EquivalentButNotEqual(t *testing.T) {
311 mockT := new(MockT)
312 JSONEq(mockT, `{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`)
313 if mockT.Failed {
314 t.Error("Check should pass")
315 }
316 }
317
318 func TestJSONEq_HashOfArraysAndHashes(t *testing.T) {
319 mockT := new(MockT)
320 JSONEq(mockT, "{\r\n\t\"numeric\": 1.5,\r\n\t\"array\": [{\"foo\": \"bar\"}, 1, \"string\", [\"nested\", \"array\", 5.5]],\r\n\t\"hash\": {\"nested\": \"hash\", \"nested_slice\": [\"this\", \"is\", \"nested\"]},\r\n\t\"string\": \"foo\"\r\n}",
321 "{\r\n\t\"numeric\": 1.5,\r\n\t\"hash\": {\"nested\": \"hash\", \"nested_slice\": [\"this\", \"is\", \"nested\"]},\r\n\t\"string\": \"foo\",\r\n\t\"array\": [{\"foo\": \"bar\"}, 1, \"string\", [\"nested\", \"array\", 5.5]]\r\n}")
322 if mockT.Failed {
323 t.Error("Check should pass")
324 }
325 }
326
327 func TestJSONEq_Array(t *testing.T) {
328 mockT := new(MockT)
329 JSONEq(mockT, `["foo", {"hello": "world", "nested": "hash"}]`, `["foo", {"nested": "hash", "hello": "world"}]`)
330 if mockT.Failed {
331 t.Error("Check should pass")
332 }
333 }
334
335 func TestJSONEq_HashAndArrayNotEquivalent(t *testing.T) {
336 mockT := new(MockT)
337 JSONEq(mockT, `["foo", {"hello": "world", "nested": "hash"}]`, `{"foo": "bar", {"nested": "hash", "hello": "world"}}`)
338 if !mockT.Failed {
339 t.Error("Check should fail")
340 }
341 }
342
343 func TestJSONEq_HashesNotEquivalent(t *testing.T) {
344 mockT := new(MockT)
345 JSONEq(mockT, `{"foo": "bar"}`, `{"foo": "bar", "hello": "world"}`)
346 if !mockT.Failed {
347 t.Error("Check should fail")
348 }
349 }
350
351 func TestJSONEq_ActualIsNotJSON(t *testing.T) {
352 mockT := new(MockT)
353 JSONEq(mockT, `{"foo": "bar"}`, "Not JSON")
354 if !mockT.Failed {
355 t.Error("Check should fail")
356 }
357 }
358
359 func TestJSONEq_ExpectedIsNotJSON(t *testing.T) {
360 mockT := new(MockT)
361 JSONEq(mockT, "Not JSON", `{"foo": "bar", "hello": "world"}`)
362 if !mockT.Failed {
363 t.Error("Check should fail")
364 }
365 }
366
367 func TestJSONEq_ExpectedAndActualNotJSON(t *testing.T) {
368 mockT := new(MockT)
369 JSONEq(mockT, "Not JSON", "Not JSON")
370 if !mockT.Failed {
371 t.Error("Check should fail")
372 }
373 }
374
375 func TestJSONEq_ArraysOfDifferentOrder(t *testing.T) {
376 mockT := new(MockT)
377 JSONEq(mockT, `["foo", {"hello": "world", "nested": "hash"}]`, `[{ "hello": "world", "nested": "hash"}, "foo"]`)
378 if !mockT.Failed {
379 t.Error("Check should fail")
380 }
381 }
382
383 func TestYAMLEq_EqualYAMLString(t *testing.T) {
384 mockT := new(MockT)
385 YAMLEq(mockT, `{"hello": "world", "foo": "bar"}`, `{"hello": "world", "foo": "bar"}`)
386 if mockT.Failed {
387 t.Error("Check should pass")
388 }
389 }
390
391 func TestYAMLEq_EquivalentButNotEqual(t *testing.T) {
392 mockT := new(MockT)
393 YAMLEq(mockT, `{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`)
394 if mockT.Failed {
395 t.Error("Check should pass")
396 }
397 }
398
399 func TestYAMLEq_HashOfArraysAndHashes(t *testing.T) {
400 mockT := new(MockT)
401 expected := `
402 numeric: 1.5
403 array:
404 - foo: bar
405 - 1
406 - "string"
407 - ["nested", "array", 5.5]
408 hash:
409 nested: hash
410 nested_slice: [this, is, nested]
411 string: "foo"
412 `
413
414 actual := `
415 numeric: 1.5
416 hash:
417 nested: hash
418 nested_slice: [this, is, nested]
419 string: "foo"
420 array:
421 - foo: bar
422 - 1
423 - "string"
424 - ["nested", "array", 5.5]
425 `
426 YAMLEq(mockT, expected, actual)
427 if mockT.Failed {
428 t.Error("Check should pass")
429 }
430 }
431
432 func TestYAMLEq_Array(t *testing.T) {
433 mockT := new(MockT)
434 YAMLEq(mockT, `["foo", {"hello": "world", "nested": "hash"}]`, `["foo", {"nested": "hash", "hello": "world"}]`)
435 if mockT.Failed {
436 t.Error("Check should pass")
437 }
438 }
439
440 func TestYAMLEq_HashAndArrayNotEquivalent(t *testing.T) {
441 mockT := new(MockT)
442 YAMLEq(mockT, `["foo", {"hello": "world", "nested": "hash"}]`, `{"foo": "bar", {"nested": "hash", "hello": "world"}}`)
443 if !mockT.Failed {
444 t.Error("Check should fail")
445 }
446 }
447
448 func TestYAMLEq_HashesNotEquivalent(t *testing.T) {
449 mockT := new(MockT)
450 YAMLEq(mockT, `{"foo": "bar"}`, `{"foo": "bar", "hello": "world"}`)
451 if !mockT.Failed {
452 t.Error("Check should fail")
453 }
454 }
455
456 func TestYAMLEq_ActualIsSimpleString(t *testing.T) {
457 mockT := new(MockT)
458 YAMLEq(mockT, `{"foo": "bar"}`, "Simple String")
459 if !mockT.Failed {
460 t.Error("Check should fail")
461 }
462 }
463
464 func TestYAMLEq_ExpectedIsSimpleString(t *testing.T) {
465 mockT := new(MockT)
466 YAMLEq(mockT, "Simple String", `{"foo": "bar", "hello": "world"}`)
467 if !mockT.Failed {
468 t.Error("Check should fail")
469 }
470 }
471
472 func TestYAMLEq_ExpectedAndActualSimpleString(t *testing.T) {
473 mockT := new(MockT)
474 YAMLEq(mockT, "Simple String", "Simple String")
475 if mockT.Failed {
476 t.Error("Check should pass")
477 }
478 }
479
480 func TestYAMLEq_ArraysOfDifferentOrder(t *testing.T) {
481 mockT := new(MockT)
482 YAMLEq(mockT, `["foo", {"hello": "world", "nested": "hash"}]`, `[{ "hello": "world", "nested": "hash"}, "foo"]`)
483 if !mockT.Failed {
484 t.Error("Check should fail")
485 }
486 }
487
488 func ExampleComparisonAssertionFunc() {
489 t := &testing.T{}
490
491 adder := func(x, y int) int {
492 return x + y
493 }
494
495 type args struct {
496 x int
497 y int
498 }
499
500 tests := []struct {
501 name string
502 args args
503 expect int
504 assertion ComparisonAssertionFunc
505 }{
506 {"2+2=4", args{2, 2}, 4, Equal},
507 {"2+2!=5", args{2, 2}, 5, NotEqual},
508 {"2+3==5", args{2, 3}, 5, Exactly},
509 }
510
511 for _, tt := range tests {
512 t.Run(tt.name, func(t *testing.T) {
513 tt.assertion(t, tt.expect, adder(tt.args.x, tt.args.y))
514 })
515 }
516 }
517
518 func TestComparisonAssertionFunc(t *testing.T) {
519 type iface interface {
520 Name() string
521 }
522
523 tests := []struct {
524 name string
525 expect interface{}
526 got interface{}
527 assertion ComparisonAssertionFunc
528 }{
529 {"implements", (*iface)(nil), t, Implements},
530 {"isType", (*testing.T)(nil), t, IsType},
531 {"equal", t, t, Equal},
532 {"equalValues", t, t, EqualValues},
533 {"exactly", t, t, Exactly},
534 {"notEqual", t, nil, NotEqual},
535 {"NotEqualValues", t, nil, NotEqualValues},
536 {"notContains", []int{1, 2, 3}, 4, NotContains},
537 {"subset", []int{1, 2, 3, 4}, []int{2, 3}, Subset},
538 {"notSubset", []int{1, 2, 3, 4}, []int{0, 3}, NotSubset},
539 {"elementsMatch", []byte("abc"), []byte("bac"), ElementsMatch},
540 {"regexp", "^t.*y$", "testify", Regexp},
541 {"notRegexp", "^t.*y$", "Testify", NotRegexp},
542 }
543
544 for _, tt := range tests {
545 t.Run(tt.name, func(t *testing.T) {
546 tt.assertion(t, tt.expect, tt.got)
547 })
548 }
549 }
550
551 func ExampleValueAssertionFunc() {
552 t := &testing.T{}
553
554 dumbParse := func(input string) interface{} {
555 var x interface{}
556 json.Unmarshal([]byte(input), &x)
557 return x
558 }
559
560 tests := []struct {
561 name string
562 arg string
563 assertion ValueAssertionFunc
564 }{
565 {"true is not nil", "true", NotNil},
566 {"empty string is nil", "", Nil},
567 {"zero is not nil", "0", NotNil},
568 {"zero is zero", "0", Zero},
569 {"false is zero", "false", Zero},
570 }
571
572 for _, tt := range tests {
573 t.Run(tt.name, func(t *testing.T) {
574 tt.assertion(t, dumbParse(tt.arg))
575 })
576 }
577 }
578
579 func TestValueAssertionFunc(t *testing.T) {
580 tests := []struct {
581 name string
582 value interface{}
583 assertion ValueAssertionFunc
584 }{
585 {"notNil", true, NotNil},
586 {"nil", nil, Nil},
587 {"empty", []int{}, Empty},
588 {"notEmpty", []int{1}, NotEmpty},
589 {"zero", false, Zero},
590 {"notZero", 42, NotZero},
591 }
592
593 for _, tt := range tests {
594 t.Run(tt.name, func(t *testing.T) {
595 tt.assertion(t, tt.value)
596 })
597 }
598 }
599
600 func ExampleBoolAssertionFunc() {
601 t := &testing.T{}
602
603 isOkay := func(x int) bool {
604 return x >= 42
605 }
606
607 tests := []struct {
608 name string
609 arg int
610 assertion BoolAssertionFunc
611 }{
612 {"-1 is bad", -1, False},
613 {"42 is good", 42, True},
614 {"41 is bad", 41, False},
615 {"45 is cool", 45, True},
616 }
617
618 for _, tt := range tests {
619 t.Run(tt.name, func(t *testing.T) {
620 tt.assertion(t, isOkay(tt.arg))
621 })
622 }
623 }
624
625 func TestBoolAssertionFunc(t *testing.T) {
626 tests := []struct {
627 name string
628 value bool
629 assertion BoolAssertionFunc
630 }{
631 {"true", true, True},
632 {"false", false, False},
633 }
634
635 for _, tt := range tests {
636 t.Run(tt.name, func(t *testing.T) {
637 tt.assertion(t, tt.value)
638 })
639 }
640 }
641
642 func ExampleErrorAssertionFunc() {
643 t := &testing.T{}
644
645 dumbParseNum := func(input string, v interface{}) error {
646 return json.Unmarshal([]byte(input), v)
647 }
648
649 tests := []struct {
650 name string
651 arg string
652 assertion ErrorAssertionFunc
653 }{
654 {"1.2 is number", "1.2", NoError},
655 {"1.2.3 not number", "1.2.3", Error},
656 {"true is not number", "true", Error},
657 {"3 is number", "3", NoError},
658 }
659
660 for _, tt := range tests {
661 t.Run(tt.name, func(t *testing.T) {
662 var x float64
663 tt.assertion(t, dumbParseNum(tt.arg, &x))
664 })
665 }
666 }
667
668 func TestErrorAssertionFunc(t *testing.T) {
669 tests := []struct {
670 name string
671 err error
672 assertion ErrorAssertionFunc
673 }{
674 {"noError", nil, NoError},
675 {"error", errors.New("whoops"), Error},
676 }
677
678 for _, tt := range tests {
679 t.Run(tt.name, func(t *testing.T) {
680 tt.assertion(t, tt.err)
681 })
682 }
683 }
684
View as plain text