1package assert 2 3import ( 4 "bytes" 5 "encoding/json" 6 "errors" 7 "fmt" 8 "io" 9 "math" 10 "os" 11 "reflect" 12 "regexp" 13 "runtime" 14 "strings" 15 "testing" 16 "time" 17) 18 19var ( 20 i interface{} 21 zeros = []interface{}{ 22 false, 23 byte(0), 24 complex64(0), 25 complex128(0), 26 float32(0), 27 float64(0), 28 int(0), 29 int8(0), 30 int16(0), 31 int32(0), 32 int64(0), 33 rune(0), 34 uint(0), 35 uint8(0), 36 uint16(0), 37 uint32(0), 38 uint64(0), 39 uintptr(0), 40 "", 41 [0]interface{}{}, 42 []interface{}(nil), 43 struct{ x int }{}, 44 (*interface{})(nil), 45 (func())(nil), 46 nil, 47 interface{}(nil), 48 map[interface{}]interface{}(nil), 49 (chan interface{})(nil), 50 (<-chan interface{})(nil), 51 (chan<- interface{})(nil), 52 } 53 nonZeros = []interface{}{ 54 true, 55 byte(1), 56 complex64(1), 57 complex128(1), 58 float32(1), 59 float64(1), 60 int(1), 61 int8(1), 62 int16(1), 63 int32(1), 64 int64(1), 65 rune(1), 66 uint(1), 67 uint8(1), 68 uint16(1), 69 uint32(1), 70 uint64(1), 71 uintptr(1), 72 "s", 73 [1]interface{}{1}, 74 []interface{}{}, 75 struct{ x int }{1}, 76 (*interface{})(&i), 77 (func())(func() {}), 78 interface{}(1), 79 map[interface{}]interface{}{}, 80 (chan interface{})(make(chan interface{})), 81 (<-chan interface{})(make(chan interface{})), 82 (chan<- interface{})(make(chan interface{})), 83 } 84) 85 86// AssertionTesterInterface defines an interface to be used for testing assertion methods 87type AssertionTesterInterface interface { 88 TestMethod() 89} 90 91// AssertionTesterConformingObject is an object that conforms to the AssertionTesterInterface interface 92type AssertionTesterConformingObject struct { 93} 94 95func (a *AssertionTesterConformingObject) TestMethod() { 96} 97 98// AssertionTesterNonConformingObject is an object that does not conform to the AssertionTesterInterface interface 99type AssertionTesterNonConformingObject struct { 100} 101 102func TestObjectsAreEqual(t *testing.T) { 103 104 if !ObjectsAreEqual("Hello World", "Hello World") { 105 t.Error("objectsAreEqual should return true") 106 } 107 if !ObjectsAreEqual(123, 123) { 108 t.Error("objectsAreEqual should return true") 109 } 110 if !ObjectsAreEqual(123.5, 123.5) { 111 t.Error("objectsAreEqual should return true") 112 } 113 if !ObjectsAreEqual([]byte("Hello World"), []byte("Hello World")) { 114 t.Error("objectsAreEqual should return true") 115 } 116 if !ObjectsAreEqual(nil, nil) { 117 t.Error("objectsAreEqual should return true") 118 } 119 if ObjectsAreEqual(map[int]int{5: 10}, map[int]int{10: 20}) { 120 t.Error("objectsAreEqual should return false") 121 } 122 if ObjectsAreEqual('x', "x") { 123 t.Error("objectsAreEqual should return false") 124 } 125 if ObjectsAreEqual("x", 'x') { 126 t.Error("objectsAreEqual should return false") 127 } 128 if ObjectsAreEqual(0, 0.1) { 129 t.Error("objectsAreEqual should return false") 130 } 131 if ObjectsAreEqual(0.1, 0) { 132 t.Error("objectsAreEqual should return false") 133 } 134 if ObjectsAreEqual(time.Now, time.Now) { 135 t.Error("objectsAreEqual should return false") 136 } 137 if ObjectsAreEqual(func() {}, func() {}) { 138 t.Error("objectsAreEqual should return false") 139 } 140 if ObjectsAreEqual(uint32(10), int32(10)) { 141 t.Error("objectsAreEqual should return false") 142 } 143 if !ObjectsAreEqualValues(uint32(10), int32(10)) { 144 t.Error("ObjectsAreEqualValues should return true") 145 } 146 if ObjectsAreEqualValues(0, nil) { 147 t.Fail() 148 } 149 if ObjectsAreEqualValues(nil, 0) { 150 t.Fail() 151 } 152 153} 154 155func TestImplements(t *testing.T) { 156 157 mockT := new(testing.T) 158 159 if !Implements(mockT, (*AssertionTesterInterface)(nil), new(AssertionTesterConformingObject)) { 160 t.Error("Implements method should return true: AssertionTesterConformingObject implements AssertionTesterInterface") 161 } 162 if Implements(mockT, (*AssertionTesterInterface)(nil), new(AssertionTesterNonConformingObject)) { 163 t.Error("Implements method should return false: AssertionTesterNonConformingObject does not implements AssertionTesterInterface") 164 } 165 if Implements(mockT, (*AssertionTesterInterface)(nil), nil) { 166 t.Error("Implements method should return false: nil does not implement AssertionTesterInterface") 167 } 168 169} 170 171func TestIsType(t *testing.T) { 172 173 mockT := new(testing.T) 174 175 if !IsType(mockT, new(AssertionTesterConformingObject), new(AssertionTesterConformingObject)) { 176 t.Error("IsType should return true: AssertionTesterConformingObject is the same type as AssertionTesterConformingObject") 177 } 178 if IsType(mockT, new(AssertionTesterConformingObject), new(AssertionTesterNonConformingObject)) { 179 t.Error("IsType should return false: AssertionTesterConformingObject is not the same type as AssertionTesterNonConformingObject") 180 } 181 182} 183 184type myType string 185 186func TestEqual(t *testing.T) { 187 188 mockT := new(testing.T) 189 190 if !Equal(mockT, "Hello World", "Hello World") { 191 t.Error("Equal should return true") 192 } 193 if !Equal(mockT, 123, 123) { 194 t.Error("Equal should return true") 195 } 196 if !Equal(mockT, 123.5, 123.5) { 197 t.Error("Equal should return true") 198 } 199 if !Equal(mockT, []byte("Hello World"), []byte("Hello World")) { 200 t.Error("Equal should return true") 201 } 202 if !Equal(mockT, nil, nil) { 203 t.Error("Equal should return true") 204 } 205 if !Equal(mockT, int32(123), int32(123)) { 206 t.Error("Equal should return true") 207 } 208 if !Equal(mockT, uint64(123), uint64(123)) { 209 t.Error("Equal should return true") 210 } 211 if !Equal(mockT, myType("1"), myType("1")) { 212 t.Error("Equal should return true") 213 } 214 if !Equal(mockT, &struct{}{}, &struct{}{}) { 215 t.Error("Equal should return true (pointer equality is based on equality of underlying value)") 216 } 217 var m map[string]interface{} 218 if Equal(mockT, m["bar"], "something") { 219 t.Error("Equal should return false") 220 } 221 if Equal(mockT, myType("1"), myType("2")) { 222 t.Error("Equal should return false") 223 } 224} 225 226func ptr(i int) *int { 227 return &i 228} 229 230func TestSame(t *testing.T) { 231 232 mockT := new(testing.T) 233 234 if Same(mockT, ptr(1), ptr(1)) { 235 t.Error("Same should return false") 236 } 237 if Same(mockT, 1, 1) { 238 t.Error("Same should return false") 239 } 240 p := ptr(2) 241 if Same(mockT, p, *p) { 242 t.Error("Same should return false") 243 } 244 if !Same(mockT, p, p) { 245 t.Error("Same should return true") 246 } 247} 248 249func TestNotSame(t *testing.T) { 250 251 mockT := new(testing.T) 252 253 if !NotSame(mockT, ptr(1), ptr(1)) { 254 t.Error("NotSame should return true; different pointers") 255 } 256 if !NotSame(mockT, 1, 1) { 257 t.Error("NotSame should return true; constant inputs") 258 } 259 p := ptr(2) 260 if !NotSame(mockT, p, *p) { 261 t.Error("NotSame should return true; mixed-type inputs") 262 } 263 if NotSame(mockT, p, p) { 264 t.Error("NotSame should return false") 265 } 266} 267 268func Test_samePointers(t *testing.T) { 269 p := ptr(2) 270 271 type args struct { 272 first interface{} 273 second interface{} 274 } 275 tests := []struct { 276 name string 277 args args 278 assertion BoolAssertionFunc 279 }{ 280 { 281 name: "1 != 2", 282 args: args{first: 1, second: 2}, 283 assertion: False, 284 }, 285 { 286 name: "1 != 1 (not same ptr)", 287 args: args{first: 1, second: 1}, 288 assertion: False, 289 }, 290 { 291 name: "ptr(1) == ptr(1)", 292 args: args{first: p, second: p}, 293 assertion: True, 294 }, 295 { 296 name: "int(1) != float32(1)", 297 args: args{first: int(1), second: float32(1)}, 298 assertion: False, 299 }, 300 { 301 name: "array != slice", 302 args: args{first: [2]int{1, 2}, second: []int{1, 2}}, 303 assertion: False, 304 }, 305 } 306 for _, tt := range tests { 307 t.Run(tt.name, func(t *testing.T) { 308 tt.assertion(t, samePointers(tt.args.first, tt.args.second)) 309 }) 310 } 311} 312 313// bufferT implements TestingT. Its implementation of Errorf writes the output that would be produced by 314// testing.T.Errorf to an internal bytes.Buffer. 315type bufferT struct { 316 buf bytes.Buffer 317} 318 319func (t *bufferT) Errorf(format string, args ...interface{}) { 320 // implementation of decorate is copied from testing.T 321 decorate := func(s string) string { 322 _, file, line, ok := runtime.Caller(3) // decorate + log + public function. 323 if ok { 324 // Truncate file name at last file name separator. 325 if index := strings.LastIndex(file, "/"); index >= 0 { 326 file = file[index+1:] 327 } else if index = strings.LastIndex(file, "\\"); index >= 0 { 328 file = file[index+1:] 329 } 330 } else { 331 file = "???" 332 line = 1 333 } 334 buf := new(bytes.Buffer) 335 // Every line is indented at least one tab. 336 buf.WriteByte('\t') 337 fmt.Fprintf(buf, "%s:%d: ", file, line) 338 lines := strings.Split(s, "\n") 339 if l := len(lines); l > 1 && lines[l-1] == "" { 340 lines = lines[:l-1] 341 } 342 for i, line := range lines { 343 if i > 0 { 344 // Second and subsequent lines are indented an extra tab. 345 buf.WriteString("\n\t\t") 346 } 347 buf.WriteString(line) 348 } 349 buf.WriteByte('\n') 350 return buf.String() 351 } 352 t.buf.WriteString(decorate(fmt.Sprintf(format, args...))) 353} 354 355func TestStringEqual(t *testing.T) { 356 for i, currCase := range []struct { 357 equalWant string 358 equalGot string 359 msgAndArgs []interface{} 360 want string 361 }{ 362 {equalWant: "hi, \nmy name is", equalGot: "what,\nmy name is", want: "\tassertions.go:\\d+: \n\t+Error Trace:\t\n\t+Error:\\s+Not equal:\\s+\n\\s+expected: \"hi, \\\\nmy name is\"\n\\s+actual\\s+: \"what,\\\\nmy name is\"\n\\s+Diff:\n\\s+-+ Expected\n\\s+\\++ Actual\n\\s+@@ -1,2 \\+1,2 @@\n\\s+-hi, \n\\s+\\+what,\n\\s+my name is"}, 363 } { 364 mockT := &bufferT{} 365 Equal(mockT, currCase.equalWant, currCase.equalGot, currCase.msgAndArgs...) 366 Regexp(t, regexp.MustCompile(currCase.want), mockT.buf.String(), "Case %d", i) 367 } 368} 369 370func TestEqualFormatting(t *testing.T) { 371 for i, currCase := range []struct { 372 equalWant string 373 equalGot string 374 msgAndArgs []interface{} 375 want string 376 }{ 377 {equalWant: "want", equalGot: "got", want: "\tassertions.go:\\d+: \n\t+Error Trace:\t\n\t+Error:\\s+Not equal:\\s+\n\\s+expected: \"want\"\n\\s+actual\\s+: \"got\"\n\\s+Diff:\n\\s+-+ Expected\n\\s+\\++ Actual\n\\s+@@ -1 \\+1 @@\n\\s+-want\n\\s+\\+got\n"}, 378 {equalWant: "want", equalGot: "got", msgAndArgs: []interface{}{"hello, %v!", "world"}, want: "\tassertions.go:[0-9]+: \n\t+Error Trace:\t\n\t+Error:\\s+Not equal:\\s+\n\\s+expected: \"want\"\n\\s+actual\\s+: \"got\"\n\\s+Diff:\n\\s+-+ Expected\n\\s+\\++ Actual\n\\s+@@ -1 \\+1 @@\n\\s+-want\n\\s+\\+got\n\\s+Messages:\\s+hello, world!\n"}, 379 {equalWant: "want", equalGot: "got", msgAndArgs: []interface{}{123}, want: "\tassertions.go:[0-9]+: \n\t+Error Trace:\t\n\t+Error:\\s+Not equal:\\s+\n\\s+expected: \"want\"\n\\s+actual\\s+: \"got\"\n\\s+Diff:\n\\s+-+ Expected\n\\s+\\++ Actual\n\\s+@@ -1 \\+1 @@\n\\s+-want\n\\s+\\+got\n\\s+Messages:\\s+123\n"}, 380 {equalWant: "want", equalGot: "got", msgAndArgs: []interface{}{struct{ a string }{"hello"}}, want: "\tassertions.go:[0-9]+: \n\t+Error Trace:\t\n\t+Error:\\s+Not equal:\\s+\n\\s+expected: \"want\"\n\\s+actual\\s+: \"got\"\n\\s+Diff:\n\\s+-+ Expected\n\\s+\\++ Actual\n\\s+@@ -1 \\+1 @@\n\\s+-want\n\\s+\\+got\n\\s+Messages:\\s+{a:hello}\n"}, 381 } { 382 mockT := &bufferT{} 383 Equal(mockT, currCase.equalWant, currCase.equalGot, currCase.msgAndArgs...) 384 Regexp(t, regexp.MustCompile(currCase.want), mockT.buf.String(), "Case %d", i) 385 } 386} 387 388func TestFormatUnequalValues(t *testing.T) { 389 expected, actual := formatUnequalValues("foo", "bar") 390 Equal(t, `"foo"`, expected, "value should not include type") 391 Equal(t, `"bar"`, actual, "value should not include type") 392 393 expected, actual = formatUnequalValues(123, 123) 394 Equal(t, `123`, expected, "value should not include type") 395 Equal(t, `123`, actual, "value should not include type") 396 397 expected, actual = formatUnequalValues(int64(123), int32(123)) 398 Equal(t, `int64(123)`, expected, "value should include type") 399 Equal(t, `int32(123)`, actual, "value should include type") 400 401 expected, actual = formatUnequalValues(int64(123), nil) 402 Equal(t, `int64(123)`, expected, "value should include type") 403 Equal(t, `<nil>(<nil>)`, actual, "value should include type") 404 405 type testStructType struct { 406 Val string 407 } 408 409 expected, actual = formatUnequalValues(&testStructType{Val: "test"}, &testStructType{Val: "test"}) 410 Equal(t, `&assert.testStructType{Val:"test"}`, expected, "value should not include type annotation") 411 Equal(t, `&assert.testStructType{Val:"test"}`, actual, "value should not include type annotation") 412} 413 414func TestNotNil(t *testing.T) { 415 416 mockT := new(testing.T) 417 418 if !NotNil(mockT, new(AssertionTesterConformingObject)) { 419 t.Error("NotNil should return true: object is not nil") 420 } 421 if NotNil(mockT, nil) { 422 t.Error("NotNil should return false: object is nil") 423 } 424 if NotNil(mockT, (*struct{})(nil)) { 425 t.Error("NotNil should return false: object is (*struct{})(nil)") 426 } 427 428} 429 430func TestNil(t *testing.T) { 431 432 mockT := new(testing.T) 433 434 if !Nil(mockT, nil) { 435 t.Error("Nil should return true: object is nil") 436 } 437 if !Nil(mockT, (*struct{})(nil)) { 438 t.Error("Nil should return true: object is (*struct{})(nil)") 439 } 440 if Nil(mockT, new(AssertionTesterConformingObject)) { 441 t.Error("Nil should return false: object is not nil") 442 } 443 444} 445 446func TestTrue(t *testing.T) { 447 448 mockT := new(testing.T) 449 450 if !True(mockT, true) { 451 t.Error("True should return true") 452 } 453 if True(mockT, false) { 454 t.Error("True should return false") 455 } 456 457} 458 459func TestFalse(t *testing.T) { 460 461 mockT := new(testing.T) 462 463 if !False(mockT, false) { 464 t.Error("False should return true") 465 } 466 if False(mockT, true) { 467 t.Error("False should return false") 468 } 469 470} 471 472func TestExactly(t *testing.T) { 473 474 mockT := new(testing.T) 475 476 a := float32(1) 477 b := float64(1) 478 c := float32(1) 479 d := float32(2) 480 481 if Exactly(mockT, a, b) { 482 t.Error("Exactly should return false") 483 } 484 if Exactly(mockT, a, d) { 485 t.Error("Exactly should return false") 486 } 487 if !Exactly(mockT, a, c) { 488 t.Error("Exactly should return true") 489 } 490 491 if Exactly(mockT, nil, a) { 492 t.Error("Exactly should return false") 493 } 494 if Exactly(mockT, a, nil) { 495 t.Error("Exactly should return false") 496 } 497 498} 499 500func TestNotEqual(t *testing.T) { 501 502 mockT := new(testing.T) 503 504 if !NotEqual(mockT, "Hello World", "Hello World!") { 505 t.Error("NotEqual should return true") 506 } 507 if !NotEqual(mockT, 123, 1234) { 508 t.Error("NotEqual should return true") 509 } 510 if !NotEqual(mockT, 123.5, 123.55) { 511 t.Error("NotEqual should return true") 512 } 513 if !NotEqual(mockT, []byte("Hello World"), []byte("Hello World!")) { 514 t.Error("NotEqual should return true") 515 } 516 if !NotEqual(mockT, nil, new(AssertionTesterConformingObject)) { 517 t.Error("NotEqual should return true") 518 } 519 funcA := func() int { return 23 } 520 funcB := func() int { return 42 } 521 if NotEqual(mockT, funcA, funcB) { 522 t.Error("NotEqual should return false") 523 } 524 if NotEqual(mockT, nil, nil) { 525 t.Error("NotEqual should return false") 526 } 527 528 if NotEqual(mockT, "Hello World", "Hello World") { 529 t.Error("NotEqual should return false") 530 } 531 if NotEqual(mockT, 123, 123) { 532 t.Error("NotEqual should return false") 533 } 534 if NotEqual(mockT, 123.5, 123.5) { 535 t.Error("NotEqual should return false") 536 } 537 if NotEqual(mockT, []byte("Hello World"), []byte("Hello World")) { 538 t.Error("NotEqual should return false") 539 } 540 if NotEqual(mockT, new(AssertionTesterConformingObject), new(AssertionTesterConformingObject)) { 541 t.Error("NotEqual should return false") 542 } 543 if NotEqual(mockT, &struct{}{}, &struct{}{}) { 544 t.Error("NotEqual should return false") 545 } 546} 547 548type A struct { 549 Name, Value string 550} 551 552func TestContains(t *testing.T) { 553 554 mockT := new(testing.T) 555 list := []string{"Foo", "Bar"} 556 complexList := []*A{ 557 {"b", "c"}, 558 {"d", "e"}, 559 {"g", "h"}, 560 {"j", "k"}, 561 } 562 simpleMap := map[interface{}]interface{}{"Foo": "Bar"} 563 564 if !Contains(mockT, "Hello World", "Hello") { 565 t.Error("Contains should return true: \"Hello World\" contains \"Hello\"") 566 } 567 if Contains(mockT, "Hello World", "Salut") { 568 t.Error("Contains should return false: \"Hello World\" does not contain \"Salut\"") 569 } 570 571 if !Contains(mockT, list, "Bar") { 572 t.Error("Contains should return true: \"[\"Foo\", \"Bar\"]\" contains \"Bar\"") 573 } 574 if Contains(mockT, list, "Salut") { 575 t.Error("Contains should return false: \"[\"Foo\", \"Bar\"]\" does not contain \"Salut\"") 576 } 577 if !Contains(mockT, complexList, &A{"g", "h"}) { 578 t.Error("Contains should return true: complexList contains {\"g\", \"h\"}") 579 } 580 if Contains(mockT, complexList, &A{"g", "e"}) { 581 t.Error("Contains should return false: complexList contains {\"g\", \"e\"}") 582 } 583 if Contains(mockT, complexList, &A{"g", "e"}) { 584 t.Error("Contains should return false: complexList contains {\"g\", \"e\"}") 585 } 586 if !Contains(mockT, simpleMap, "Foo") { 587 t.Error("Contains should return true: \"{\"Foo\": \"Bar\"}\" contains \"Foo\"") 588 } 589 if Contains(mockT, simpleMap, "Bar") { 590 t.Error("Contains should return false: \"{\"Foo\": \"Bar\"}\" does not contains \"Bar\"") 591 } 592} 593 594func TestNotContains(t *testing.T) { 595 596 mockT := new(testing.T) 597 list := []string{"Foo", "Bar"} 598 simpleMap := map[interface{}]interface{}{"Foo": "Bar"} 599 600 if !NotContains(mockT, "Hello World", "Hello!") { 601 t.Error("NotContains should return true: \"Hello World\" does not contain \"Hello!\"") 602 } 603 if NotContains(mockT, "Hello World", "Hello") { 604 t.Error("NotContains should return false: \"Hello World\" contains \"Hello\"") 605 } 606 607 if !NotContains(mockT, list, "Foo!") { 608 t.Error("NotContains should return true: \"[\"Foo\", \"Bar\"]\" does not contain \"Foo!\"") 609 } 610 if NotContains(mockT, list, "Foo") { 611 t.Error("NotContains should return false: \"[\"Foo\", \"Bar\"]\" contains \"Foo\"") 612 } 613 if NotContains(mockT, simpleMap, "Foo") { 614 t.Error("Contains should return true: \"{\"Foo\": \"Bar\"}\" contains \"Foo\"") 615 } 616 if !NotContains(mockT, simpleMap, "Bar") { 617 t.Error("Contains should return false: \"{\"Foo\": \"Bar\"}\" does not contains \"Bar\"") 618 } 619} 620 621func TestSubset(t *testing.T) { 622 mockT := new(testing.T) 623 624 if !Subset(mockT, []int{1, 2, 3}, nil) { 625 t.Error("Subset should return true: given subset is nil") 626 } 627 if !Subset(mockT, []int{1, 2, 3}, []int{}) { 628 t.Error("Subset should return true: any set contains the nil set") 629 } 630 if !Subset(mockT, []int{1, 2, 3}, []int{1, 2}) { 631 t.Error("Subset should return true: [1, 2, 3] contains [1, 2]") 632 } 633 if !Subset(mockT, []int{1, 2, 3}, []int{1, 2, 3}) { 634 t.Error("Subset should return true: [1, 2, 3] contains [1, 2, 3]") 635 } 636 if !Subset(mockT, []string{"hello", "world"}, []string{"hello"}) { 637 t.Error("Subset should return true: [\"hello\", \"world\"] contains [\"hello\"]") 638 } 639 640 if Subset(mockT, []string{"hello", "world"}, []string{"hello", "testify"}) { 641 t.Error("Subset should return false: [\"hello\", \"world\"] does not contain [\"hello\", \"testify\"]") 642 } 643 if Subset(mockT, []int{1, 2, 3}, []int{4, 5}) { 644 t.Error("Subset should return false: [1, 2, 3] does not contain [4, 5]") 645 } 646 if Subset(mockT, []int{1, 2, 3}, []int{1, 5}) { 647 t.Error("Subset should return false: [1, 2, 3] does not contain [1, 5]") 648 } 649} 650 651func TestNotSubset(t *testing.T) { 652 mockT := new(testing.T) 653 654 if NotSubset(mockT, []int{1, 2, 3}, nil) { 655 t.Error("NotSubset should return false: given subset is nil") 656 } 657 if NotSubset(mockT, []int{1, 2, 3}, []int{}) { 658 t.Error("NotSubset should return false: any set contains the nil set") 659 } 660 if NotSubset(mockT, []int{1, 2, 3}, []int{1, 2}) { 661 t.Error("NotSubset should return false: [1, 2, 3] contains [1, 2]") 662 } 663 if NotSubset(mockT, []int{1, 2, 3}, []int{1, 2, 3}) { 664 t.Error("NotSubset should return false: [1, 2, 3] contains [1, 2, 3]") 665 } 666 if NotSubset(mockT, []string{"hello", "world"}, []string{"hello"}) { 667 t.Error("NotSubset should return false: [\"hello\", \"world\"] contains [\"hello\"]") 668 } 669 670 if !NotSubset(mockT, []string{"hello", "world"}, []string{"hello", "testify"}) { 671 t.Error("NotSubset should return true: [\"hello\", \"world\"] does not contain [\"hello\", \"testify\"]") 672 } 673 if !NotSubset(mockT, []int{1, 2, 3}, []int{4, 5}) { 674 t.Error("NotSubset should return true: [1, 2, 3] does not contain [4, 5]") 675 } 676 if !NotSubset(mockT, []int{1, 2, 3}, []int{1, 5}) { 677 t.Error("NotSubset should return true: [1, 2, 3] does not contain [1, 5]") 678 } 679} 680 681func TestNotSubsetNil(t *testing.T) { 682 mockT := new(testing.T) 683 NotSubset(mockT, []string{"foo"}, nil) 684 if !mockT.Failed() { 685 t.Error("NotSubset on nil set should have failed the test") 686 } 687} 688 689func Test_includeElement(t *testing.T) { 690 691 list1 := []string{"Foo", "Bar"} 692 list2 := []int{1, 2} 693 simpleMap := map[interface{}]interface{}{"Foo": "Bar"} 694 695 ok, found := includeElement("Hello World", "World") 696 True(t, ok) 697 True(t, found) 698 699 ok, found = includeElement(list1, "Foo") 700 True(t, ok) 701 True(t, found) 702 703 ok, found = includeElement(list1, "Bar") 704 True(t, ok) 705 True(t, found) 706 707 ok, found = includeElement(list2, 1) 708 True(t, ok) 709 True(t, found) 710 711 ok, found = includeElement(list2, 2) 712 True(t, ok) 713 True(t, found) 714 715 ok, found = includeElement(list1, "Foo!") 716 True(t, ok) 717 False(t, found) 718 719 ok, found = includeElement(list2, 3) 720 True(t, ok) 721 False(t, found) 722 723 ok, found = includeElement(list2, "1") 724 True(t, ok) 725 False(t, found) 726 727 ok, found = includeElement(simpleMap, "Foo") 728 True(t, ok) 729 True(t, found) 730 731 ok, found = includeElement(simpleMap, "Bar") 732 True(t, ok) 733 False(t, found) 734 735 ok, found = includeElement(1433, "1") 736 False(t, ok) 737 False(t, found) 738} 739 740func TestElementsMatch(t *testing.T) { 741 mockT := new(testing.T) 742 743 if !ElementsMatch(mockT, nil, nil) { 744 t.Error("ElementsMatch should return true") 745 } 746 if !ElementsMatch(mockT, []int{}, []int{}) { 747 t.Error("ElementsMatch should return true") 748 } 749 if !ElementsMatch(mockT, []int{1}, []int{1}) { 750 t.Error("ElementsMatch should return true") 751 } 752 if !ElementsMatch(mockT, []int{1, 1}, []int{1, 1}) { 753 t.Error("ElementsMatch should return true") 754 } 755 if !ElementsMatch(mockT, []int{1, 2}, []int{1, 2}) { 756 t.Error("ElementsMatch should return true") 757 } 758 if !ElementsMatch(mockT, []int{1, 2}, []int{2, 1}) { 759 t.Error("ElementsMatch should return true") 760 } 761 if !ElementsMatch(mockT, [2]int{1, 2}, [2]int{2, 1}) { 762 t.Error("ElementsMatch should return true") 763 } 764 if !ElementsMatch(mockT, []string{"hello", "world"}, []string{"world", "hello"}) { 765 t.Error("ElementsMatch should return true") 766 } 767 if !ElementsMatch(mockT, []string{"hello", "hello"}, []string{"hello", "hello"}) { 768 t.Error("ElementsMatch should return true") 769 } 770 if !ElementsMatch(mockT, []string{"hello", "hello", "world"}, []string{"hello", "world", "hello"}) { 771 t.Error("ElementsMatch should return true") 772 } 773 if !ElementsMatch(mockT, [3]string{"hello", "hello", "world"}, [3]string{"hello", "world", "hello"}) { 774 t.Error("ElementsMatch should return true") 775 } 776 if !ElementsMatch(mockT, []int{}, nil) { 777 t.Error("ElementsMatch should return true") 778 } 779 780 if ElementsMatch(mockT, []int{1}, []int{1, 1}) { 781 t.Error("ElementsMatch should return false") 782 } 783 if ElementsMatch(mockT, []int{1, 2}, []int{2, 2}) { 784 t.Error("ElementsMatch should return false") 785 } 786 if ElementsMatch(mockT, []string{"hello", "hello"}, []string{"hello"}) { 787 t.Error("ElementsMatch should return false") 788 } 789} 790 791func TestCondition(t *testing.T) { 792 mockT := new(testing.T) 793 794 if !Condition(mockT, func() bool { return true }, "Truth") { 795 t.Error("Condition should return true") 796 } 797 798 if Condition(mockT, func() bool { return false }, "Lie") { 799 t.Error("Condition should return false") 800 } 801 802} 803 804func TestDidPanic(t *testing.T) { 805 806 if funcDidPanic, _, _ := didPanic(func() { 807 panic("Panic!") 808 }); !funcDidPanic { 809 t.Error("didPanic should return true") 810 } 811 812 if funcDidPanic, _, _ := didPanic(func() { 813 }); funcDidPanic { 814 t.Error("didPanic should return false") 815 } 816 817} 818 819func TestPanics(t *testing.T) { 820 821 mockT := new(testing.T) 822 823 if !Panics(mockT, func() { 824 panic("Panic!") 825 }) { 826 t.Error("Panics should return true") 827 } 828 829 if Panics(mockT, func() { 830 }) { 831 t.Error("Panics should return false") 832 } 833 834} 835 836func TestPanicsWithValue(t *testing.T) { 837 838 mockT := new(testing.T) 839 840 if !PanicsWithValue(mockT, "Panic!", func() { 841 panic("Panic!") 842 }) { 843 t.Error("PanicsWithValue should return true") 844 } 845 846 if PanicsWithValue(mockT, "Panic!", func() { 847 }) { 848 t.Error("PanicsWithValue should return false") 849 } 850 851 if PanicsWithValue(mockT, "at the disco", func() { 852 panic("Panic!") 853 }) { 854 t.Error("PanicsWithValue should return false") 855 } 856} 857 858func TestPanicsWithError(t *testing.T) { 859 860 mockT := new(testing.T) 861 862 if !PanicsWithError(mockT, "panic", func() { 863 panic(errors.New("panic")) 864 }) { 865 t.Error("PanicsWithError should return true") 866 } 867 868 if PanicsWithError(mockT, "Panic!", func() { 869 }) { 870 t.Error("PanicsWithError should return false") 871 } 872 873 if PanicsWithError(mockT, "at the disco", func() { 874 panic(errors.New("panic")) 875 }) { 876 t.Error("PanicsWithError should return false") 877 } 878 879 if PanicsWithError(mockT, "Panic!", func() { 880 panic("panic") 881 }) { 882 t.Error("PanicsWithError should return false") 883 } 884} 885 886func TestNotPanics(t *testing.T) { 887 888 mockT := new(testing.T) 889 890 if !NotPanics(mockT, func() { 891 }) { 892 t.Error("NotPanics should return true") 893 } 894 895 if NotPanics(mockT, func() { 896 panic("Panic!") 897 }) { 898 t.Error("NotPanics should return false") 899 } 900 901} 902 903func TestNoError(t *testing.T) { 904 905 mockT := new(testing.T) 906 907 // start with a nil error 908 var err error 909 910 True(t, NoError(mockT, err), "NoError should return True for nil arg") 911 912 // now set an error 913 err = errors.New("some error") 914 915 False(t, NoError(mockT, err), "NoError with error should return False") 916 917 // returning an empty error interface 918 err = func() error { 919 var err *customError 920 if err != nil { 921 t.Fatal("err should be nil here") 922 } 923 return err 924 }() 925 926 if err == nil { // err is not nil here! 927 t.Errorf("Error should be nil due to empty interface: %s", err) 928 } 929 930 False(t, NoError(mockT, err), "NoError should fail with empty error interface") 931} 932 933type customError struct{} 934 935func (*customError) Error() string { return "fail" } 936 937func TestError(t *testing.T) { 938 939 mockT := new(testing.T) 940 941 // start with a nil error 942 var err error 943 944 False(t, Error(mockT, err), "Error should return False for nil arg") 945 946 // now set an error 947 err = errors.New("some error") 948 949 True(t, Error(mockT, err), "Error with error should return True") 950 951 // go vet check 952 True(t, Errorf(mockT, err, "example with %s", "formatted message"), "Errorf with error should rturn True") 953 954 // returning an empty error interface 955 err = func() error { 956 var err *customError 957 if err != nil { 958 t.Fatal("err should be nil here") 959 } 960 return err 961 }() 962 963 if err == nil { // err is not nil here! 964 t.Errorf("Error should be nil due to empty interface: %s", err) 965 } 966 967 True(t, Error(mockT, err), "Error should pass with empty error interface") 968} 969 970func TestEqualError(t *testing.T) { 971 mockT := new(testing.T) 972 973 // start with a nil error 974 var err error 975 False(t, EqualError(mockT, err, ""), 976 "EqualError should return false for nil arg") 977 978 // now set an error 979 err = errors.New("some error") 980 False(t, EqualError(mockT, err, "Not some error"), 981 "EqualError should return false for different error string") 982 True(t, EqualError(mockT, err, "some error"), 983 "EqualError should return true") 984} 985 986func Test_isEmpty(t *testing.T) { 987 988 chWithValue := make(chan struct{}, 1) 989 chWithValue <- struct{}{} 990 991 True(t, isEmpty("")) 992 True(t, isEmpty(nil)) 993 True(t, isEmpty([]string{})) 994 True(t, isEmpty(0)) 995 True(t, isEmpty(int32(0))) 996 True(t, isEmpty(int64(0))) 997 True(t, isEmpty(false)) 998 True(t, isEmpty(map[string]string{})) 999 True(t, isEmpty(new(time.Time))) 1000 True(t, isEmpty(time.Time{})) 1001 True(t, isEmpty(make(chan struct{}))) 1002 False(t, isEmpty("something")) 1003 False(t, isEmpty(errors.New("something"))) 1004 False(t, isEmpty([]string{"something"})) 1005 False(t, isEmpty(1)) 1006 False(t, isEmpty(true)) 1007 False(t, isEmpty(map[string]string{"Hello": "World"})) 1008 False(t, isEmpty(chWithValue)) 1009 1010} 1011 1012func TestEmpty(t *testing.T) { 1013 1014 mockT := new(testing.T) 1015 chWithValue := make(chan struct{}, 1) 1016 chWithValue <- struct{}{} 1017 var tiP *time.Time 1018 var tiNP time.Time 1019 var s *string 1020 var f *os.File 1021 sP := &s 1022 x := 1 1023 xP := &x 1024 1025 type TString string 1026 type TStruct struct { 1027 x int 1028 s []int 1029 } 1030 1031 True(t, Empty(mockT, ""), "Empty string is empty") 1032 True(t, Empty(mockT, nil), "Nil is empty") 1033 True(t, Empty(mockT, []string{}), "Empty string array is empty") 1034 True(t, Empty(mockT, 0), "Zero int value is empty") 1035 True(t, Empty(mockT, false), "False value is empty") 1036 True(t, Empty(mockT, make(chan struct{})), "Channel without values is empty") 1037 True(t, Empty(mockT, s), "Nil string pointer is empty") 1038 True(t, Empty(mockT, f), "Nil os.File pointer is empty") 1039 True(t, Empty(mockT, tiP), "Nil time.Time pointer is empty") 1040 True(t, Empty(mockT, tiNP), "time.Time is empty") 1041 True(t, Empty(mockT, TStruct{}), "struct with zero values is empty") 1042 True(t, Empty(mockT, TString("")), "empty aliased string is empty") 1043 True(t, Empty(mockT, sP), "ptr to nil value is empty") 1044 1045 False(t, Empty(mockT, "something"), "Non Empty string is not empty") 1046 False(t, Empty(mockT, errors.New("something")), "Non nil object is not empty") 1047 False(t, Empty(mockT, []string{"something"}), "Non empty string array is not empty") 1048 False(t, Empty(mockT, 1), "Non-zero int value is not empty") 1049 False(t, Empty(mockT, true), "True value is not empty") 1050 False(t, Empty(mockT, chWithValue), "Channel with values is not empty") 1051 False(t, Empty(mockT, TStruct{x: 1}), "struct with initialized values is empty") 1052 False(t, Empty(mockT, TString("abc")), "non-empty aliased string is empty") 1053 False(t, Empty(mockT, xP), "ptr to non-nil value is not empty") 1054} 1055 1056func TestNotEmpty(t *testing.T) { 1057 1058 mockT := new(testing.T) 1059 chWithValue := make(chan struct{}, 1) 1060 chWithValue <- struct{}{} 1061 1062 False(t, NotEmpty(mockT, ""), "Empty string is empty") 1063 False(t, NotEmpty(mockT, nil), "Nil is empty") 1064 False(t, NotEmpty(mockT, []string{}), "Empty string array is empty") 1065 False(t, NotEmpty(mockT, 0), "Zero int value is empty") 1066 False(t, NotEmpty(mockT, false), "False value is empty") 1067 False(t, NotEmpty(mockT, make(chan struct{})), "Channel without values is empty") 1068 1069 True(t, NotEmpty(mockT, "something"), "Non Empty string is not empty") 1070 True(t, NotEmpty(mockT, errors.New("something")), "Non nil object is not empty") 1071 True(t, NotEmpty(mockT, []string{"something"}), "Non empty string array is not empty") 1072 True(t, NotEmpty(mockT, 1), "Non-zero int value is not empty") 1073 True(t, NotEmpty(mockT, true), "True value is not empty") 1074 True(t, NotEmpty(mockT, chWithValue), "Channel with values is not empty") 1075} 1076 1077func Test_getLen(t *testing.T) { 1078 falseCases := []interface{}{ 1079 nil, 1080 0, 1081 true, 1082 false, 1083 'A', 1084 struct{}{}, 1085 } 1086 for _, v := range falseCases { 1087 ok, l := getLen(v) 1088 False(t, ok, "Expected getLen fail to get length of %#v", v) 1089 Equal(t, 0, l, "getLen should return 0 for %#v", v) 1090 } 1091 1092 ch := make(chan int, 5) 1093 ch <- 1 1094 ch <- 2 1095 ch <- 3 1096 trueCases := []struct { 1097 v interface{} 1098 l int 1099 }{ 1100 {[]int{1, 2, 3}, 3}, 1101 {[...]int{1, 2, 3}, 3}, 1102 {"ABC", 3}, 1103 {map[int]int{1: 2, 2: 4, 3: 6}, 3}, 1104 {ch, 3}, 1105 1106 {[]int{}, 0}, 1107 {map[int]int{}, 0}, 1108 {make(chan int), 0}, 1109 1110 {[]int(nil), 0}, 1111 {map[int]int(nil), 0}, 1112 {(chan int)(nil), 0}, 1113 } 1114 1115 for _, c := range trueCases { 1116 ok, l := getLen(c.v) 1117 True(t, ok, "Expected getLen success to get length of %#v", c.v) 1118 Equal(t, c.l, l) 1119 } 1120} 1121 1122func TestLen(t *testing.T) { 1123 mockT := new(testing.T) 1124 1125 False(t, Len(mockT, nil, 0), "nil does not have length") 1126 False(t, Len(mockT, 0, 0), "int does not have length") 1127 False(t, Len(mockT, true, 0), "true does not have length") 1128 False(t, Len(mockT, false, 0), "false does not have length") 1129 False(t, Len(mockT, 'A', 0), "Rune does not have length") 1130 False(t, Len(mockT, struct{}{}, 0), "Struct does not have length") 1131 1132 ch := make(chan int, 5) 1133 ch <- 1 1134 ch <- 2 1135 ch <- 3 1136 1137 cases := []struct { 1138 v interface{} 1139 l int 1140 }{ 1141 {[]int{1, 2, 3}, 3}, 1142 {[...]int{1, 2, 3}, 3}, 1143 {"ABC", 3}, 1144 {map[int]int{1: 2, 2: 4, 3: 6}, 3}, 1145 {ch, 3}, 1146 1147 {[]int{}, 0}, 1148 {map[int]int{}, 0}, 1149 {make(chan int), 0}, 1150 1151 {[]int(nil), 0}, 1152 {map[int]int(nil), 0}, 1153 {(chan int)(nil), 0}, 1154 } 1155 1156 for _, c := range cases { 1157 True(t, Len(mockT, c.v, c.l), "%#v have %d items", c.v, c.l) 1158 } 1159 1160 cases = []struct { 1161 v interface{} 1162 l int 1163 }{ 1164 {[]int{1, 2, 3}, 4}, 1165 {[...]int{1, 2, 3}, 2}, 1166 {"ABC", 2}, 1167 {map[int]int{1: 2, 2: 4, 3: 6}, 4}, 1168 {ch, 2}, 1169 1170 {[]int{}, 1}, 1171 {map[int]int{}, 1}, 1172 {make(chan int), 1}, 1173 1174 {[]int(nil), 1}, 1175 {map[int]int(nil), 1}, 1176 {(chan int)(nil), 1}, 1177 } 1178 1179 for _, c := range cases { 1180 False(t, Len(mockT, c.v, c.l), "%#v have %d items", c.v, c.l) 1181 } 1182} 1183 1184func TestWithinDuration(t *testing.T) { 1185 1186 mockT := new(testing.T) 1187 a := time.Now() 1188 b := a.Add(10 * time.Second) 1189 1190 True(t, WithinDuration(mockT, a, b, 10*time.Second), "A 10s difference is within a 10s time difference") 1191 True(t, WithinDuration(mockT, b, a, 10*time.Second), "A 10s difference is within a 10s time difference") 1192 1193 False(t, WithinDuration(mockT, a, b, 9*time.Second), "A 10s difference is not within a 9s time difference") 1194 False(t, WithinDuration(mockT, b, a, 9*time.Second), "A 10s difference is not within a 9s time difference") 1195 1196 False(t, WithinDuration(mockT, a, b, -9*time.Second), "A 10s difference is not within a 9s time difference") 1197 False(t, WithinDuration(mockT, b, a, -9*time.Second), "A 10s difference is not within a 9s time difference") 1198 1199 False(t, WithinDuration(mockT, a, b, -11*time.Second), "A 10s difference is not within a 9s time difference") 1200 False(t, WithinDuration(mockT, b, a, -11*time.Second), "A 10s difference is not within a 9s time difference") 1201} 1202 1203func TestInDelta(t *testing.T) { 1204 mockT := new(testing.T) 1205 1206 True(t, InDelta(mockT, 1.001, 1, 0.01), "|1.001 - 1| <= 0.01") 1207 True(t, InDelta(mockT, 1, 1.001, 0.01), "|1 - 1.001| <= 0.01") 1208 True(t, InDelta(mockT, 1, 2, 1), "|1 - 2| <= 1") 1209 False(t, InDelta(mockT, 1, 2, 0.5), "Expected |1 - 2| <= 0.5 to fail") 1210 False(t, InDelta(mockT, 2, 1, 0.5), "Expected |2 - 1| <= 0.5 to fail") 1211 False(t, InDelta(mockT, "", nil, 1), "Expected non numerals to fail") 1212 False(t, InDelta(mockT, 42, math.NaN(), 0.01), "Expected NaN for actual to fail") 1213 False(t, InDelta(mockT, math.NaN(), 42, 0.01), "Expected NaN for expected to fail") 1214 1215 cases := []struct { 1216 a, b interface{} 1217 delta float64 1218 }{ 1219 {uint8(2), uint8(1), 1}, 1220 {uint16(2), uint16(1), 1}, 1221 {uint32(2), uint32(1), 1}, 1222 {uint64(2), uint64(1), 1}, 1223 1224 {int(2), int(1), 1}, 1225 {int8(2), int8(1), 1}, 1226 {int16(2), int16(1), 1}, 1227 {int32(2), int32(1), 1}, 1228 {int64(2), int64(1), 1}, 1229 1230 {float32(2), float32(1), 1}, 1231 {float64(2), float64(1), 1}, 1232 } 1233 1234 for _, tc := range cases { 1235 True(t, InDelta(mockT, tc.a, tc.b, tc.delta), "Expected |%V - %V| <= %v", tc.a, tc.b, tc.delta) 1236 } 1237} 1238 1239func TestInDeltaSlice(t *testing.T) { 1240 mockT := new(testing.T) 1241 1242 True(t, InDeltaSlice(mockT, 1243 []float64{1.001, 0.999}, 1244 []float64{1, 1}, 1245 0.1), "{1.001, 0.009} is element-wise close to {1, 1} in delta=0.1") 1246 1247 True(t, InDeltaSlice(mockT, 1248 []float64{1, 2}, 1249 []float64{0, 3}, 1250 1), "{1, 2} is element-wise close to {0, 3} in delta=1") 1251 1252 False(t, InDeltaSlice(mockT, 1253 []float64{1, 2}, 1254 []float64{0, 3}, 1255 0.1), "{1, 2} is not element-wise close to {0, 3} in delta=0.1") 1256 1257 False(t, InDeltaSlice(mockT, "", nil, 1), "Expected non numeral slices to fail") 1258} 1259 1260func TestInDeltaMapValues(t *testing.T) { 1261 mockT := new(testing.T) 1262 1263 for _, tc := range []struct { 1264 title string 1265 expect interface{} 1266 actual interface{} 1267 f func(TestingT, bool, ...interface{}) bool 1268 delta float64 1269 }{ 1270 { 1271 title: "Within delta", 1272 expect: map[string]float64{ 1273 "foo": 1.0, 1274 "bar": 2.0, 1275 }, 1276 actual: map[string]float64{ 1277 "foo": 1.01, 1278 "bar": 1.99, 1279 }, 1280 delta: 0.1, 1281 f: True, 1282 }, 1283 { 1284 title: "Within delta", 1285 expect: map[int]float64{ 1286 1: 1.0, 1287 2: 2.0, 1288 }, 1289 actual: map[int]float64{ 1290 1: 1.0, 1291 2: 1.99, 1292 }, 1293 delta: 0.1, 1294 f: True, 1295 }, 1296 { 1297 title: "Different number of keys", 1298 expect: map[int]float64{ 1299 1: 1.0, 1300 2: 2.0, 1301 }, 1302 actual: map[int]float64{ 1303 1: 1.0, 1304 }, 1305 delta: 0.1, 1306 f: False, 1307 }, 1308 { 1309 title: "Within delta with zero value", 1310 expect: map[string]float64{ 1311 "zero": 0.0, 1312 }, 1313 actual: map[string]float64{ 1314 "zero": 0.0, 1315 }, 1316 delta: 0.1, 1317 f: True, 1318 }, 1319 { 1320 title: "With missing key with zero value", 1321 expect: map[string]float64{ 1322 "zero": 0.0, 1323 "foo": 0.0, 1324 }, 1325 actual: map[string]float64{ 1326 "zero": 0.0, 1327 "bar": 0.0, 1328 }, 1329 f: False, 1330 }, 1331 } { 1332 tc.f(t, InDeltaMapValues(mockT, tc.expect, tc.actual, tc.delta), tc.title+"\n"+diff(tc.expect, tc.actual)) 1333 } 1334} 1335 1336func TestInEpsilon(t *testing.T) { 1337 mockT := new(testing.T) 1338 1339 cases := []struct { 1340 a, b interface{} 1341 epsilon float64 1342 }{ 1343 {uint8(2), uint16(2), .001}, 1344 {2.1, 2.2, 0.1}, 1345 {2.2, 2.1, 0.1}, 1346 {-2.1, -2.2, 0.1}, 1347 {-2.2, -2.1, 0.1}, 1348 {uint64(100), uint8(101), 0.01}, 1349 {0.1, -0.1, 2}, 1350 {0.1, 0, 2}, 1351 {time.Second, time.Second + time.Millisecond, 0.002}, 1352 } 1353 1354 for _, tc := range cases { 1355 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) 1356 } 1357 1358 cases = []struct { 1359 a, b interface{} 1360 epsilon float64 1361 }{ 1362 {uint8(2), int16(-2), .001}, 1363 {uint64(100), uint8(102), 0.01}, 1364 {2.1, 2.2, 0.001}, 1365 {2.2, 2.1, 0.001}, 1366 {2.1, -2.2, 1}, 1367 {2.1, "bla-bla", 0}, 1368 {0.1, -0.1, 1.99}, 1369 {0, 0.1, 2}, // expected must be different to zero 1370 {time.Second, time.Second + 10*time.Millisecond, 0.002}, 1371 } 1372 1373 for _, tc := range cases { 1374 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)) 1375 } 1376 1377} 1378 1379func TestInEpsilonSlice(t *testing.T) { 1380 mockT := new(testing.T) 1381 1382 True(t, InEpsilonSlice(mockT, 1383 []float64{2.2, 2.0}, 1384 []float64{2.1, 2.1}, 1385 0.06), "{2.2, 2.0} is element-wise close to {2.1, 2.1} in espilon=0.06") 1386 1387 False(t, InEpsilonSlice(mockT, 1388 []float64{2.2, 2.0}, 1389 []float64{2.1, 2.1}, 1390 0.04), "{2.2, 2.0} is not element-wise close to {2.1, 2.1} in espilon=0.04") 1391 1392 False(t, InEpsilonSlice(mockT, "", nil, 1), "Expected non numeral slices to fail") 1393} 1394 1395func TestRegexp(t *testing.T) { 1396 mockT := new(testing.T) 1397 1398 cases := []struct { 1399 rx, str string 1400 }{ 1401 {"^start", "start of the line"}, 1402 {"end$", "in the end"}, 1403 {"[0-9]{3}[.-]?[0-9]{2}[.-]?[0-9]{2}", "My phone number is 650.12.34"}, 1404 } 1405 1406 for _, tc := range cases { 1407 True(t, Regexp(mockT, tc.rx, tc.str)) 1408 True(t, Regexp(mockT, regexp.MustCompile(tc.rx), tc.str)) 1409 False(t, NotRegexp(mockT, tc.rx, tc.str)) 1410 False(t, NotRegexp(mockT, regexp.MustCompile(tc.rx), tc.str)) 1411 } 1412 1413 cases = []struct { 1414 rx, str string 1415 }{ 1416 {"^asdfastart", "Not the start of the line"}, 1417 {"end$", "in the end."}, 1418 {"[0-9]{3}[.-]?[0-9]{2}[.-]?[0-9]{2}", "My phone number is 650.12a.34"}, 1419 } 1420 1421 for _, tc := range cases { 1422 False(t, Regexp(mockT, tc.rx, tc.str), "Expected \"%s\" to not match \"%s\"", tc.rx, tc.str) 1423 False(t, Regexp(mockT, regexp.MustCompile(tc.rx), tc.str)) 1424 True(t, NotRegexp(mockT, tc.rx, tc.str)) 1425 True(t, NotRegexp(mockT, regexp.MustCompile(tc.rx), tc.str)) 1426 } 1427} 1428 1429func testAutogeneratedFunction() { 1430 defer func() { 1431 if err := recover(); err == nil { 1432 panic("did not panic") 1433 } 1434 CallerInfo() 1435 }() 1436 t := struct { 1437 io.Closer 1438 }{} 1439 var c io.Closer 1440 c = t 1441 c.Close() 1442} 1443 1444func TestCallerInfoWithAutogeneratedFunctions(t *testing.T) { 1445 NotPanics(t, func() { 1446 testAutogeneratedFunction() 1447 }) 1448} 1449 1450func TestZero(t *testing.T) { 1451 mockT := new(testing.T) 1452 1453 for _, test := range zeros { 1454 True(t, Zero(mockT, test, "%#v is not the %v zero value", test, reflect.TypeOf(test))) 1455 } 1456 1457 for _, test := range nonZeros { 1458 False(t, Zero(mockT, test, "%#v is not the %v zero value", test, reflect.TypeOf(test))) 1459 } 1460} 1461 1462func TestNotZero(t *testing.T) { 1463 mockT := new(testing.T) 1464 1465 for _, test := range zeros { 1466 False(t, NotZero(mockT, test, "%#v is not the %v zero value", test, reflect.TypeOf(test))) 1467 } 1468 1469 for _, test := range nonZeros { 1470 True(t, NotZero(mockT, test, "%#v is not the %v zero value", test, reflect.TypeOf(test))) 1471 } 1472} 1473 1474func TestFileExists(t *testing.T) { 1475 mockT := new(testing.T) 1476 True(t, FileExists(mockT, "assertions.go")) 1477 1478 mockT = new(testing.T) 1479 False(t, FileExists(mockT, "random_file")) 1480 1481 mockT = new(testing.T) 1482 False(t, FileExists(mockT, "../_codegen")) 1483 1484 var tempFiles []string 1485 1486 link, err := getTempSymlinkPath("assertions.go") 1487 if err != nil { 1488 t.Fatal("could not create temp symlink, err:", err) 1489 } 1490 tempFiles = append(tempFiles, link) 1491 mockT = new(testing.T) 1492 True(t, FileExists(mockT, link)) 1493 1494 link, err = getTempSymlinkPath("non_existent_file") 1495 if err != nil { 1496 t.Fatal("could not create temp symlink, err:", err) 1497 } 1498 tempFiles = append(tempFiles, link) 1499 mockT = new(testing.T) 1500 True(t, FileExists(mockT, link)) 1501 1502 errs := cleanUpTempFiles(tempFiles) 1503 if len(errs) > 0 { 1504 t.Fatal("could not clean up temporary files") 1505 } 1506} 1507 1508func TestNoFileExists(t *testing.T) { 1509 mockT := new(testing.T) 1510 False(t, NoFileExists(mockT, "assertions.go")) 1511 1512 mockT = new(testing.T) 1513 True(t, NoFileExists(mockT, "non_existent_file")) 1514 1515 mockT = new(testing.T) 1516 True(t, NoFileExists(mockT, "../_codegen")) 1517 1518 var tempFiles []string 1519 1520 link, err := getTempSymlinkPath("assertions.go") 1521 if err != nil { 1522 t.Fatal("could not create temp symlink, err:", err) 1523 } 1524 tempFiles = append(tempFiles, link) 1525 mockT = new(testing.T) 1526 False(t, NoFileExists(mockT, link)) 1527 1528 link, err = getTempSymlinkPath("non_existent_file") 1529 if err != nil { 1530 t.Fatal("could not create temp symlink, err:", err) 1531 } 1532 tempFiles = append(tempFiles, link) 1533 mockT = new(testing.T) 1534 False(t, NoFileExists(mockT, link)) 1535 1536 errs := cleanUpTempFiles(tempFiles) 1537 if len(errs) > 0 { 1538 t.Fatal("could not clean up temporary files") 1539 } 1540} 1541 1542func getTempSymlinkPath(file string) (string, error) { 1543 link := file + "_symlink" 1544 err := os.Symlink(file, link) 1545 return link, err 1546} 1547 1548func cleanUpTempFiles(paths []string) []error { 1549 var res []error 1550 for _, path := range paths { 1551 err := os.Remove(path) 1552 if err != nil { 1553 res = append(res, err) 1554 } 1555 } 1556 return res 1557} 1558 1559func TestDirExists(t *testing.T) { 1560 mockT := new(testing.T) 1561 False(t, DirExists(mockT, "assertions.go")) 1562 1563 mockT = new(testing.T) 1564 False(t, DirExists(mockT, "non_existent_dir")) 1565 1566 mockT = new(testing.T) 1567 True(t, DirExists(mockT, "../_codegen")) 1568 1569 var tempFiles []string 1570 1571 link, err := getTempSymlinkPath("assertions.go") 1572 if err != nil { 1573 t.Fatal("could not create temp symlink, err:", err) 1574 } 1575 tempFiles = append(tempFiles, link) 1576 mockT = new(testing.T) 1577 False(t, DirExists(mockT, link)) 1578 1579 link, err = getTempSymlinkPath("non_existent_dir") 1580 if err != nil { 1581 t.Fatal("could not create temp symlink, err:", err) 1582 } 1583 tempFiles = append(tempFiles, link) 1584 mockT = new(testing.T) 1585 False(t, DirExists(mockT, link)) 1586 1587 errs := cleanUpTempFiles(tempFiles) 1588 if len(errs) > 0 { 1589 t.Fatal("could not clean up temporary files") 1590 } 1591} 1592 1593func TestNoDirExists(t *testing.T) { 1594 mockT := new(testing.T) 1595 True(t, NoDirExists(mockT, "assertions.go")) 1596 1597 mockT = new(testing.T) 1598 True(t, NoDirExists(mockT, "non_existent_dir")) 1599 1600 mockT = new(testing.T) 1601 False(t, NoDirExists(mockT, "../_codegen")) 1602 1603 var tempFiles []string 1604 1605 link, err := getTempSymlinkPath("assertions.go") 1606 if err != nil { 1607 t.Fatal("could not create temp symlink, err:", err) 1608 } 1609 tempFiles = append(tempFiles, link) 1610 mockT = new(testing.T) 1611 True(t, NoDirExists(mockT, link)) 1612 1613 link, err = getTempSymlinkPath("non_existent_dir") 1614 if err != nil { 1615 t.Fatal("could not create temp symlink, err:", err) 1616 } 1617 tempFiles = append(tempFiles, link) 1618 mockT = new(testing.T) 1619 True(t, NoDirExists(mockT, link)) 1620 1621 errs := cleanUpTempFiles(tempFiles) 1622 if len(errs) > 0 { 1623 t.Fatal("could not clean up temporary files") 1624 } 1625} 1626 1627func TestJSONEq_EqualSONString(t *testing.T) { 1628 mockT := new(testing.T) 1629 True(t, JSONEq(mockT, `{"hello": "world", "foo": "bar"}`, `{"hello": "world", "foo": "bar"}`)) 1630} 1631 1632func TestJSONEq_EquivalentButNotEqual(t *testing.T) { 1633 mockT := new(testing.T) 1634 True(t, JSONEq(mockT, `{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`)) 1635} 1636 1637func TestJSONEq_HashOfArraysAndHashes(t *testing.T) { 1638 mockT := new(testing.T) 1639 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}", 1640 "{\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}")) 1641} 1642 1643func TestJSONEq_Array(t *testing.T) { 1644 mockT := new(testing.T) 1645 True(t, JSONEq(mockT, `["foo", {"hello": "world", "nested": "hash"}]`, `["foo", {"nested": "hash", "hello": "world"}]`)) 1646} 1647 1648func TestJSONEq_HashAndArrayNotEquivalent(t *testing.T) { 1649 mockT := new(testing.T) 1650 False(t, JSONEq(mockT, `["foo", {"hello": "world", "nested": "hash"}]`, `{"foo": "bar", {"nested": "hash", "hello": "world"}}`)) 1651} 1652 1653func TestJSONEq_HashesNotEquivalent(t *testing.T) { 1654 mockT := new(testing.T) 1655 False(t, JSONEq(mockT, `{"foo": "bar"}`, `{"foo": "bar", "hello": "world"}`)) 1656} 1657 1658func TestJSONEq_ActualIsNotJSON(t *testing.T) { 1659 mockT := new(testing.T) 1660 False(t, JSONEq(mockT, `{"foo": "bar"}`, "Not JSON")) 1661} 1662 1663func TestJSONEq_ExpectedIsNotJSON(t *testing.T) { 1664 mockT := new(testing.T) 1665 False(t, JSONEq(mockT, "Not JSON", `{"foo": "bar", "hello": "world"}`)) 1666} 1667 1668func TestJSONEq_ExpectedAndActualNotJSON(t *testing.T) { 1669 mockT := new(testing.T) 1670 False(t, JSONEq(mockT, "Not JSON", "Not JSON")) 1671} 1672 1673func TestJSONEq_ArraysOfDifferentOrder(t *testing.T) { 1674 mockT := new(testing.T) 1675 False(t, JSONEq(mockT, `["foo", {"hello": "world", "nested": "hash"}]`, `[{ "hello": "world", "nested": "hash"}, "foo"]`)) 1676} 1677 1678func TestYAMLEq_EqualYAMLString(t *testing.T) { 1679 mockT := new(testing.T) 1680 True(t, YAMLEq(mockT, `{"hello": "world", "foo": "bar"}`, `{"hello": "world", "foo": "bar"}`)) 1681} 1682 1683func TestYAMLEq_EquivalentButNotEqual(t *testing.T) { 1684 mockT := new(testing.T) 1685 True(t, YAMLEq(mockT, `{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`)) 1686} 1687 1688func TestYAMLEq_HashOfArraysAndHashes(t *testing.T) { 1689 mockT := new(testing.T) 1690 expected := ` 1691numeric: 1.5 1692array: 1693 - foo: bar 1694 - 1 1695 - "string" 1696 - ["nested", "array", 5.5] 1697hash: 1698 nested: hash 1699 nested_slice: [this, is, nested] 1700string: "foo" 1701` 1702 1703 actual := ` 1704numeric: 1.5 1705hash: 1706 nested: hash 1707 nested_slice: [this, is, nested] 1708string: "foo" 1709array: 1710 - foo: bar 1711 - 1 1712 - "string" 1713 - ["nested", "array", 5.5] 1714` 1715 True(t, YAMLEq(mockT, expected, actual)) 1716} 1717 1718func TestYAMLEq_Array(t *testing.T) { 1719 mockT := new(testing.T) 1720 True(t, YAMLEq(mockT, `["foo", {"hello": "world", "nested": "hash"}]`, `["foo", {"nested": "hash", "hello": "world"}]`)) 1721} 1722 1723func TestYAMLEq_HashAndArrayNotEquivalent(t *testing.T) { 1724 mockT := new(testing.T) 1725 False(t, YAMLEq(mockT, `["foo", {"hello": "world", "nested": "hash"}]`, `{"foo": "bar", {"nested": "hash", "hello": "world"}}`)) 1726} 1727 1728func TestYAMLEq_HashesNotEquivalent(t *testing.T) { 1729 mockT := new(testing.T) 1730 False(t, YAMLEq(mockT, `{"foo": "bar"}`, `{"foo": "bar", "hello": "world"}`)) 1731} 1732 1733func TestYAMLEq_ActualIsSimpleString(t *testing.T) { 1734 mockT := new(testing.T) 1735 False(t, YAMLEq(mockT, `{"foo": "bar"}`, "Simple String")) 1736} 1737 1738func TestYAMLEq_ExpectedIsSimpleString(t *testing.T) { 1739 mockT := new(testing.T) 1740 False(t, YAMLEq(mockT, "Simple String", `{"foo": "bar", "hello": "world"}`)) 1741} 1742 1743func TestYAMLEq_ExpectedAndActualSimpleString(t *testing.T) { 1744 mockT := new(testing.T) 1745 True(t, YAMLEq(mockT, "Simple String", "Simple String")) 1746} 1747 1748func TestYAMLEq_ArraysOfDifferentOrder(t *testing.T) { 1749 mockT := new(testing.T) 1750 False(t, YAMLEq(mockT, `["foo", {"hello": "world", "nested": "hash"}]`, `[{ "hello": "world", "nested": "hash"}, "foo"]`)) 1751} 1752 1753func TestDiff(t *testing.T) { 1754 expected := ` 1755 1756Diff: 1757--- Expected 1758+++ Actual 1759@@ -1,3 +1,3 @@ 1760 (struct { foo string }) { 1761- foo: (string) (len=5) "hello" 1762+ foo: (string) (len=3) "bar" 1763 } 1764` 1765 actual := diff( 1766 struct{ foo string }{"hello"}, 1767 struct{ foo string }{"bar"}, 1768 ) 1769 Equal(t, expected, actual) 1770 1771 expected = ` 1772 1773Diff: 1774--- Expected 1775+++ Actual 1776@@ -2,5 +2,5 @@ 1777 (int) 1, 1778- (int) 2, 1779 (int) 3, 1780- (int) 4 1781+ (int) 5, 1782+ (int) 7 1783 } 1784` 1785 actual = diff( 1786 []int{1, 2, 3, 4}, 1787 []int{1, 3, 5, 7}, 1788 ) 1789 Equal(t, expected, actual) 1790 1791 expected = ` 1792 1793Diff: 1794--- Expected 1795+++ Actual 1796@@ -2,4 +2,4 @@ 1797 (int) 1, 1798- (int) 2, 1799- (int) 3 1800+ (int) 3, 1801+ (int) 5 1802 } 1803` 1804 actual = diff( 1805 []int{1, 2, 3, 4}[0:3], 1806 []int{1, 3, 5, 7}[0:3], 1807 ) 1808 Equal(t, expected, actual) 1809 1810 expected = ` 1811 1812Diff: 1813--- Expected 1814+++ Actual 1815@@ -1,6 +1,6 @@ 1816 (map[string]int) (len=4) { 1817- (string) (len=4) "four": (int) 4, 1818+ (string) (len=4) "five": (int) 5, 1819 (string) (len=3) "one": (int) 1, 1820- (string) (len=5) "three": (int) 3, 1821- (string) (len=3) "two": (int) 2 1822+ (string) (len=5) "seven": (int) 7, 1823+ (string) (len=5) "three": (int) 3 1824 } 1825` 1826 1827 actual = diff( 1828 map[string]int{"one": 1, "two": 2, "three": 3, "four": 4}, 1829 map[string]int{"one": 1, "three": 3, "five": 5, "seven": 7}, 1830 ) 1831 Equal(t, expected, actual) 1832} 1833 1834func TestTimeEqualityErrorFormatting(t *testing.T) { 1835 mockT := new(mockTestingT) 1836 1837 Equal(mockT, time.Second*2, time.Millisecond) 1838 1839 expectedErr := "\\s+Error Trace:\\s+Error:\\s+Not equal:\\s+\n\\s+expected: 2s\n\\s+actual\\s+: 1ms\n" 1840 Regexp(t, regexp.MustCompile(expectedErr), mockT.errorString()) 1841} 1842 1843func TestDiffEmptyCases(t *testing.T) { 1844 Equal(t, "", diff(nil, nil)) 1845 Equal(t, "", diff(struct{ foo string }{}, nil)) 1846 Equal(t, "", diff(nil, struct{ foo string }{})) 1847 Equal(t, "", diff(1, 2)) 1848 Equal(t, "", diff(1, 2)) 1849 Equal(t, "", diff([]int{1}, []bool{true})) 1850} 1851 1852// Ensure there are no data races 1853func TestDiffRace(t *testing.T) { 1854 t.Parallel() 1855 1856 expected := map[string]string{ 1857 "a": "A", 1858 "b": "B", 1859 "c": "C", 1860 } 1861 1862 actual := map[string]string{ 1863 "d": "D", 1864 "e": "E", 1865 "f": "F", 1866 } 1867 1868 // run diffs in parallel simulating tests with t.Parallel() 1869 numRoutines := 10 1870 rChans := make([]chan string, numRoutines) 1871 for idx := range rChans { 1872 rChans[idx] = make(chan string) 1873 go func(ch chan string) { 1874 defer close(ch) 1875 ch <- diff(expected, actual) 1876 }(rChans[idx]) 1877 } 1878 1879 for _, ch := range rChans { 1880 for msg := range ch { 1881 NotZero(t, msg) // dummy assert 1882 } 1883 } 1884} 1885 1886type mockTestingT struct { 1887 errorFmt string 1888 args []interface{} 1889} 1890 1891func (m *mockTestingT) errorString() string { 1892 return fmt.Sprintf(m.errorFmt, m.args...) 1893} 1894 1895func (m *mockTestingT) Errorf(format string, args ...interface{}) { 1896 m.errorFmt = format 1897 m.args = args 1898} 1899 1900func TestFailNowWithPlainTestingT(t *testing.T) { 1901 mockT := &mockTestingT{} 1902 1903 Panics(t, func() { 1904 FailNow(mockT, "failed") 1905 }, "should panic since mockT is missing FailNow()") 1906} 1907 1908type mockFailNowTestingT struct { 1909} 1910 1911func (m *mockFailNowTestingT) Errorf(format string, args ...interface{}) {} 1912 1913func (m *mockFailNowTestingT) FailNow() {} 1914 1915func TestFailNowWithFullTestingT(t *testing.T) { 1916 mockT := &mockFailNowTestingT{} 1917 1918 NotPanics(t, func() { 1919 FailNow(mockT, "failed") 1920 }, "should call mockT.FailNow() rather than panicking") 1921} 1922 1923func TestBytesEqual(t *testing.T) { 1924 var cases = []struct { 1925 a, b []byte 1926 }{ 1927 {make([]byte, 2), make([]byte, 2)}, 1928 {make([]byte, 2), make([]byte, 2, 3)}, 1929 {nil, make([]byte, 0)}, 1930 } 1931 for i, c := range cases { 1932 Equal(t, reflect.DeepEqual(c.a, c.b), ObjectsAreEqual(c.a, c.b), "case %d failed", i+1) 1933 } 1934} 1935 1936func BenchmarkBytesEqual(b *testing.B) { 1937 const size = 1024 * 8 1938 s := make([]byte, size) 1939 for i := range s { 1940 s[i] = byte(i % 255) 1941 } 1942 s2 := make([]byte, size) 1943 copy(s2, s) 1944 1945 mockT := &mockFailNowTestingT{} 1946 b.ResetTimer() 1947 for i := 0; i < b.N; i++ { 1948 Equal(mockT, s, s2) 1949 } 1950} 1951 1952func ExampleComparisonAssertionFunc() { 1953 t := &testing.T{} // provided by test 1954 1955 adder := func(x, y int) int { 1956 return x + y 1957 } 1958 1959 type args struct { 1960 x int 1961 y int 1962 } 1963 1964 tests := []struct { 1965 name string 1966 args args 1967 expect int 1968 assertion ComparisonAssertionFunc 1969 }{ 1970 {"2+2=4", args{2, 2}, 4, Equal}, 1971 {"2+2!=5", args{2, 2}, 5, NotEqual}, 1972 {"2+3==5", args{2, 3}, 5, Exactly}, 1973 } 1974 1975 for _, tt := range tests { 1976 t.Run(tt.name, func(t *testing.T) { 1977 tt.assertion(t, tt.expect, adder(tt.args.x, tt.args.y)) 1978 }) 1979 } 1980} 1981 1982func TestComparisonAssertionFunc(t *testing.T) { 1983 type iface interface { 1984 Name() string 1985 } 1986 1987 tests := []struct { 1988 name string 1989 expect interface{} 1990 got interface{} 1991 assertion ComparisonAssertionFunc 1992 }{ 1993 {"implements", (*iface)(nil), t, Implements}, 1994 {"isType", (*testing.T)(nil), t, IsType}, 1995 {"equal", t, t, Equal}, 1996 {"equalValues", t, t, EqualValues}, 1997 {"exactly", t, t, Exactly}, 1998 {"notEqual", t, nil, NotEqual}, 1999 {"notContains", []int{1, 2, 3}, 4, NotContains}, 2000 {"subset", []int{1, 2, 3, 4}, []int{2, 3}, Subset}, 2001 {"notSubset", []int{1, 2, 3, 4}, []int{0, 3}, NotSubset}, 2002 {"elementsMatch", []byte("abc"), []byte("bac"), ElementsMatch}, 2003 {"regexp", "^t.*y$", "testify", Regexp}, 2004 {"notRegexp", "^t.*y$", "Testify", NotRegexp}, 2005 } 2006 2007 for _, tt := range tests { 2008 t.Run(tt.name, func(t *testing.T) { 2009 tt.assertion(t, tt.expect, tt.got) 2010 }) 2011 } 2012} 2013 2014func ExampleValueAssertionFunc() { 2015 t := &testing.T{} // provided by test 2016 2017 dumbParse := func(input string) interface{} { 2018 var x interface{} 2019 json.Unmarshal([]byte(input), &x) 2020 return x 2021 } 2022 2023 tests := []struct { 2024 name string 2025 arg string 2026 assertion ValueAssertionFunc 2027 }{ 2028 {"true is not nil", "true", NotNil}, 2029 {"empty string is nil", "", Nil}, 2030 {"zero is not nil", "0", NotNil}, 2031 {"zero is zero", "0", Zero}, 2032 {"false is zero", "false", Zero}, 2033 } 2034 2035 for _, tt := range tests { 2036 t.Run(tt.name, func(t *testing.T) { 2037 tt.assertion(t, dumbParse(tt.arg)) 2038 }) 2039 } 2040} 2041 2042func TestValueAssertionFunc(t *testing.T) { 2043 tests := []struct { 2044 name string 2045 value interface{} 2046 assertion ValueAssertionFunc 2047 }{ 2048 {"notNil", true, NotNil}, 2049 {"nil", nil, Nil}, 2050 {"empty", []int{}, Empty}, 2051 {"notEmpty", []int{1}, NotEmpty}, 2052 {"zero", false, Zero}, 2053 {"notZero", 42, NotZero}, 2054 } 2055 2056 for _, tt := range tests { 2057 t.Run(tt.name, func(t *testing.T) { 2058 tt.assertion(t, tt.value) 2059 }) 2060 } 2061} 2062 2063func ExampleBoolAssertionFunc() { 2064 t := &testing.T{} // provided by test 2065 2066 isOkay := func(x int) bool { 2067 return x >= 42 2068 } 2069 2070 tests := []struct { 2071 name string 2072 arg int 2073 assertion BoolAssertionFunc 2074 }{ 2075 {"-1 is bad", -1, False}, 2076 {"42 is good", 42, True}, 2077 {"41 is bad", 41, False}, 2078 {"45 is cool", 45, True}, 2079 } 2080 2081 for _, tt := range tests { 2082 t.Run(tt.name, func(t *testing.T) { 2083 tt.assertion(t, isOkay(tt.arg)) 2084 }) 2085 } 2086} 2087 2088func TestBoolAssertionFunc(t *testing.T) { 2089 tests := []struct { 2090 name string 2091 value bool 2092 assertion BoolAssertionFunc 2093 }{ 2094 {"true", true, True}, 2095 {"false", false, False}, 2096 } 2097 2098 for _, tt := range tests { 2099 t.Run(tt.name, func(t *testing.T) { 2100 tt.assertion(t, tt.value) 2101 }) 2102 } 2103} 2104 2105func ExampleErrorAssertionFunc() { 2106 t := &testing.T{} // provided by test 2107 2108 dumbParseNum := func(input string, v interface{}) error { 2109 return json.Unmarshal([]byte(input), v) 2110 } 2111 2112 tests := []struct { 2113 name string 2114 arg string 2115 assertion ErrorAssertionFunc 2116 }{ 2117 {"1.2 is number", "1.2", NoError}, 2118 {"1.2.3 not number", "1.2.3", Error}, 2119 {"true is not number", "true", Error}, 2120 {"3 is number", "3", NoError}, 2121 } 2122 2123 for _, tt := range tests { 2124 t.Run(tt.name, func(t *testing.T) { 2125 var x float64 2126 tt.assertion(t, dumbParseNum(tt.arg, &x)) 2127 }) 2128 } 2129} 2130 2131func TestErrorAssertionFunc(t *testing.T) { 2132 tests := []struct { 2133 name string 2134 err error 2135 assertion ErrorAssertionFunc 2136 }{ 2137 {"noError", nil, NoError}, 2138 {"error", errors.New("whoops"), Error}, 2139 } 2140 2141 for _, tt := range tests { 2142 t.Run(tt.name, func(t *testing.T) { 2143 tt.assertion(t, tt.err) 2144 }) 2145 } 2146} 2147 2148func TestEventuallyFalse(t *testing.T) { 2149 mockT := new(testing.T) 2150 2151 condition := func() bool { 2152 return false 2153 } 2154 2155 False(t, Eventually(mockT, condition, 100*time.Millisecond, 20*time.Millisecond)) 2156} 2157 2158func TestEventuallyTrue(t *testing.T) { 2159 state := 0 2160 condition := func() bool { 2161 defer func() { 2162 state = state + 1 2163 }() 2164 return state == 2 2165 } 2166 2167 True(t, Eventually(t, condition, 100*time.Millisecond, 20*time.Millisecond)) 2168} 2169 2170func TestNeverFalse(t *testing.T) { 2171 condition := func() bool { 2172 return false 2173 } 2174 2175 True(t, Never(t, condition, 100*time.Millisecond, 20*time.Millisecond)) 2176} 2177 2178func TestNeverTrue(t *testing.T) { 2179 mockT := new(testing.T) 2180 state := 0 2181 condition := func() bool { 2182 defer func() { 2183 state = state + 1 2184 }() 2185 return state == 2 2186 } 2187 2188 False(t, Never(mockT, condition, 100*time.Millisecond, 20*time.Millisecond)) 2189} 2190 2191func TestEventuallyIssue805(t *testing.T) { 2192 mockT := new(testing.T) 2193 2194 NotPanics(t, func() { 2195 condition := func() bool { <-time.After(time.Millisecond); return true } 2196 False(t, Eventually(mockT, condition, time.Millisecond, time.Microsecond)) 2197 }) 2198} 2199 2200func Test_validateEqualArgs(t *testing.T) { 2201 if validateEqualArgs(func() {}, func() {}) == nil { 2202 t.Error("non-nil functions should error") 2203 } 2204 2205 if validateEqualArgs(func() {}, func() {}) == nil { 2206 t.Error("non-nil functions should error") 2207 } 2208 2209 if validateEqualArgs(nil, nil) != nil { 2210 t.Error("nil functions are equal") 2211 } 2212} 2213