github.com/blend/go-sdk@v1.20220411.3/assert/assert_test.go (about) 1 /* 2 3 Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package assert 9 10 import ( 11 "bytes" 12 "strings" 13 "testing" 14 "time" 15 ) 16 17 func Test_Empty(t *testing.T) { 18 t.Parallel() 19 20 buf := bytes.NewBuffer(nil) 21 a := Empty(OptOutput(buf)) 22 23 if a.Output == nil { 24 t.Error("The empty assertion helper should have an output set") 25 t.Fail() 26 } 27 if a.NonFatal().True(false, "this should fail") { 28 t.Error("NonFatal true(false) didn't fail.") 29 } 30 if !a.NonFatal().True(true, "this should pass") { 31 t.Error("NonFatal true(true) didn't pass.") 32 } 33 34 if len(buf.String()) == 0 { 35 t.Error("We should have produced output.") 36 } 37 } 38 39 func Test_isZero(t *testing.T) { 40 t.Parallel() 41 42 zeroShort := int16(0) 43 if !isZero(zeroShort) { 44 t.Error("isZero failed") 45 } 46 47 notZeroShort := int16(3) 48 if isZero(notZeroShort) { 49 t.Error("isZero failed") 50 } 51 52 zero := 0 53 if !isZero(zero) { 54 t.Error("isZero failed") 55 } 56 notZero := 3 57 if isZero(notZero) { 58 t.Error("isZero failed") 59 } 60 61 zeroFloat64 := 0.0 62 if !isZero(zeroFloat64) { 63 t.Error("isZero failed") 64 } 65 notZeroFloat64 := 3.14 66 if isZero(notZeroFloat64) { 67 t.Error("isZero failed") 68 } 69 70 zeroFloat32 := float32(0.0) 71 if !isZero(zeroFloat32) { 72 t.Error("isZero failed") 73 } 74 notZeroFloat32 := float32(3.14) 75 if isZero(notZeroFloat32) { 76 t.Error("isZero failed") 77 } 78 } 79 80 func Test_getLength(t *testing.T) { 81 t.Parallel() 82 83 emptyString := "" 84 l := getLength(emptyString) 85 if l != 0 { 86 t.Errorf("getLength incorrect.") 87 } 88 89 notEmptyString := "foo" 90 l = getLength(notEmptyString) 91 if l != 3 { 92 t.Errorf("getLength incorrect.") 93 } 94 95 emptyArray := []int{} 96 l = getLength(emptyArray) 97 if l != 0 { 98 t.Errorf("getLength incorrect.") 99 } 100 101 notEmptyArray := []int{1, 2, 3} 102 l = getLength(notEmptyArray) 103 if l != 3 { 104 t.Errorf("getLength incorrect.") 105 } 106 107 emptyMap := map[string]int{} 108 l = getLength(emptyMap) 109 if l != 0 { 110 t.Errorf("getLength incorrect.") 111 } 112 113 notEmptyMap := map[string]int{"foo": 1, "bar": 2, "baz": 3} 114 l = getLength(notEmptyMap) 115 if l != 3 { 116 t.Errorf("getLength incorrect.") 117 } 118 } 119 120 type myNestedStruct struct { 121 ID int 122 Name string 123 } 124 125 type myTestStruct struct { 126 ID int 127 Name string 128 SingleValue float32 129 DoubleValue float64 130 Timestamp time.Time 131 Struct myNestedStruct 132 133 IDPtr *int 134 NamePptr *string 135 StructPtr *myNestedStruct 136 137 Slice []myNestedStruct 138 SlicePtr *[]myNestedStruct 139 } 140 141 func createTestStruct() myTestStruct { 142 testInt := 1 143 testName := "test struct" 144 145 nestedA := myNestedStruct{1, "A"} 146 nestedB := myNestedStruct{1, "B"} 147 nestedC := myNestedStruct{1, "C"} 148 149 testStruct := myTestStruct{ 150 ID: testInt, 151 Name: testName, 152 SingleValue: float32(3.14), 153 DoubleValue: 6.28, 154 Timestamp: time.Now(), 155 Struct: nestedA, 156 IDPtr: &testInt, 157 NamePptr: &testName, 158 StructPtr: &nestedB, 159 Slice: []myNestedStruct{nestedA, nestedB, nestedC}, 160 } 161 162 testStruct.SlicePtr = &testStruct.Slice 163 return testStruct 164 } 165 166 func Test_shouldBeEqual_nestedStructsAreEqual(t *testing.T) { 167 t.Parallel() 168 169 testStructA := createTestStruct() 170 testStructB := createTestStruct() 171 testStructB.Name = "not test struct" 172 173 if didFail, _ := shouldBeEqual(testStructA, testStructA); didFail { 174 t.Error("shouldBeEqual Failed.") 175 t.FailNow() 176 } 177 178 if didFail, _ := shouldBeEqual(testStructA, testStructB); !didFail { 179 t.Error("shouldBeEqual Failed.") 180 t.FailNow() 181 } 182 } 183 184 func Test_shouldBeEqual(t *testing.T) { 185 t.Parallel() 186 187 byteA := byte('a') 188 byteB := byte('b') 189 190 if didFail, _ := shouldBeEqual(byteA, byteA); didFail { 191 t.Error("shouldBeEqual Failed.") 192 t.FailNow() 193 } 194 if didFail, _ := shouldBeEqual(byteA, byteB); !didFail { 195 t.Error("shouldBeEqual Failed.") 196 t.FailNow() 197 } 198 199 stringA := "test" 200 stringB := "not test" 201 202 if didFail, _ := shouldBeEqual(stringA, stringA); didFail { 203 t.Error("shouldBeEqual Equal Failed.") 204 t.FailNow() 205 } 206 if didFail, _ := shouldBeEqual(stringA, stringB); !didFail { 207 t.Error("shouldBeEqual Failed.") 208 t.FailNow() 209 } 210 211 intA := 1 212 intB := 2 213 214 if didFail, _ := shouldBeEqual(intA, intA); didFail { 215 t.Error("shouldBeEqual Equal Failed.") 216 t.FailNow() 217 } 218 if didFail, _ := shouldBeEqual(intA, intB); !didFail { 219 t.Error("shouldBeEqual Failed.") 220 t.FailNow() 221 } 222 223 float32A := float32(3.14) 224 float32B := float32(6.28) 225 226 if didFail, _ := shouldBeEqual(float32A, float32A); didFail { 227 t.Error("shouldBeEqual Equal Failed.") 228 t.FailNow() 229 } 230 if didFail, _ := shouldBeEqual(float32A, float32B); !didFail { 231 t.Error("shouldBeEqual Failed.") 232 t.FailNow() 233 } 234 235 floatA := 3.14 236 floatB := 6.28 237 238 if didFail, _ := shouldBeEqual(floatA, floatA); didFail { 239 t.Error("shouldBeEqual Equal Failed.") 240 t.FailNow() 241 } 242 if didFail, _ := shouldBeEqual(floatA, floatB); !didFail { 243 t.Error("shouldBeEqual Failed.") 244 t.FailNow() 245 } 246 } 247 248 func makesThings(shouldReturnNil bool) *myTestStruct { 249 if !shouldReturnNil { 250 return &myTestStruct{} 251 } 252 return nil 253 } 254 255 func Test_shouldBeNil(t *testing.T) { 256 t.Parallel() 257 258 assertsToNil := makesThings(true) 259 assertsToNotNil := makesThings(false) 260 261 didFail, didFailErrMsg := shouldBeNil(assertsToNil) 262 if didFail { 263 t.Error(didFailErrMsg) 264 t.FailNow() 265 } 266 267 didFail, _ = shouldBeNil(assertsToNotNil) 268 if !didFail { 269 t.Error("shouldBeNil returned did_fail as `true` for a not nil object") 270 t.FailNow() 271 } 272 } 273 274 func Test_shouldNotBeNil(t *testing.T) { 275 t.Parallel() 276 277 assertsToNil := makesThings(true) 278 assertsToNotNil := makesThings(false) 279 280 didFail, didFailErrMsg := shouldNotBeNil(assertsToNotNil) 281 if didFail { 282 t.Error(didFailErrMsg) 283 t.FailNow() 284 } 285 286 didFail, _ = shouldNotBeNil(assertsToNil) 287 if !didFail { 288 t.Error("shouldNotBeNil returned did_fail as `true` for a not nil object") 289 t.FailNow() 290 } 291 } 292 293 func Test_shouldContain(t *testing.T) { 294 t.Parallel() 295 296 shouldNotHaveFailed, _ := shouldContain("this is a test", "is a") 297 if shouldNotHaveFailed { 298 t.Errorf("shouldConatain failed.") 299 t.FailNow() 300 } 301 302 shouldHaveFailed, _ := shouldContain("this is a test", "beer") 303 if !shouldHaveFailed { 304 t.Errorf("shouldConatain failed.") 305 t.FailNow() 306 } 307 } 308 309 type anyTestObj struct { 310 ID int 311 Name string 312 } 313 314 func Test_shouldAny(t *testing.T) { 315 t.Parallel() 316 317 testObjs := []anyTestObj{{1, "Test"}, {2, "Test2"}, {3, "Foo"}} 318 319 didFail, _ := shouldAny(testObjs, func(obj interface{}) bool { 320 if typed, didType := obj.(anyTestObj); didType { 321 return strings.HasPrefix(typed.Name, "Foo") 322 } 323 return false 324 }) 325 if didFail { 326 t.Errorf("shouldAny failed.") 327 t.FailNow() 328 } 329 330 didFail, _ = shouldAny(testObjs, func(obj interface{}) bool { 331 if typed, didType := obj.(anyTestObj); didType { 332 return strings.HasPrefix(typed.Name, "Bar") 333 } 334 return false 335 }) 336 if !didFail { 337 t.Errorf("shouldAny should have failed.") 338 t.FailNow() 339 } 340 341 didFail, _ = shouldAny(anyTestObj{1, "test"}, func(obj interface{}) bool { 342 return true 343 }) 344 if !didFail { 345 t.Errorf("shouldAny should have failed on non-slice target.") 346 t.FailNow() 347 } 348 } 349 350 func Test_shouldAll(t *testing.T) { 351 t.Parallel() 352 353 testObjs := []anyTestObj{{1, "Test"}, {2, "Test2"}, {3, "Foo"}} 354 355 didFail, _ := shouldAll(testObjs, func(obj interface{}) bool { 356 if typed, didType := obj.(anyTestObj); didType { 357 return typed.ID > 0 358 } 359 return false 360 }) 361 if didFail { 362 t.Errorf("shouldAll shouldnt have failed.") 363 t.FailNow() 364 } 365 366 didFail, _ = shouldAll(testObjs, func(obj interface{}) bool { 367 if typed, didType := obj.(anyTestObj); didType { 368 return strings.HasPrefix(typed.Name, "Test") 369 } 370 return false 371 }) 372 if !didFail { 373 t.Errorf("shouldAll should have failed.") 374 t.FailNow() 375 } 376 377 didFail, _ = shouldAll(anyTestObj{1, "test"}, func(obj interface{}) bool { 378 return true 379 }) 380 if !didFail { 381 t.Errorf("shouldAll should have failed on non-slice target.") 382 t.FailNow() 383 } 384 } 385 386 func Test_shouldNone(t *testing.T) { 387 t.Parallel() 388 389 testObjs := []anyTestObj{{1, "Test"}, {2, "Test2"}, {3, "Foo"}} 390 391 didFail, _ := shouldNone(testObjs, func(obj interface{}) bool { 392 if typed, didType := obj.(anyTestObj); didType { 393 return typed.ID > 4 394 } 395 return false 396 }) 397 if didFail { 398 t.Errorf("shouldAll shouldnt have failed.") 399 t.FailNow() 400 } 401 402 didFail, _ = shouldNone(testObjs, func(obj interface{}) bool { 403 if typed, didType := obj.(anyTestObj); didType { 404 return typed.ID > 0 405 } 406 return false 407 }) 408 if !didFail { 409 t.Errorf("shouldNone should have failed.") 410 t.FailNow() 411 } 412 } 413 414 func Test_shouldBeInTimeDelta(t *testing.T) { 415 t.Parallel() 416 417 value1 := time.Date(2016, 1, 29, 9, 0, 0, 0, time.UTC) 418 value2 := time.Date(2016, 1, 29, 9, 0, 0, 1, time.UTC) 419 value3 := time.Date(2016, 1, 29, 8, 0, 0, 0, time.UTC) 420 value4 := time.Date(2015, 1, 29, 9, 0, 0, 0, time.UTC) 421 422 didFail, _ := shouldBeInTimeDelta(value1, value2, 1*time.Minute) 423 if didFail { 424 t.Errorf("shouldBeInTimeDelta shouldnt have failed.") 425 t.FailNow() 426 } 427 428 didFail, _ = shouldBeInTimeDelta(value1, value3, 1*time.Minute) 429 if !didFail { 430 t.Errorf("shouldBeInTimeDelta should have failed.") 431 t.FailNow() 432 } 433 434 didFail, _ = shouldBeInTimeDelta(value1, value4, 1*time.Minute) 435 if !didFail { 436 t.Errorf("shouldBeInTimeDelta should have failed.") 437 t.FailNow() 438 } 439 } 440 441 func Test_New(t *testing.T) { 442 t.Parallel() 443 444 buffer := new(bytes.Buffer) 445 a := New(t, 446 OptOutput(buffer), 447 ) 448 449 if a.T == nil { 450 t.Errorf("should pass t to the assertion helper") 451 t.Fail() 452 } 453 if a.Output == nil { 454 t.Errorf("should run provided options") 455 t.Fail() 456 } 457 if a.OutputFormat != OutputFormatFromEnv() { 458 t.Errorf("should set the correct output format") 459 t.Fail() 460 } 461 if a.Context == nil { 462 t.Errorf("should set the context") 463 t.Fail() 464 } 465 if GetTestName(a.Context) != t.Name() { 466 t.Errorf("should set the context test name") 467 t.Fail() 468 } 469 if GetContextID(a.Context) == "" { 470 t.Errorf("should set the context id") 471 t.Fail() 472 } 473 } 474 475 func Test_Assert_NotFatal(t *testing.T) { 476 t.Parallel() 477 478 buf := bytes.NewBuffer(nil) 479 a := New(t, OptOutput(buf)) 480 nf := a.NonFatal() 481 if nf.T == nil { 482 t.Errorf("should set t") 483 t.FailNow() 484 } 485 if nf.Output == nil { 486 t.Errorf("should set output") 487 t.FailNow() 488 } 489 } 490 491 func TestAssertNil(t *testing.T) { 492 err := safeExec(func() { 493 New(nil).Nil(nil) // should be ok 494 }) 495 if err != nil { 496 t.Errorf("should not have produced a panic") 497 t.FailNow() 498 } 499 500 output := bytes.NewBuffer(nil) 501 err = safeExec(func() { 502 New(nil, OptOutput(output)).Nil("foo") 503 }) 504 if err == nil { 505 t.Errorf("should have produced a panic") 506 t.FailNow() 507 } 508 if len(output.String()) == 0 { 509 t.Errorf("Should have written output on failure") 510 t.FailNow() 511 } 512 } 513 514 func TestAssertNotNil(t *testing.T) { 515 err := safeExec(func() { 516 New(nil).NotNil("foo") // should be ok 517 }) 518 if err != nil { 519 t.Errorf("should not have produced a panic") 520 t.FailNow() 521 } 522 523 output := bytes.NewBuffer(nil) 524 err = safeExec(func() { 525 New(nil, OptOutput(output)).NotNil(nil) 526 }) 527 if err == nil { 528 t.Errorf("should have produced a panic") 529 t.FailNow() 530 } 531 if len(output.String()) == 0 { 532 t.Errorf("Should have written output on failure") 533 t.FailNow() 534 } 535 } 536 537 func TestAssertLen(t *testing.T) { 538 err := safeExec(func() { 539 New(nil).Len("foo", 3) // should be ok 540 }) 541 if err != nil { 542 t.Errorf("should not have produced a panic") 543 t.FailNow() 544 } 545 546 output := bytes.NewBuffer(nil) 547 err = safeExec(func() { 548 New(nil, OptOutput(output)).Len([]string{}, 3) 549 }) 550 if err == nil { 551 t.Errorf("should have produced a panic") 552 t.FailNow() 553 } 554 if len(output.String()) == 0 { 555 t.Errorf("Should have written output on failure") 556 t.FailNow() 557 } 558 } 559 560 func TestAssertEmpty(t *testing.T) { 561 err := safeExec(func() { 562 New(nil).Empty("") // should be ok 563 }) 564 if err != nil { 565 t.Errorf("should not have produced a panic") 566 t.FailNow() 567 } 568 569 output := bytes.NewBuffer(nil) 570 err = safeExec(func() { 571 New(nil, OptOutput(output)).Empty("foo") 572 }) 573 if err == nil { 574 t.Errorf("should have produced a panic") 575 t.FailNow() 576 } 577 if len(output.String()) == 0 { 578 t.Errorf("Should have written output on failure") 579 t.FailNow() 580 } 581 } 582 583 func TestAssertNotEmpty(t *testing.T) { 584 err := safeExec(func() { 585 New(nil).NotEmpty("foo") // should be ok 586 }) 587 if err != nil { 588 t.Errorf("should not have produced a panic") 589 t.FailNow() 590 } 591 592 output := bytes.NewBuffer(nil) 593 err = safeExec(func() { 594 New(nil, OptOutput(output)).NotEmpty("") 595 }) 596 if err == nil { 597 t.Errorf("should have produced a panic") 598 t.FailNow() 599 } 600 if len(output.String()) == 0 { 601 t.Errorf("Should have written output on failure") 602 t.FailNow() 603 } 604 } 605 606 func TestAssertEqual(t *testing.T) { 607 err := safeExec(func() { 608 New(nil).Equal("foo", "foo") // should be ok 609 }) 610 if err != nil { 611 t.Errorf("should not have produced a panic") 612 t.FailNow() 613 } 614 615 output := bytes.NewBuffer(nil) 616 err = safeExec(func() { 617 New(nil, OptOutput(output)).Equal("foo", "bar") 618 }) 619 if err == nil { 620 t.Errorf("should have produced a panic") 621 t.FailNow() 622 } 623 if len(output.String()) == 0 { 624 t.Errorf("Should have written output on failure") 625 t.FailNow() 626 } 627 } 628 629 func TestAssertReferenceEqual(t *testing.T) { 630 obj1 := "foo" 631 obj2 := "foo" 632 ref1 := &obj1 633 ref2 := &obj1 634 ref3 := &obj2 635 636 err := safeExec(func() { 637 New(nil).ReferenceEqual(ref1, ref2) // should be ok 638 }) 639 if err != nil { 640 t.Errorf("should not have produced a panic") 641 t.FailNow() 642 } 643 644 output := bytes.NewBuffer(nil) 645 err = safeExec(func() { 646 New(nil, OptOutput(output)).ReferenceEqual(ref1, ref3) 647 }) 648 if err == nil { 649 t.Errorf("should have produced a panic") 650 t.FailNow() 651 } 652 if len(output.String()) == 0 { 653 t.Errorf("Should have written output on failure") 654 t.FailNow() 655 } 656 } 657 658 func TestAssertNotEqual(t *testing.T) { 659 err := safeExec(func() { 660 New(nil).NotEqual("foo", "bar") // should be ok 661 }) 662 if err != nil { 663 t.Errorf("should not have produced a panic") 664 t.FailNow() 665 } 666 667 output := bytes.NewBuffer(nil) 668 err = safeExec(func() { 669 New(nil, OptOutput(output)).NotEqual("foo", "foo") 670 }) 671 if err == nil { 672 t.Errorf("should have produced a panic") 673 t.FailNow() 674 } 675 if len(output.String()) == 0 { 676 t.Errorf("Should have written output on failure") 677 t.FailNow() 678 } 679 } 680 681 func TestAssertZero(t *testing.T) { 682 err := safeExec(func() { 683 New(nil).Zero(0) // should be ok 684 }) 685 if err != nil { 686 t.Errorf("should not have produced a panic") 687 t.FailNow() 688 } 689 690 output := bytes.NewBuffer(nil) 691 err = safeExec(func() { 692 New(nil, OptOutput(output)).Zero(1) 693 }) 694 if err == nil { 695 t.Errorf("should have produced a panic") 696 t.FailNow() 697 } 698 if len(output.String()) == 0 { 699 t.Errorf("Should have written output on failure") 700 t.FailNow() 701 } 702 } 703 704 func TestAssertNotZero(t *testing.T) { 705 err := safeExec(func() { 706 New(nil).NotZero(1) // should be ok 707 }) 708 if err != nil { 709 t.Errorf("should not have produced a panic") 710 t.FailNow() 711 } 712 713 output := bytes.NewBuffer(nil) 714 err = safeExec(func() { 715 New(nil, OptOutput(output)).NotZero(0) 716 }) 717 if err == nil { 718 t.Errorf("should have produced a panic") 719 t.FailNow() 720 } 721 if len(output.String()) == 0 { 722 t.Errorf("Should have written output on failure") 723 t.FailNow() 724 } 725 } 726 727 func TestAssertTrue(t *testing.T) { 728 err := safeExec(func() { 729 New(nil).True(true) // should be ok 730 }) 731 if err != nil { 732 t.Errorf("should not have produced a panic") 733 t.FailNow() 734 } 735 736 output := bytes.NewBuffer(nil) 737 err = safeExec(func() { 738 New(nil, OptOutput(output)).True(1 == 0) 739 }) 740 if err == nil { 741 t.Errorf("should have produced a panic") 742 t.FailNow() 743 } 744 if len(output.String()) == 0 { 745 t.Errorf("Should have written output on failure") 746 t.FailNow() 747 } 748 } 749 750 func TestAssertFalse(t *testing.T) { 751 err := safeExec(func() { 752 New(nil).False(false) // should be ok 753 }) 754 if err != nil { 755 t.Errorf("should not have produced a panic") 756 t.FailNow() 757 } 758 759 output := bytes.NewBuffer(nil) 760 err = safeExec(func() { 761 New(nil, OptOutput(output)).False(true) 762 }) 763 if err == nil { 764 t.Errorf("should have produced a panic") 765 t.FailNow() 766 } 767 if len(output.String()) == 0 { 768 t.Errorf("Should have written output on failure") 769 t.FailNow() 770 } 771 } 772 773 func TestAssertInDelta(t *testing.T) { 774 err := safeExec(func() { 775 New(nil).InDelta(1, 2, 1) // should be ok 776 New(nil).InDelta(1, 1.5, 1) // should be ok 777 }) 778 if err != nil { 779 t.Errorf("should not have produced a panic") 780 t.FailNow() 781 } 782 783 output := bytes.NewBuffer(nil) 784 err = safeExec(func() { 785 New(nil, OptOutput(output)).InDelta(1, 3, 1) 786 }) 787 if err == nil { 788 t.Errorf("should have produced a panic") 789 t.FailNow() 790 } 791 if len(output.String()) == 0 { 792 t.Errorf("Should have written output on failure") 793 t.FailNow() 794 } 795 } 796 797 func TestAssertInTimeDelta(t *testing.T) { 798 t1 := time.Date(2018, 04, 10, 12, 00, 00, 00, time.UTC) 799 t2 := time.Date(2018, 04, 10, 12, 00, 01, 00, time.UTC) 800 t3 := time.Date(2018, 04, 10, 12, 01, 00, 00, time.UTC) 801 802 err := safeExec(func() { 803 New(nil).InTimeDelta(t1, t2, time.Second) // should be ok 804 }) 805 if err != nil { 806 t.Errorf("should not have produced a panic") 807 t.FailNow() 808 } 809 810 output := bytes.NewBuffer(nil) 811 err = safeExec(func() { 812 New(nil, OptOutput(output)).InTimeDelta(t1, t3, time.Second) 813 }) 814 if err == nil { 815 t.Errorf("should have produced a panic") 816 t.FailNow() 817 } 818 if len(output.String()) == 0 { 819 t.Errorf("Should have written output on failure") 820 t.FailNow() 821 } 822 } 823 824 func TestAssertContains(t *testing.T) { 825 err := safeExec(func() { 826 New(nil).Contains("foo bar", "foo") // should be ok 827 }) 828 if err != nil { 829 t.Errorf("should not have produced a panic") 830 t.FailNow() 831 } 832 833 output := bytes.NewBuffer(nil) 834 err = safeExec(func() { 835 New(nil, OptOutput(output)).Contains("foo bar", "baz") 836 }) 837 if err == nil { 838 t.Errorf("should have produced a panic") 839 t.FailNow() 840 } 841 if len(output.String()) == 0 { 842 t.Errorf("Should have written output on failure") 843 t.FailNow() 844 } 845 } 846 847 func TestAssertNotContains(t *testing.T) { 848 err := safeExec(func() { 849 New(nil).NotContains("foo bar", "buzz") // should be ok 850 }) 851 if err != nil { 852 t.Errorf("should not have produced a panic") 853 t.FailNow() 854 } 855 856 output := bytes.NewBuffer(nil) 857 err = safeExec(func() { 858 New(nil, OptOutput(output)).NotContains("foo bar", "foo") 859 }) 860 if err == nil { 861 t.Errorf("should have produced a panic") 862 t.FailNow() 863 } 864 if len(output.String()) == 0 { 865 t.Errorf("Should have written output on failure") 866 t.FailNow() 867 } 868 } 869 870 func TestAssertHasPrefix(t *testing.T) { 871 err := safeExec(func() { 872 New(nil).HasPrefix("foo bar", "foo") // should be ok 873 }) 874 if err != nil { 875 t.Errorf("should not have produced a panic") 876 t.FailNow() 877 } 878 879 output := bytes.NewBuffer(nil) 880 err = safeExec(func() { 881 New(nil, OptOutput(output)).HasPrefix("foo bar", "baz") 882 }) 883 if err == nil { 884 t.Errorf("should have produced a panic") 885 t.FailNow() 886 } 887 if len(output.String()) == 0 { 888 t.Errorf("Should have written output on failure") 889 t.FailNow() 890 } 891 } 892 893 func TestAssertNotHasPrefix(t *testing.T) { 894 err := safeExec(func() { 895 New(nil).NotHasPrefix("foo bar", "buzz") // should be ok 896 }) 897 if err != nil { 898 t.Errorf("should not have produced a panic") 899 t.FailNow() 900 } 901 902 output := bytes.NewBuffer(nil) 903 err = safeExec(func() { 904 New(nil, OptOutput(output)).NotHasPrefix("foo bar", "foo") 905 }) 906 if err == nil { 907 t.Errorf("should have produced a panic") 908 t.FailNow() 909 } 910 if len(output.String()) == 0 { 911 t.Errorf("Should have written output on failure") 912 t.FailNow() 913 } 914 } 915 916 func TestAssertHasSuffix(t *testing.T) { 917 err := safeExec(func() { 918 New(nil).HasSuffix("foo bar", "bar") // should be ok 919 }) 920 if err != nil { 921 t.Errorf("should not have produced a panic") 922 t.FailNow() 923 } 924 925 output := bytes.NewBuffer(nil) 926 err = safeExec(func() { 927 New(nil, OptOutput(output)).HasSuffix("foo bar", "baz") 928 }) 929 if err == nil { 930 t.Errorf("should have produced a panic") 931 t.FailNow() 932 } 933 if len(output.String()) == 0 { 934 t.Errorf("Should have written output on failure") 935 t.FailNow() 936 } 937 } 938 939 func TestAssertNotHasSuffix(t *testing.T) { 940 err := safeExec(func() { 941 New(nil).NotHasSuffix("foo bar", "buzz") // should be ok 942 }) 943 if err != nil { 944 t.Errorf("should not have produced a panic") 945 t.FailNow() 946 } 947 948 output := bytes.NewBuffer(nil) 949 err = safeExec(func() { 950 New(nil, OptOutput(output)).NotHasSuffix("foo bar", "bar") 951 }) 952 if err == nil { 953 t.Errorf("should have produced a panic") 954 t.FailNow() 955 } 956 if len(output.String()) == 0 { 957 t.Errorf("Should have written output on failure") 958 t.FailNow() 959 } 960 } 961 962 func TestAssertAny(t *testing.T) { 963 err := safeExec(func() { 964 New(nil).Any([]int{1, 2, 3}, func(v interface{}) bool { return v.(int) == 1 }) // should be ok 965 }) 966 if err != nil { 967 t.Errorf("should not have produced a panic") 968 t.FailNow() 969 } 970 971 output := bytes.NewBuffer(nil) 972 err = safeExec(func() { 973 New(nil, OptOutput(output)).Any([]int{1, 2, 3}, func(v interface{}) bool { return v.(int) == 0 }) // should not be ok 974 }) 975 if err == nil { 976 t.Errorf("should have produced a panic") 977 t.FailNow() 978 } 979 if len(output.String()) == 0 { 980 t.Errorf("Should have written output on failure") 981 t.FailNow() 982 } 983 } 984 985 func TestAssertAnyOfInt(t *testing.T) { 986 err := safeExec(func() { 987 New(nil).AnyOfInt([]int{1, 2, 3}, func(v int) bool { return v == 1 }) // should be ok 988 }) 989 if err != nil { 990 t.Errorf("should not have produced a panic") 991 t.FailNow() 992 } 993 994 output := bytes.NewBuffer(nil) 995 err = safeExec(func() { 996 New(nil, OptOutput(output)).AnyOfInt([]int{1, 2, 3}, func(v int) bool { return v == 0 }) // should not be ok 997 }) 998 if err == nil { 999 t.Errorf("should have produced a panic") 1000 t.FailNow() 1001 } 1002 if len(output.String()) == 0 { 1003 t.Errorf("Should have written output on failure") 1004 t.FailNow() 1005 } 1006 } 1007 1008 func TestAssertAnyOfFloat64(t *testing.T) { 1009 err := safeExec(func() { 1010 New(nil).AnyOfFloat64([]float64{1, 2, 3}, func(v float64) bool { return v == 1 }) // should be ok 1011 }) 1012 if err != nil { 1013 t.Errorf("should not have produced a panic") 1014 t.FailNow() 1015 } 1016 1017 output := bytes.NewBuffer(nil) 1018 err = safeExec(func() { 1019 New(nil, OptOutput(output)).AnyOfFloat64([]float64{1, 2, 3}, func(v float64) bool { return v == 0 }) // should not be ok 1020 }) 1021 if err == nil { 1022 t.Errorf("should have produced a panic") 1023 t.FailNow() 1024 } 1025 if len(output.String()) == 0 { 1026 t.Errorf("Should have written output on failure") 1027 t.FailNow() 1028 } 1029 } 1030 1031 func TestAssertAnyOfString(t *testing.T) { 1032 err := safeExec(func() { 1033 New(nil).AnyOfString([]string{"foo", "bar", "baz"}, func(v string) bool { return v == "foo" }) // should be ok 1034 }) 1035 if err != nil { 1036 t.Errorf("should not have produced a panic") 1037 t.FailNow() 1038 } 1039 1040 output := bytes.NewBuffer(nil) 1041 err = safeExec(func() { 1042 New(nil, OptOutput(output)).AnyOfString([]string{"foo", "bar", "baz"}, func(v string) bool { return v == "buzz" }) // should not be ok 1043 }) 1044 if err == nil { 1045 t.Errorf("should have produced a panic") 1046 t.FailNow() 1047 } 1048 if len(output.String()) == 0 { 1049 t.Errorf("Should have written output on failure") 1050 t.FailNow() 1051 } 1052 } 1053 1054 func TestAssertAll(t *testing.T) { 1055 err := safeExec(func() { 1056 New(nil).All([]int{1, 2, 3}, func(v interface{}) bool { return v.(int) > 0 }) // should be ok 1057 }) 1058 if err != nil { 1059 t.Errorf("should not have produced a panic") 1060 t.FailNow() 1061 } 1062 1063 output := bytes.NewBuffer(nil) 1064 err = safeExec(func() { 1065 New(nil, OptOutput(output)).All([]int{1, 2, 3}, func(v interface{}) bool { return v.(int) > 1 }) // should not be ok 1066 }) 1067 if err == nil { 1068 t.Errorf("should have produced a panic") 1069 t.FailNow() 1070 } 1071 if len(output.String()) == 0 { 1072 t.Errorf("Should have written output on failure") 1073 t.FailNow() 1074 } 1075 } 1076 1077 func TestAssertAllOfInt(t *testing.T) { 1078 err := safeExec(func() { 1079 New(nil).AllOfInt([]int{1, 2, 3}, func(v int) bool { return v > 0 }) // should be ok 1080 }) 1081 if err != nil { 1082 t.Errorf("should not have produced a panic") 1083 t.FailNow() 1084 } 1085 1086 output := bytes.NewBuffer(nil) 1087 err = safeExec(func() { 1088 New(nil, OptOutput(output)).AllOfInt([]int{1, 2, 3}, func(v int) bool { return v > 1 }) // should not be ok 1089 }) 1090 if err == nil { 1091 t.Errorf("should have produced a panic") 1092 t.FailNow() 1093 } 1094 if len(output.String()) == 0 { 1095 t.Errorf("Should have written output on failure") 1096 t.FailNow() 1097 } 1098 } 1099 1100 func TestAssertAllOfFloat64(t *testing.T) { 1101 err := safeExec(func() { 1102 New(nil).AllOfFloat64([]float64{1, 2, 3}, func(v float64) bool { return v > 0 }) // should be ok 1103 }) 1104 if err != nil { 1105 t.Errorf("should not have produced a panic") 1106 t.FailNow() 1107 } 1108 1109 output := bytes.NewBuffer(nil) 1110 err = safeExec(func() { 1111 New(nil, OptOutput(output)).AllOfFloat64([]float64{1, 2, 3}, func(v float64) bool { return v > 1 }) // should not be ok 1112 }) 1113 if err == nil { 1114 t.Errorf("should have produced a panic") 1115 t.FailNow() 1116 } 1117 if len(output.String()) == 0 { 1118 t.Errorf("Should have written output on failure") 1119 t.FailNow() 1120 } 1121 } 1122 1123 func TestAssertAllOfString(t *testing.T) { 1124 err := safeExec(func() { 1125 New(nil).AllOfString([]string{"foo", "bar", "baz"}, func(v string) bool { return len(v) == 3 }) // should be ok 1126 }) 1127 if err != nil { 1128 t.Errorf("should not have produced a panic") 1129 t.FailNow() 1130 } 1131 1132 output := bytes.NewBuffer(nil) 1133 err = safeExec(func() { 1134 New(nil, OptOutput(output)).AllOfString([]string{"foo", "bar", "baz"}, func(v string) bool { return v == "foo" }) // should not be ok 1135 }) 1136 if err == nil { 1137 t.Errorf("should have produced a panic") 1138 t.FailNow() 1139 } 1140 if len(output.String()) == 0 { 1141 t.Errorf("Should have written output on failure") 1142 t.FailNow() 1143 } 1144 } 1145 1146 func TestAssertNone(t *testing.T) { 1147 err := safeExec(func() { 1148 New(nil).None([]int{1, 2, 3}, func(v interface{}) bool { return v.(int) > 3 }) // should be ok 1149 }) 1150 if err != nil { 1151 t.Errorf("should not have produced a panic") 1152 t.FailNow() 1153 } 1154 1155 output := bytes.NewBuffer(nil) 1156 err = safeExec(func() { 1157 New(nil, OptOutput(output)).None([]int{1, 2, 3}, func(v interface{}) bool { return v.(int) > 2 }) // should not be ok 1158 }) 1159 if err == nil { 1160 t.Errorf("should have produced a panic") 1161 t.FailNow() 1162 } 1163 if len(output.String()) == 0 { 1164 t.Errorf("Should have written output on failure") 1165 t.FailNow() 1166 } 1167 } 1168 1169 func TestAssertNoneOfInt(t *testing.T) { 1170 err := safeExec(func() { 1171 New(nil).NoneOfInt([]int{1, 2, 3}, func(v int) bool { return v > 3 }) // should be ok 1172 }) 1173 if err != nil { 1174 t.Errorf("should not have produced a panic") 1175 t.FailNow() 1176 } 1177 1178 output := bytes.NewBuffer(nil) 1179 err = safeExec(func() { 1180 New(nil, OptOutput(output)).NoneOfInt([]int{1, 2, 3}, func(v int) bool { return v > 2 }) // should not be ok 1181 }) 1182 if err == nil { 1183 t.Errorf("should have produced a panic") 1184 t.FailNow() 1185 } 1186 if len(output.String()) == 0 { 1187 t.Errorf("Should have written output on failure") 1188 t.FailNow() 1189 } 1190 } 1191 1192 func TestAssertNoneOfFloat64(t *testing.T) { 1193 err := safeExec(func() { 1194 New(nil).NoneOfFloat64([]float64{1, 2, 3}, func(v float64) bool { return v > 3 }) // should be ok 1195 }) 1196 if err != nil { 1197 t.Errorf("should not have produced a panic") 1198 t.FailNow() 1199 } 1200 1201 output := bytes.NewBuffer(nil) 1202 err = safeExec(func() { 1203 New(nil, OptOutput(output)).NoneOfFloat64([]float64{1, 2, 3}, func(v float64) bool { return v > 2 }) // should not be ok 1204 }) 1205 if err == nil { 1206 t.Errorf("should have produced a panic") 1207 t.FailNow() 1208 } 1209 if len(output.String()) == 0 { 1210 t.Errorf("Should have written output on failure") 1211 t.FailNow() 1212 } 1213 } 1214 1215 func TestAssertNoneOfString(t *testing.T) { 1216 err := safeExec(func() { 1217 New(nil).NoneOfString([]string{"foo", "bar", "baz"}, func(v string) bool { return len(v) == 0 }) // should be ok 1218 }) 1219 if err != nil { 1220 t.Errorf("should not have produced a panic") 1221 t.FailNow() 1222 } 1223 1224 output := bytes.NewBuffer(nil) 1225 err = safeExec(func() { 1226 New(nil, OptOutput(output)).NoneOfString([]string{"foo", "bar", "baz"}, func(v string) bool { return v == "foo" }) // should not be ok 1227 }) 1228 if err == nil { 1229 t.Errorf("should have produced a panic") 1230 t.FailNow() 1231 } 1232 if len(output.String()) == 0 { 1233 t.Errorf("Should have written output on failure") 1234 t.FailNow() 1235 } 1236 } 1237 1238 func TestAssertNotPanic(t *testing.T) { 1239 err := safeExec(func() { 1240 New(nil).NotPanic(func() {}) 1241 }) 1242 if err != nil { 1243 t.Errorf("should not have produced a panic") 1244 t.FailNow() 1245 } 1246 1247 err = safeExec(func() { 1248 New(nil).NotPanic(func() { 1249 panic("i'm gonna panic") 1250 }) 1251 }) 1252 if err == nil { 1253 t.Errorf("should have produced a panic") 1254 t.FailNow() 1255 } 1256 } 1257 1258 // ----- 1259 // Optional / NotFatal 1260 // ----- 1261 1262 func TestAssertNonFatalNil(t *testing.T) { 1263 if !New(nil).NonFatal().Nil(nil) { // should be ok { 1264 t.Errorf("should not have failed") 1265 t.FailNow() 1266 } 1267 1268 output := bytes.NewBuffer(nil) 1269 if New(nil, OptOutput(output)).NonFatal().Nil("foo") { 1270 t.Errorf("should have failed") 1271 t.FailNow() 1272 } 1273 if len(output.String()) == 0 { 1274 t.Errorf("should have produced output") 1275 t.FailNow() 1276 } 1277 } 1278 1279 func TestAssertNonFatalNotNil(t *testing.T) { 1280 if !New(nil).NonFatal().NotNil("foo") { // should be ok { 1281 t.Errorf("should not have failed") 1282 t.FailNow() 1283 } 1284 1285 output := bytes.NewBuffer(nil) 1286 if New(nil, OptOutput(output)).NonFatal().NotNil(nil) { 1287 t.Errorf("should have failed") 1288 t.FailNow() 1289 } 1290 if len(output.String()) == 0 { 1291 t.Errorf("should have produced output") 1292 t.FailNow() 1293 } 1294 } 1295 1296 func TestAssertNonFatalLen(t *testing.T) { 1297 if !New(nil).NonFatal().Len("foo", 3) { // should be ok { 1298 t.Errorf("should not have failed") 1299 t.FailNow() 1300 } 1301 1302 output := bytes.NewBuffer(nil) 1303 if New(nil, OptOutput(output)).NonFatal().Len("foo", 4) { 1304 t.Errorf("should have failed") 1305 t.FailNow() 1306 } 1307 if len(output.String()) == 0 { 1308 t.Errorf("should have produced output") 1309 t.FailNow() 1310 } 1311 } 1312 1313 func TestAssertNonFatalEmpty(t *testing.T) { 1314 if !New(nil).NonFatal().Empty("") { // should be ok { 1315 t.Errorf("should not have failed") 1316 t.FailNow() 1317 } 1318 1319 output := bytes.NewBuffer(nil) 1320 if New(nil, OptOutput(output)).NonFatal().Empty("foo") { 1321 t.Errorf("should have failed") 1322 t.FailNow() 1323 } 1324 if len(output.String()) == 0 { 1325 t.Errorf("should have produced output") 1326 t.FailNow() 1327 } 1328 } 1329 1330 func TestAssertNonFatalNotEmpty(t *testing.T) { 1331 if !New(nil).NonFatal().NotEmpty("foo") { // should be ok { 1332 t.Errorf("should not have failed") 1333 t.FailNow() 1334 } 1335 1336 output := bytes.NewBuffer(nil) 1337 if New(nil, OptOutput(output)).NonFatal().NotEmpty("") { 1338 t.Errorf("should have failed") 1339 t.FailNow() 1340 } 1341 if len(output.String()) == 0 { 1342 t.Errorf("should have produced output") 1343 t.FailNow() 1344 } 1345 } 1346 1347 func TestAssertNonFatalEqual(t *testing.T) { 1348 if !New(nil).NonFatal().Equal("foo", "foo") { // should be ok { 1349 t.Errorf("should not have failed") 1350 t.FailNow() 1351 } 1352 1353 output := bytes.NewBuffer(nil) 1354 if New(nil, OptOutput(output)).NonFatal().Equal("foo", "bar") { 1355 t.Errorf("should have failed") 1356 t.FailNow() 1357 } 1358 if len(output.String()) == 0 { 1359 t.Errorf("should have produced output") 1360 t.FailNow() 1361 } 1362 } 1363 1364 func TestAssertNonFatalReferenceEqual(t *testing.T) { 1365 obj1 := "foo" 1366 obj2 := "foo" 1367 ref1 := &obj1 1368 ref2 := &obj1 1369 ref3 := &obj2 1370 1371 if !New(nil).NonFatal().ReferenceEqual(ref1, ref2) { // should be ok { 1372 t.Errorf("should not have failed") 1373 t.FailNow() 1374 } 1375 1376 output := bytes.NewBuffer(nil) 1377 if New(nil, OptOutput(output)).NonFatal().ReferenceEqual(ref1, ref3) { 1378 t.Errorf("should have failed") 1379 t.FailNow() 1380 } 1381 if len(output.String()) == 0 { 1382 t.Errorf("should have produced output") 1383 t.FailNow() 1384 } 1385 } 1386 1387 func TestAssertNonFatalNotEqual(t *testing.T) { 1388 if !New(nil).NonFatal().NotEqual("bar", "foo") { // should be ok { 1389 t.Errorf("should not have failed") 1390 t.FailNow() 1391 } 1392 1393 output := bytes.NewBuffer(nil) 1394 if New(nil, OptOutput(output)).NonFatal().NotEqual("foo", "foo") { 1395 t.Errorf("should have failed") 1396 t.FailNow() 1397 } 1398 if len(output.String()) == 0 { 1399 t.Errorf("should have produced output") 1400 t.FailNow() 1401 } 1402 } 1403 1404 func TestAssertNonFatalZero(t *testing.T) { 1405 if !New(nil).NonFatal().Zero(0) { // should be ok { 1406 t.Errorf("should not have failed") 1407 t.FailNow() 1408 } 1409 1410 output := bytes.NewBuffer(nil) 1411 if New(nil, OptOutput(output)).NonFatal().Zero(1) { 1412 t.Errorf("should have failed") 1413 t.FailNow() 1414 } 1415 if len(output.String()) == 0 { 1416 t.Errorf("should have produced output") 1417 t.FailNow() 1418 } 1419 } 1420 1421 func TestAssertNonFatalNotZero(t *testing.T) { 1422 if !New(nil).NonFatal().NotZero(1) { // should be ok { 1423 t.Errorf("should not have failed") 1424 t.FailNow() 1425 } 1426 1427 output := bytes.NewBuffer(nil) 1428 if New(nil, OptOutput(output)).NonFatal().NotZero(0) { 1429 t.Errorf("should have failed") 1430 t.FailNow() 1431 } 1432 if len(output.String()) == 0 { 1433 t.Errorf("should have produced output") 1434 t.FailNow() 1435 } 1436 } 1437 1438 func TestAssertNonFatalTrue(t *testing.T) { 1439 if !New(nil).NonFatal().True(true) { // should be ok { 1440 t.Errorf("should not have failed") 1441 t.FailNow() 1442 } 1443 1444 output := bytes.NewBuffer(nil) 1445 if New(nil, OptOutput(output)).NonFatal().True(1 == 0) { 1446 t.Errorf("should have failed") 1447 t.FailNow() 1448 } 1449 if len(output.String()) == 0 { 1450 t.Errorf("should have produced output") 1451 t.FailNow() 1452 } 1453 } 1454 1455 func TestAssertNonFatalFalse(t *testing.T) { 1456 if !New(nil).NonFatal().False(false) { // should be ok { 1457 t.Errorf("should not have failed") 1458 t.FailNow() 1459 } 1460 1461 output := bytes.NewBuffer(nil) 1462 if New(nil, OptOutput(output)).NonFatal().False(true) { 1463 t.Errorf("should have failed") 1464 t.FailNow() 1465 } 1466 if len(output.String()) == 0 { 1467 t.Errorf("should have produced output") 1468 t.FailNow() 1469 } 1470 } 1471 1472 func TestAssertNonFatalInDelta(t *testing.T) { 1473 if !New(nil).NonFatal().InDelta(1, 2, 1) { // should be ok { 1474 t.Errorf("should not have failed") 1475 t.FailNow() 1476 } 1477 1478 output := bytes.NewBuffer(nil) 1479 if New(nil, OptOutput(output)).NonFatal().InDelta(1, 3, 1) { 1480 t.Errorf("should have failed") 1481 t.FailNow() 1482 } 1483 if len(output.String()) == 0 { 1484 t.Errorf("should have produced output") 1485 t.FailNow() 1486 } 1487 } 1488 1489 func TestAssertNonFatalInTimeDelta(t *testing.T) { 1490 t1 := time.Date(2018, 04, 10, 12, 00, 00, 00, time.UTC) 1491 t2 := time.Date(2018, 04, 10, 12, 00, 01, 00, time.UTC) 1492 t3 := time.Date(2018, 04, 10, 12, 01, 00, 00, time.UTC) 1493 1494 if !New(nil).NonFatal().InTimeDelta(t1, t2, time.Second) { // should be ok { 1495 t.Errorf("should not have failed") 1496 t.FailNow() 1497 } 1498 1499 output := bytes.NewBuffer(nil) 1500 if New(nil, OptOutput(output)).NonFatal().InTimeDelta(t1, t3, time.Second) { 1501 t.Errorf("should have failed") 1502 t.FailNow() 1503 } 1504 if len(output.String()) == 0 { 1505 t.Errorf("should have produced output") 1506 t.FailNow() 1507 } 1508 } 1509 1510 func TestAssertNonFatalContains(t *testing.T) { 1511 if !New(nil).NonFatal().Contains("foo bar", "bar") { // should be ok { 1512 t.Errorf("should not have failed") 1513 t.FailNow() 1514 } 1515 1516 output := bytes.NewBuffer(nil) 1517 if New(nil, OptOutput(output)).NonFatal().Contains("foo bar", "something") { 1518 t.Errorf("should have failed") 1519 t.FailNow() 1520 } 1521 if len(output.String()) == 0 { 1522 t.Errorf("should have produced output") 1523 t.FailNow() 1524 } 1525 } 1526 1527 func TestAssertNonFatalNotContains(t *testing.T) { 1528 if !New(nil).NonFatal().NotContains("foo bar", "buzz") { // should be ok { 1529 t.Errorf("should not have failed") 1530 t.FailNow() 1531 } 1532 1533 output := bytes.NewBuffer(nil) 1534 if New(nil, OptOutput(output)).NonFatal().NotContains("foo bar", "bar") { 1535 t.Errorf("should have failed") 1536 t.FailNow() 1537 } 1538 if len(output.String()) == 0 { 1539 t.Errorf("should have produced output") 1540 t.FailNow() 1541 } 1542 } 1543 1544 func TestAssertNonFatalMatches(t *testing.T) { 1545 if !New(nil).NonFatal().Matches("(.*)", "bar") { // should be ok { 1546 t.Errorf("should not have failed") 1547 t.FailNow() 1548 } 1549 1550 output := bytes.NewBuffer(nil) 1551 if New(nil, OptOutput(output)).NonFatal().Matches("foo", "bar") { 1552 t.Errorf("should have failed") 1553 t.FailNow() 1554 } 1555 if len(output.String()) == 0 { 1556 t.Errorf("should have produced output") 1557 t.FailNow() 1558 } 1559 } 1560 1561 func TestAssertNonFatalNotMatches(t *testing.T) { 1562 if !New(nil).NonFatal().NotMatches("foo", "bar") { // should be ok { 1563 t.Errorf("should not have failed") 1564 t.FailNow() 1565 } 1566 1567 output := bytes.NewBuffer(nil) 1568 if New(nil, OptOutput(output)).NonFatal().NotMatches("(.*)", "bar") { 1569 t.Errorf("should have failed") 1570 t.FailNow() 1571 } 1572 if len(output.String()) == 0 { 1573 t.Errorf("should have produced output") 1574 t.FailNow() 1575 } 1576 } 1577 1578 func TestAssertNonFatalAny(t *testing.T) { 1579 t.Parallel() 1580 1581 if !New(nil).NonFatal().Any([]int{1, 2, 3}, func(v interface{}) bool { return v.(int) == 2 }) { // should be ok { 1582 t.Errorf("should not have failed") 1583 t.FailNow() 1584 } 1585 1586 output := bytes.NewBuffer(nil) 1587 if New(nil, OptOutput(output)).NonFatal().Any([]int{1, 2, 3}, func(v interface{}) bool { return v.(int) == 0 }) { 1588 t.Errorf("should have failed") 1589 t.FailNow() 1590 } 1591 if len(output.String()) == 0 { 1592 t.Errorf("should have produced output") 1593 t.FailNow() 1594 } 1595 } 1596 1597 func TestAssertNonFatalAll(t *testing.T) { 1598 t.Parallel() 1599 1600 if !New(nil).NonFatal().All([]int{1, 2, 3}, func(v interface{}) bool { return v.(int) > 0 }) { // should be ok { 1601 t.Errorf("should not have failed") 1602 t.FailNow() 1603 } 1604 1605 output := bytes.NewBuffer(nil) 1606 if New(nil, OptOutput(output)).NonFatal().All([]int{1, 2, 3}, func(v interface{}) bool { return v.(int) > 1 }) { 1607 t.Errorf("should have failed") 1608 t.FailNow() 1609 } 1610 if len(output.String()) == 0 { 1611 t.Errorf("should have produced output") 1612 t.FailNow() 1613 } 1614 } 1615 1616 func TestAssertNonFatalNone(t *testing.T) { 1617 t.Parallel() 1618 1619 if !New(nil).NonFatal().None([]int{1, 2, 3}, func(v interface{}) bool { return v.(int) > 3 }) { // should be ok { 1620 t.Errorf("should not have failed") 1621 t.FailNow() 1622 } 1623 1624 output := bytes.NewBuffer(nil) 1625 if New(nil, OptOutput(output)).NonFatal().None([]int{1, 2, 3}, func(v interface{}) bool { return v.(int) > 2 }) { 1626 t.Errorf("should have failed") 1627 t.FailNow() 1628 } 1629 if len(output.String()) == 0 { 1630 t.Errorf("should have produced output") 1631 t.FailNow() 1632 } 1633 } 1634 1635 func TestAssertNonFatalPanicEqual(t *testing.T) { 1636 t.Parallel() 1637 1638 if !New(nil).NonFatal().PanicEqual("this is only a test", func() { 1639 panic("this is only a test") 1640 }) { 1641 t.Errorf("should not have failed") 1642 t.FailNow() 1643 } 1644 1645 if New(nil).NonFatal().PanicEqual("this is only a test", func() {}) { 1646 t.Errorf("should have failed without a panic triggered") 1647 t.FailNow() 1648 } 1649 1650 if New(nil).NonFatal().PanicEqual("this is only a test", func() { 1651 panic("not what we want") 1652 }) { 1653 t.Errorf("should have failed on a wrong panic result") 1654 t.FailNow() 1655 } 1656 } 1657 1658 func TestAssertNonFatalNotPanic(t *testing.T) { 1659 t.Parallel() 1660 1661 if !New(nil).NonFatal().NotPanic(func() {}) { 1662 t.Errorf("should not have failed") 1663 t.FailNow() 1664 } 1665 1666 if New(nil).NonFatal().NotPanic(func() { 1667 panic("i'm gonna panic") 1668 }) { 1669 t.Errorf("should have produced a panic") 1670 t.FailNow() 1671 } 1672 }