|
| 1 | +package assert |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | +) |
| 7 | + |
| 8 | +func ExampleAssertableError_Equals_pass() { |
| 9 | + cases := []struct { |
| 10 | + id string |
| 11 | + actual error |
| 12 | + expected error |
| 13 | + }{ |
| 14 | + {"both are nil", nil, nil}, |
| 15 | + {"same type and message", errors.New("foo"), errors.New("foo")}, |
| 16 | + {"different struct types, same message", ErrorString("foo"), errors.New("foo")}, |
| 17 | + } |
| 18 | + |
| 19 | + for _, c := range cases { |
| 20 | + if For(t, c.id).ThatActualError(c.actual).Equals(c.expected).Passed() { |
| 21 | + fmt.Println("Passed: " + c.id) |
| 22 | + } |
| 23 | + } |
| 24 | + // Output: |
| 25 | + // Passed: both are nil |
| 26 | + // Passed: same type and message |
| 27 | + // Passed: different struct types, same message |
| 28 | +} |
| 29 | + |
| 30 | +func ExampleAssertableError_Equals_fail() { |
| 31 | + cases := []struct { |
| 32 | + id string |
| 33 | + actual error |
| 34 | + expected error |
| 35 | + }{ |
| 36 | + {"expected is nil while actual isn't", ErrorString("foo"), nil}, |
| 37 | + {"actual is nil while expected isn't", nil, ErrorString("foo")}, |
| 38 | + {"different messages", ErrorString("foo"), ErrorString("bar")}, |
| 39 | + } |
| 40 | + |
| 41 | + for _, c := range cases { |
| 42 | + if !mockTestContextToAssert(c.id).ThatActualError(c.actual).Equals(c.expected).Passed() { |
| 43 | + fmt.Println("Assertion failed successfully!") |
| 44 | + } |
| 45 | + } |
| 46 | + // Output: |
| 47 | + // file:3: [expected is nil while actual isn't] Actual error was not <nil>. |
| 48 | + // Actual: foo |
| 49 | + // Assertion failed successfully! |
| 50 | + // file:3: [actual is nil while expected isn't] Error mismatch. |
| 51 | + // Actual was <nil>. |
| 52 | + // Expected: foo |
| 53 | + // Assertion failed successfully! |
| 54 | + // file:3: [different messages] Error mismatch. |
| 55 | + // Actual: foo |
| 56 | + // Expected: bar |
| 57 | + // Assertion failed successfully! |
| 58 | +} |
| 59 | + |
| 60 | +func ExampleAssertableError_IsNil_pass() { |
| 61 | + if For(t).ThatActualError(nil).IsNil().Passed() { |
| 62 | + fmt.Println("Passed!") |
| 63 | + } |
| 64 | + // Output: Passed! |
| 65 | +} |
| 66 | + |
| 67 | +func ExampleAssertableError_IsNil_fail() { |
| 68 | + if !mockTestContextToAssert().ThatActualError(errors.New("foo")).IsNil().Passed() { |
| 69 | + fmt.Println("Assertion failed successfully!") |
| 70 | + } |
| 71 | + // Output: |
| 72 | + // file:3: Actual error was not <nil>. |
| 73 | + // Actual: foo |
| 74 | + // Assertion failed successfully! |
| 75 | +} |
| 76 | + |
| 77 | +func ExampleAssertableError_IsNotNil_pass() { |
| 78 | + if For(t).ThatActualError(errors.New("foo")).IsNotNil().Passed() { |
| 79 | + fmt.Println("Passed!") |
| 80 | + } |
| 81 | + // Output: Passed! |
| 82 | +} |
| 83 | + |
| 84 | +func ExampleAssertableError_IsNotNil_fail() { |
| 85 | + if !mockTestContextToAssert().ThatActualError(nil).IsNotNil().ThenDiffOnFail().Passed() { |
| 86 | + fmt.Println("Assertion failed successfully!") |
| 87 | + } |
| 88 | + // Output: |
| 89 | + // file:3: Actual error was <nil>. |
| 90 | + // Diff: |
| 91 | + // nil != &assert.anyOtherValue{} |
| 92 | + // Assertion failed successfully! |
| 93 | +} |
0 commit comments