OSDN Git Service

Hulk did something
[bytom/vapor.git] / vendor / github.com / stretchr / testify / require / requirements_test.go
1 package require
2
3 import (
4         "errors"
5         "testing"
6         "time"
7 )
8
9 // AssertionTesterInterface defines an interface to be used for testing assertion methods
10 type AssertionTesterInterface interface {
11         TestMethod()
12 }
13
14 // AssertionTesterConformingObject is an object that conforms to the AssertionTesterInterface interface
15 type AssertionTesterConformingObject struct {
16 }
17
18 func (a *AssertionTesterConformingObject) TestMethod() {
19 }
20
21 // AssertionTesterNonConformingObject is an object that does not conform to the AssertionTesterInterface interface
22 type AssertionTesterNonConformingObject struct {
23 }
24
25 type MockT struct {
26         Failed bool
27 }
28
29 func (t *MockT) FailNow() {
30         t.Failed = true
31 }
32
33 func (t *MockT) Errorf(format string, args ...interface{}) {
34         _, _ = format, args
35 }
36
37 func 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
48 func 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
59 func 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
71 func 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
81 func 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
96 func 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
107 func 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
118 func 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
129 func 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
140 func 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
151 func 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
162 func 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
175 func 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
188 func 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
199 func 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
210 func 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
221 func 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
232 func 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
243 func 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
257 func 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
268 func 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
279 func 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
290 func 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
298 func 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
306 func 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
315 func 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
323 func 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
331 func 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
339 func 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
347 func 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
355 func 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
363 func 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 }