1package assert
2
3import (
4	"errors"
5	"testing"
6	"time"
7)
8
9// AssertionTesterInterface defines an interface to be used for testing assertion methods
10type AssertionTesterInterface interface {
11	TestMethod()
12}
13
14// AssertionTesterConformingObject is an object that conforms to the AssertionTesterInterface interface
15type AssertionTesterConformingObject struct {
16}
17
18func (a *AssertionTesterConformingObject) TestMethod() {
19}
20
21// AssertionTesterNonConformingObject is an object that does not conform to the AssertionTesterInterface interface
22type AssertionTesterNonConformingObject struct {
23}
24
25type MockT struct {
26	Failed bool
27}
28
29func (t *MockT) FailNow() {
30	t.Failed = true
31}
32
33func (t *MockT) Errorf(format string, args ...interface{}) {
34	_, _ = format, args
35}
36
37func TestImplements(t *testing.T) {
38
39	Implements(t, (*AssertionTesterInterface)(nil), new(AssertionTesterConformingObject))
40
41	mockT := new(MockT)
42	Implements(mockT, (*AssertionTesterInterface)(nil), new(AssertionTesterNonConformingObject))
43	if !mockT.Failed {
44		t.Error("Check should fail")
45	}
46}
47
48func TestIsType(t *testing.T) {
49
50	IsType(t, new(AssertionTesterConformingObject), new(AssertionTesterConformingObject))
51
52	mockT := new(MockT)
53	IsType(mockT, new(AssertionTesterConformingObject), new(AssertionTesterNonConformingObject))
54	if !mockT.Failed {
55		t.Error("Check should fail")
56	}
57}
58
59func TestEqual(t *testing.T) {
60
61	Equal(t, 1, 1)
62
63	mockT := new(MockT)
64	Equal(mockT, 1, 2)
65	if !mockT.Failed {
66		t.Error("Check should fail")
67	}
68
69}
70
71func TestNotEqual(t *testing.T) {
72
73	NotEqual(t, 1, 2)
74	mockT := new(MockT)
75	NotEqual(mockT, 2, 2)
76	if !mockT.Failed {
77		t.Error("Check should fail")
78	}
79}
80
81func TestExactly(t *testing.T) {
82
83	a := float32(1)
84	b := float32(1)
85	c := float64(1)
86
87	Exactly(t, a, b)
88
89	mockT := new(MockT)
90	Exactly(mockT, a, c)
91	if !mockT.Failed {
92		t.Error("Check should fail")
93	}
94}
95
96func TestNotNil(t *testing.T) {
97
98	NotNil(t, new(AssertionTesterConformingObject))
99
100	mockT := new(MockT)
101	NotNil(mockT, nil)
102	if !mockT.Failed {
103		t.Error("Check should fail")
104	}
105}
106
107func TestNil(t *testing.T) {
108
109	Nil(t, nil)
110
111	mockT := new(MockT)
112	Nil(mockT, new(AssertionTesterConformingObject))
113	if !mockT.Failed {
114		t.Error("Check should fail")
115	}
116}
117
118func TestTrue(t *testing.T) {
119
120	True(t, true)
121
122	mockT := new(MockT)
123	True(mockT, false)
124	if !mockT.Failed {
125		t.Error("Check should fail")
126	}
127}
128
129func TestFalse(t *testing.T) {
130
131	False(t, false)
132
133	mockT := new(MockT)
134	False(mockT, true)
135	if !mockT.Failed {
136		t.Error("Check should fail")
137	}
138}
139
140func TestContains(t *testing.T) {
141
142	Contains(t, "Hello World", "Hello")
143
144	mockT := new(MockT)
145	Contains(mockT, "Hello World", "Salut")
146	if !mockT.Failed {
147		t.Error("Check should fail")
148	}
149}
150
151func TestNotContains(t *testing.T) {
152
153	NotContains(t, "Hello World", "Hello!")
154
155	mockT := new(MockT)
156	NotContains(mockT, "Hello World", "Hello")
157	if !mockT.Failed {
158		t.Error("Check should fail")
159	}
160}
161
162func TestPanics(t *testing.T) {
163
164	Panics(t, func() {
165		panic("Panic!")
166	})
167
168	mockT := new(MockT)
169	Panics(mockT, func() {})
170	if !mockT.Failed {
171		t.Error("Check should fail")
172	}
173}
174
175func TestNotPanics(t *testing.T) {
176
177	NotPanics(t, func() {})
178
179	mockT := new(MockT)
180	NotPanics(mockT, func() {
181		panic("Panic!")
182	})
183	if !mockT.Failed {
184		t.Error("Check should fail")
185	}
186}
187
188func TestNoError(t *testing.T) {
189
190	NoError(t, nil)
191
192	mockT := new(MockT)
193	NoError(mockT, errors.New("some error"))
194	if !mockT.Failed {
195		t.Error("Check should fail")
196	}
197}
198
199func TestError(t *testing.T) {
200
201	Error(t, errors.New("some error"))
202
203	mockT := new(MockT)
204	Error(mockT, nil)
205	if !mockT.Failed {
206		t.Error("Check should fail")
207	}
208}
209
210func TestEqualError(t *testing.T) {
211
212	EqualError(t, errors.New("some error"), "some error")
213
214	mockT := new(MockT)
215	EqualError(mockT, errors.New("some error"), "Not some error")
216	if !mockT.Failed {
217		t.Error("Check should fail")
218	}
219}
220
221func TestEmpty(t *testing.T) {
222
223	Empty(t, "")
224
225	mockT := new(MockT)
226	Empty(mockT, "x")
227	if !mockT.Failed {
228		t.Error("Check should fail")
229	}
230}
231
232func TestNotEmpty(t *testing.T) {
233
234	NotEmpty(t, "x")
235
236	mockT := new(MockT)
237	NotEmpty(mockT, "")
238	if !mockT.Failed {
239		t.Error("Check should fail")
240	}
241}
242
243func TestWithinDuration(t *testing.T) {
244
245	a := time.Now()
246	b := a.Add(10 * time.Second)
247
248	WithinDuration(t, a, b, 15*time.Second)
249
250	mockT := new(MockT)
251	WithinDuration(mockT, a, b, 5*time.Second)
252	if !mockT.Failed {
253		t.Error("Check should fail")
254	}
255}
256
257func TestInDelta(t *testing.T) {
258
259	InDelta(t, 1.001, 1, 0.01)
260
261	mockT := new(MockT)
262	InDelta(mockT, 1, 2, 0.5)
263	if !mockT.Failed {
264		t.Error("Check should fail")
265	}
266}
267
268func TestZero(t *testing.T) {
269
270	Zero(t, "")
271
272	mockT := new(MockT)
273	Zero(mockT, "x")
274	if !mockT.Failed {
275		t.Error("Check should fail")
276	}
277}
278
279func TestNotZero(t *testing.T) {
280
281	NotZero(t, "x")
282
283	mockT := new(MockT)
284	NotZero(mockT, "")
285	if !mockT.Failed {
286		t.Error("Check should fail")
287	}
288}
289
290func TestJSONEq_EqualSONString(t *testing.T) {
291	mockT := new(MockT)
292	JSONEq(mockT, `{"hello": "world", "foo": "bar"}`, `{"hello": "world", "foo": "bar"}`)
293	if mockT.Failed {
294		t.Error("Check should pass")
295	}
296}
297
298func TestJSONEq_EquivalentButNotEqual(t *testing.T) {
299	mockT := new(MockT)
300	JSONEq(mockT, `{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`)
301	if mockT.Failed {
302		t.Error("Check should pass")
303	}
304}
305
306func TestJSONEq_HashOfArraysAndHashes(t *testing.T) {
307	mockT := new(MockT)
308	JSONEq(mockT, "{\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}",
309		"{\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}")
310	if mockT.Failed {
311		t.Error("Check should pass")
312	}
313}
314
315func TestJSONEq_Array(t *testing.T) {
316	mockT := new(MockT)
317	JSONEq(mockT, `["foo", {"hello": "world", "nested": "hash"}]`, `["foo", {"nested": "hash", "hello": "world"}]`)
318	if mockT.Failed {
319		t.Error("Check should pass")
320	}
321}
322
323func TestJSONEq_HashAndArrayNotEquivalent(t *testing.T) {
324	mockT := new(MockT)
325	JSONEq(mockT, `["foo", {"hello": "world", "nested": "hash"}]`, `{"foo": "bar", {"nested": "hash", "hello": "world"}}`)
326	if !mockT.Failed {
327		t.Error("Check should fail")
328	}
329}
330
331func TestJSONEq_HashesNotEquivalent(t *testing.T) {
332	mockT := new(MockT)
333	JSONEq(mockT, `{"foo": "bar"}`, `{"foo": "bar", "hello": "world"}`)
334	if !mockT.Failed {
335		t.Error("Check should fail")
336	}
337}
338
339func TestJSONEq_ActualIsNotJSON(t *testing.T) {
340	mockT := new(MockT)
341	JSONEq(mockT, `{"foo": "bar"}`, "Not JSON")
342	if !mockT.Failed {
343		t.Error("Check should fail")
344	}
345}
346
347func TestJSONEq_ExpectedIsNotJSON(t *testing.T) {
348	mockT := new(MockT)
349	JSONEq(mockT, "Not JSON", `{"foo": "bar", "hello": "world"}`)
350	if !mockT.Failed {
351		t.Error("Check should fail")
352	}
353}
354
355func TestJSONEq_ExpectedAndActualNotJSON(t *testing.T) {
356	mockT := new(MockT)
357	JSONEq(mockT, "Not JSON", "Not JSON")
358	if !mockT.Failed {
359		t.Error("Check should fail")
360	}
361}
362
363func TestJSONEq_ArraysOfDifferentOrder(t *testing.T) {
364	mockT := new(MockT)
365	JSONEq(mockT, `["foo", {"hello": "world", "nested": "hash"}]`, `[{ "hello": "world", "nested": "hash"}, "foo"]`)
366	if !mockT.Failed {
367		t.Error("Check should fail")
368	}
369}
370