| 1 | package assert |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "testing" |
| 6 | "time" |
| 7 | ) |
| 8 | |
| 9 | func TestImplementsWrapper(t *testing.T) { |
| 10 | require := New(t) |
| 11 | |
| 12 | require.Implements((*AssertionTesterInterface)(nil), new(AssertionTesterConformingObject)) |
| 13 | |
| 14 | mockT := new(MockT) |
| 15 | mockRequire := New(mockT) |
| 16 | mockRequire.Implements((*AssertionTesterInterface)(nil), new(AssertionTesterNonConformingObject)) |
| 17 | if !mockT.Failed { |
| 18 | t.Error("Check should fail") |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | func TestIsTypeWrapper(t *testing.T) { |
| 23 | require := New(t) |
| 24 | require.IsType(new(AssertionTesterConformingObject), new(AssertionTesterConformingObject)) |
| 25 | |
| 26 | mockT := new(MockT) |
| 27 | mockRequire := New(mockT) |
| 28 | mockRequire.IsType(new(AssertionTesterConformingObject), new(AssertionTesterNonConformingObject)) |
| 29 | if !mockT.Failed { |
| 30 | t.Error("Check should fail") |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | func TestEqualWrapper(t *testing.T) { |
| 35 | require := New(t) |
| 36 | require.Equal(1, 1) |
| 37 | |
| 38 | mockT := new(MockT) |
| 39 | mockRequire := New(mockT) |
| 40 | mockRequire.Equal(1, 2) |
| 41 | if !mockT.Failed { |
| 42 | t.Error("Check should fail") |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | func TestNotEqualWrapper(t *testing.T) { |
| 47 | require := New(t) |
| 48 | require.NotEqual(1, 2) |
| 49 | |
| 50 | mockT := new(MockT) |
| 51 | mockRequire := New(mockT) |
| 52 | mockRequire.NotEqual(2, 2) |
| 53 | if !mockT.Failed { |
| 54 | t.Error("Check should fail") |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | func TestExactlyWrapper(t *testing.T) { |
| 59 | require := New(t) |
| 60 | |
| 61 | a := float32(1) |
| 62 | b := float32(1) |
| 63 | c := float64(1) |
| 64 | |
| 65 | require.Exactly(a, b) |
| 66 | |
| 67 | mockT := new(MockT) |
| 68 | mockRequire := New(mockT) |
| 69 | mockRequire.Exactly(a, c) |
| 70 | if !mockT.Failed { |
| 71 | t.Error("Check should fail") |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | func TestNotNilWrapper(t *testing.T) { |
| 76 | require := New(t) |
| 77 | require.NotNil(t, new(AssertionTesterConformingObject)) |
| 78 | |
| 79 | mockT := new(MockT) |
| 80 | mockRequire := New(mockT) |
| 81 | mockRequire.NotNil(nil) |
| 82 | if !mockT.Failed { |
| 83 | t.Error("Check should fail") |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | func TestNilWrapper(t *testing.T) { |
| 88 | require := New(t) |
| 89 | require.Nil(nil) |
| 90 | |
| 91 | mockT := new(MockT) |
| 92 | mockRequire := New(mockT) |
| 93 | mockRequire.Nil(new(AssertionTesterConformingObject)) |
| 94 | if !mockT.Failed { |
| 95 | t.Error("Check should fail") |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | func TestTrueWrapper(t *testing.T) { |
| 100 | require := New(t) |
| 101 | require.True(true) |
| 102 | |
| 103 | mockT := new(MockT) |
| 104 | mockRequire := New(mockT) |
| 105 | mockRequire.True(false) |
| 106 | if !mockT.Failed { |
| 107 | t.Error("Check should fail") |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | func TestFalseWrapper(t *testing.T) { |
| 112 | require := New(t) |
| 113 | require.False(false) |
| 114 | |
| 115 | mockT := new(MockT) |
| 116 | mockRequire := New(mockT) |
| 117 | mockRequire.False(true) |
| 118 | if !mockT.Failed { |
| 119 | t.Error("Check should fail") |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | func TestContainsWrapper(t *testing.T) { |
| 124 | require := New(t) |
| 125 | require.Contains("Hello World", "Hello") |
| 126 | |
| 127 | mockT := new(MockT) |
| 128 | mockRequire := New(mockT) |
| 129 | mockRequire.Contains("Hello World", "Salut") |
| 130 | if !mockT.Failed { |
| 131 | t.Error("Check should fail") |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | func TestNotContainsWrapper(t *testing.T) { |
| 136 | require := New(t) |
| 137 | require.NotContains("Hello World", "Hello!") |
| 138 | |
| 139 | mockT := new(MockT) |
| 140 | mockRequire := New(mockT) |
| 141 | mockRequire.NotContains("Hello World", "Hello") |
| 142 | if !mockT.Failed { |
| 143 | t.Error("Check should fail") |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | func TestPanicsWrapper(t *testing.T) { |
| 148 | require := New(t) |
| 149 | require.Panics(func() { |
| 150 | panic("Panic!") |
| 151 | }) |
| 152 | |
| 153 | mockT := new(MockT) |
| 154 | mockRequire := New(mockT) |
| 155 | mockRequire.Panics(func() {}) |
| 156 | if !mockT.Failed { |
| 157 | t.Error("Check should fail") |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | func TestNotPanicsWrapper(t *testing.T) { |
| 162 | require := New(t) |
| 163 | require.NotPanics(func() {}) |
| 164 | |
| 165 | mockT := new(MockT) |
| 166 | mockRequire := New(mockT) |
| 167 | mockRequire.NotPanics(func() { |
| 168 | panic("Panic!") |
| 169 | }) |
| 170 | if !mockT.Failed { |
| 171 | t.Error("Check should fail") |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | func TestNoErrorWrapper(t *testing.T) { |
| 176 | require := New(t) |
| 177 | require.NoError(nil) |
| 178 | |
| 179 | mockT := new(MockT) |
| 180 | mockRequire := New(mockT) |
| 181 | mockRequire.NoError(errors.New("some error")) |
| 182 | if !mockT.Failed { |
| 183 | t.Error("Check should fail") |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | func TestErrorWrapper(t *testing.T) { |
| 188 | require := New(t) |
| 189 | require.Error(errors.New("some error")) |
| 190 | |
| 191 | mockT := new(MockT) |
| 192 | mockRequire := New(mockT) |
| 193 | mockRequire.Error(nil) |
| 194 | if !mockT.Failed { |
| 195 | t.Error("Check should fail") |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | func TestEqualErrorWrapper(t *testing.T) { |
| 200 | require := New(t) |
| 201 | require.EqualError(errors.New("some error"), "some error") |
| 202 | |
| 203 | mockT := new(MockT) |
| 204 | mockRequire := New(mockT) |
| 205 | mockRequire.EqualError(errors.New("some error"), "Not some error") |
| 206 | if !mockT.Failed { |
| 207 | t.Error("Check should fail") |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | func TestEmptyWrapper(t *testing.T) { |
| 212 | require := New(t) |
| 213 | require.Empty("") |
| 214 | |
| 215 | mockT := new(MockT) |
| 216 | mockRequire := New(mockT) |
| 217 | mockRequire.Empty("x") |
| 218 | if !mockT.Failed { |
| 219 | t.Error("Check should fail") |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | func TestNotEmptyWrapper(t *testing.T) { |
| 224 | require := New(t) |
| 225 | require.NotEmpty("x") |
| 226 | |
| 227 | mockT := new(MockT) |
| 228 | mockRequire := New(mockT) |
| 229 | mockRequire.NotEmpty("") |
| 230 | if !mockT.Failed { |
| 231 | t.Error("Check should fail") |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | func TestWithinDurationWrapper(t *testing.T) { |
| 236 | require := New(t) |
| 237 | a := time.Now() |
| 238 | b := a.Add(10 * time.Second) |
| 239 | |
| 240 | require.WithinDuration(a, b, 15*time.Second) |
| 241 | |
| 242 | mockT := new(MockT) |
| 243 | mockRequire := New(mockT) |
| 244 | mockRequire.WithinDuration(a, b, 5*time.Second) |
| 245 | if !mockT.Failed { |
| 246 | t.Error("Check should fail") |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | func TestInDeltaWrapper(t *testing.T) { |
| 251 | require := New(t) |
| 252 | require.InDelta(1.001, 1, 0.01) |
| 253 | |
| 254 | mockT := new(MockT) |
| 255 | mockRequire := New(mockT) |
| 256 | mockRequire.InDelta(1, 2, 0.5) |
| 257 | if !mockT.Failed { |
| 258 | t.Error("Check should fail") |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | func TestZeroWrapper(t *testing.T) { |
| 263 | require := New(t) |
| 264 | require.Zero(0) |
| 265 | |
| 266 | mockT := new(MockT) |
| 267 | mockRequire := New(mockT) |
| 268 | mockRequire.Zero(1) |
| 269 | if !mockT.Failed { |
| 270 | t.Error("Check should fail") |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | func TestNotZeroWrapper(t *testing.T) { |
| 275 | require := New(t) |
| 276 | require.NotZero(1) |
| 277 | |
| 278 | mockT := new(MockT) |
| 279 | mockRequire := New(mockT) |
| 280 | mockRequire.NotZero(0) |
| 281 | if !mockT.Failed { |
| 282 | t.Error("Check should fail") |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | func TestJSONEqWrapper_EqualSONString(t *testing.T) { |
| 287 | mockT := new(MockT) |
| 288 | mockRequire := New(mockT) |
| 289 | |
| 290 | mockRequire.JSONEq(`{"hello": "world", "foo": "bar"}`, `{"hello": "world", "foo": "bar"}`) |
| 291 | if mockT.Failed { |
| 292 | t.Error("Check should pass") |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | func TestJSONEqWrapper_EquivalentButNotEqual(t *testing.T) { |
| 297 | mockT := new(MockT) |
| 298 | mockRequire := New(mockT) |
| 299 | |
| 300 | mockRequire.JSONEq(`{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`) |
| 301 | if mockT.Failed { |
| 302 | t.Error("Check should pass") |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | func TestJSONEqWrapper_HashOfArraysAndHashes(t *testing.T) { |
| 307 | mockT := new(MockT) |
| 308 | mockRequire := New(mockT) |
| 309 | |
| 310 | mockRequire.JSONEq("{\r\n\t\"numeric\": 1.5,\r\n\t\"array\": [{\"foo\": \"bar\"}, 1, \"string\", [\"nested\", \"array\", 5.5]],\r\n\t\"hash\": {\"nested\": \"hash\", \"nested_slice\": [\"this\", \"is\", \"nested\"]},\r\n\t\"string\": \"foo\"\r\n}", |
| 311 | "{\r\n\t\"numeric\": 1.5,\r\n\t\"hash\": {\"nested\": \"hash\", \"nested_slice\": [\"this\", \"is\", \"nested\"]},\r\n\t\"string\": \"foo\",\r\n\t\"array\": [{\"foo\": \"bar\"}, 1, \"string\", [\"nested\", \"array\", 5.5]]\r\n}") |
| 312 | if mockT.Failed { |
| 313 | t.Error("Check should pass") |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | func TestJSONEqWrapper_Array(t *testing.T) { |
| 318 | mockT := new(MockT) |
| 319 | mockRequire := New(mockT) |
| 320 | |
| 321 | mockRequire.JSONEq(`["foo", {"hello": "world", "nested": "hash"}]`, `["foo", {"nested": "hash", "hello": "world"}]`) |
| 322 | if mockT.Failed { |
| 323 | t.Error("Check should pass") |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | func TestJSONEqWrapper_HashAndArrayNotEquivalent(t *testing.T) { |
| 328 | mockT := new(MockT) |
| 329 | mockRequire := New(mockT) |
| 330 | |
| 331 | mockRequire.JSONEq(`["foo", {"hello": "world", "nested": "hash"}]`, `{"foo": "bar", {"nested": "hash", "hello": "world"}}`) |
| 332 | if !mockT.Failed { |
| 333 | t.Error("Check should fail") |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | func TestJSONEqWrapper_HashesNotEquivalent(t *testing.T) { |
| 338 | mockT := new(MockT) |
| 339 | mockRequire := New(mockT) |
| 340 | |
| 341 | mockRequire.JSONEq(`{"foo": "bar"}`, `{"foo": "bar", "hello": "world"}`) |
| 342 | if !mockT.Failed { |
| 343 | t.Error("Check should fail") |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | func TestJSONEqWrapper_ActualIsNotJSON(t *testing.T) { |
| 348 | mockT := new(MockT) |
| 349 | mockRequire := New(mockT) |
| 350 | |
| 351 | mockRequire.JSONEq(`{"foo": "bar"}`, "Not JSON") |
| 352 | if !mockT.Failed { |
| 353 | t.Error("Check should fail") |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | func TestJSONEqWrapper_ExpectedIsNotJSON(t *testing.T) { |
| 358 | mockT := new(MockT) |
| 359 | mockRequire := New(mockT) |
| 360 | |
| 361 | mockRequire.JSONEq("Not JSON", `{"foo": "bar", "hello": "world"}`) |
| 362 | if !mockT.Failed { |
| 363 | t.Error("Check should fail") |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | func TestJSONEqWrapper_ExpectedAndActualNotJSON(t *testing.T) { |
| 368 | mockT := new(MockT) |
| 369 | mockRequire := New(mockT) |
| 370 | |
| 371 | mockRequire.JSONEq("Not JSON", "Not JSON") |
| 372 | if !mockT.Failed { |
| 373 | t.Error("Check should fail") |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | func TestJSONEqWrapper_ArraysOfDifferentOrder(t *testing.T) { |
| 378 | mockT := new(MockT) |
| 379 | mockRequire := New(mockT) |
| 380 | |
| 381 | mockRequire.JSONEq(`["foo", {"hello": "world", "nested": "hash"}]`, `[{ "hello": "world", "nested": "hash"}, "foo"]`) |
| 382 | if !mockT.Failed { |
| 383 | t.Error("Check should fail") |
| 384 | } |
| 385 | } |