1 package require
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 TestErrorContainsWrapper(t *testing.T) {
200 require := New(t)
201 require.ErrorContains(errors.New("some error: another error"), "some error")
202
203 mockT := new(MockT)
204 mockRequire := New(mockT)
205 mockRequire.ErrorContains(errors.New("some error: another error"), "different error")
206 if !mockT.Failed {
207 t.Error("Check should fail")
208 }
209 }
210
211 func TestEqualErrorWrapper(t *testing.T) {
212 require := New(t)
213 require.EqualError(errors.New("some error"), "some error")
214
215 mockT := new(MockT)
216 mockRequire := New(mockT)
217 mockRequire.EqualError(errors.New("some error"), "Not some error")
218 if !mockT.Failed {
219 t.Error("Check should fail")
220 }
221 }
222
223 func TestEmptyWrapper(t *testing.T) {
224 require := New(t)
225 require.Empty("")
226
227 mockT := new(MockT)
228 mockRequire := New(mockT)
229 mockRequire.Empty("x")
230 if !mockT.Failed {
231 t.Error("Check should fail")
232 }
233 }
234
235 func TestNotEmptyWrapper(t *testing.T) {
236 require := New(t)
237 require.NotEmpty("x")
238
239 mockT := new(MockT)
240 mockRequire := New(mockT)
241 mockRequire.NotEmpty("")
242 if !mockT.Failed {
243 t.Error("Check should fail")
244 }
245 }
246
247 func TestWithinDurationWrapper(t *testing.T) {
248 require := New(t)
249 a := time.Now()
250 b := a.Add(10 * time.Second)
251
252 require.WithinDuration(a, b, 15*time.Second)
253
254 mockT := new(MockT)
255 mockRequire := New(mockT)
256 mockRequire.WithinDuration(a, b, 5*time.Second)
257 if !mockT.Failed {
258 t.Error("Check should fail")
259 }
260 }
261
262 func TestInDeltaWrapper(t *testing.T) {
263 require := New(t)
264 require.InDelta(1.001, 1, 0.01)
265
266 mockT := new(MockT)
267 mockRequire := New(mockT)
268 mockRequire.InDelta(1, 2, 0.5)
269 if !mockT.Failed {
270 t.Error("Check should fail")
271 }
272 }
273
274 func TestZeroWrapper(t *testing.T) {
275 require := New(t)
276 require.Zero(0)
277
278 mockT := new(MockT)
279 mockRequire := New(mockT)
280 mockRequire.Zero(1)
281 if !mockT.Failed {
282 t.Error("Check should fail")
283 }
284 }
285
286 func TestNotZeroWrapper(t *testing.T) {
287 require := New(t)
288 require.NotZero(1)
289
290 mockT := new(MockT)
291 mockRequire := New(mockT)
292 mockRequire.NotZero(0)
293 if !mockT.Failed {
294 t.Error("Check should fail")
295 }
296 }
297
298 func TestJSONEqWrapper_EqualSONString(t *testing.T) {
299 mockT := new(MockT)
300 mockRequire := New(mockT)
301
302 mockRequire.JSONEq(`{"hello": "world", "foo": "bar"}`, `{"hello": "world", "foo": "bar"}`)
303 if mockT.Failed {
304 t.Error("Check should pass")
305 }
306 }
307
308 func TestJSONEqWrapper_EquivalentButNotEqual(t *testing.T) {
309 mockT := new(MockT)
310 mockRequire := New(mockT)
311
312 mockRequire.JSONEq(`{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`)
313 if mockT.Failed {
314 t.Error("Check should pass")
315 }
316 }
317
318 func TestJSONEqWrapper_HashOfArraysAndHashes(t *testing.T) {
319 mockT := new(MockT)
320 mockRequire := New(mockT)
321
322 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}",
323 "{\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}")
324 if mockT.Failed {
325 t.Error("Check should pass")
326 }
327 }
328
329 func TestJSONEqWrapper_Array(t *testing.T) {
330 mockT := new(MockT)
331 mockRequire := New(mockT)
332
333 mockRequire.JSONEq(`["foo", {"hello": "world", "nested": "hash"}]`, `["foo", {"nested": "hash", "hello": "world"}]`)
334 if mockT.Failed {
335 t.Error("Check should pass")
336 }
337 }
338
339 func TestJSONEqWrapper_HashAndArrayNotEquivalent(t *testing.T) {
340 mockT := new(MockT)
341 mockRequire := New(mockT)
342
343 mockRequire.JSONEq(`["foo", {"hello": "world", "nested": "hash"}]`, `{"foo": "bar", {"nested": "hash", "hello": "world"}}`)
344 if !mockT.Failed {
345 t.Error("Check should fail")
346 }
347 }
348
349 func TestJSONEqWrapper_HashesNotEquivalent(t *testing.T) {
350 mockT := new(MockT)
351 mockRequire := New(mockT)
352
353 mockRequire.JSONEq(`{"foo": "bar"}`, `{"foo": "bar", "hello": "world"}`)
354 if !mockT.Failed {
355 t.Error("Check should fail")
356 }
357 }
358
359 func TestJSONEqWrapper_ActualIsNotJSON(t *testing.T) {
360 mockT := new(MockT)
361 mockRequire := New(mockT)
362
363 mockRequire.JSONEq(`{"foo": "bar"}`, "Not JSON")
364 if !mockT.Failed {
365 t.Error("Check should fail")
366 }
367 }
368
369 func TestJSONEqWrapper_ExpectedIsNotJSON(t *testing.T) {
370 mockT := new(MockT)
371 mockRequire := New(mockT)
372
373 mockRequire.JSONEq("Not JSON", `{"foo": "bar", "hello": "world"}`)
374 if !mockT.Failed {
375 t.Error("Check should fail")
376 }
377 }
378
379 func TestJSONEqWrapper_ExpectedAndActualNotJSON(t *testing.T) {
380 mockT := new(MockT)
381 mockRequire := New(mockT)
382
383 mockRequire.JSONEq("Not JSON", "Not JSON")
384 if !mockT.Failed {
385 t.Error("Check should fail")
386 }
387 }
388
389 func TestJSONEqWrapper_ArraysOfDifferentOrder(t *testing.T) {
390 mockT := new(MockT)
391 mockRequire := New(mockT)
392
393 mockRequire.JSONEq(`["foo", {"hello": "world", "nested": "hash"}]`, `[{ "hello": "world", "nested": "hash"}, "foo"]`)
394 if !mockT.Failed {
395 t.Error("Check should fail")
396 }
397 }
398
399 func TestYAMLEqWrapper_EqualYAMLString(t *testing.T) {
400 mockT := new(MockT)
401 mockRequire := New(mockT)
402
403 mockRequire.YAMLEq(`{"hello": "world", "foo": "bar"}`, `{"hello": "world", "foo": "bar"}`)
404 if mockT.Failed {
405 t.Error("Check should pass")
406 }
407 }
408
409 func TestYAMLEqWrapper_EquivalentButNotEqual(t *testing.T) {
410 mockT := new(MockT)
411 mockRequire := New(mockT)
412
413 mockRequire.YAMLEq(`{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`)
414 if mockT.Failed {
415 t.Error("Check should pass")
416 }
417 }
418
419 func TestYAMLEqWrapper_HashOfArraysAndHashes(t *testing.T) {
420 mockT := new(MockT)
421 mockRequire := New(mockT)
422
423 expected := `
424 numeric: 1.5
425 array:
426 - foo: bar
427 - 1
428 - "string"
429 - ["nested", "array", 5.5]
430 hash:
431 nested: hash
432 nested_slice: [this, is, nested]
433 string: "foo"
434 `
435
436 actual := `
437 numeric: 1.5
438 hash:
439 nested: hash
440 nested_slice: [this, is, nested]
441 string: "foo"
442 array:
443 - foo: bar
444 - 1
445 - "string"
446 - ["nested", "array", 5.5]
447 `
448
449 mockRequire.YAMLEq(expected, actual)
450 if mockT.Failed {
451 t.Error("Check should pass")
452 }
453 }
454
455 func TestYAMLEqWrapper_Array(t *testing.T) {
456 mockT := new(MockT)
457 mockRequire := New(mockT)
458
459 mockRequire.YAMLEq(`["foo", {"hello": "world", "nested": "hash"}]`, `["foo", {"nested": "hash", "hello": "world"}]`)
460 if mockT.Failed {
461 t.Error("Check should pass")
462 }
463 }
464
465 func TestYAMLEqWrapper_HashAndArrayNotEquivalent(t *testing.T) {
466 mockT := new(MockT)
467 mockRequire := New(mockT)
468
469 mockRequire.YAMLEq(`["foo", {"hello": "world", "nested": "hash"}]`, `{"foo": "bar", {"nested": "hash", "hello": "world"}}`)
470 if !mockT.Failed {
471 t.Error("Check should fail")
472 }
473 }
474
475 func TestYAMLEqWrapper_HashesNotEquivalent(t *testing.T) {
476 mockT := new(MockT)
477 mockRequire := New(mockT)
478
479 mockRequire.YAMLEq(`{"foo": "bar"}`, `{"foo": "bar", "hello": "world"}`)
480 if !mockT.Failed {
481 t.Error("Check should fail")
482 }
483 }
484
485 func TestYAMLEqWrapper_ActualIsSimpleString(t *testing.T) {
486 mockT := new(MockT)
487 mockRequire := New(mockT)
488
489 mockRequire.YAMLEq(`{"foo": "bar"}`, "Simple String")
490 if !mockT.Failed {
491 t.Error("Check should fail")
492 }
493 }
494
495 func TestYAMLEqWrapper_ExpectedIsSimpleString(t *testing.T) {
496 mockT := new(MockT)
497 mockRequire := New(mockT)
498
499 mockRequire.YAMLEq("Simple String", `{"foo": "bar", "hello": "world"}`)
500 if !mockT.Failed {
501 t.Error("Check should fail")
502 }
503 }
504
505 func TestYAMLEqWrapper_ExpectedAndActualSimpleString(t *testing.T) {
506 mockT := new(MockT)
507 mockRequire := New(mockT)
508
509 mockRequire.YAMLEq("Simple String", "Simple String")
510 if mockT.Failed {
511 t.Error("Check should pass")
512 }
513 }
514
515 func TestYAMLEqWrapper_ArraysOfDifferentOrder(t *testing.T) {
516 mockT := new(MockT)
517 mockRequire := New(mockT)
518
519 mockRequire.YAMLEq(`["foo", {"hello": "world", "nested": "hash"}]`, `[{ "hello": "world", "nested": "hash"}, "foo"]`)
520 if !mockT.Failed {
521 t.Error("Check should fail")
522 }
523 }
524
View as plain text