20 zeros = []interface{}{
47 map[interface{}]interface{}(nil),
48 (chan interface{})(nil),
49 (<-chan interface{})(nil),
50 (chan<- interface{})(nil),
52 nonZeros = []interface{}{
78 map[interface{}]interface{}{},
79 (chan interface{})(make(chan interface{})),
80 (<-chan interface{})(make(chan interface{})),
81 (chan<- interface{})(make(chan interface{})),
85 // AssertionTesterInterface defines an interface to be used for testing assertion methods
86 type AssertionTesterInterface interface {
90 // AssertionTesterConformingObject is an object that conforms to the AssertionTesterInterface interface
91 type AssertionTesterConformingObject struct {
94 func (a *AssertionTesterConformingObject) TestMethod() {
97 // AssertionTesterNonConformingObject is an object that does not conform to the AssertionTesterInterface interface
98 type AssertionTesterNonConformingObject struct {
101 func TestObjectsAreEqual(t *testing.T) {
103 if !ObjectsAreEqual("Hello World", "Hello World") {
104 t.Error("objectsAreEqual should return true")
106 if !ObjectsAreEqual(123, 123) {
107 t.Error("objectsAreEqual should return true")
109 if !ObjectsAreEqual(123.5, 123.5) {
110 t.Error("objectsAreEqual should return true")
112 if !ObjectsAreEqual([]byte("Hello World"), []byte("Hello World")) {
113 t.Error("objectsAreEqual should return true")
115 if !ObjectsAreEqual(nil, nil) {
116 t.Error("objectsAreEqual should return true")
118 if ObjectsAreEqual(map[int]int{5: 10}, map[int]int{10: 20}) {
119 t.Error("objectsAreEqual should return false")
121 if ObjectsAreEqual('x', "x") {
122 t.Error("objectsAreEqual should return false")
124 if ObjectsAreEqual("x", 'x') {
125 t.Error("objectsAreEqual should return false")
127 if ObjectsAreEqual(0, 0.1) {
128 t.Error("objectsAreEqual should return false")
130 if ObjectsAreEqual(0.1, 0) {
131 t.Error("objectsAreEqual should return false")
133 if ObjectsAreEqual(uint32(10), int32(10)) {
134 t.Error("objectsAreEqual should return false")
136 if !ObjectsAreEqualValues(uint32(10), int32(10)) {
137 t.Error("ObjectsAreEqualValues should return true")
139 if ObjectsAreEqualValues(0, nil) {
142 if ObjectsAreEqualValues(nil, 0) {
148 func TestImplements(t *testing.T) {
150 mockT := new(testing.T)
152 if !Implements(mockT, (*AssertionTesterInterface)(nil), new(AssertionTesterConformingObject)) {
153 t.Error("Implements method should return true: AssertionTesterConformingObject implements AssertionTesterInterface")
155 if Implements(mockT, (*AssertionTesterInterface)(nil), new(AssertionTesterNonConformingObject)) {
156 t.Error("Implements method should return false: AssertionTesterNonConformingObject does not implements AssertionTesterInterface")
161 func TestIsType(t *testing.T) {
163 mockT := new(testing.T)
165 if !IsType(mockT, new(AssertionTesterConformingObject), new(AssertionTesterConformingObject)) {
166 t.Error("IsType should return true: AssertionTesterConformingObject is the same type as AssertionTesterConformingObject")
168 if IsType(mockT, new(AssertionTesterConformingObject), new(AssertionTesterNonConformingObject)) {
169 t.Error("IsType should return false: AssertionTesterConformingObject is not the same type as AssertionTesterNonConformingObject")
174 func TestEqual(t *testing.T) {
176 mockT := new(testing.T)
178 if !Equal(mockT, "Hello World", "Hello World") {
179 t.Error("Equal should return true")
181 if !Equal(mockT, 123, 123) {
182 t.Error("Equal should return true")
184 if !Equal(mockT, 123.5, 123.5) {
185 t.Error("Equal should return true")
187 if !Equal(mockT, []byte("Hello World"), []byte("Hello World")) {
188 t.Error("Equal should return true")
190 if !Equal(mockT, nil, nil) {
191 t.Error("Equal should return true")
193 if !Equal(mockT, int32(123), int32(123)) {
194 t.Error("Equal should return true")
196 if !Equal(mockT, uint64(123), uint64(123)) {
197 t.Error("Equal should return true")
199 if !Equal(mockT, &struct{}{}, &struct{}{}) {
200 t.Error("Equal should return true (pointer equality is based on equality of underlying value)")
202 var m map[string]interface{}
203 if Equal(mockT, m["bar"], "something") {
204 t.Error("Equal should return false")
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 {
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.
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:]
229 buf := new(bytes.Buffer)
230 // Every line is indented at least one tab.
232 fmt.Fprintf(buf, "%s:%d: ", file, line)
233 lines := strings.Split(s, "\n")
234 if l := len(lines); l > 1 && lines[l-1] == "" {
237 for i, line := range lines {
239 // Second and subsequent lines are indented an extra tab.
240 buf.WriteString("\n\t\t")
242 buf.WriteString(line)
247 t.buf.WriteString(decorate(fmt.Sprintf(format, args...)))
250 func TestEqualFormatting(t *testing.T) {
251 for i, currCase := range []struct {
254 msgAndArgs []interface{}
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"},
261 Equal(mockT, currCase.equalWant, currCase.equalGot, currCase.msgAndArgs...)
262 Regexp(t, regexp.MustCompile(currCase.want), mockT.buf.String(), "Case %d", i)
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")
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")
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")
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")
283 type testStructType struct {
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")
292 func TestNotNil(t *testing.T) {
294 mockT := new(testing.T)
296 if !NotNil(mockT, new(AssertionTesterConformingObject)) {
297 t.Error("NotNil should return true: object is not nil")
299 if NotNil(mockT, nil) {
300 t.Error("NotNil should return false: object is nil")
302 if NotNil(mockT, (*struct{})(nil)) {
303 t.Error("NotNil should return false: object is (*struct{})(nil)")
308 func TestNil(t *testing.T) {
310 mockT := new(testing.T)
312 if !Nil(mockT, nil) {
313 t.Error("Nil should return true: object is nil")
315 if !Nil(mockT, (*struct{})(nil)) {
316 t.Error("Nil should return true: object is (*struct{})(nil)")
318 if Nil(mockT, new(AssertionTesterConformingObject)) {
319 t.Error("Nil should return false: object is not nil")
324 func TestTrue(t *testing.T) {
326 mockT := new(testing.T)
328 if !True(mockT, true) {
329 t.Error("True should return true")
331 if True(mockT, false) {
332 t.Error("True should return false")
337 func TestFalse(t *testing.T) {
339 mockT := new(testing.T)
341 if !False(mockT, false) {
342 t.Error("False should return true")
344 if False(mockT, true) {
345 t.Error("False should return false")
350 func TestExactly(t *testing.T) {
352 mockT := new(testing.T)
359 if Exactly(mockT, a, b) {
360 t.Error("Exactly should return false")
362 if Exactly(mockT, a, d) {
363 t.Error("Exactly should return false")
365 if !Exactly(mockT, a, c) {
366 t.Error("Exactly should return true")
369 if Exactly(mockT, nil, a) {
370 t.Error("Exactly should return false")
372 if Exactly(mockT, a, nil) {
373 t.Error("Exactly should return false")
378 func TestNotEqual(t *testing.T) {
380 mockT := new(testing.T)
382 if !NotEqual(mockT, "Hello World", "Hello World!") {
383 t.Error("NotEqual should return true")
385 if !NotEqual(mockT, 123, 1234) {
386 t.Error("NotEqual should return true")
388 if !NotEqual(mockT, 123.5, 123.55) {
389 t.Error("NotEqual should return true")
391 if !NotEqual(mockT, []byte("Hello World"), []byte("Hello World!")) {
392 t.Error("NotEqual should return true")
394 if !NotEqual(mockT, nil, new(AssertionTesterConformingObject)) {
395 t.Error("NotEqual should return true")
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")
403 if NotEqual(mockT, "Hello World", "Hello World") {
404 t.Error("NotEqual should return false")
406 if NotEqual(mockT, 123, 123) {
407 t.Error("NotEqual should return false")
409 if NotEqual(mockT, 123.5, 123.5) {
410 t.Error("NotEqual should return false")
412 if NotEqual(mockT, []byte("Hello World"), []byte("Hello World")) {
413 t.Error("NotEqual should return false")
415 if NotEqual(mockT, new(AssertionTesterConformingObject), new(AssertionTesterConformingObject)) {
416 t.Error("NotEqual should return false")
418 if NotEqual(mockT, &struct{}{}, &struct{}{}) {
419 t.Error("NotEqual should return false")
427 func TestContains(t *testing.T) {
429 mockT := new(testing.T)
430 list := []string{"Foo", "Bar"}
437 simpleMap := map[interface{}]interface{}{"Foo": "Bar"}
439 if !Contains(mockT, "Hello World", "Hello") {
440 t.Error("Contains should return true: \"Hello World\" contains \"Hello\"")
442 if Contains(mockT, "Hello World", "Salut") {
443 t.Error("Contains should return false: \"Hello World\" does not contain \"Salut\"")
446 if !Contains(mockT, list, "Bar") {
447 t.Error("Contains should return true: \"[\"Foo\", \"Bar\"]\" contains \"Bar\"")
449 if Contains(mockT, list, "Salut") {
450 t.Error("Contains should return false: \"[\"Foo\", \"Bar\"]\" does not contain \"Salut\"")
452 if !Contains(mockT, complexList, &A{"g", "h"}) {
453 t.Error("Contains should return true: complexList contains {\"g\", \"h\"}")
455 if Contains(mockT, complexList, &A{"g", "e"}) {
456 t.Error("Contains should return false: complexList contains {\"g\", \"e\"}")
458 if Contains(mockT, complexList, &A{"g", "e"}) {
459 t.Error("Contains should return false: complexList contains {\"g\", \"e\"}")
461 if !Contains(mockT, simpleMap, "Foo") {
462 t.Error("Contains should return true: \"{\"Foo\": \"Bar\"}\" contains \"Foo\"")
464 if Contains(mockT, simpleMap, "Bar") {
465 t.Error("Contains should return false: \"{\"Foo\": \"Bar\"}\" does not contains \"Bar\"")
469 func TestNotContains(t *testing.T) {
471 mockT := new(testing.T)
472 list := []string{"Foo", "Bar"}
473 simpleMap := map[interface{}]interface{}{"Foo": "Bar"}
475 if !NotContains(mockT, "Hello World", "Hello!") {
476 t.Error("NotContains should return true: \"Hello World\" does not contain \"Hello!\"")
478 if NotContains(mockT, "Hello World", "Hello") {
479 t.Error("NotContains should return false: \"Hello World\" contains \"Hello\"")
482 if !NotContains(mockT, list, "Foo!") {
483 t.Error("NotContains should return true: \"[\"Foo\", \"Bar\"]\" does not contain \"Foo!\"")
485 if NotContains(mockT, list, "Foo") {
486 t.Error("NotContains should return false: \"[\"Foo\", \"Bar\"]\" contains \"Foo\"")
488 if NotContains(mockT, simpleMap, "Foo") {
489 t.Error("Contains should return true: \"{\"Foo\": \"Bar\"}\" contains \"Foo\"")
491 if !NotContains(mockT, simpleMap, "Bar") {
492 t.Error("Contains should return false: \"{\"Foo\": \"Bar\"}\" does not contains \"Bar\"")
496 func TestSubset(t *testing.T) {
497 mockT := new(testing.T)
499 if !Subset(mockT, []int{1, 2, 3}, nil) {
500 t.Error("Subset should return true: given subset is nil")
502 if !Subset(mockT, []int{1, 2, 3}, []int{}) {
503 t.Error("Subset should return true: any set contains the nil set")
505 if !Subset(mockT, []int{1, 2, 3}, []int{1, 2}) {
506 t.Error("Subset should return true: [1, 2, 3] contains [1, 2]")
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]")
511 if !Subset(mockT, []string{"hello", "world"}, []string{"hello"}) {
512 t.Error("Subset should return true: [\"hello\", \"world\"] contains [\"hello\"]")
515 if Subset(mockT, []string{"hello", "world"}, []string{"hello", "testify"}) {
516 t.Error("Subset should return false: [\"hello\", \"world\"] does not contain [\"hello\", \"testify\"]")
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]")
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]")
526 func TestNotSubset(t *testing.T) {
527 mockT := new(testing.T)
529 if NotSubset(mockT, []int{1, 2, 3}, nil) {
530 t.Error("NotSubset should return false: given subset is nil")
532 if NotSubset(mockT, []int{1, 2, 3}, []int{}) {
533 t.Error("NotSubset should return false: any set contains the nil set")
535 if NotSubset(mockT, []int{1, 2, 3}, []int{1, 2}) {
536 t.Error("NotSubset should return false: [1, 2, 3] contains [1, 2]")
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]")
541 if NotSubset(mockT, []string{"hello", "world"}, []string{"hello"}) {
542 t.Error("NotSubset should return false: [\"hello\", \"world\"] contains [\"hello\"]")
545 if !NotSubset(mockT, []string{"hello", "world"}, []string{"hello", "testify"}) {
546 t.Error("NotSubset should return true: [\"hello\", \"world\"] does not contain [\"hello\", \"testify\"]")
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]")
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]")
556 func Test_includeElement(t *testing.T) {
558 list1 := []string{"Foo", "Bar"}
560 simpleMap := map[interface{}]interface{}{"Foo": "Bar"}
562 ok, found := includeElement("Hello World", "World")
566 ok, found = includeElement(list1, "Foo")
570 ok, found = includeElement(list1, "Bar")
574 ok, found = includeElement(list2, 1)
578 ok, found = includeElement(list2, 2)
582 ok, found = includeElement(list1, "Foo!")
586 ok, found = includeElement(list2, 3)
590 ok, found = includeElement(list2, "1")
594 ok, found = includeElement(simpleMap, "Foo")
598 ok, found = includeElement(simpleMap, "Bar")
602 ok, found = includeElement(1433, "1")
607 func TestCondition(t *testing.T) {
608 mockT := new(testing.T)
610 if !Condition(mockT, func() bool { return true }, "Truth") {
611 t.Error("Condition should return true")
614 if Condition(mockT, func() bool { return false }, "Lie") {
615 t.Error("Condition should return false")
620 func TestDidPanic(t *testing.T) {
622 if funcDidPanic, _ := didPanic(func() {
625 t.Error("didPanic should return true")
628 if funcDidPanic, _ := didPanic(func() {
630 t.Error("didPanic should return false")
635 func TestPanics(t *testing.T) {
637 mockT := new(testing.T)
639 if !Panics(mockT, func() {
642 t.Error("Panics should return true")
645 if Panics(mockT, func() {
647 t.Error("Panics should return false")
652 func TestPanicsWithValue(t *testing.T) {
654 mockT := new(testing.T)
656 if !PanicsWithValue(mockT, "Panic!", func() {
659 t.Error("PanicsWithValue should return true")
662 if PanicsWithValue(mockT, "Panic!", func() {
664 t.Error("PanicsWithValue should return false")
667 if PanicsWithValue(mockT, "at the disco", func() {
670 t.Error("PanicsWithValue should return false")
674 func TestNotPanics(t *testing.T) {
676 mockT := new(testing.T)
678 if !NotPanics(mockT, func() {
680 t.Error("NotPanics should return true")
683 if NotPanics(mockT, func() {
686 t.Error("NotPanics should return false")
691 func TestNoError(t *testing.T) {
693 mockT := new(testing.T)
695 // start with a nil error
698 True(t, NoError(mockT, err), "NoError should return True for nil arg")
701 err = errors.New("some error")
703 False(t, NoError(mockT, err), "NoError with error should return False")
705 // returning an empty error interface
709 t.Fatal("err should be nil here")
714 if err == nil { // err is not nil here!
715 t.Errorf("Error should be nil due to empty interface: %s", err)
718 False(t, NoError(mockT, err), "NoError should fail with empty error interface")
721 type customError struct{}
723 func (*customError) Error() string { return "fail" }
725 func TestError(t *testing.T) {
727 mockT := new(testing.T)
729 // start with a nil error
732 False(t, Error(mockT, err), "Error should return False for nil arg")
735 err = errors.New("some error")
737 True(t, Error(mockT, err), "Error with error should return True")
740 True(t, Errorf(mockT, err, "example with %s", "formatted message"), "Errorf with error should rturn True")
742 // returning an empty error interface
746 t.Fatal("err should be nil here")
751 if err == nil { // err is not nil here!
752 t.Errorf("Error should be nil due to empty interface: %s", err)
755 True(t, Error(mockT, err), "Error should pass with empty error interface")
758 func TestEqualError(t *testing.T) {
759 mockT := new(testing.T)
761 // start with a nil error
763 False(t, EqualError(mockT, err, ""),
764 "EqualError should return false for nil arg")
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")
774 func Test_isEmpty(t *testing.T) {
776 chWithValue := make(chan struct{}, 1)
777 chWithValue <- struct{}{}
780 True(t, isEmpty(nil))
781 True(t, isEmpty([]string{}))
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"}))
794 False(t, isEmpty(true))
795 False(t, isEmpty(map[string]string{"Hello": "World"}))
796 False(t, isEmpty(chWithValue))
800 func TestEmpty(t *testing.T) {
802 mockT := new(testing.T)
803 chWithValue := make(chan struct{}, 1)
804 chWithValue <- struct{}{}
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")
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")
829 func TestNotEmpty(t *testing.T) {
831 mockT := new(testing.T)
832 chWithValue := make(chan struct{}, 1)
833 chWithValue <- struct{}{}
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")
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")
850 func Test_getLen(t *testing.T) {
851 falseCases := []interface{}{
859 for _, v := range falseCases {
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)
865 ch := make(chan int, 5)
869 trueCases := []struct {
874 {[...]int{1, 2, 3}, 3},
876 {map[int]int{1: 2, 2: 4, 3: 6}, 3},
884 {map[int]int(nil), 0},
885 {(chan int)(nil), 0},
888 for _, c := range trueCases {
890 True(t, ok, "Expected getLen success to get length of %#v", c.v)
895 func TestLen(t *testing.T) {
896 mockT := new(testing.T)
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")
905 ch := make(chan int, 5)
915 {[...]int{1, 2, 3}, 3},
917 {map[int]int{1: 2, 2: 4, 3: 6}, 3},
925 {map[int]int(nil), 0},
926 {(chan int)(nil), 0},
929 for _, c := range cases {
930 True(t, Len(mockT, c.v, c.l), "%#v have %d items", c.v, c.l)
938 {[...]int{1, 2, 3}, 2},
940 {map[int]int{1: 2, 2: 4, 3: 6}, 4},
948 {map[int]int(nil), 1},
949 {(chan int)(nil), 1},
952 for _, c := range cases {
953 False(t, Len(mockT, c.v, c.l), "%#v have %d items", c.v, c.l)
957 func TestWithinDuration(t *testing.T) {
959 mockT := new(testing.T)
961 b := a.Add(10 * time.Second)
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")
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")
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")
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")
976 func TestInDelta(t *testing.T) {
977 mockT := new(testing.T)
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")
992 {uint8(2), uint8(1), 1},
993 {uint16(2), uint16(1), 1},
994 {uint32(2), uint32(1), 1},
995 {uint64(2), uint64(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},
1003 {float32(2), float32(1), 1},
1004 {float64(2), float64(1), 1},
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)
1012 func TestInDeltaSlice(t *testing.T) {
1013 mockT := new(testing.T)
1015 True(t, InDeltaSlice(mockT,
1016 []float64{1.001, 0.999},
1018 0.1), "{1.001, 0.009} is element-wise close to {1, 1} in delta=0.1")
1020 True(t, InDeltaSlice(mockT,
1023 1), "{1, 2} is element-wise close to {0, 3} in delta=1")
1025 False(t, InDeltaSlice(mockT,
1028 0.1), "{1, 2} is not element-wise close to {0, 3} in delta=0.1")
1030 False(t, InDeltaSlice(mockT, "", nil, 1), "Expected non numeral slices to fail")
1033 func TestInEpsilon(t *testing.T) {
1034 mockT := new(testing.T)
1040 {uint8(2), uint16(2), .001},
1045 {uint64(100), uint8(101), 0.01},
1048 {time.Second, time.Second + time.Millisecond, 0.002},
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)
1059 {uint8(2), int16(-2), .001},
1060 {uint64(100), uint8(102), 0.01},
1064 {2.1, "bla-bla", 0},
1066 {0, 0.1, 2}, // expected must be different to zero
1067 {time.Second, time.Second + 10*time.Millisecond, 0.002},
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))
1076 func TestInEpsilonSlice(t *testing.T) {
1077 mockT := new(testing.T)
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")
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")
1089 False(t, InEpsilonSlice(mockT, "", nil, 1), "Expected non numeral slices to fail")
1092 func TestRegexp(t *testing.T) {
1093 mockT := new(testing.T)
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"},
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))
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"},
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))
1126 func testAutogeneratedFunction() {
1128 if err := recover(); err == nil {
1129 panic("did not panic")
1141 func TestCallerInfoWithAutogeneratedFunctions(t *testing.T) {
1142 NotPanics(t, func() {
1143 testAutogeneratedFunction()
1147 func TestZero(t *testing.T) {
1148 mockT := new(testing.T)
1150 for _, test := range zeros {
1151 True(t, Zero(mockT, test, "%#v is not the %v zero value", test, reflect.TypeOf(test)))
1154 for _, test := range nonZeros {
1155 False(t, Zero(mockT, test, "%#v is not the %v zero value", test, reflect.TypeOf(test)))
1159 func TestNotZero(t *testing.T) {
1160 mockT := new(testing.T)
1162 for _, test := range zeros {
1163 False(t, NotZero(mockT, test, "%#v is not the %v zero value", test, reflect.TypeOf(test)))
1166 for _, test := range nonZeros {
1167 True(t, NotZero(mockT, test, "%#v is not the %v zero value", test, reflect.TypeOf(test)))
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"}`))
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"}`))
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}"))
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"}]`))
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"}}`))
1197 func TestJSONEq_HashesNotEquivalent(t *testing.T) {
1198 mockT := new(testing.T)
1199 False(t, JSONEq(mockT, `{"foo": "bar"}`, `{"foo": "bar", "hello": "world"}`))
1202 func TestJSONEq_ActualIsNotJSON(t *testing.T) {
1203 mockT := new(testing.T)
1204 False(t, JSONEq(mockT, `{"foo": "bar"}`, "Not JSON"))
1207 func TestJSONEq_ExpectedIsNotJSON(t *testing.T) {
1208 mockT := new(testing.T)
1209 False(t, JSONEq(mockT, "Not JSON", `{"foo": "bar", "hello": "world"}`))
1212 func TestJSONEq_ExpectedAndActualNotJSON(t *testing.T) {
1213 mockT := new(testing.T)
1214 False(t, JSONEq(mockT, "Not JSON", "Not JSON"))
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"]`))
1222 func TestDiff(t *testing.T) {
1229 (struct { foo string }) {
1230 - foo: (string) (len=5) "hello"
1231 + foo: (string) (len=3) "bar"
1235 struct{ foo string }{"hello"},
1236 struct{ foo string }{"bar"},
1238 Equal(t, expected, actual)
1258 Equal(t, expected, actual)
1274 []int{1, 2, 3, 4}[0:3],
1275 []int{1, 3, 5, 7}[0:3],
1277 Equal(t, expected, actual)
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
1297 map[string]int{"one": 1, "two": 2, "three": 3, "four": 4},
1298 map[string]int{"one": 1, "three": 3, "five": 5, "seven": 7},
1300 Equal(t, expected, actual)
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}))
1312 // Ensure there are no data races
1313 func TestDiffRace(t *testing.T) {
1316 expected := map[string]string{
1322 actual := map[string]string{
1328 // run diffs in parallel simulating tests with t.Parallel()
1330 rChans := make([]chan string, numRoutines)
1331 for idx := range rChans {
1332 rChans[idx] = make(chan string)
1333 go func(ch chan string) {
1335 ch <- diff(expected, actual)
1339 for _, ch := range rChans {
1340 for msg := range ch {
1341 NotZero(t, msg) // dummy assert
1346 type mockTestingT struct {
1349 func (m *mockTestingT) Errorf(format string, args ...interface{}) {}
1351 func TestFailNowWithPlainTestingT(t *testing.T) {
1352 mockT := &mockTestingT{}
1355 FailNow(mockT, "failed")
1356 }, "should panic since mockT is missing FailNow()")
1359 type mockFailNowTestingT struct {
1362 func (m *mockFailNowTestingT) Errorf(format string, args ...interface{}) {}
1364 func (m *mockFailNowTestingT) FailNow() {}
1366 func TestFailNowWithFullTestingT(t *testing.T) {
1367 mockT := &mockFailNowTestingT{}
1369 NotPanics(t, func() {
1370 FailNow(mockT, "failed")
1371 }, "should call mockT.FailNow() rather than panicking")
1374 func TestBytesEqual(t *testing.T) {
1375 var cases = []struct {
1378 {make([]byte, 2), make([]byte, 2)},
1379 {make([]byte, 2), make([]byte, 2, 3)},
1380 {nil, make([]byte, 0)},
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)
1387 func BenchmarkBytesEqual(b *testing.B) {
1388 const size = 1024 * 8
1389 s := make([]byte, size)
1391 s[i] = byte(i % 255)
1393 s2 := make([]byte, size)
1396 mockT := &mockFailNowTestingT{}
1398 for i := 0; i < b.N; i++ {
1403 func TestEqualArgsValidation(t *testing.T) {
1404 err := validateEqualArgs(time.Now, time.Now)
1405 EqualError(t, err, "cannot take func type as argument")