OSDN Git Service

Hulk did something
[bytom/vapor.git] / vendor / github.com / stretchr / testify / assert / assertions_test.go
1 package assert
2
3 import (
4         "bytes"
5         "errors"
6         "fmt"
7         "io"
8         "math"
9         "os"
10         "reflect"
11         "regexp"
12         "runtime"
13         "strings"
14         "testing"
15         "time"
16 )
17
18 var (
19         i     interface{}
20         zeros = []interface{}{
21                 false,
22                 byte(0),
23                 complex64(0),
24                 complex128(0),
25                 float32(0),
26                 float64(0),
27                 int(0),
28                 int8(0),
29                 int16(0),
30                 int32(0),
31                 int64(0),
32                 rune(0),
33                 uint(0),
34                 uint8(0),
35                 uint16(0),
36                 uint32(0),
37                 uint64(0),
38                 uintptr(0),
39                 "",
40                 [0]interface{}{},
41                 []interface{}(nil),
42                 struct{ x int }{},
43                 (*interface{})(nil),
44                 (func())(nil),
45                 nil,
46                 interface{}(nil),
47                 map[interface{}]interface{}(nil),
48                 (chan interface{})(nil),
49                 (<-chan interface{})(nil),
50                 (chan<- interface{})(nil),
51         }
52         nonZeros = []interface{}{
53                 true,
54                 byte(1),
55                 complex64(1),
56                 complex128(1),
57                 float32(1),
58                 float64(1),
59                 int(1),
60                 int8(1),
61                 int16(1),
62                 int32(1),
63                 int64(1),
64                 rune(1),
65                 uint(1),
66                 uint8(1),
67                 uint16(1),
68                 uint32(1),
69                 uint64(1),
70                 uintptr(1),
71                 "s",
72                 [1]interface{}{1},
73                 []interface{}{},
74                 struct{ x int }{1},
75                 (*interface{})(&i),
76                 (func())(func() {}),
77                 interface{}(1),
78                 map[interface{}]interface{}{},
79                 (chan interface{})(make(chan interface{})),
80                 (<-chan interface{})(make(chan interface{})),
81                 (chan<- interface{})(make(chan interface{})),
82         }
83 )
84
85 // AssertionTesterInterface defines an interface to be used for testing assertion methods
86 type AssertionTesterInterface interface {
87         TestMethod()
88 }
89
90 // AssertionTesterConformingObject is an object that conforms to the AssertionTesterInterface interface
91 type AssertionTesterConformingObject struct {
92 }
93
94 func (a *AssertionTesterConformingObject) TestMethod() {
95 }
96
97 // AssertionTesterNonConformingObject is an object that does not conform to the AssertionTesterInterface interface
98 type AssertionTesterNonConformingObject struct {
99 }
100
101 func TestObjectsAreEqual(t *testing.T) {
102
103         if !ObjectsAreEqual("Hello World", "Hello World") {
104                 t.Error("objectsAreEqual should return true")
105         }
106         if !ObjectsAreEqual(123, 123) {
107                 t.Error("objectsAreEqual should return true")
108         }
109         if !ObjectsAreEqual(123.5, 123.5) {
110                 t.Error("objectsAreEqual should return true")
111         }
112         if !ObjectsAreEqual([]byte("Hello World"), []byte("Hello World")) {
113                 t.Error("objectsAreEqual should return true")
114         }
115         if !ObjectsAreEqual(nil, nil) {
116                 t.Error("objectsAreEqual should return true")
117         }
118         if ObjectsAreEqual(map[int]int{5: 10}, map[int]int{10: 20}) {
119                 t.Error("objectsAreEqual should return false")
120         }
121         if ObjectsAreEqual('x', "x") {
122                 t.Error("objectsAreEqual should return false")
123         }
124         if ObjectsAreEqual("x", 'x') {
125                 t.Error("objectsAreEqual should return false")
126         }
127         if ObjectsAreEqual(0, 0.1) {
128                 t.Error("objectsAreEqual should return false")
129         }
130         if ObjectsAreEqual(0.1, 0) {
131                 t.Error("objectsAreEqual should return false")
132         }
133         if ObjectsAreEqual(uint32(10), int32(10)) {
134                 t.Error("objectsAreEqual should return false")
135         }
136         if !ObjectsAreEqualValues(uint32(10), int32(10)) {
137                 t.Error("ObjectsAreEqualValues should return true")
138         }
139         if ObjectsAreEqualValues(0, nil) {
140                 t.Fail()
141         }
142         if ObjectsAreEqualValues(nil, 0) {
143                 t.Fail()
144         }
145
146 }
147
148 func TestImplements(t *testing.T) {
149
150         mockT := new(testing.T)
151
152         if !Implements(mockT, (*AssertionTesterInterface)(nil), new(AssertionTesterConformingObject)) {
153                 t.Error("Implements method should return true: AssertionTesterConformingObject implements AssertionTesterInterface")
154         }
155         if Implements(mockT, (*AssertionTesterInterface)(nil), new(AssertionTesterNonConformingObject)) {
156                 t.Error("Implements method should return false: AssertionTesterNonConformingObject does not implements AssertionTesterInterface")
157         }
158
159 }
160
161 func TestIsType(t *testing.T) {
162
163         mockT := new(testing.T)
164
165         if !IsType(mockT, new(AssertionTesterConformingObject), new(AssertionTesterConformingObject)) {
166                 t.Error("IsType should return true: AssertionTesterConformingObject is the same type as AssertionTesterConformingObject")
167         }
168         if IsType(mockT, new(AssertionTesterConformingObject), new(AssertionTesterNonConformingObject)) {
169                 t.Error("IsType should return false: AssertionTesterConformingObject is not the same type as AssertionTesterNonConformingObject")
170         }
171
172 }
173
174 func TestEqual(t *testing.T) {
175
176         mockT := new(testing.T)
177
178         if !Equal(mockT, "Hello World", "Hello World") {
179                 t.Error("Equal should return true")
180         }
181         if !Equal(mockT, 123, 123) {
182                 t.Error("Equal should return true")
183         }
184         if !Equal(mockT, 123.5, 123.5) {
185                 t.Error("Equal should return true")
186         }
187         if !Equal(mockT, []byte("Hello World"), []byte("Hello World")) {
188                 t.Error("Equal should return true")
189         }
190         if !Equal(mockT, nil, nil) {
191                 t.Error("Equal should return true")
192         }
193         if !Equal(mockT, int32(123), int32(123)) {
194                 t.Error("Equal should return true")
195         }
196         if !Equal(mockT, uint64(123), uint64(123)) {
197                 t.Error("Equal should return true")
198         }
199         if !Equal(mockT, &struct{}{}, &struct{}{}) {
200                 t.Error("Equal should return true (pointer equality is based on equality of underlying value)")
201         }
202         var m map[string]interface{}
203         if Equal(mockT, m["bar"], "something") {
204                 t.Error("Equal should return false")
205         }
206 }
207
208 // bufferT implements TestingT. Its implementation of Errorf writes the output that would be produced by
209 // testing.T.Errorf to an internal bytes.Buffer.
210 type bufferT struct {
211         buf bytes.Buffer
212 }
213
214 func (t *bufferT) Errorf(format string, args ...interface{}) {
215         // implementation of decorate is copied from testing.T
216         decorate := func(s string) string {
217                 _, file, line, ok := runtime.Caller(3) // decorate + log + public function.
218                 if ok {
219                         // Truncate file name at last file name separator.
220                         if index := strings.LastIndex(file, "/"); index >= 0 {
221                                 file = file[index+1:]
222                         } else if index = strings.LastIndex(file, "\\"); index >= 0 {
223                                 file = file[index+1:]
224                         }
225                 } else {
226                         file = "???"
227                         line = 1
228                 }
229                 buf := new(bytes.Buffer)
230                 // Every line is indented at least one tab.
231                 buf.WriteByte('\t')
232                 fmt.Fprintf(buf, "%s:%d: ", file, line)
233                 lines := strings.Split(s, "\n")
234                 if l := len(lines); l > 1 && lines[l-1] == "" {
235                         lines = lines[:l-1]
236                 }
237                 for i, line := range lines {
238                         if i > 0 {
239                                 // Second and subsequent lines are indented an extra tab.
240                                 buf.WriteString("\n\t\t")
241                         }
242                         buf.WriteString(line)
243                 }
244                 buf.WriteByte('\n')
245                 return buf.String()
246         }
247         t.buf.WriteString(decorate(fmt.Sprintf(format, args...)))
248 }
249
250 func TestEqualFormatting(t *testing.T) {
251         for i, currCase := range []struct {
252                 equalWant  string
253                 equalGot   string
254                 msgAndArgs []interface{}
255                 want       string
256         }{
257                 {equalWant: "want", equalGot: "got", want: "\tassertions.go:[0-9]+: \r                          \r\tError Trace:\t\n\t\t\r\tError:      \tNot equal: \n\t\t\r\t            \texpected: \"want\"\n\t\t\r\t            \tactual: \"got\"\n"},
258                 {equalWant: "want", equalGot: "got", msgAndArgs: []interface{}{"hello, %v!", "world"}, want: "\tassertions.go:[0-9]+: \r                          \r\tError Trace:\t\n\t\t\r\tError:      \tNot equal: \n\t\t\r\t            \texpected: \"want\"\n\t\t\r\t            \tactual: \"got\"\n\t\t\r\tMessages:   \thello, world!\n"},
259         } {
260                 mockT := &bufferT{}
261                 Equal(mockT, currCase.equalWant, currCase.equalGot, currCase.msgAndArgs...)
262                 Regexp(t, regexp.MustCompile(currCase.want), mockT.buf.String(), "Case %d", i)
263         }
264 }
265
266 func TestFormatUnequalValues(t *testing.T) {
267         expected, actual := formatUnequalValues("foo", "bar")
268         Equal(t, `"foo"`, expected, "value should not include type")
269         Equal(t, `"bar"`, actual, "value should not include type")
270
271         expected, actual = formatUnequalValues(123, 123)
272         Equal(t, `123`, expected, "value should not include type")
273         Equal(t, `123`, actual, "value should not include type")
274
275         expected, actual = formatUnequalValues(int64(123), int32(123))
276         Equal(t, `int64(123)`, expected, "value should include type")
277         Equal(t, `int32(123)`, actual, "value should include type")
278
279         expected, actual = formatUnequalValues(int64(123), nil)
280         Equal(t, `int64(123)`, expected, "value should include type")
281         Equal(t, `<nil>(<nil>)`, actual, "value should include type")
282
283         type testStructType struct {
284                 Val string
285         }
286
287         expected, actual = formatUnequalValues(&testStructType{Val: "test"}, &testStructType{Val: "test"})
288         Equal(t, `&assert.testStructType{Val:"test"}`, expected, "value should not include type annotation")
289         Equal(t, `&assert.testStructType{Val:"test"}`, actual, "value should not include type annotation")
290 }
291
292 func TestNotNil(t *testing.T) {
293
294         mockT := new(testing.T)
295
296         if !NotNil(mockT, new(AssertionTesterConformingObject)) {
297                 t.Error("NotNil should return true: object is not nil")
298         }
299         if NotNil(mockT, nil) {
300                 t.Error("NotNil should return false: object is nil")
301         }
302         if NotNil(mockT, (*struct{})(nil)) {
303                 t.Error("NotNil should return false: object is (*struct{})(nil)")
304         }
305
306 }
307
308 func TestNil(t *testing.T) {
309
310         mockT := new(testing.T)
311
312         if !Nil(mockT, nil) {
313                 t.Error("Nil should return true: object is nil")
314         }
315         if !Nil(mockT, (*struct{})(nil)) {
316                 t.Error("Nil should return true: object is (*struct{})(nil)")
317         }
318         if Nil(mockT, new(AssertionTesterConformingObject)) {
319                 t.Error("Nil should return false: object is not nil")
320         }
321
322 }
323
324 func TestTrue(t *testing.T) {
325
326         mockT := new(testing.T)
327
328         if !True(mockT, true) {
329                 t.Error("True should return true")
330         }
331         if True(mockT, false) {
332                 t.Error("True should return false")
333         }
334
335 }
336
337 func TestFalse(t *testing.T) {
338
339         mockT := new(testing.T)
340
341         if !False(mockT, false) {
342                 t.Error("False should return true")
343         }
344         if False(mockT, true) {
345                 t.Error("False should return false")
346         }
347
348 }
349
350 func TestExactly(t *testing.T) {
351
352         mockT := new(testing.T)
353
354         a := float32(1)
355         b := float64(1)
356         c := float32(1)
357         d := float32(2)
358
359         if Exactly(mockT, a, b) {
360                 t.Error("Exactly should return false")
361         }
362         if Exactly(mockT, a, d) {
363                 t.Error("Exactly should return false")
364         }
365         if !Exactly(mockT, a, c) {
366                 t.Error("Exactly should return true")
367         }
368
369         if Exactly(mockT, nil, a) {
370                 t.Error("Exactly should return false")
371         }
372         if Exactly(mockT, a, nil) {
373                 t.Error("Exactly should return false")
374         }
375
376 }
377
378 func TestNotEqual(t *testing.T) {
379
380         mockT := new(testing.T)
381
382         if !NotEqual(mockT, "Hello World", "Hello World!") {
383                 t.Error("NotEqual should return true")
384         }
385         if !NotEqual(mockT, 123, 1234) {
386                 t.Error("NotEqual should return true")
387         }
388         if !NotEqual(mockT, 123.5, 123.55) {
389                 t.Error("NotEqual should return true")
390         }
391         if !NotEqual(mockT, []byte("Hello World"), []byte("Hello World!")) {
392                 t.Error("NotEqual should return true")
393         }
394         if !NotEqual(mockT, nil, new(AssertionTesterConformingObject)) {
395                 t.Error("NotEqual should return true")
396         }
397         funcA := func() int { return 23 }
398         funcB := func() int { return 42 }
399         if NotEqual(mockT, funcA, funcB) {
400                 t.Error("NotEqual should return false")
401         }
402
403         if NotEqual(mockT, "Hello World", "Hello World") {
404                 t.Error("NotEqual should return false")
405         }
406         if NotEqual(mockT, 123, 123) {
407                 t.Error("NotEqual should return false")
408         }
409         if NotEqual(mockT, 123.5, 123.5) {
410                 t.Error("NotEqual should return false")
411         }
412         if NotEqual(mockT, []byte("Hello World"), []byte("Hello World")) {
413                 t.Error("NotEqual should return false")
414         }
415         if NotEqual(mockT, new(AssertionTesterConformingObject), new(AssertionTesterConformingObject)) {
416                 t.Error("NotEqual should return false")
417         }
418         if NotEqual(mockT, &struct{}{}, &struct{}{}) {
419                 t.Error("NotEqual should return false")
420         }
421 }
422
423 type A struct {
424         Name, Value string
425 }
426
427 func TestContains(t *testing.T) {
428
429         mockT := new(testing.T)
430         list := []string{"Foo", "Bar"}
431         complexList := []*A{
432                 {"b", "c"},
433                 {"d", "e"},
434                 {"g", "h"},
435                 {"j", "k"},
436         }
437         simpleMap := map[interface{}]interface{}{"Foo": "Bar"}
438
439         if !Contains(mockT, "Hello World", "Hello") {
440                 t.Error("Contains should return true: \"Hello World\" contains \"Hello\"")
441         }
442         if Contains(mockT, "Hello World", "Salut") {
443                 t.Error("Contains should return false: \"Hello World\" does not contain \"Salut\"")
444         }
445
446         if !Contains(mockT, list, "Bar") {
447                 t.Error("Contains should return true: \"[\"Foo\", \"Bar\"]\" contains \"Bar\"")
448         }
449         if Contains(mockT, list, "Salut") {
450                 t.Error("Contains should return false: \"[\"Foo\", \"Bar\"]\" does not contain \"Salut\"")
451         }
452         if !Contains(mockT, complexList, &A{"g", "h"}) {
453                 t.Error("Contains should return true: complexList contains {\"g\", \"h\"}")
454         }
455         if Contains(mockT, complexList, &A{"g", "e"}) {
456                 t.Error("Contains should return false: complexList contains {\"g\", \"e\"}")
457         }
458         if Contains(mockT, complexList, &A{"g", "e"}) {
459                 t.Error("Contains should return false: complexList contains {\"g\", \"e\"}")
460         }
461         if !Contains(mockT, simpleMap, "Foo") {
462                 t.Error("Contains should return true: \"{\"Foo\": \"Bar\"}\" contains \"Foo\"")
463         }
464         if Contains(mockT, simpleMap, "Bar") {
465                 t.Error("Contains should return false: \"{\"Foo\": \"Bar\"}\" does not contains \"Bar\"")
466         }
467 }
468
469 func TestNotContains(t *testing.T) {
470
471         mockT := new(testing.T)
472         list := []string{"Foo", "Bar"}
473         simpleMap := map[interface{}]interface{}{"Foo": "Bar"}
474
475         if !NotContains(mockT, "Hello World", "Hello!") {
476                 t.Error("NotContains should return true: \"Hello World\" does not contain \"Hello!\"")
477         }
478         if NotContains(mockT, "Hello World", "Hello") {
479                 t.Error("NotContains should return false: \"Hello World\" contains \"Hello\"")
480         }
481
482         if !NotContains(mockT, list, "Foo!") {
483                 t.Error("NotContains should return true: \"[\"Foo\", \"Bar\"]\" does not contain \"Foo!\"")
484         }
485         if NotContains(mockT, list, "Foo") {
486                 t.Error("NotContains should return false: \"[\"Foo\", \"Bar\"]\" contains \"Foo\"")
487         }
488         if NotContains(mockT, simpleMap, "Foo") {
489                 t.Error("Contains should return true: \"{\"Foo\": \"Bar\"}\" contains \"Foo\"")
490         }
491         if !NotContains(mockT, simpleMap, "Bar") {
492                 t.Error("Contains should return false: \"{\"Foo\": \"Bar\"}\" does not contains \"Bar\"")
493         }
494 }
495
496 func TestSubset(t *testing.T) {
497         mockT := new(testing.T)
498
499         if !Subset(mockT, []int{1, 2, 3}, nil) {
500                 t.Error("Subset should return true: given subset is nil")
501         }
502         if !Subset(mockT, []int{1, 2, 3}, []int{}) {
503                 t.Error("Subset should return true: any set contains the nil set")
504         }
505         if !Subset(mockT, []int{1, 2, 3}, []int{1, 2}) {
506                 t.Error("Subset should return true: [1, 2, 3] contains [1, 2]")
507         }
508         if !Subset(mockT, []int{1, 2, 3}, []int{1, 2, 3}) {
509                 t.Error("Subset should return true: [1, 2, 3] contains [1, 2, 3]")
510         }
511         if !Subset(mockT, []string{"hello", "world"}, []string{"hello"}) {
512                 t.Error("Subset should return true: [\"hello\", \"world\"] contains [\"hello\"]")
513         }
514
515         if Subset(mockT, []string{"hello", "world"}, []string{"hello", "testify"}) {
516                 t.Error("Subset should return false: [\"hello\", \"world\"] does not contain [\"hello\", \"testify\"]")
517         }
518         if Subset(mockT, []int{1, 2, 3}, []int{4, 5}) {
519                 t.Error("Subset should return false: [1, 2, 3] does not contain [4, 5]")
520         }
521         if Subset(mockT, []int{1, 2, 3}, []int{1, 5}) {
522                 t.Error("Subset should return false: [1, 2, 3] does not contain [1, 5]")
523         }
524 }
525
526 func TestNotSubset(t *testing.T) {
527         mockT := new(testing.T)
528
529         if NotSubset(mockT, []int{1, 2, 3}, nil) {
530                 t.Error("NotSubset should return false: given subset is nil")
531         }
532         if NotSubset(mockT, []int{1, 2, 3}, []int{}) {
533                 t.Error("NotSubset should return false: any set contains the nil set")
534         }
535         if NotSubset(mockT, []int{1, 2, 3}, []int{1, 2}) {
536                 t.Error("NotSubset should return false: [1, 2, 3] contains [1, 2]")
537         }
538         if NotSubset(mockT, []int{1, 2, 3}, []int{1, 2, 3}) {
539                 t.Error("NotSubset should return false: [1, 2, 3] contains [1, 2, 3]")
540         }
541         if NotSubset(mockT, []string{"hello", "world"}, []string{"hello"}) {
542                 t.Error("NotSubset should return false: [\"hello\", \"world\"] contains [\"hello\"]")
543         }
544
545         if !NotSubset(mockT, []string{"hello", "world"}, []string{"hello", "testify"}) {
546                 t.Error("NotSubset should return true: [\"hello\", \"world\"] does not contain [\"hello\", \"testify\"]")
547         }
548         if !NotSubset(mockT, []int{1, 2, 3}, []int{4, 5}) {
549                 t.Error("NotSubset should return true: [1, 2, 3] does not contain [4, 5]")
550         }
551         if !NotSubset(mockT, []int{1, 2, 3}, []int{1, 5}) {
552                 t.Error("NotSubset should return true: [1, 2, 3] does not contain [1, 5]")
553         }
554 }
555
556 func Test_includeElement(t *testing.T) {
557
558         list1 := []string{"Foo", "Bar"}
559         list2 := []int{1, 2}
560         simpleMap := map[interface{}]interface{}{"Foo": "Bar"}
561
562         ok, found := includeElement("Hello World", "World")
563         True(t, ok)
564         True(t, found)
565
566         ok, found = includeElement(list1, "Foo")
567         True(t, ok)
568         True(t, found)
569
570         ok, found = includeElement(list1, "Bar")
571         True(t, ok)
572         True(t, found)
573
574         ok, found = includeElement(list2, 1)
575         True(t, ok)
576         True(t, found)
577
578         ok, found = includeElement(list2, 2)
579         True(t, ok)
580         True(t, found)
581
582         ok, found = includeElement(list1, "Foo!")
583         True(t, ok)
584         False(t, found)
585
586         ok, found = includeElement(list2, 3)
587         True(t, ok)
588         False(t, found)
589
590         ok, found = includeElement(list2, "1")
591         True(t, ok)
592         False(t, found)
593
594         ok, found = includeElement(simpleMap, "Foo")
595         True(t, ok)
596         True(t, found)
597
598         ok, found = includeElement(simpleMap, "Bar")
599         True(t, ok)
600         False(t, found)
601
602         ok, found = includeElement(1433, "1")
603         False(t, ok)
604         False(t, found)
605 }
606
607 func TestCondition(t *testing.T) {
608         mockT := new(testing.T)
609
610         if !Condition(mockT, func() bool { return true }, "Truth") {
611                 t.Error("Condition should return true")
612         }
613
614         if Condition(mockT, func() bool { return false }, "Lie") {
615                 t.Error("Condition should return false")
616         }
617
618 }
619
620 func TestDidPanic(t *testing.T) {
621
622         if funcDidPanic, _ := didPanic(func() {
623                 panic("Panic!")
624         }); !funcDidPanic {
625                 t.Error("didPanic should return true")
626         }
627
628         if funcDidPanic, _ := didPanic(func() {
629         }); funcDidPanic {
630                 t.Error("didPanic should return false")
631         }
632
633 }
634
635 func TestPanics(t *testing.T) {
636
637         mockT := new(testing.T)
638
639         if !Panics(mockT, func() {
640                 panic("Panic!")
641         }) {
642                 t.Error("Panics should return true")
643         }
644
645         if Panics(mockT, func() {
646         }) {
647                 t.Error("Panics should return false")
648         }
649
650 }
651
652 func TestPanicsWithValue(t *testing.T) {
653
654         mockT := new(testing.T)
655
656         if !PanicsWithValue(mockT, "Panic!", func() {
657                 panic("Panic!")
658         }) {
659                 t.Error("PanicsWithValue should return true")
660         }
661
662         if PanicsWithValue(mockT, "Panic!", func() {
663         }) {
664                 t.Error("PanicsWithValue should return false")
665         }
666
667         if PanicsWithValue(mockT, "at the disco", func() {
668                 panic("Panic!")
669         }) {
670                 t.Error("PanicsWithValue should return false")
671         }
672 }
673
674 func TestNotPanics(t *testing.T) {
675
676         mockT := new(testing.T)
677
678         if !NotPanics(mockT, func() {
679         }) {
680                 t.Error("NotPanics should return true")
681         }
682
683         if NotPanics(mockT, func() {
684                 panic("Panic!")
685         }) {
686                 t.Error("NotPanics should return false")
687         }
688
689 }
690
691 func TestNoError(t *testing.T) {
692
693         mockT := new(testing.T)
694
695         // start with a nil error
696         var err error
697
698         True(t, NoError(mockT, err), "NoError should return True for nil arg")
699
700         // now set an error
701         err = errors.New("some error")
702
703         False(t, NoError(mockT, err), "NoError with error should return False")
704
705         // returning an empty error interface
706         err = func() error {
707                 var err *customError
708                 if err != nil {
709                         t.Fatal("err should be nil here")
710                 }
711                 return err
712         }()
713
714         if err == nil { // err is not nil here!
715                 t.Errorf("Error should be nil due to empty interface: %s", err)
716         }
717
718         False(t, NoError(mockT, err), "NoError should fail with empty error interface")
719 }
720
721 type customError struct{}
722
723 func (*customError) Error() string { return "fail" }
724
725 func TestError(t *testing.T) {
726
727         mockT := new(testing.T)
728
729         // start with a nil error
730         var err error
731
732         False(t, Error(mockT, err), "Error should return False for nil arg")
733
734         // now set an error
735         err = errors.New("some error")
736
737         True(t, Error(mockT, err), "Error with error should return True")
738
739         // go vet check
740         True(t, Errorf(mockT, err, "example with %s", "formatted message"), "Errorf with error should rturn True")
741
742         // returning an empty error interface
743         err = func() error {
744                 var err *customError
745                 if err != nil {
746                         t.Fatal("err should be nil here")
747                 }
748                 return err
749         }()
750
751         if err == nil { // err is not nil here!
752                 t.Errorf("Error should be nil due to empty interface: %s", err)
753         }
754
755         True(t, Error(mockT, err), "Error should pass with empty error interface")
756 }
757
758 func TestEqualError(t *testing.T) {
759         mockT := new(testing.T)
760
761         // start with a nil error
762         var err error
763         False(t, EqualError(mockT, err, ""),
764                 "EqualError should return false for nil arg")
765
766         // now set an error
767         err = errors.New("some error")
768         False(t, EqualError(mockT, err, "Not some error"),
769                 "EqualError should return false for different error string")
770         True(t, EqualError(mockT, err, "some error"),
771                 "EqualError should return true")
772 }
773
774 func Test_isEmpty(t *testing.T) {
775
776         chWithValue := make(chan struct{}, 1)
777         chWithValue <- struct{}{}
778
779         True(t, isEmpty(""))
780         True(t, isEmpty(nil))
781         True(t, isEmpty([]string{}))
782         True(t, isEmpty(0))
783         True(t, isEmpty(int32(0)))
784         True(t, isEmpty(int64(0)))
785         True(t, isEmpty(false))
786         True(t, isEmpty(map[string]string{}))
787         True(t, isEmpty(new(time.Time)))
788         True(t, isEmpty(time.Time{}))
789         True(t, isEmpty(make(chan struct{})))
790         False(t, isEmpty("something"))
791         False(t, isEmpty(errors.New("something")))
792         False(t, isEmpty([]string{"something"}))
793         False(t, isEmpty(1))
794         False(t, isEmpty(true))
795         False(t, isEmpty(map[string]string{"Hello": "World"}))
796         False(t, isEmpty(chWithValue))
797
798 }
799
800 func TestEmpty(t *testing.T) {
801
802         mockT := new(testing.T)
803         chWithValue := make(chan struct{}, 1)
804         chWithValue <- struct{}{}
805         var tiP *time.Time
806         var tiNP time.Time
807         var s *string
808         var f *os.File
809
810         True(t, Empty(mockT, ""), "Empty string is empty")
811         True(t, Empty(mockT, nil), "Nil is empty")
812         True(t, Empty(mockT, []string{}), "Empty string array is empty")
813         True(t, Empty(mockT, 0), "Zero int value is empty")
814         True(t, Empty(mockT, false), "False value is empty")
815         True(t, Empty(mockT, make(chan struct{})), "Channel without values is empty")
816         True(t, Empty(mockT, s), "Nil string pointer is empty")
817         True(t, Empty(mockT, f), "Nil os.File pointer is empty")
818         True(t, Empty(mockT, tiP), "Nil time.Time pointer is empty")
819         True(t, Empty(mockT, tiNP), "time.Time is empty")
820
821         False(t, Empty(mockT, "something"), "Non Empty string is not empty")
822         False(t, Empty(mockT, errors.New("something")), "Non nil object is not empty")
823         False(t, Empty(mockT, []string{"something"}), "Non empty string array is not empty")
824         False(t, Empty(mockT, 1), "Non-zero int value is not empty")
825         False(t, Empty(mockT, true), "True value is not empty")
826         False(t, Empty(mockT, chWithValue), "Channel with values is not empty")
827 }
828
829 func TestNotEmpty(t *testing.T) {
830
831         mockT := new(testing.T)
832         chWithValue := make(chan struct{}, 1)
833         chWithValue <- struct{}{}
834
835         False(t, NotEmpty(mockT, ""), "Empty string is empty")
836         False(t, NotEmpty(mockT, nil), "Nil is empty")
837         False(t, NotEmpty(mockT, []string{}), "Empty string array is empty")
838         False(t, NotEmpty(mockT, 0), "Zero int value is empty")
839         False(t, NotEmpty(mockT, false), "False value is empty")
840         False(t, NotEmpty(mockT, make(chan struct{})), "Channel without values is empty")
841
842         True(t, NotEmpty(mockT, "something"), "Non Empty string is not empty")
843         True(t, NotEmpty(mockT, errors.New("something")), "Non nil object is not empty")
844         True(t, NotEmpty(mockT, []string{"something"}), "Non empty string array is not empty")
845         True(t, NotEmpty(mockT, 1), "Non-zero int value is not empty")
846         True(t, NotEmpty(mockT, true), "True value is not empty")
847         True(t, NotEmpty(mockT, chWithValue), "Channel with values is not empty")
848 }
849
850 func Test_getLen(t *testing.T) {
851         falseCases := []interface{}{
852                 nil,
853                 0,
854                 true,
855                 false,
856                 'A',
857                 struct{}{},
858         }
859         for _, v := range falseCases {
860                 ok, l := getLen(v)
861                 False(t, ok, "Expected getLen fail to get length of %#v", v)
862                 Equal(t, 0, l, "getLen should return 0 for %#v", v)
863         }
864
865         ch := make(chan int, 5)
866         ch <- 1
867         ch <- 2
868         ch <- 3
869         trueCases := []struct {
870                 v interface{}
871                 l int
872         }{
873                 {[]int{1, 2, 3}, 3},
874                 {[...]int{1, 2, 3}, 3},
875                 {"ABC", 3},
876                 {map[int]int{1: 2, 2: 4, 3: 6}, 3},
877                 {ch, 3},
878
879                 {[]int{}, 0},
880                 {map[int]int{}, 0},
881                 {make(chan int), 0},
882
883                 {[]int(nil), 0},
884                 {map[int]int(nil), 0},
885                 {(chan int)(nil), 0},
886         }
887
888         for _, c := range trueCases {
889                 ok, l := getLen(c.v)
890                 True(t, ok, "Expected getLen success to get length of %#v", c.v)
891                 Equal(t, c.l, l)
892         }
893 }
894
895 func TestLen(t *testing.T) {
896         mockT := new(testing.T)
897
898         False(t, Len(mockT, nil, 0), "nil does not have length")
899         False(t, Len(mockT, 0, 0), "int does not have length")
900         False(t, Len(mockT, true, 0), "true does not have length")
901         False(t, Len(mockT, false, 0), "false does not have length")
902         False(t, Len(mockT, 'A', 0), "Rune does not have length")
903         False(t, Len(mockT, struct{}{}, 0), "Struct does not have length")
904
905         ch := make(chan int, 5)
906         ch <- 1
907         ch <- 2
908         ch <- 3
909
910         cases := []struct {
911                 v interface{}
912                 l int
913         }{
914                 {[]int{1, 2, 3}, 3},
915                 {[...]int{1, 2, 3}, 3},
916                 {"ABC", 3},
917                 {map[int]int{1: 2, 2: 4, 3: 6}, 3},
918                 {ch, 3},
919
920                 {[]int{}, 0},
921                 {map[int]int{}, 0},
922                 {make(chan int), 0},
923
924                 {[]int(nil), 0},
925                 {map[int]int(nil), 0},
926                 {(chan int)(nil), 0},
927         }
928
929         for _, c := range cases {
930                 True(t, Len(mockT, c.v, c.l), "%#v have %d items", c.v, c.l)
931         }
932
933         cases = []struct {
934                 v interface{}
935                 l int
936         }{
937                 {[]int{1, 2, 3}, 4},
938                 {[...]int{1, 2, 3}, 2},
939                 {"ABC", 2},
940                 {map[int]int{1: 2, 2: 4, 3: 6}, 4},
941                 {ch, 2},
942
943                 {[]int{}, 1},
944                 {map[int]int{}, 1},
945                 {make(chan int), 1},
946
947                 {[]int(nil), 1},
948                 {map[int]int(nil), 1},
949                 {(chan int)(nil), 1},
950         }
951
952         for _, c := range cases {
953                 False(t, Len(mockT, c.v, c.l), "%#v have %d items", c.v, c.l)
954         }
955 }
956
957 func TestWithinDuration(t *testing.T) {
958
959         mockT := new(testing.T)
960         a := time.Now()
961         b := a.Add(10 * time.Second)
962
963         True(t, WithinDuration(mockT, a, b, 10*time.Second), "A 10s difference is within a 10s time difference")
964         True(t, WithinDuration(mockT, b, a, 10*time.Second), "A 10s difference is within a 10s time difference")
965
966         False(t, WithinDuration(mockT, a, b, 9*time.Second), "A 10s difference is not within a 9s time difference")
967         False(t, WithinDuration(mockT, b, a, 9*time.Second), "A 10s difference is not within a 9s time difference")
968
969         False(t, WithinDuration(mockT, a, b, -9*time.Second), "A 10s difference is not within a 9s time difference")
970         False(t, WithinDuration(mockT, b, a, -9*time.Second), "A 10s difference is not within a 9s time difference")
971
972         False(t, WithinDuration(mockT, a, b, -11*time.Second), "A 10s difference is not within a 9s time difference")
973         False(t, WithinDuration(mockT, b, a, -11*time.Second), "A 10s difference is not within a 9s time difference")
974 }
975
976 func TestInDelta(t *testing.T) {
977         mockT := new(testing.T)
978
979         True(t, InDelta(mockT, 1.001, 1, 0.01), "|1.001 - 1| <= 0.01")
980         True(t, InDelta(mockT, 1, 1.001, 0.01), "|1 - 1.001| <= 0.01")
981         True(t, InDelta(mockT, 1, 2, 1), "|1 - 2| <= 1")
982         False(t, InDelta(mockT, 1, 2, 0.5), "Expected |1 - 2| <= 0.5 to fail")
983         False(t, InDelta(mockT, 2, 1, 0.5), "Expected |2 - 1| <= 0.5 to fail")
984         False(t, InDelta(mockT, "", nil, 1), "Expected non numerals to fail")
985         False(t, InDelta(mockT, 42, math.NaN(), 0.01), "Expected NaN for actual to fail")
986         False(t, InDelta(mockT, math.NaN(), 42, 0.01), "Expected NaN for expected to fail")
987
988         cases := []struct {
989                 a, b  interface{}
990                 delta float64
991         }{
992                 {uint8(2), uint8(1), 1},
993                 {uint16(2), uint16(1), 1},
994                 {uint32(2), uint32(1), 1},
995                 {uint64(2), uint64(1), 1},
996
997                 {int(2), int(1), 1},
998                 {int8(2), int8(1), 1},
999                 {int16(2), int16(1), 1},
1000                 {int32(2), int32(1), 1},
1001                 {int64(2), int64(1), 1},
1002
1003                 {float32(2), float32(1), 1},
1004                 {float64(2), float64(1), 1},
1005         }
1006
1007         for _, tc := range cases {
1008                 True(t, InDelta(mockT, tc.a, tc.b, tc.delta), "Expected |%V - %V| <= %v", tc.a, tc.b, tc.delta)
1009         }
1010 }
1011
1012 func TestInDeltaSlice(t *testing.T) {
1013         mockT := new(testing.T)
1014
1015         True(t, InDeltaSlice(mockT,
1016                 []float64{1.001, 0.999},
1017                 []float64{1, 1},
1018                 0.1), "{1.001, 0.009} is element-wise close to {1, 1} in delta=0.1")
1019
1020         True(t, InDeltaSlice(mockT,
1021                 []float64{1, 2},
1022                 []float64{0, 3},
1023                 1), "{1, 2} is element-wise close to {0, 3} in delta=1")
1024
1025         False(t, InDeltaSlice(mockT,
1026                 []float64{1, 2},
1027                 []float64{0, 3},
1028                 0.1), "{1, 2} is not element-wise close to {0, 3} in delta=0.1")
1029
1030         False(t, InDeltaSlice(mockT, "", nil, 1), "Expected non numeral slices to fail")
1031 }
1032
1033 func TestInEpsilon(t *testing.T) {
1034         mockT := new(testing.T)
1035
1036         cases := []struct {
1037                 a, b    interface{}
1038                 epsilon float64
1039         }{
1040                 {uint8(2), uint16(2), .001},
1041                 {2.1, 2.2, 0.1},
1042                 {2.2, 2.1, 0.1},
1043                 {-2.1, -2.2, 0.1},
1044                 {-2.2, -2.1, 0.1},
1045                 {uint64(100), uint8(101), 0.01},
1046                 {0.1, -0.1, 2},
1047                 {0.1, 0, 2},
1048                 {time.Second, time.Second + time.Millisecond, 0.002},
1049         }
1050
1051         for _, tc := range cases {
1052                 True(t, InEpsilon(t, tc.a, tc.b, tc.epsilon, "Expected %V and %V to have a relative difference of %v", tc.a, tc.b, tc.epsilon), "test: %q", tc)
1053         }
1054
1055         cases = []struct {
1056                 a, b    interface{}
1057                 epsilon float64
1058         }{
1059                 {uint8(2), int16(-2), .001},
1060                 {uint64(100), uint8(102), 0.01},
1061                 {2.1, 2.2, 0.001},
1062                 {2.2, 2.1, 0.001},
1063                 {2.1, -2.2, 1},
1064                 {2.1, "bla-bla", 0},
1065                 {0.1, -0.1, 1.99},
1066                 {0, 0.1, 2}, // expected must be different to zero
1067                 {time.Second, time.Second + 10*time.Millisecond, 0.002},
1068         }
1069
1070         for _, tc := range cases {
1071                 False(t, InEpsilon(mockT, tc.a, tc.b, tc.epsilon, "Expected %V and %V to have a relative difference of %v", tc.a, tc.b, tc.epsilon))
1072         }
1073
1074 }
1075
1076 func TestInEpsilonSlice(t *testing.T) {
1077         mockT := new(testing.T)
1078
1079         True(t, InEpsilonSlice(mockT,
1080                 []float64{2.2, 2.0},
1081                 []float64{2.1, 2.1},
1082                 0.06), "{2.2, 2.0} is element-wise close to {2.1, 2.1} in espilon=0.06")
1083
1084         False(t, InEpsilonSlice(mockT,
1085                 []float64{2.2, 2.0},
1086                 []float64{2.1, 2.1},
1087                 0.04), "{2.2, 2.0} is not element-wise close to {2.1, 2.1} in espilon=0.04")
1088
1089         False(t, InEpsilonSlice(mockT, "", nil, 1), "Expected non numeral slices to fail")
1090 }
1091
1092 func TestRegexp(t *testing.T) {
1093         mockT := new(testing.T)
1094
1095         cases := []struct {
1096                 rx, str string
1097         }{
1098                 {"^start", "start of the line"},
1099                 {"end$", "in the end"},
1100                 {"[0-9]{3}[.-]?[0-9]{2}[.-]?[0-9]{2}", "My phone number is 650.12.34"},
1101         }
1102
1103         for _, tc := range cases {
1104                 True(t, Regexp(mockT, tc.rx, tc.str))
1105                 True(t, Regexp(mockT, regexp.MustCompile(tc.rx), tc.str))
1106                 False(t, NotRegexp(mockT, tc.rx, tc.str))
1107                 False(t, NotRegexp(mockT, regexp.MustCompile(tc.rx), tc.str))
1108         }
1109
1110         cases = []struct {
1111                 rx, str string
1112         }{
1113                 {"^asdfastart", "Not the start of the line"},
1114                 {"end$", "in the end."},
1115                 {"[0-9]{3}[.-]?[0-9]{2}[.-]?[0-9]{2}", "My phone number is 650.12a.34"},
1116         }
1117
1118         for _, tc := range cases {
1119                 False(t, Regexp(mockT, tc.rx, tc.str), "Expected \"%s\" to not match \"%s\"", tc.rx, tc.str)
1120                 False(t, Regexp(mockT, regexp.MustCompile(tc.rx), tc.str))
1121                 True(t, NotRegexp(mockT, tc.rx, tc.str))
1122                 True(t, NotRegexp(mockT, regexp.MustCompile(tc.rx), tc.str))
1123         }
1124 }
1125
1126 func testAutogeneratedFunction() {
1127         defer func() {
1128                 if err := recover(); err == nil {
1129                         panic("did not panic")
1130                 }
1131                 CallerInfo()
1132         }()
1133         t := struct {
1134                 io.Closer
1135         }{}
1136         var c io.Closer
1137         c = t
1138         c.Close()
1139 }
1140
1141 func TestCallerInfoWithAutogeneratedFunctions(t *testing.T) {
1142         NotPanics(t, func() {
1143                 testAutogeneratedFunction()
1144         })
1145 }
1146
1147 func TestZero(t *testing.T) {
1148         mockT := new(testing.T)
1149
1150         for _, test := range zeros {
1151                 True(t, Zero(mockT, test, "%#v is not the %v zero value", test, reflect.TypeOf(test)))
1152         }
1153
1154         for _, test := range nonZeros {
1155                 False(t, Zero(mockT, test, "%#v is not the %v zero value", test, reflect.TypeOf(test)))
1156         }
1157 }
1158
1159 func TestNotZero(t *testing.T) {
1160         mockT := new(testing.T)
1161
1162         for _, test := range zeros {
1163                 False(t, NotZero(mockT, test, "%#v is not the %v zero value", test, reflect.TypeOf(test)))
1164         }
1165
1166         for _, test := range nonZeros {
1167                 True(t, NotZero(mockT, test, "%#v is not the %v zero value", test, reflect.TypeOf(test)))
1168         }
1169 }
1170
1171 func TestJSONEq_EqualSONString(t *testing.T) {
1172         mockT := new(testing.T)
1173         True(t, JSONEq(mockT, `{"hello": "world", "foo": "bar"}`, `{"hello": "world", "foo": "bar"}`))
1174 }
1175
1176 func TestJSONEq_EquivalentButNotEqual(t *testing.T) {
1177         mockT := new(testing.T)
1178         True(t, JSONEq(mockT, `{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`))
1179 }
1180
1181 func TestJSONEq_HashOfArraysAndHashes(t *testing.T) {
1182         mockT := new(testing.T)
1183         True(t, 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}",
1184                 "{\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}"))
1185 }
1186
1187 func TestJSONEq_Array(t *testing.T) {
1188         mockT := new(testing.T)
1189         True(t, JSONEq(mockT, `["foo", {"hello": "world", "nested": "hash"}]`, `["foo", {"nested": "hash", "hello": "world"}]`))
1190 }
1191
1192 func TestJSONEq_HashAndArrayNotEquivalent(t *testing.T) {
1193         mockT := new(testing.T)
1194         False(t, JSONEq(mockT, `["foo", {"hello": "world", "nested": "hash"}]`, `{"foo": "bar", {"nested": "hash", "hello": "world"}}`))
1195 }
1196
1197 func TestJSONEq_HashesNotEquivalent(t *testing.T) {
1198         mockT := new(testing.T)
1199         False(t, JSONEq(mockT, `{"foo": "bar"}`, `{"foo": "bar", "hello": "world"}`))
1200 }
1201
1202 func TestJSONEq_ActualIsNotJSON(t *testing.T) {
1203         mockT := new(testing.T)
1204         False(t, JSONEq(mockT, `{"foo": "bar"}`, "Not JSON"))
1205 }
1206
1207 func TestJSONEq_ExpectedIsNotJSON(t *testing.T) {
1208         mockT := new(testing.T)
1209         False(t, JSONEq(mockT, "Not JSON", `{"foo": "bar", "hello": "world"}`))
1210 }
1211
1212 func TestJSONEq_ExpectedAndActualNotJSON(t *testing.T) {
1213         mockT := new(testing.T)
1214         False(t, JSONEq(mockT, "Not JSON", "Not JSON"))
1215 }
1216
1217 func TestJSONEq_ArraysOfDifferentOrder(t *testing.T) {
1218         mockT := new(testing.T)
1219         False(t, JSONEq(mockT, `["foo", {"hello": "world", "nested": "hash"}]`, `[{ "hello": "world", "nested": "hash"}, "foo"]`))
1220 }
1221
1222 func TestDiff(t *testing.T) {
1223         expected := `
1224
1225 Diff:
1226 --- Expected
1227 +++ Actual
1228 @@ -1,3 +1,3 @@
1229  (struct { foo string }) {
1230 - foo: (string) (len=5) "hello"
1231 + foo: (string) (len=3) "bar"
1232  }
1233 `
1234         actual := diff(
1235                 struct{ foo string }{"hello"},
1236                 struct{ foo string }{"bar"},
1237         )
1238         Equal(t, expected, actual)
1239
1240         expected = `
1241
1242 Diff:
1243 --- Expected
1244 +++ Actual
1245 @@ -2,5 +2,5 @@
1246   (int) 1,
1247 - (int) 2,
1248   (int) 3,
1249 - (int) 4
1250 + (int) 5,
1251 + (int) 7
1252  }
1253 `
1254         actual = diff(
1255                 []int{1, 2, 3, 4},
1256                 []int{1, 3, 5, 7},
1257         )
1258         Equal(t, expected, actual)
1259
1260         expected = `
1261
1262 Diff:
1263 --- Expected
1264 +++ Actual
1265 @@ -2,4 +2,4 @@
1266   (int) 1,
1267 - (int) 2,
1268 - (int) 3
1269 + (int) 3,
1270 + (int) 5
1271  }
1272 `
1273         actual = diff(
1274                 []int{1, 2, 3, 4}[0:3],
1275                 []int{1, 3, 5, 7}[0:3],
1276         )
1277         Equal(t, expected, actual)
1278
1279         expected = `
1280
1281 Diff:
1282 --- Expected
1283 +++ Actual
1284 @@ -1,6 +1,6 @@
1285  (map[string]int) (len=4) {
1286 - (string) (len=4) "four": (int) 4,
1287 + (string) (len=4) "five": (int) 5,
1288   (string) (len=3) "one": (int) 1,
1289 - (string) (len=5) "three": (int) 3,
1290 - (string) (len=3) "two": (int) 2
1291 + (string) (len=5) "seven": (int) 7,
1292 + (string) (len=5) "three": (int) 3
1293  }
1294 `
1295
1296         actual = diff(
1297                 map[string]int{"one": 1, "two": 2, "three": 3, "four": 4},
1298                 map[string]int{"one": 1, "three": 3, "five": 5, "seven": 7},
1299         )
1300         Equal(t, expected, actual)
1301 }
1302
1303 func TestDiffEmptyCases(t *testing.T) {
1304         Equal(t, "", diff(nil, nil))
1305         Equal(t, "", diff(struct{ foo string }{}, nil))
1306         Equal(t, "", diff(nil, struct{ foo string }{}))
1307         Equal(t, "", diff(1, 2))
1308         Equal(t, "", diff(1, 2))
1309         Equal(t, "", diff([]int{1}, []bool{true}))
1310 }
1311
1312 // Ensure there are no data races
1313 func TestDiffRace(t *testing.T) {
1314         t.Parallel()
1315
1316         expected := map[string]string{
1317                 "a": "A",
1318                 "b": "B",
1319                 "c": "C",
1320         }
1321
1322         actual := map[string]string{
1323                 "d": "D",
1324                 "e": "E",
1325                 "f": "F",
1326         }
1327
1328         // run diffs in parallel simulating tests with t.Parallel()
1329         numRoutines := 10
1330         rChans := make([]chan string, numRoutines)
1331         for idx := range rChans {
1332                 rChans[idx] = make(chan string)
1333                 go func(ch chan string) {
1334                         defer close(ch)
1335                         ch <- diff(expected, actual)
1336                 }(rChans[idx])
1337         }
1338
1339         for _, ch := range rChans {
1340                 for msg := range ch {
1341                         NotZero(t, msg) // dummy assert
1342                 }
1343         }
1344 }
1345
1346 type mockTestingT struct {
1347 }
1348
1349 func (m *mockTestingT) Errorf(format string, args ...interface{}) {}
1350
1351 func TestFailNowWithPlainTestingT(t *testing.T) {
1352         mockT := &mockTestingT{}
1353
1354         Panics(t, func() {
1355                 FailNow(mockT, "failed")
1356         }, "should panic since mockT is missing FailNow()")
1357 }
1358
1359 type mockFailNowTestingT struct {
1360 }
1361
1362 func (m *mockFailNowTestingT) Errorf(format string, args ...interface{}) {}
1363
1364 func (m *mockFailNowTestingT) FailNow() {}
1365
1366 func TestFailNowWithFullTestingT(t *testing.T) {
1367         mockT := &mockFailNowTestingT{}
1368
1369         NotPanics(t, func() {
1370                 FailNow(mockT, "failed")
1371         }, "should call mockT.FailNow() rather than panicking")
1372 }
1373
1374 func TestBytesEqual(t *testing.T) {
1375         var cases = []struct {
1376                 a, b []byte
1377         }{
1378                 {make([]byte, 2), make([]byte, 2)},
1379                 {make([]byte, 2), make([]byte, 2, 3)},
1380                 {nil, make([]byte, 0)},
1381         }
1382         for i, c := range cases {
1383                 Equal(t, reflect.DeepEqual(c.a, c.b), ObjectsAreEqual(c.a, c.b), "case %d failed", i+1)
1384         }
1385 }
1386
1387 func BenchmarkBytesEqual(b *testing.B) {
1388         const size = 1024 * 8
1389         s := make([]byte, size)
1390         for i := range s {
1391                 s[i] = byte(i % 255)
1392         }
1393         s2 := make([]byte, size)
1394         copy(s2, s)
1395
1396         mockT := &mockFailNowTestingT{}
1397         b.ResetTimer()
1398         for i := 0; i < b.N; i++ {
1399                 Equal(mockT, s, s2)
1400         }
1401 }
1402
1403 func TestEqualArgsValidation(t *testing.T) {
1404         err := validateEqualArgs(time.Now, time.Now)
1405         EqualError(t, err, "cannot take func type as argument")
1406 }