1package require
2
3import (
4	"errors"
5	"testing"
6	"time"
7)
8
9func 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
22func 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
34func 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
46func 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
58func 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
75func 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
87func 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
99func 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
111func 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
123func 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
135func 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
147func 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
161func 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
175func 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
187func 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
199func 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
211func 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
223func 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
235func 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
250func 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
262func 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
274func 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
286func 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
296func 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
306func 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
317func 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
327func 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
337func 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
347func 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
357func 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
367func 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
377func 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}
386
387func TestYAMLEqWrapper_EqualYAMLString(t *testing.T) {
388	mockT := new(MockT)
389	mockRequire := New(mockT)
390
391	mockRequire.YAMLEq(`{"hello": "world", "foo": "bar"}`, `{"hello": "world", "foo": "bar"}`)
392	if mockT.Failed {
393		t.Error("Check should pass")
394	}
395}
396
397func TestYAMLEqWrapper_EquivalentButNotEqual(t *testing.T) {
398	mockT := new(MockT)
399	mockRequire := New(mockT)
400
401	mockRequire.YAMLEq(`{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`)
402	if mockT.Failed {
403		t.Error("Check should pass")
404	}
405}
406
407func TestYAMLEqWrapper_HashOfArraysAndHashes(t *testing.T) {
408	mockT := new(MockT)
409	mockRequire := New(mockT)
410
411	expected := `
412numeric: 1.5
413array:
414  - foo: bar
415  - 1
416  - "string"
417  - ["nested", "array", 5.5]
418hash:
419  nested: hash
420  nested_slice: [this, is, nested]
421string: "foo"
422`
423
424	actual := `
425numeric: 1.5
426hash:
427  nested: hash
428  nested_slice: [this, is, nested]
429string: "foo"
430array:
431  - foo: bar
432  - 1
433  - "string"
434  - ["nested", "array", 5.5]
435`
436
437	mockRequire.YAMLEq(expected, actual)
438	if mockT.Failed {
439		t.Error("Check should pass")
440	}
441}
442
443func TestYAMLEqWrapper_Array(t *testing.T) {
444	mockT := new(MockT)
445	mockRequire := New(mockT)
446
447	mockRequire.YAMLEq(`["foo", {"hello": "world", "nested": "hash"}]`, `["foo", {"nested": "hash", "hello": "world"}]`)
448	if mockT.Failed {
449		t.Error("Check should pass")
450	}
451}
452
453func TestYAMLEqWrapper_HashAndArrayNotEquivalent(t *testing.T) {
454	mockT := new(MockT)
455	mockRequire := New(mockT)
456
457	mockRequire.YAMLEq(`["foo", {"hello": "world", "nested": "hash"}]`, `{"foo": "bar", {"nested": "hash", "hello": "world"}}`)
458	if !mockT.Failed {
459		t.Error("Check should fail")
460	}
461}
462
463func TestYAMLEqWrapper_HashesNotEquivalent(t *testing.T) {
464	mockT := new(MockT)
465	mockRequire := New(mockT)
466
467	mockRequire.YAMLEq(`{"foo": "bar"}`, `{"foo": "bar", "hello": "world"}`)
468	if !mockT.Failed {
469		t.Error("Check should fail")
470	}
471}
472
473func TestYAMLEqWrapper_ActualIsSimpleString(t *testing.T) {
474	mockT := new(MockT)
475	mockRequire := New(mockT)
476
477	mockRequire.YAMLEq(`{"foo": "bar"}`, "Simple String")
478	if !mockT.Failed {
479		t.Error("Check should fail")
480	}
481}
482
483func TestYAMLEqWrapper_ExpectedIsSimpleString(t *testing.T) {
484	mockT := new(MockT)
485	mockRequire := New(mockT)
486
487	mockRequire.YAMLEq("Simple String", `{"foo": "bar", "hello": "world"}`)
488	if !mockT.Failed {
489		t.Error("Check should fail")
490	}
491}
492
493func TestYAMLEqWrapper_ExpectedAndActualSimpleString(t *testing.T) {
494	mockT := new(MockT)
495	mockRequire := New(mockT)
496
497	mockRequire.YAMLEq("Simple String", "Simple String")
498	if mockT.Failed {
499		t.Error("Check should pass")
500	}
501}
502
503func TestYAMLEqWrapper_ArraysOfDifferentOrder(t *testing.T) {
504	mockT := new(MockT)
505	mockRequire := New(mockT)
506
507	mockRequire.YAMLEq(`["foo", {"hello": "world", "nested": "hash"}]`, `[{ "hello": "world", "nested": "hash"}, "foo"]`)
508	if !mockT.Failed {
509		t.Error("Check should fail")
510	}
511}
512