|
| 1 | +package assert |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +// MapHasKey tests whether the map contains the specified key or not, it will fail if the map does |
| 9 | +// not contain the key, or the type of the key cannot assign to the type of the key of the map. |
| 10 | +// |
| 11 | +// assertion.MapHasKey(map[string]int{"a":1}, "a") // success |
| 12 | +// assertion.MapHasKey(map[string]int{"a":1}, "b") // fail |
| 13 | +// assertion.MapHasKey(map[string]int{"a":1}, 1) // fail |
| 14 | +func (a *Assertion) MapHasKey(m, key any, message ...any) error { |
| 15 | + a.Helper() |
| 16 | + |
| 17 | + return tryMapHasKey(a.T, false, m, key, message...) |
| 18 | +} |
| 19 | + |
| 20 | +// MapHasKeyNow tests whether the map contains the specified key or not, and it will terminate the |
| 21 | +// execution if the test fails. It will fail if the map does not contain the key, or the type of |
| 22 | +// the key cannot assign to the type of the key of the map. |
| 23 | +// |
| 24 | +// assertion.MapHasKeyNow(map[string]int{"a":1}, "a") // success |
| 25 | +// assertion.MapHasKeyNow(map[string]int{"a":1}, "b") // fail and terminate |
| 26 | +// // never run |
| 27 | +func (a *Assertion) MapHasKeyNow(m, key any, message ...any) error { |
| 28 | + a.Helper() |
| 29 | + |
| 30 | + return tryMapHasKey(a.T, true, m, key, message...) |
| 31 | +} |
| 32 | + |
| 33 | +// NotMapHasKey tests whether the map contains the specified key or not, it will fail if the map |
| 34 | +// contain the key. It will also set the test result to success if the type of the key cannot |
| 35 | +// assign to the type of the key of the map. |
| 36 | +// |
| 37 | +// assertion.NotMapHasKey(map[string]int{"a":1}, "b") // success |
| 38 | +// assertion.NotMapHasKey(map[string]int{"a":1}, 1) // success |
| 39 | +// assertion.NotMapHasKey(map[string]int{"a":1}, "a") // fail |
| 40 | +func (a *Assertion) NotMapHasKey(m, key any, message ...any) error { |
| 41 | + a.Helper() |
| 42 | + |
| 43 | + return tryNotMapHasKey(a.T, false, m, key, message...) |
| 44 | +} |
| 45 | + |
| 46 | +// NotMapHasKeyNow tests whether the map contains the specified key or not, it will fail if the map |
| 47 | +// contain the key, and it will terminate the execution if the test fails. It will also set the |
| 48 | +// test result to success if the type of the key cannot assign to the type of the key of the map. |
| 49 | +// |
| 50 | +// assertion.NotMapHasKeyNow(map[string]int{"a":1}, "b") // success |
| 51 | +// assertion.NotMapHasKeyNow(map[string]int{"a":1}, 1) // success |
| 52 | +// assertion.NotMapHasKeyNow(map[string]int{"a":1}, "a") // fail and terminate |
| 53 | +// // never run |
| 54 | +func (a *Assertion) NotMapHasKeyNow(m, key any, message ...any) error { |
| 55 | + a.Helper() |
| 56 | + |
| 57 | + return tryNotMapHasKey(a.T, true, m, key, message...) |
| 58 | +} |
| 59 | + |
| 60 | +// tryMapHasKey tries to test whether the map contains the specified key or not, and it'll fail if |
| 61 | +// the map does not contains the specified key. |
| 62 | +func tryMapHasKey( |
| 63 | + t *testing.T, |
| 64 | + failedNow bool, |
| 65 | + m, key any, |
| 66 | + message ...any, |
| 67 | +) error { |
| 68 | + t.Helper() |
| 69 | + |
| 70 | + return test( |
| 71 | + t, |
| 72 | + func() bool { return isMapHasKey(m, key) }, |
| 73 | + failedNow, |
| 74 | + fmt.Sprintf(defaultErrMessageMapHasKey, key), |
| 75 | + message..., |
| 76 | + ) |
| 77 | +} |
| 78 | + |
| 79 | +// tryNotMapHasKey tries to test whether the map contains the specified key or not, and it'll fail |
| 80 | +// if the map contains the specified key. |
| 81 | +func tryNotMapHasKey( |
| 82 | + t *testing.T, |
| 83 | + failedNow bool, |
| 84 | + m, key any, |
| 85 | + message ...any, |
| 86 | +) error { |
| 87 | + t.Helper() |
| 88 | + |
| 89 | + return test( |
| 90 | + t, |
| 91 | + func() bool { return !isMapHasKey(m, key) }, |
| 92 | + failedNow, |
| 93 | + fmt.Sprintf(defaultErrMessageNotMapHasKey, key), |
| 94 | + message..., |
| 95 | + ) |
| 96 | +} |
| 97 | + |
| 98 | +// MapHasValue tests whether the map contains the specified value or not, it will fail if the map |
| 99 | +// does not contain the value, or the type of the value cannot assign to the type of the values of |
| 100 | +// the map. |
| 101 | +// |
| 102 | +// assertion.MapHasValue(map[string]int{"a":1}, 1) // success |
| 103 | +// assertion.MapHasValue(map[string]int{"a":1}, 2) // fail |
| 104 | +// assertion.MapHasValue(map[string]int{"a":1}, "a") // fail |
| 105 | +func (a *Assertion) MapHasValue(m, value any, message ...any) error { |
| 106 | + a.Helper() |
| 107 | + |
| 108 | + return tryMapHasValue(a.T, false, m, value, message...) |
| 109 | +} |
| 110 | + |
| 111 | +// MapHasValueNow tests whether the map contains the specified value or not, and it will terminate |
| 112 | +// the execution if the test fails. It will fail if the map does not contain the value, or the type |
| 113 | +// of the value cannot assign to the type of the value of the map. |
| 114 | +// |
| 115 | +// assertion.MapHasValueNow(map[string]int{"a":1}, 1) // success |
| 116 | +// assertion.MapHasValueNow(map[string]int{"a":1}, 2) // fail and terminate |
| 117 | +// // never run |
| 118 | +func (a *Assertion) MapHasValueNow(m, value any, message ...any) error { |
| 119 | + a.Helper() |
| 120 | + |
| 121 | + return tryMapHasValue(a.T, true, m, value, message...) |
| 122 | +} |
| 123 | + |
| 124 | +// NotMapHasValue tests whether the map contains the specified value or not, it will fail if the |
| 125 | +// map contain the value. It will also set the test result to success if the type of the value |
| 126 | +// cannot assign to the type of the value of the map. |
| 127 | +// |
| 128 | +// assertion.NotMapHasValue(map[string]int{"a":1}, 2) // success |
| 129 | +// assertion.NotMapHasValue(map[string]int{"a":1}, "a") // success |
| 130 | +// assertion.NotMapHasValue(map[string]int{"a":1}, 1) // fail |
| 131 | +func (a *Assertion) NotMapHasValue(m, value any, message ...any) error { |
| 132 | + a.Helper() |
| 133 | + |
| 134 | + return tryNotMapHasValue(a.T, false, m, value, message...) |
| 135 | +} |
| 136 | + |
| 137 | +// NotMapHasValueNow tests whether the map contains the specified value or not, it will fail if the |
| 138 | +// map contain the value, and it will terminate the execution if the test fails. It will also set |
| 139 | +// the test result to success if the type of the value cannot assign to the type of the value of |
| 140 | +// the map. |
| 141 | +// |
| 142 | +// assertion.NotMapHasValueNow(map[string]int{"a":1}, 2) // success |
| 143 | +// assertion.NotMapHasValueNow(map[string]int{"a":1}, "a") // success |
| 144 | +// assertion.NotMapHasValueNow(map[string]int{"a":1}, 1) // fail and terminate |
| 145 | +// // never run |
| 146 | +func (a *Assertion) NotMapHasValueNow(m, value any, message ...any) error { |
| 147 | + a.Helper() |
| 148 | + |
| 149 | + return tryNotMapHasValue(a.T, true, m, value, message...) |
| 150 | +} |
| 151 | + |
| 152 | +// tryMapHasValue tries to test whether the map contains the specified value or not, and it'll fail |
| 153 | +// if the map does not contains the specified value. |
| 154 | +func tryMapHasValue( |
| 155 | + t *testing.T, |
| 156 | + failedNow bool, |
| 157 | + m, value any, |
| 158 | + message ...any, |
| 159 | +) error { |
| 160 | + t.Helper() |
| 161 | + |
| 162 | + return test( |
| 163 | + t, |
| 164 | + func() bool { return isMapHasValue(m, value) }, |
| 165 | + failedNow, |
| 166 | + fmt.Sprintf(defaultErrMessageMapHasValue, value), |
| 167 | + message..., |
| 168 | + ) |
| 169 | +} |
| 170 | + |
| 171 | +// tryNotMapHasValue tries to test whether the map contains the specified value or not, and it'll |
| 172 | +// fail if the map contains the specified value. |
| 173 | +func tryNotMapHasValue( |
| 174 | + t *testing.T, |
| 175 | + failedNow bool, |
| 176 | + m, value any, |
| 177 | + message ...any, |
| 178 | +) error { |
| 179 | + t.Helper() |
| 180 | + |
| 181 | + return test( |
| 182 | + t, |
| 183 | + func() bool { return !isMapHasValue(m, value) }, |
| 184 | + failedNow, |
| 185 | + fmt.Sprintf(defaultErrMessageNotMapHasValue, value), |
| 186 | + message..., |
| 187 | + ) |
| 188 | +} |
0 commit comments