|
| 1 | +package assert |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/ghosind/go-assert/internal" |
| 7 | +) |
| 8 | + |
| 9 | +func TestElementContainsAndNotContains(t *testing.T) { |
| 10 | + mockT := new(testing.T) |
| 11 | + assert := New(mockT) |
| 12 | + |
| 13 | + testElementContainsAndNotContains(t, assert, []int{1}, 1, true) |
| 14 | + testElementContainsAndNotContains(t, assert, []int{1, 2, 3}, 3, true) |
| 15 | + testElementContainsAndNotContains(t, assert, []int{1, 2, 3}, 4, false) |
| 16 | + testElementContainsAndNotContains(t, assert, []int{}, 1, false) |
| 17 | + testElementContainsAndNotContains(t, assert, [1]int{1}, 1, true) |
| 18 | + testElementContainsAndNotContains(t, assert, [3]int{1, 2, 3}, 3, true) |
| 19 | + testElementContainsAndNotContains(t, assert, [3]int{1, 2, 3}, 4, false) |
| 20 | + testElementContainsAndNotContains(t, assert, [0]int{}, 1, false) |
| 21 | +} |
| 22 | + |
| 23 | +func testElementContainsAndNotContains( |
| 24 | + t *testing.T, |
| 25 | + assertion *Assertion, |
| 26 | + source, expect any, |
| 27 | + isContains bool, |
| 28 | +) { |
| 29 | + testContainsElement(t, assertion, source, expect, isContains) |
| 30 | + |
| 31 | + testNotContainsElement(t, assertion, source, expect, isContains) |
| 32 | + |
| 33 | + testContainsElementNow(t, assertion, source, expect, isContains) |
| 34 | + |
| 35 | + testNotContainsElementNow(t, assertion, source, expect, isContains) |
| 36 | +} |
| 37 | + |
| 38 | +func testContainsElement( |
| 39 | + t *testing.T, |
| 40 | + assertion *Assertion, |
| 41 | + source, expect any, |
| 42 | + isContains bool, |
| 43 | +) { |
| 44 | + err := assertion.ContainsElement(source, expect) |
| 45 | + if isContains && err != nil { |
| 46 | + t.Errorf("ContainsElement(\"%v\", \"%v\") = %v, want nil", source, expect, err) |
| 47 | + } else if !isContains && err == nil { |
| 48 | + t.Errorf("ContainsElement(\"%v\", \"%v\") = nil, want error", source, expect) |
| 49 | + } |
| 50 | + |
| 51 | + err = ContainsElement(assertion.T, source, expect) |
| 52 | + if isContains && err != nil { |
| 53 | + t.Errorf("ContainsElement(\"%v\", \"%v\") = %v, want nil", source, expect, err) |
| 54 | + } else if !isContains && err == nil { |
| 55 | + t.Errorf("ContainsElement(\"%v\", \"%v\") = nil, want error", source, expect) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func testNotContainsElement( |
| 60 | + t *testing.T, |
| 61 | + assertion *Assertion, |
| 62 | + source, expect any, |
| 63 | + isContains bool, |
| 64 | +) { |
| 65 | + err := assertion.NotContainsElement(source, expect) |
| 66 | + if isContains && err == nil { |
| 67 | + t.Errorf("NotContainsElement(\"%v\", \"%v\") = nil, want error", source, expect) |
| 68 | + } else if !isContains && err != nil { |
| 69 | + t.Errorf("NotContainsElement(\"%v\", \"%v\") = %v, want nil", source, expect, err) |
| 70 | + } |
| 71 | + |
| 72 | + err = NotContainsElement(assertion.T, source, expect) |
| 73 | + if isContains && err == nil { |
| 74 | + t.Errorf("NotContainsElement(\"%v\", \"%v\") = nil, want error", source, expect) |
| 75 | + } else if !isContains && err != nil { |
| 76 | + t.Errorf("NotContainsElement(\"%v\", \"%v\") = %v, want nil", source, expect, err) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func testContainsElementNow( |
| 81 | + t *testing.T, |
| 82 | + assertion *Assertion, |
| 83 | + source, expect any, |
| 84 | + isContains bool, |
| 85 | +) { |
| 86 | + isTerminated := internal.CheckTermination(func() { |
| 87 | + assertion.ContainsElementNow(source, expect) |
| 88 | + }) |
| 89 | + if isContains && isTerminated { |
| 90 | + t.Error("execution stopped, want do not stop") |
| 91 | + } else if !isContains && !isTerminated { |
| 92 | + t.Error("execution do not stopped, want stop") |
| 93 | + } |
| 94 | + |
| 95 | + isTerminated = internal.CheckTermination(func() { |
| 96 | + ContainsElementNow(assertion.T, source, expect) |
| 97 | + }) |
| 98 | + if isContains && isTerminated { |
| 99 | + t.Error("execution stopped, want do not stop") |
| 100 | + } else if !isContains && !isTerminated { |
| 101 | + t.Error("execution do not stopped, want stop") |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +func testNotContainsElementNow( |
| 106 | + t *testing.T, |
| 107 | + assertion *Assertion, |
| 108 | + source, expect any, |
| 109 | + isContains bool, |
| 110 | +) { |
| 111 | + isTerminated := internal.CheckTermination(func() { |
| 112 | + assertion.NotContainsElementNow(source, expect) |
| 113 | + }) |
| 114 | + if !isContains && isTerminated { |
| 115 | + t.Error("execution stopped, want do not stop") |
| 116 | + } else if isContains && !isTerminated { |
| 117 | + t.Error("execution do not stopped, want stop") |
| 118 | + } |
| 119 | + |
| 120 | + isTerminated = internal.CheckTermination(func() { |
| 121 | + NotContainsElementNow(assertion.T, source, expect) |
| 122 | + }) |
| 123 | + if !isContains && isTerminated { |
| 124 | + t.Error("execution stopped, want do not stop") |
| 125 | + } else if isContains && !isTerminated { |
| 126 | + t.Error("execution do not stopped, want stop") |
| 127 | + } |
| 128 | +} |
0 commit comments