github.com/blend/go-sdk@v1.20240719.1/assert/assert_test.go (about) 1 /* 2 3 Copyright (c) 2024 - 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 "fmt" 13 "strings" 14 "testing" 15 "time" 16 ) 17 18 func Test_Empty(t *testing.T) { 19 t.Parallel() 20 21 buf := bytes.NewBuffer(nil) 22 a := Empty(OptOutput(buf)) 23 24 if a.Output == nil { 25 t.Error("The empty assertion helper should have an output set") 26 t.Fail() 27 } 28 if a.NonFatal().True(false, "this should fail") { 29 t.Error("NonFatal true(false) didn't fail.") 30 } 31 if !a.NonFatal().True(true, "this should pass") { 32 t.Error("NonFatal true(true) didn't pass.") 33 } 34 35 if len(buf.String()) == 0 { 36 t.Error("We should have produced output.") 37 } 38 } 39 40 func Test_isZero(t *testing.T) { 41 t.Parallel() 42 43 zeroShort := int16(0) 44 if !isZero(zeroShort) { 45 t.Error("isZero failed") 46 } 47 48 notZeroShort := int16(3) 49 if isZero(notZeroShort) { 50 t.Error("isZero failed") 51 } 52 53 zero := 0 54 if !isZero(zero) { 55 t.Error("isZero failed") 56 } 57 notZero := 3 58 if isZero(notZero) { 59 t.Error("isZero failed") 60 } 61 62 zeroFloat64 := 0.0 63 if !isZero(zeroFloat64) { 64 t.Error("isZero failed") 65 } 66 notZeroFloat64 := 3.14 67 if isZero(notZeroFloat64) { 68 t.Error("isZero failed") 69 } 70 71 zeroFloat32 := float32(0.0) 72 if !isZero(zeroFloat32) { 73 t.Error("isZero failed") 74 } 75 notZeroFloat32 := float32(3.14) 76 if isZero(notZeroFloat32) { 77 t.Error("isZero failed") 78 } 79 } 80 81 func Test_getLength(t *testing.T) { 82 t.Parallel() 83 84 emptyString := "" 85 l := getLength(emptyString) 86 if l != 0 { 87 t.Errorf("getLength incorrect.") 88 } 89 90 notEmptyString := "foo" 91 l = getLength(notEmptyString) 92 if l != 3 { 93 t.Errorf("getLength incorrect.") 94 } 95 96 emptyArray := []int{} 97 l = getLength(emptyArray) 98 if l != 0 { 99 t.Errorf("getLength incorrect.") 100 } 101 102 notEmptyArray := []int{1, 2, 3} 103 l = getLength(notEmptyArray) 104 if l != 3 { 105 t.Errorf("getLength incorrect.") 106 } 107 108 emptyMap := map[string]int{} 109 l = getLength(emptyMap) 110 if l != 0 { 111 t.Errorf("getLength incorrect.") 112 } 113 114 notEmptyMap := map[string]int{"foo": 1, "bar": 2, "baz": 3} 115 l = getLength(notEmptyMap) 116 if l != 3 { 117 t.Errorf("getLength incorrect.") 118 } 119 } 120 121 func Test_chanShouldHaveNonzeroCapacity(t *testing.T) { 122 t.Parallel() 123 124 var didFail bool 125 var msg string 126 const typeExpected = "Should be a channel" 127 const expected = "Should not have capacity 0" 128 129 didFail, msg = chanShouldHaveNonzeroCapacity(nil) 130 if !didFail { 131 t.Error("untyped nil is not a channel and should fail") 132 } 133 if msg != typeExpected { 134 t.Errorf("unexpected error message. expected=%q, actual=%q", expected, msg) 135 } 136 137 didFail, msg = chanShouldHaveNonzeroCapacity(map[string]string{}) 138 if !didFail { 139 t.Error("map is not a channel and should fail") 140 } 141 if msg != typeExpected { 142 t.Errorf("unexpected error message. expected=%q, actual=%q", expected, msg) 143 } 144 145 var nilChan chan any 146 didFail, msg = chanShouldHaveNonzeroCapacity(nilChan) 147 if !didFail { 148 t.Error("nil has zero capacity and should fail") 149 } 150 if msg != expected { 151 t.Errorf("unexpected error message. expected=%q, actual=%q", expected, msg) 152 } 153 154 didFail, msg = chanShouldHaveNonzeroCapacity(make(chan any)) 155 if !didFail { 156 t.Error("channel has zero capacity and should fail") 157 } 158 if msg != expected { 159 t.Errorf("unexpected error message. expected=%q, actual=%q", expected, msg) 160 } 161 162 didFail, msg = chanShouldHaveNonzeroCapacity(make(chan string)) 163 if !didFail { 164 t.Error("channel has zero capacity and should fail") 165 } 166 if msg != expected { 167 t.Errorf("unexpected error message. expected=%q, actual=%q", expected, msg) 168 } 169 170 didFail, msg = chanShouldHaveNonzeroCapacity(make(chan any, 10)) 171 if didFail { 172 t.Error("channel has non-zero capacity and should not fail") 173 } 174 if msg != "" { 175 t.Errorf("error message should be an empty string, found %q", msg) 176 } 177 } 178 179 func TestAssertions_EmptyBufferedChannel(t *testing.T) { 180 t.Parallel() 181 182 buf := bytes.NewBuffer(nil) 183 a := Empty(OptOutput(buf)) 184 a.Optional = true // don't immediately panic for assertion failures 185 a.OutputFormat = OutputFormatUnitTest 186 if a.Output == nil { 187 t.Error("The empty assertion helper should have an output set") 188 } 189 190 var didPass bool 191 var msg string 192 var expected string 193 var ch chan any 194 195 ch = make(chan any) 196 didPass = a.EmptyBufferedChannel(ch) 197 if didPass { 198 t.Error("expected unbuffered channel to fail the assertion, but it succeeded") 199 } 200 msg = buf.String() 201 expected = "Should not have capacity 0" 202 if msg != expected { 203 t.Errorf("unexpected error message. expected=%q, actual=%q", expected, msg) 204 } 205 buf.Reset() 206 207 ch = make(chan any, 1) 208 ch <- "fill" 209 didPass = a.EmptyBufferedChannel(ch) 210 if didPass { 211 t.Error("expected buffered channel with length 1 to fail the assertion, but it succeeded") 212 } 213 msg = buf.String() 214 expected = "Should be empty\n\t\x1b[37;01mActual\x1b[0m: \t(chan interface {})(" 215 if !strings.HasPrefix(msg, expected) { 216 t.Errorf("unexpected error message. expected prefix=%q, actual message=%q", expected, msg) 217 } 218 buf.Reset() 219 220 ch = make(chan any, 1) 221 didPass = a.EmptyBufferedChannel(ch) 222 if !didPass { 223 t.Error("expected buffered channel with length 0 to pass the assertion, but it failed") 224 } 225 msg = buf.String() 226 expected = "" 227 if msg != expected { 228 t.Errorf("unexpected error message. expected=%q, actual=%q", expected, msg) 229 } 230 buf.Reset() 231 232 errChan := make(chan error, 1) 233 errChan <- fmt.Errorf("fill") 234 didPass = a.EmptyBufferedChannel(errChan) 235 if didPass { 236 t.Error("expected buffered channel with length 1 to fail the assertion, but it succeeded") 237 } 238 msg = buf.String() 239 expected = "Should be empty\n\t\x1b[37;01mActual\x1b[0m: \t(chan error)(" 240 if !strings.HasPrefix(msg, expected) { 241 t.Errorf("unexpected error message. expected prefix=%q, actual message=%q", expected, msg) 242 } 243 buf.Reset() 244 245 errChan = make(chan error, 1) 246 didPass = a.EmptyBufferedChannel(errChan) 247 if !didPass { 248 t.Error("expected buffered channel with length 0 to pass the assertion, but it failed") 249 } 250 msg = buf.String() 251 expected = "" 252 if msg != expected { 253 t.Errorf("unexpected error message. expected=%q, actual=%q", expected, msg) 254 } 255 buf.Reset() 256 } 257 258 type myNestedStruct struct { 259 ID int 260 Name string 261 } 262 263 type myTestStruct struct { 264 ID int 265 Name string 266 SingleValue float32 267 DoubleValue float64 268 Timestamp time.Time 269 Struct myNestedStruct 270 271 IDPtr *int 272 NamePptr *string 273 StructPtr *myNestedStruct 274 275 Slice []myNestedStruct 276 SlicePtr *[]myNestedStruct 277 } 278 279 func createTestStruct() myTestStruct { 280 testInt := 1 281 testName := "test struct" 282 283 nestedA := myNestedStruct{1, "A"} 284 nestedB := myNestedStruct{1, "B"} 285 nestedC := myNestedStruct{1, "C"} 286 287 testStruct := myTestStruct{ 288 ID: testInt, 289 Name: testName, 290 SingleValue: float32(3.14), 291 DoubleValue: 6.28, 292 Timestamp: time.Now(), 293 Struct: nestedA, 294 IDPtr: &testInt, 295 NamePptr: &testName, 296 StructPtr: &nestedB, 297 Slice: []myNestedStruct{nestedA, nestedB, nestedC}, 298 } 299 300 testStruct.SlicePtr = &testStruct.Slice 301 return testStruct 302 } 303 304 func Test_shouldBeEqual_nestedStructsAreEqual(t *testing.T) { 305 t.Parallel() 306 307 testStructA := createTestStruct() 308 testStructB := createTestStruct() 309 testStructB.Name = "not test struct" 310 311 if didFail, _ := shouldBeEqual(testStructA, testStructA); didFail { 312 t.Error("shouldBeEqual Failed.") 313 t.FailNow() 314 } 315 316 if didFail, _ := shouldBeEqual(testStructA, testStructB); !didFail { 317 t.Error("shouldBeEqual Failed.") 318 t.FailNow() 319 } 320 } 321 322 func Test_shouldBeEqual(t *testing.T) { 323 t.Parallel() 324 325 byteA := byte('a') 326 byteB := byte('b') 327 328 if didFail, _ := shouldBeEqual(byteA, byteA); didFail { 329 t.Error("shouldBeEqual Failed.") 330 t.FailNow() 331 } 332 if didFail, _ := shouldBeEqual(byteA, byteB); !didFail { 333 t.Error("shouldBeEqual Failed.") 334 t.FailNow() 335 } 336 337 stringA := "test" 338 stringB := "not test" 339 340 if didFail, _ := shouldBeEqual(stringA, stringA); didFail { 341 t.Error("shouldBeEqual Equal Failed.") 342 t.FailNow() 343 } 344 if didFail, _ := shouldBeEqual(stringA, stringB); !didFail { 345 t.Error("shouldBeEqual Failed.") 346 t.FailNow() 347 } 348 349 intA := 1 350 intB := 2 351 352 if didFail, _ := shouldBeEqual(intA, intA); didFail { 353 t.Error("shouldBeEqual Equal Failed.") 354 t.FailNow() 355 } 356 if didFail, _ := shouldBeEqual(intA, intB); !didFail { 357 t.Error("shouldBeEqual Failed.") 358 t.FailNow() 359 } 360 361 float32A := float32(3.14) 362 float32B := float32(6.28) 363 364 if didFail, _ := shouldBeEqual(float32A, float32A); didFail { 365 t.Error("shouldBeEqual Equal Failed.") 366 t.FailNow() 367 } 368 if didFail, _ := shouldBeEqual(float32A, float32B); !didFail { 369 t.Error("shouldBeEqual Failed.") 370 t.FailNow() 371 } 372 373 floatA := 3.14 374 floatB := 6.28 375 376 if didFail, _ := shouldBeEqual(floatA, floatA); didFail { 377 t.Error("shouldBeEqual Equal Failed.") 378 t.FailNow() 379 } 380 if didFail, _ := shouldBeEqual(floatA, floatB); !didFail { 381 t.Error("shouldBeEqual Failed.") 382 t.FailNow() 383 } 384 } 385 386 func makesThings(shouldReturnNil bool) *myTestStruct { 387 if !shouldReturnNil { 388 return &myTestStruct{} 389 } 390 return nil 391 } 392 393 func Test_shouldBeNil(t *testing.T) { 394 t.Parallel() 395 396 assertsToNil := makesThings(true) 397 assertsToNotNil := makesThings(false) 398 399 didFail, didFailErrMsg := shouldBeNil(assertsToNil) 400 if didFail { 401 t.Error(didFailErrMsg) 402 t.FailNow() 403 } 404 405 didFail, _ = shouldBeNil(assertsToNotNil) 406 if !didFail { 407 t.Error("shouldBeNil returned did_fail as `true` for a not nil object") 408 t.FailNow() 409 } 410 } 411 412 func Test_shouldNotBeNil(t *testing.T) { 413 t.Parallel() 414 415 assertsToNil := makesThings(true) 416 assertsToNotNil := makesThings(false) 417 418 didFail, didFailErrMsg := shouldNotBeNil(assertsToNotNil) 419 if didFail { 420 t.Error(didFailErrMsg) 421 t.FailNow() 422 } 423 424 didFail, _ = shouldNotBeNil(assertsToNil) 425 if !didFail { 426 t.Error("shouldNotBeNil returned did_fail as `true` for a not nil object") 427 t.FailNow() 428 } 429 } 430 431 func Test_shouldContain(t *testing.T) { 432 t.Parallel() 433 434 shouldNotHaveFailed, _ := shouldContain("this is a test", "is a") 435 if shouldNotHaveFailed { 436 t.Errorf("shouldConatain failed.") 437 t.FailNow() 438 } 439 440 shouldHaveFailed, _ := shouldContain("this is a test", "beer") 441 if !shouldHaveFailed { 442 t.Errorf("shouldConatain failed.") 443 t.FailNow() 444 } 445 } 446 447 type anyTestObj struct { 448 ID int 449 Name string 450 } 451 452 func Test_shouldAny(t *testing.T) { 453 t.Parallel() 454 455 testObjs := []anyTestObj{{1, "Test"}, {2, "Test2"}, {3, "Foo"}} 456 457 didFail, _ := shouldAny(testObjs, func(obj interface{}) bool { 458 if typed, didType := obj.(anyTestObj); didType { 459 return strings.HasPrefix(typed.Name, "Foo") 460 } 461 return false 462 }) 463 if didFail { 464 t.Errorf("shouldAny failed.") 465 t.FailNow() 466 } 467 468 didFail, _ = shouldAny(testObjs, func(obj interface{}) bool { 469 if typed, didType := obj.(anyTestObj); didType { 470 return strings.HasPrefix(typed.Name, "Bar") 471 } 472 return false 473 }) 474 if !didFail { 475 t.Errorf("shouldAny should have failed.") 476 t.FailNow() 477 } 478 479 didFail, _ = shouldAny(anyTestObj{1, "test"}, func(obj interface{}) bool { 480 return true 481 }) 482 if !didFail { 483 t.Errorf("shouldAny should have failed on non-slice target.") 484 t.FailNow() 485 } 486 } 487 488 func Test_shouldAll(t *testing.T) { 489 t.Parallel() 490 491 testObjs := []anyTestObj{{1, "Test"}, {2, "Test2"}, {3, "Foo"}} 492 493 didFail, _ := shouldAll(testObjs, func(obj interface{}) bool { 494 if typed, didType := obj.(anyTestObj); didType { 495 return typed.ID > 0 496 } 497 return false 498 }) 499 if didFail { 500 t.Errorf("shouldAll shouldnt have failed.") 501 t.FailNow() 502 } 503 504 didFail, _ = shouldAll(testObjs, func(obj interface{}) bool { 505 if typed, didType := obj.(anyTestObj); didType { 506 return strings.HasPrefix(typed.Name, "Test") 507 } 508 return false 509 }) 510 if !didFail { 511 t.Errorf("shouldAll should have failed.") 512 t.FailNow() 513 } 514 515 didFail, _ = shouldAll(anyTestObj{1, "test"}, func(obj interface{}) bool { 516 return true 517 }) 518 if !didFail { 519 t.Errorf("shouldAll should have failed on non-slice target.") 520 t.FailNow() 521 } 522 } 523 524 func Test_shouldNone(t *testing.T) { 525 t.Parallel() 526 527 testObjs := []anyTestObj{{1, "Test"}, {2, "Test2"}, {3, "Foo"}} 528 529 didFail, _ := shouldNone(testObjs, func(obj interface{}) bool { 530 if typed, didType := obj.(anyTestObj); didType { 531 return typed.ID > 4 532 } 533 return false 534 }) 535 if didFail { 536 t.Errorf("shouldAll shouldnt have failed.") 537 t.FailNow() 538 } 539 540 didFail, _ = shouldNone(testObjs, func(obj interface{}) bool { 541 if typed, didType := obj.(anyTestObj); didType { 542 return typed.ID > 0 543 } 544 return false 545 }) 546 if !didFail { 547 t.Errorf("shouldNone should have failed.") 548 t.FailNow() 549 } 550 } 551 552 func Test_shouldBeInTimeDelta(t *testing.T) { 553 t.Parallel() 554 555 value1 := time.Date(2016, 1, 29, 9, 0, 0, 0, time.UTC) 556 value2 := time.Date(2016, 1, 29, 9, 0, 0, 1, time.UTC) 557 value3 := time.Date(2016, 1, 29, 8, 0, 0, 0, time.UTC) 558 value4 := time.Date(2015, 1, 29, 9, 0, 0, 0, time.UTC) 559 560 didFail, _ := shouldBeInTimeDelta(value1, value2, 1*time.Minute) 561 if didFail { 562 t.Errorf("shouldBeInTimeDelta shouldnt have failed.") 563 t.FailNow() 564 } 565 566 didFail, _ = shouldBeInTimeDelta(value1, value3, 1*time.Minute) 567 if !didFail { 568 t.Errorf("shouldBeInTimeDelta should have failed.") 569 t.FailNow() 570 } 571 572 didFail, _ = shouldBeInTimeDelta(value1, value4, 1*time.Minute) 573 if !didFail { 574 t.Errorf("shouldBeInTimeDelta should have failed.") 575 t.FailNow() 576 } 577 } 578 579 func Test_New(t *testing.T) { 580 t.Parallel() 581 582 buffer := new(bytes.Buffer) 583 a := New(t, 584 OptOutput(buffer), 585 ) 586 587 if a.T == nil { 588 t.Errorf("should pass t to the assertion helper") 589 t.Fail() 590 } 591 if a.Output == nil { 592 t.Errorf("should run provided options") 593 t.Fail() 594 } 595 if a.OutputFormat != OutputFormatFromEnv() { 596 t.Errorf("should set the correct output format") 597 t.Fail() 598 } 599 if a.Context == nil { 600 t.Errorf("should set the context") 601 t.Fail() 602 } 603 if GetTestName(a.Context) != t.Name() { 604 t.Errorf("should set the context test name") 605 t.Fail() 606 } 607 if GetContextID(a.Context) == "" { 608 t.Errorf("should set the context id") 609 t.Fail() 610 } 611 } 612 613 func Test_Assert_NotFatal(t *testing.T) { 614 t.Parallel() 615 616 buf := bytes.NewBuffer(nil) 617 a := New(t, OptOutput(buf)) 618 nf := a.NonFatal() 619 if nf.T == nil { 620 t.Errorf("should set t") 621 t.FailNow() 622 } 623 if nf.Output == nil { 624 t.Errorf("should set output") 625 t.FailNow() 626 } 627 } 628 629 func TestAssertNil(t *testing.T) { 630 err := safeExec(func() { 631 New(nil).Nil(nil) // should be ok 632 }) 633 if err != nil { 634 t.Errorf("should not have produced a panic") 635 t.FailNow() 636 } 637 638 output := bytes.NewBuffer(nil) 639 err = safeExec(func() { 640 New(nil, OptOutput(output)).Nil("foo") 641 }) 642 if err == nil { 643 t.Errorf("should have produced a panic") 644 t.FailNow() 645 } 646 if len(output.String()) == 0 { 647 t.Errorf("Should have written output on failure") 648 t.FailNow() 649 } 650 } 651 652 func TestAssertNotNil(t *testing.T) { 653 err := safeExec(func() { 654 New(nil).NotNil("foo") // should be ok 655 }) 656 if err != nil { 657 t.Errorf("should not have produced a panic") 658 t.FailNow() 659 } 660 661 output := bytes.NewBuffer(nil) 662 err = safeExec(func() { 663 New(nil, OptOutput(output)).NotNil(nil) 664 }) 665 if err == nil { 666 t.Errorf("should have produced a panic") 667 t.FailNow() 668 } 669 if len(output.String()) == 0 { 670 t.Errorf("Should have written output on failure") 671 t.FailNow() 672 } 673 } 674 675 func TestAssertLen(t *testing.T) { 676 err := safeExec(func() { 677 New(nil).Len("foo", 3) // should be ok 678 }) 679 if err != nil { 680 t.Errorf("should not have produced a panic") 681 t.FailNow() 682 } 683 684 output := bytes.NewBuffer(nil) 685 err = safeExec(func() { 686 New(nil, OptOutput(output)).Len([]string{}, 3) 687 }) 688 if err == nil { 689 t.Errorf("should have produced a panic") 690 t.FailNow() 691 } 692 if len(output.String()) == 0 { 693 t.Errorf("Should have written output on failure") 694 t.FailNow() 695 } 696 } 697 698 func TestAssertEmpty(t *testing.T) { 699 err := safeExec(func() { 700 New(nil).Empty("") // should be ok 701 }) 702 if err != nil { 703 t.Errorf("should not have produced a panic") 704 t.FailNow() 705 } 706 707 output := bytes.NewBuffer(nil) 708 err = safeExec(func() { 709 New(nil, OptOutput(output)).Empty("foo") 710 }) 711 if err == nil { 712 t.Errorf("should have produced a panic") 713 t.FailNow() 714 } 715 if len(output.String()) == 0 { 716 t.Errorf("Should have written output on failure") 717 t.FailNow() 718 } 719 } 720 721 func TestAssertNotEmpty(t *testing.T) { 722 err := safeExec(func() { 723 New(nil).NotEmpty("foo") // should be ok 724 }) 725 if err != nil { 726 t.Errorf("should not have produced a panic") 727 t.FailNow() 728 } 729 730 output := bytes.NewBuffer(nil) 731 err = safeExec(func() { 732 New(nil, OptOutput(output)).NotEmpty("") 733 }) 734 if err == nil { 735 t.Errorf("should have produced a panic") 736 t.FailNow() 737 } 738 if len(output.String()) == 0 { 739 t.Errorf("Should have written output on failure") 740 t.FailNow() 741 } 742 } 743 744 func TestAssertEqual(t *testing.T) { 745 err := safeExec(func() { 746 New(nil).Equal("foo", "foo") // should be ok 747 }) 748 if err != nil { 749 t.Errorf("should not have produced a panic") 750 t.FailNow() 751 } 752 753 output := bytes.NewBuffer(nil) 754 err = safeExec(func() { 755 New(nil, OptOutput(output)).Equal("foo", "bar") 756 }) 757 if err == nil { 758 t.Errorf("should have produced a panic") 759 t.FailNow() 760 } 761 if len(output.String()) == 0 { 762 t.Errorf("Should have written output on failure") 763 t.FailNow() 764 } 765 } 766 767 func TestAssertReferenceEqual(t *testing.T) { 768 obj1 := "foo" 769 obj2 := "foo" 770 ref1 := &obj1 771 ref2 := &obj1 772 ref3 := &obj2 773 774 err := safeExec(func() { 775 New(nil).ReferenceEqual(ref1, ref2) // should be ok 776 }) 777 if err != nil { 778 t.Errorf("should not have produced a panic") 779 t.FailNow() 780 } 781 782 output := bytes.NewBuffer(nil) 783 err = safeExec(func() { 784 New(nil, OptOutput(output)).ReferenceEqual(ref1, ref3) 785 }) 786 if err == nil { 787 t.Errorf("should have produced a panic") 788 t.FailNow() 789 } 790 if len(output.String()) == 0 { 791 t.Errorf("Should have written output on failure") 792 t.FailNow() 793 } 794 } 795 796 func TestAssertNotEqual(t *testing.T) { 797 err := safeExec(func() { 798 New(nil).NotEqual("foo", "bar") // should be ok 799 }) 800 if err != nil { 801 t.Errorf("should not have produced a panic") 802 t.FailNow() 803 } 804 805 output := bytes.NewBuffer(nil) 806 err = safeExec(func() { 807 New(nil, OptOutput(output)).NotEqual("foo", "foo") 808 }) 809 if err == nil { 810 t.Errorf("should have produced a panic") 811 t.FailNow() 812 } 813 if len(output.String()) == 0 { 814 t.Errorf("Should have written output on failure") 815 t.FailNow() 816 } 817 } 818 819 func TestAssertZero(t *testing.T) { 820 err := safeExec(func() { 821 New(nil).Zero(0) // should be ok 822 }) 823 if err != nil { 824 t.Errorf("should not have produced a panic") 825 t.FailNow() 826 } 827 828 output := bytes.NewBuffer(nil) 829 err = safeExec(func() { 830 New(nil, OptOutput(output)).Zero(1) 831 }) 832 if err == nil { 833 t.Errorf("should have produced a panic") 834 t.FailNow() 835 } 836 if len(output.String()) == 0 { 837 t.Errorf("Should have written output on failure") 838 t.FailNow() 839 } 840 } 841 842 func TestAssertNotZero(t *testing.T) { 843 err := safeExec(func() { 844 New(nil).NotZero(1) // should be ok 845 }) 846 if err != nil { 847 t.Errorf("should not have produced a panic") 848 t.FailNow() 849 } 850 851 output := bytes.NewBuffer(nil) 852 err = safeExec(func() { 853 New(nil, OptOutput(output)).NotZero(0) 854 }) 855 if err == nil { 856 t.Errorf("should have produced a panic") 857 t.FailNow() 858 } 859 if len(output.String()) == 0 { 860 t.Errorf("Should have written output on failure") 861 t.FailNow() 862 } 863 } 864 865 func TestAssertTrue(t *testing.T) { 866 err := safeExec(func() { 867 New(nil).True(true) // should be ok 868 }) 869 if err != nil { 870 t.Errorf("should not have produced a panic") 871 t.FailNow() 872 } 873 874 output := bytes.NewBuffer(nil) 875 err = safeExec(func() { 876 New(nil, OptOutput(output)).True(1 == 0) 877 }) 878 if err == nil { 879 t.Errorf("should have produced a panic") 880 t.FailNow() 881 } 882 if len(output.String()) == 0 { 883 t.Errorf("Should have written output on failure") 884 t.FailNow() 885 } 886 } 887 888 func TestAssertFalse(t *testing.T) { 889 err := safeExec(func() { 890 New(nil).False(false) // should be ok 891 }) 892 if err != nil { 893 t.Errorf("should not have produced a panic") 894 t.FailNow() 895 } 896 897 output := bytes.NewBuffer(nil) 898 err = safeExec(func() { 899 New(nil, OptOutput(output)).False(true) 900 }) 901 if err == nil { 902 t.Errorf("should have produced a panic") 903 t.FailNow() 904 } 905 if len(output.String()) == 0 { 906 t.Errorf("Should have written output on failure") 907 t.FailNow() 908 } 909 } 910 911 func TestAssertInDelta(t *testing.T) { 912 err := safeExec(func() { 913 New(nil).InDelta(1, 2, 1) // should be ok 914 New(nil).InDelta(1, 1.5, 1) // should be ok 915 }) 916 if err != nil { 917 t.Errorf("should not have produced a panic") 918 t.FailNow() 919 } 920 921 output := bytes.NewBuffer(nil) 922 err = safeExec(func() { 923 New(nil, OptOutput(output)).InDelta(1, 3, 1) 924 }) 925 if err == nil { 926 t.Errorf("should have produced a panic") 927 t.FailNow() 928 } 929 if len(output.String()) == 0 { 930 t.Errorf("Should have written output on failure") 931 t.FailNow() 932 } 933 } 934 935 func TestAssertInTimeDelta(t *testing.T) { 936 t1 := time.Date(2018, 04, 10, 12, 00, 00, 00, time.UTC) 937 t2 := time.Date(2018, 04, 10, 12, 00, 01, 00, time.UTC) 938 t3 := time.Date(2018, 04, 10, 12, 01, 00, 00, time.UTC) 939 940 err := safeExec(func() { 941 New(nil).InTimeDelta(t1, t2, time.Second) // 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)).InTimeDelta(t1, t3, time.Second) 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 TestAssertContains(t *testing.T) { 963 err := safeExec(func() { 964 New(nil).Contains("foo bar", "foo") // 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)).Contains("foo bar", "baz") 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 TestAssertNotContains(t *testing.T) { 986 err := safeExec(func() { 987 New(nil).NotContains("foo bar", "buzz") // 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)).NotContains("foo bar", "foo") 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 TestAssertHasPrefix(t *testing.T) { 1009 err := safeExec(func() { 1010 New(nil).HasPrefix("foo bar", "foo") // 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)).HasPrefix("foo bar", "baz") 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 TestAssertNotHasPrefix(t *testing.T) { 1032 err := safeExec(func() { 1033 New(nil).NotHasPrefix("foo bar", "buzz") // 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)).NotHasPrefix("foo bar", "foo") 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 TestAssertHasSuffix(t *testing.T) { 1055 err := safeExec(func() { 1056 New(nil).HasSuffix("foo bar", "bar") // 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)).HasSuffix("foo bar", "baz") 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 TestAssertNotHasSuffix(t *testing.T) { 1078 err := safeExec(func() { 1079 New(nil).NotHasSuffix("foo bar", "buzz") // 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)).NotHasSuffix("foo bar", "bar") 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 TestAssertAny(t *testing.T) { 1101 err := safeExec(func() { 1102 New(nil).Any([]int{1, 2, 3}, func(v interface{}) bool { return v.(int) == 1 }) // 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)).Any([]int{1, 2, 3}, func(v interface{}) bool { return v.(int) == 0 }) // 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 TestAssertAnyOfInt(t *testing.T) { 1124 err := safeExec(func() { 1125 New(nil).AnyOfInt([]int{1, 2, 3}, func(v int) bool { return v == 1 }) // 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)).AnyOfInt([]int{1, 2, 3}, func(v int) bool { return v == 0 }) // 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 TestAssertAnyOfFloat64(t *testing.T) { 1147 err := safeExec(func() { 1148 New(nil).AnyOfFloat64([]float64{1, 2, 3}, func(v float64) bool { return v == 1 }) // 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)).AnyOfFloat64([]float64{1, 2, 3}, func(v float64) bool { return v == 0 }) // 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 TestAssertAnyOfString(t *testing.T) { 1170 err := safeExec(func() { 1171 New(nil).AnyOfString([]string{"foo", "bar", "baz"}, func(v string) bool { return v == "foo" }) // 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)).AnyOfString([]string{"foo", "bar", "baz"}, func(v string) bool { return v == "buzz" }) // 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 TestAssertAll(t *testing.T) { 1193 err := safeExec(func() { 1194 New(nil).All([]int{1, 2, 3}, func(v interface{}) bool { return v.(int) > 0 }) // 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)).All([]int{1, 2, 3}, func(v interface{}) bool { return v.(int) > 1 }) // 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 TestAssertAllOfInt(t *testing.T) { 1216 err := safeExec(func() { 1217 New(nil).AllOfInt([]int{1, 2, 3}, func(v int) bool { return 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)).AllOfInt([]int{1, 2, 3}, func(v int) bool { return v > 1 }) // 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 TestAssertAllOfFloat64(t *testing.T) { 1239 err := safeExec(func() { 1240 New(nil).AllOfFloat64([]float64{1, 2, 3}, func(v float64) bool { return v > 0 }) // should be ok 1241 }) 1242 if err != nil { 1243 t.Errorf("should not have produced a panic") 1244 t.FailNow() 1245 } 1246 1247 output := bytes.NewBuffer(nil) 1248 err = safeExec(func() { 1249 New(nil, OptOutput(output)).AllOfFloat64([]float64{1, 2, 3}, func(v float64) bool { return v > 1 }) // should not be ok 1250 }) 1251 if err == nil { 1252 t.Errorf("should have produced a panic") 1253 t.FailNow() 1254 } 1255 if len(output.String()) == 0 { 1256 t.Errorf("Should have written output on failure") 1257 t.FailNow() 1258 } 1259 } 1260 1261 func TestAssertAllOfString(t *testing.T) { 1262 err := safeExec(func() { 1263 New(nil).AllOfString([]string{"foo", "bar", "baz"}, func(v string) bool { return len(v) == 3 }) // should be ok 1264 }) 1265 if err != nil { 1266 t.Errorf("should not have produced a panic") 1267 t.FailNow() 1268 } 1269 1270 output := bytes.NewBuffer(nil) 1271 err = safeExec(func() { 1272 New(nil, OptOutput(output)).AllOfString([]string{"foo", "bar", "baz"}, func(v string) bool { return v == "foo" }) // should not be ok 1273 }) 1274 if err == nil { 1275 t.Errorf("should have produced a panic") 1276 t.FailNow() 1277 } 1278 if len(output.String()) == 0 { 1279 t.Errorf("Should have written output on failure") 1280 t.FailNow() 1281 } 1282 } 1283 1284 func TestAssertNone(t *testing.T) { 1285 err := safeExec(func() { 1286 New(nil).None([]int{1, 2, 3}, func(v interface{}) bool { return v.(int) > 3 }) // should be ok 1287 }) 1288 if err != nil { 1289 t.Errorf("should not have produced a panic") 1290 t.FailNow() 1291 } 1292 1293 output := bytes.NewBuffer(nil) 1294 err = safeExec(func() { 1295 New(nil, OptOutput(output)).None([]int{1, 2, 3}, func(v interface{}) bool { return v.(int) > 2 }) // should not be ok 1296 }) 1297 if err == nil { 1298 t.Errorf("should have produced a panic") 1299 t.FailNow() 1300 } 1301 if len(output.String()) == 0 { 1302 t.Errorf("Should have written output on failure") 1303 t.FailNow() 1304 } 1305 } 1306 1307 func TestAssertNoneOfInt(t *testing.T) { 1308 err := safeExec(func() { 1309 New(nil).NoneOfInt([]int{1, 2, 3}, func(v int) bool { return v > 3 }) // should be ok 1310 }) 1311 if err != nil { 1312 t.Errorf("should not have produced a panic") 1313 t.FailNow() 1314 } 1315 1316 output := bytes.NewBuffer(nil) 1317 err = safeExec(func() { 1318 New(nil, OptOutput(output)).NoneOfInt([]int{1, 2, 3}, func(v int) bool { return v > 2 }) // should not be ok 1319 }) 1320 if err == nil { 1321 t.Errorf("should have produced a panic") 1322 t.FailNow() 1323 } 1324 if len(output.String()) == 0 { 1325 t.Errorf("Should have written output on failure") 1326 t.FailNow() 1327 } 1328 } 1329 1330 func TestAssertNoneOfFloat64(t *testing.T) { 1331 err := safeExec(func() { 1332 New(nil).NoneOfFloat64([]float64{1, 2, 3}, func(v float64) bool { return v > 3 }) // should be ok 1333 }) 1334 if err != nil { 1335 t.Errorf("should not have produced a panic") 1336 t.FailNow() 1337 } 1338 1339 output := bytes.NewBuffer(nil) 1340 err = safeExec(func() { 1341 New(nil, OptOutput(output)).NoneOfFloat64([]float64{1, 2, 3}, func(v float64) bool { return v > 2 }) // should not be ok 1342 }) 1343 if err == nil { 1344 t.Errorf("should have produced a panic") 1345 t.FailNow() 1346 } 1347 if len(output.String()) == 0 { 1348 t.Errorf("Should have written output on failure") 1349 t.FailNow() 1350 } 1351 } 1352 1353 func TestAssertNoneOfString(t *testing.T) { 1354 err := safeExec(func() { 1355 New(nil).NoneOfString([]string{"foo", "bar", "baz"}, func(v string) bool { return len(v) == 0 }) // should be ok 1356 }) 1357 if err != nil { 1358 t.Errorf("should not have produced a panic") 1359 t.FailNow() 1360 } 1361 1362 output := bytes.NewBuffer(nil) 1363 err = safeExec(func() { 1364 New(nil, OptOutput(output)).NoneOfString([]string{"foo", "bar", "baz"}, func(v string) bool { return v == "foo" }) // should not be ok 1365 }) 1366 if err == nil { 1367 t.Errorf("should have produced a panic") 1368 t.FailNow() 1369 } 1370 if len(output.String()) == 0 { 1371 t.Errorf("Should have written output on failure") 1372 t.FailNow() 1373 } 1374 } 1375 1376 func TestAssertNotPanic(t *testing.T) { 1377 err := safeExec(func() { 1378 New(nil).NotPanic(func() {}) 1379 }) 1380 if err != nil { 1381 t.Errorf("should not have produced a panic") 1382 t.FailNow() 1383 } 1384 1385 err = safeExec(func() { 1386 New(nil).NotPanic(func() { 1387 panic("i'm gonna panic") 1388 }) 1389 }) 1390 if err == nil { 1391 t.Errorf("should have produced a panic") 1392 t.FailNow() 1393 } 1394 } 1395 1396 // ----- 1397 // Optional / NotFatal 1398 // ----- 1399 1400 func TestAssertNonFatalNil(t *testing.T) { 1401 if !New(nil).NonFatal().Nil(nil) { // should be ok { 1402 t.Errorf("should not have failed") 1403 t.FailNow() 1404 } 1405 1406 output := bytes.NewBuffer(nil) 1407 if New(nil, OptOutput(output)).NonFatal().Nil("foo") { 1408 t.Errorf("should have failed") 1409 t.FailNow() 1410 } 1411 if len(output.String()) == 0 { 1412 t.Errorf("should have produced output") 1413 t.FailNow() 1414 } 1415 } 1416 1417 func TestAssertNonFatalNotNil(t *testing.T) { 1418 if !New(nil).NonFatal().NotNil("foo") { // should be ok { 1419 t.Errorf("should not have failed") 1420 t.FailNow() 1421 } 1422 1423 output := bytes.NewBuffer(nil) 1424 if New(nil, OptOutput(output)).NonFatal().NotNil(nil) { 1425 t.Errorf("should have failed") 1426 t.FailNow() 1427 } 1428 if len(output.String()) == 0 { 1429 t.Errorf("should have produced output") 1430 t.FailNow() 1431 } 1432 } 1433 1434 func TestAssertNonFatalLen(t *testing.T) { 1435 if !New(nil).NonFatal().Len("foo", 3) { // should be ok { 1436 t.Errorf("should not have failed") 1437 t.FailNow() 1438 } 1439 1440 output := bytes.NewBuffer(nil) 1441 if New(nil, OptOutput(output)).NonFatal().Len("foo", 4) { 1442 t.Errorf("should have failed") 1443 t.FailNow() 1444 } 1445 if len(output.String()) == 0 { 1446 t.Errorf("should have produced output") 1447 t.FailNow() 1448 } 1449 } 1450 1451 func TestAssertNonFatalEmpty(t *testing.T) { 1452 if !New(nil).NonFatal().Empty("") { // should be ok { 1453 t.Errorf("should not have failed") 1454 t.FailNow() 1455 } 1456 1457 output := bytes.NewBuffer(nil) 1458 if New(nil, OptOutput(output)).NonFatal().Empty("foo") { 1459 t.Errorf("should have failed") 1460 t.FailNow() 1461 } 1462 if len(output.String()) == 0 { 1463 t.Errorf("should have produced output") 1464 t.FailNow() 1465 } 1466 } 1467 1468 func TestAssertNonFatalNotEmpty(t *testing.T) { 1469 if !New(nil).NonFatal().NotEmpty("foo") { // should be ok { 1470 t.Errorf("should not have failed") 1471 t.FailNow() 1472 } 1473 1474 output := bytes.NewBuffer(nil) 1475 if New(nil, OptOutput(output)).NonFatal().NotEmpty("") { 1476 t.Errorf("should have failed") 1477 t.FailNow() 1478 } 1479 if len(output.String()) == 0 { 1480 t.Errorf("should have produced output") 1481 t.FailNow() 1482 } 1483 } 1484 1485 func TestAssertNonFatalEqual(t *testing.T) { 1486 if !New(nil).NonFatal().Equal("foo", "foo") { // should be ok { 1487 t.Errorf("should not have failed") 1488 t.FailNow() 1489 } 1490 1491 output := bytes.NewBuffer(nil) 1492 if New(nil, OptOutput(output)).NonFatal().Equal("foo", "bar") { 1493 t.Errorf("should have failed") 1494 t.FailNow() 1495 } 1496 if len(output.String()) == 0 { 1497 t.Errorf("should have produced output") 1498 t.FailNow() 1499 } 1500 } 1501 1502 func TestAssertNonFatalReferenceEqual(t *testing.T) { 1503 obj1 := "foo" 1504 obj2 := "foo" 1505 ref1 := &obj1 1506 ref2 := &obj1 1507 ref3 := &obj2 1508 1509 if !New(nil).NonFatal().ReferenceEqual(ref1, ref2) { // should be ok { 1510 t.Errorf("should not have failed") 1511 t.FailNow() 1512 } 1513 1514 output := bytes.NewBuffer(nil) 1515 if New(nil, OptOutput(output)).NonFatal().ReferenceEqual(ref1, ref3) { 1516 t.Errorf("should have failed") 1517 t.FailNow() 1518 } 1519 if len(output.String()) == 0 { 1520 t.Errorf("should have produced output") 1521 t.FailNow() 1522 } 1523 } 1524 1525 func TestAssertNonFatalNotEqual(t *testing.T) { 1526 if !New(nil).NonFatal().NotEqual("bar", "foo") { // should be ok { 1527 t.Errorf("should not have failed") 1528 t.FailNow() 1529 } 1530 1531 output := bytes.NewBuffer(nil) 1532 if New(nil, OptOutput(output)).NonFatal().NotEqual("foo", "foo") { 1533 t.Errorf("should have failed") 1534 t.FailNow() 1535 } 1536 if len(output.String()) == 0 { 1537 t.Errorf("should have produced output") 1538 t.FailNow() 1539 } 1540 } 1541 1542 func TestAssertNonFatalZero(t *testing.T) { 1543 if !New(nil).NonFatal().Zero(0) { // should be ok { 1544 t.Errorf("should not have failed") 1545 t.FailNow() 1546 } 1547 1548 output := bytes.NewBuffer(nil) 1549 if New(nil, OptOutput(output)).NonFatal().Zero(1) { 1550 t.Errorf("should have failed") 1551 t.FailNow() 1552 } 1553 if len(output.String()) == 0 { 1554 t.Errorf("should have produced output") 1555 t.FailNow() 1556 } 1557 } 1558 1559 func TestAssertNonFatalNotZero(t *testing.T) { 1560 if !New(nil).NonFatal().NotZero(1) { // should be ok { 1561 t.Errorf("should not have failed") 1562 t.FailNow() 1563 } 1564 1565 output := bytes.NewBuffer(nil) 1566 if New(nil, OptOutput(output)).NonFatal().NotZero(0) { 1567 t.Errorf("should have failed") 1568 t.FailNow() 1569 } 1570 if len(output.String()) == 0 { 1571 t.Errorf("should have produced output") 1572 t.FailNow() 1573 } 1574 } 1575 1576 func TestAssertNonFatalTrue(t *testing.T) { 1577 if !New(nil).NonFatal().True(true) { // should be ok { 1578 t.Errorf("should not have failed") 1579 t.FailNow() 1580 } 1581 1582 output := bytes.NewBuffer(nil) 1583 if New(nil, OptOutput(output)).NonFatal().True(1 == 0) { 1584 t.Errorf("should have failed") 1585 t.FailNow() 1586 } 1587 if len(output.String()) == 0 { 1588 t.Errorf("should have produced output") 1589 t.FailNow() 1590 } 1591 } 1592 1593 func TestAssertNonFatalFalse(t *testing.T) { 1594 if !New(nil).NonFatal().False(false) { // should be ok { 1595 t.Errorf("should not have failed") 1596 t.FailNow() 1597 } 1598 1599 output := bytes.NewBuffer(nil) 1600 if New(nil, OptOutput(output)).NonFatal().False(true) { 1601 t.Errorf("should have failed") 1602 t.FailNow() 1603 } 1604 if len(output.String()) == 0 { 1605 t.Errorf("should have produced output") 1606 t.FailNow() 1607 } 1608 } 1609 1610 func TestAssertNonFatalInDelta(t *testing.T) { 1611 if !New(nil).NonFatal().InDelta(1, 2, 1) { // should be ok { 1612 t.Errorf("should not have failed") 1613 t.FailNow() 1614 } 1615 1616 output := bytes.NewBuffer(nil) 1617 if New(nil, OptOutput(output)).NonFatal().InDelta(1, 3, 1) { 1618 t.Errorf("should have failed") 1619 t.FailNow() 1620 } 1621 if len(output.String()) == 0 { 1622 t.Errorf("should have produced output") 1623 t.FailNow() 1624 } 1625 } 1626 1627 func TestAssertNonFatalInTimeDelta(t *testing.T) { 1628 t1 := time.Date(2018, 04, 10, 12, 00, 00, 00, time.UTC) 1629 t2 := time.Date(2018, 04, 10, 12, 00, 01, 00, time.UTC) 1630 t3 := time.Date(2018, 04, 10, 12, 01, 00, 00, time.UTC) 1631 1632 if !New(nil).NonFatal().InTimeDelta(t1, t2, time.Second) { // should be ok { 1633 t.Errorf("should not have failed") 1634 t.FailNow() 1635 } 1636 1637 output := bytes.NewBuffer(nil) 1638 if New(nil, OptOutput(output)).NonFatal().InTimeDelta(t1, t3, time.Second) { 1639 t.Errorf("should have failed") 1640 t.FailNow() 1641 } 1642 if len(output.String()) == 0 { 1643 t.Errorf("should have produced output") 1644 t.FailNow() 1645 } 1646 } 1647 1648 func TestAssertNonFatalContains(t *testing.T) { 1649 if !New(nil).NonFatal().Contains("foo bar", "bar") { // should be ok { 1650 t.Errorf("should not have failed") 1651 t.FailNow() 1652 } 1653 1654 output := bytes.NewBuffer(nil) 1655 if New(nil, OptOutput(output)).NonFatal().Contains("foo bar", "something") { 1656 t.Errorf("should have failed") 1657 t.FailNow() 1658 } 1659 if len(output.String()) == 0 { 1660 t.Errorf("should have produced output") 1661 t.FailNow() 1662 } 1663 } 1664 1665 func TestAssertNonFatalNotContains(t *testing.T) { 1666 if !New(nil).NonFatal().NotContains("foo bar", "buzz") { // should be ok { 1667 t.Errorf("should not have failed") 1668 t.FailNow() 1669 } 1670 1671 output := bytes.NewBuffer(nil) 1672 if New(nil, OptOutput(output)).NonFatal().NotContains("foo bar", "bar") { 1673 t.Errorf("should have failed") 1674 t.FailNow() 1675 } 1676 if len(output.String()) == 0 { 1677 t.Errorf("should have produced output") 1678 t.FailNow() 1679 } 1680 } 1681 1682 func TestAssertNonFatalMatches(t *testing.T) { 1683 if !New(nil).NonFatal().Matches("(.*)", "bar") { // should be ok { 1684 t.Errorf("should not have failed") 1685 t.FailNow() 1686 } 1687 1688 output := bytes.NewBuffer(nil) 1689 if New(nil, OptOutput(output)).NonFatal().Matches("foo", "bar") { 1690 t.Errorf("should have failed") 1691 t.FailNow() 1692 } 1693 if len(output.String()) == 0 { 1694 t.Errorf("should have produced output") 1695 t.FailNow() 1696 } 1697 } 1698 1699 func TestAssertNonFatalNotMatches(t *testing.T) { 1700 if !New(nil).NonFatal().NotMatches("foo", "bar") { // should be ok { 1701 t.Errorf("should not have failed") 1702 t.FailNow() 1703 } 1704 1705 output := bytes.NewBuffer(nil) 1706 if New(nil, OptOutput(output)).NonFatal().NotMatches("(.*)", "bar") { 1707 t.Errorf("should have failed") 1708 t.FailNow() 1709 } 1710 if len(output.String()) == 0 { 1711 t.Errorf("should have produced output") 1712 t.FailNow() 1713 } 1714 } 1715 1716 func TestAssertNonFatalAny(t *testing.T) { 1717 t.Parallel() 1718 1719 if !New(nil).NonFatal().Any([]int{1, 2, 3}, func(v interface{}) bool { return v.(int) == 2 }) { // should be ok { 1720 t.Errorf("should not have failed") 1721 t.FailNow() 1722 } 1723 1724 output := bytes.NewBuffer(nil) 1725 if New(nil, OptOutput(output)).NonFatal().Any([]int{1, 2, 3}, func(v interface{}) bool { return v.(int) == 0 }) { 1726 t.Errorf("should have failed") 1727 t.FailNow() 1728 } 1729 if len(output.String()) == 0 { 1730 t.Errorf("should have produced output") 1731 t.FailNow() 1732 } 1733 } 1734 1735 func TestAssertNonFatalAll(t *testing.T) { 1736 t.Parallel() 1737 1738 if !New(nil).NonFatal().All([]int{1, 2, 3}, func(v interface{}) bool { return v.(int) > 0 }) { // should be ok { 1739 t.Errorf("should not have failed") 1740 t.FailNow() 1741 } 1742 1743 output := bytes.NewBuffer(nil) 1744 if New(nil, OptOutput(output)).NonFatal().All([]int{1, 2, 3}, func(v interface{}) bool { return v.(int) > 1 }) { 1745 t.Errorf("should have failed") 1746 t.FailNow() 1747 } 1748 if len(output.String()) == 0 { 1749 t.Errorf("should have produced output") 1750 t.FailNow() 1751 } 1752 } 1753 1754 func TestAssertNonFatalNone(t *testing.T) { 1755 t.Parallel() 1756 1757 if !New(nil).NonFatal().None([]int{1, 2, 3}, func(v interface{}) bool { return v.(int) > 3 }) { // should be ok { 1758 t.Errorf("should not have failed") 1759 t.FailNow() 1760 } 1761 1762 output := bytes.NewBuffer(nil) 1763 if New(nil, OptOutput(output)).NonFatal().None([]int{1, 2, 3}, func(v interface{}) bool { return v.(int) > 2 }) { 1764 t.Errorf("should have failed") 1765 t.FailNow() 1766 } 1767 if len(output.String()) == 0 { 1768 t.Errorf("should have produced output") 1769 t.FailNow() 1770 } 1771 } 1772 1773 func TestAssertNonFatalPanicEqual(t *testing.T) { 1774 t.Parallel() 1775 1776 if !New(nil).NonFatal().PanicEqual("this is only a test", func() { 1777 panic("this is only a test") 1778 }) { 1779 t.Errorf("should not have failed") 1780 t.FailNow() 1781 } 1782 1783 if New(nil).NonFatal().PanicEqual("this is only a test", func() {}) { 1784 t.Errorf("should have failed without a panic triggered") 1785 t.FailNow() 1786 } 1787 1788 if New(nil).NonFatal().PanicEqual("this is only a test", func() { 1789 panic("not what we want") 1790 }) { 1791 t.Errorf("should have failed on a wrong panic result") 1792 t.FailNow() 1793 } 1794 } 1795 1796 func TestAssertNonFatalNotPanic(t *testing.T) { 1797 t.Parallel() 1798 1799 if !New(nil).NonFatal().NotPanic(func() {}) { 1800 t.Errorf("should not have failed") 1801 t.FailNow() 1802 } 1803 1804 if New(nil).NonFatal().NotPanic(func() { 1805 panic("i'm gonna panic") 1806 }) { 1807 t.Errorf("should have produced a panic") 1808 t.FailNow() 1809 } 1810 }