OSDN Git Service

Hulk did something
[bytom/vapor.git] / vendor / github.com / stretchr / testify / assert / forward_assertions_test.go
1 package assert
2
3 import (
4         "errors"
5         "regexp"
6         "testing"
7         "time"
8 )
9
10 func TestImplementsWrapper(t *testing.T) {
11         assert := New(new(testing.T))
12
13         if !assert.Implements((*AssertionTesterInterface)(nil), new(AssertionTesterConformingObject)) {
14                 t.Error("Implements method should return true: AssertionTesterConformingObject implements AssertionTesterInterface")
15         }
16         if assert.Implements((*AssertionTesterInterface)(nil), new(AssertionTesterNonConformingObject)) {
17                 t.Error("Implements method should return false: AssertionTesterNonConformingObject does not implements AssertionTesterInterface")
18         }
19 }
20
21 func TestIsTypeWrapper(t *testing.T) {
22         assert := New(new(testing.T))
23
24         if !assert.IsType(new(AssertionTesterConformingObject), new(AssertionTesterConformingObject)) {
25                 t.Error("IsType should return true: AssertionTesterConformingObject is the same type as AssertionTesterConformingObject")
26         }
27         if assert.IsType(new(AssertionTesterConformingObject), new(AssertionTesterNonConformingObject)) {
28                 t.Error("IsType should return false: AssertionTesterConformingObject is not the same type as AssertionTesterNonConformingObject")
29         }
30
31 }
32
33 func TestEqualWrapper(t *testing.T) {
34         assert := New(new(testing.T))
35
36         if !assert.Equal("Hello World", "Hello World") {
37                 t.Error("Equal should return true")
38         }
39         if !assert.Equal(123, 123) {
40                 t.Error("Equal should return true")
41         }
42         if !assert.Equal(123.5, 123.5) {
43                 t.Error("Equal should return true")
44         }
45         if !assert.Equal([]byte("Hello World"), []byte("Hello World")) {
46                 t.Error("Equal should return true")
47         }
48         if !assert.Equal(nil, nil) {
49                 t.Error("Equal should return true")
50         }
51 }
52
53 func TestEqualValuesWrapper(t *testing.T) {
54         assert := New(new(testing.T))
55
56         if !assert.EqualValues(uint32(10), int32(10)) {
57                 t.Error("EqualValues should return true")
58         }
59 }
60
61 func TestNotNilWrapper(t *testing.T) {
62         assert := New(new(testing.T))
63
64         if !assert.NotNil(new(AssertionTesterConformingObject)) {
65                 t.Error("NotNil should return true: object is not nil")
66         }
67         if assert.NotNil(nil) {
68                 t.Error("NotNil should return false: object is nil")
69         }
70
71 }
72
73 func TestNilWrapper(t *testing.T) {
74         assert := New(new(testing.T))
75
76         if !assert.Nil(nil) {
77                 t.Error("Nil should return true: object is nil")
78         }
79         if assert.Nil(new(AssertionTesterConformingObject)) {
80                 t.Error("Nil should return false: object is not nil")
81         }
82
83 }
84
85 func TestTrueWrapper(t *testing.T) {
86         assert := New(new(testing.T))
87
88         if !assert.True(true) {
89                 t.Error("True should return true")
90         }
91         if assert.True(false) {
92                 t.Error("True should return false")
93         }
94
95 }
96
97 func TestFalseWrapper(t *testing.T) {
98         assert := New(new(testing.T))
99
100         if !assert.False(false) {
101                 t.Error("False should return true")
102         }
103         if assert.False(true) {
104                 t.Error("False should return false")
105         }
106
107 }
108
109 func TestExactlyWrapper(t *testing.T) {
110         assert := New(new(testing.T))
111
112         a := float32(1)
113         b := float64(1)
114         c := float32(1)
115         d := float32(2)
116
117         if assert.Exactly(a, b) {
118                 t.Error("Exactly should return false")
119         }
120         if assert.Exactly(a, d) {
121                 t.Error("Exactly should return false")
122         }
123         if !assert.Exactly(a, c) {
124                 t.Error("Exactly should return true")
125         }
126
127         if assert.Exactly(nil, a) {
128                 t.Error("Exactly should return false")
129         }
130         if assert.Exactly(a, nil) {
131                 t.Error("Exactly should return false")
132         }
133
134 }
135
136 func TestNotEqualWrapper(t *testing.T) {
137
138         assert := New(new(testing.T))
139
140         if !assert.NotEqual("Hello World", "Hello World!") {
141                 t.Error("NotEqual should return true")
142         }
143         if !assert.NotEqual(123, 1234) {
144                 t.Error("NotEqual should return true")
145         }
146         if !assert.NotEqual(123.5, 123.55) {
147                 t.Error("NotEqual should return true")
148         }
149         if !assert.NotEqual([]byte("Hello World"), []byte("Hello World!")) {
150                 t.Error("NotEqual should return true")
151         }
152         if !assert.NotEqual(nil, new(AssertionTesterConformingObject)) {
153                 t.Error("NotEqual should return true")
154         }
155 }
156
157 func TestContainsWrapper(t *testing.T) {
158
159         assert := New(new(testing.T))
160         list := []string{"Foo", "Bar"}
161
162         if !assert.Contains("Hello World", "Hello") {
163                 t.Error("Contains should return true: \"Hello World\" contains \"Hello\"")
164         }
165         if assert.Contains("Hello World", "Salut") {
166                 t.Error("Contains should return false: \"Hello World\" does not contain \"Salut\"")
167         }
168
169         if !assert.Contains(list, "Foo") {
170                 t.Error("Contains should return true: \"[\"Foo\", \"Bar\"]\" contains \"Foo\"")
171         }
172         if assert.Contains(list, "Salut") {
173                 t.Error("Contains should return false: \"[\"Foo\", \"Bar\"]\" does not contain \"Salut\"")
174         }
175
176 }
177
178 func TestNotContainsWrapper(t *testing.T) {
179
180         assert := New(new(testing.T))
181         list := []string{"Foo", "Bar"}
182
183         if !assert.NotContains("Hello World", "Hello!") {
184                 t.Error("NotContains should return true: \"Hello World\" does not contain \"Hello!\"")
185         }
186         if assert.NotContains("Hello World", "Hello") {
187                 t.Error("NotContains should return false: \"Hello World\" contains \"Hello\"")
188         }
189
190         if !assert.NotContains(list, "Foo!") {
191                 t.Error("NotContains should return true: \"[\"Foo\", \"Bar\"]\" does not contain \"Foo!\"")
192         }
193         if assert.NotContains(list, "Foo") {
194                 t.Error("NotContains should return false: \"[\"Foo\", \"Bar\"]\" contains \"Foo\"")
195         }
196
197 }
198
199 func TestConditionWrapper(t *testing.T) {
200
201         assert := New(new(testing.T))
202
203         if !assert.Condition(func() bool { return true }, "Truth") {
204                 t.Error("Condition should return true")
205         }
206
207         if assert.Condition(func() bool { return false }, "Lie") {
208                 t.Error("Condition should return false")
209         }
210
211 }
212
213 func TestDidPanicWrapper(t *testing.T) {
214
215         if funcDidPanic, _ := didPanic(func() {
216                 panic("Panic!")
217         }); !funcDidPanic {
218                 t.Error("didPanic should return true")
219         }
220
221         if funcDidPanic, _ := didPanic(func() {
222         }); funcDidPanic {
223                 t.Error("didPanic should return false")
224         }
225
226 }
227
228 func TestPanicsWrapper(t *testing.T) {
229
230         assert := New(new(testing.T))
231
232         if !assert.Panics(func() {
233                 panic("Panic!")
234         }) {
235                 t.Error("Panics should return true")
236         }
237
238         if assert.Panics(func() {
239         }) {
240                 t.Error("Panics should return false")
241         }
242
243 }
244
245 func TestNotPanicsWrapper(t *testing.T) {
246
247         assert := New(new(testing.T))
248
249         if !assert.NotPanics(func() {
250         }) {
251                 t.Error("NotPanics should return true")
252         }
253
254         if assert.NotPanics(func() {
255                 panic("Panic!")
256         }) {
257                 t.Error("NotPanics should return false")
258         }
259
260 }
261
262 func TestNoErrorWrapper(t *testing.T) {
263         assert := New(t)
264         mockAssert := New(new(testing.T))
265
266         // start with a nil error
267         var err error
268
269         assert.True(mockAssert.NoError(err), "NoError should return True for nil arg")
270
271         // now set an error
272         err = errors.New("Some error")
273
274         assert.False(mockAssert.NoError(err), "NoError with error should return False")
275
276 }
277
278 func TestErrorWrapper(t *testing.T) {
279         assert := New(t)
280         mockAssert := New(new(testing.T))
281
282         // start with a nil error
283         var err error
284
285         assert.False(mockAssert.Error(err), "Error should return False for nil arg")
286
287         // now set an error
288         err = errors.New("Some error")
289
290         assert.True(mockAssert.Error(err), "Error with error should return True")
291
292 }
293
294 func TestEqualErrorWrapper(t *testing.T) {
295         assert := New(t)
296         mockAssert := New(new(testing.T))
297
298         // start with a nil error
299         var err error
300         assert.False(mockAssert.EqualError(err, ""),
301                 "EqualError should return false for nil arg")
302
303         // now set an error
304         err = errors.New("some error")
305         assert.False(mockAssert.EqualError(err, "Not some error"),
306                 "EqualError should return false for different error string")
307         assert.True(mockAssert.EqualError(err, "some error"),
308                 "EqualError should return true")
309 }
310
311 func TestEmptyWrapper(t *testing.T) {
312         assert := New(t)
313         mockAssert := New(new(testing.T))
314
315         assert.True(mockAssert.Empty(""), "Empty string is empty")
316         assert.True(mockAssert.Empty(nil), "Nil is empty")
317         assert.True(mockAssert.Empty([]string{}), "Empty string array is empty")
318         assert.True(mockAssert.Empty(0), "Zero int value is empty")
319         assert.True(mockAssert.Empty(false), "False value is empty")
320
321         assert.False(mockAssert.Empty("something"), "Non Empty string is not empty")
322         assert.False(mockAssert.Empty(errors.New("something")), "Non nil object is not empty")
323         assert.False(mockAssert.Empty([]string{"something"}), "Non empty string array is not empty")
324         assert.False(mockAssert.Empty(1), "Non-zero int value is not empty")
325         assert.False(mockAssert.Empty(true), "True value is not empty")
326
327 }
328
329 func TestNotEmptyWrapper(t *testing.T) {
330         assert := New(t)
331         mockAssert := New(new(testing.T))
332
333         assert.False(mockAssert.NotEmpty(""), "Empty string is empty")
334         assert.False(mockAssert.NotEmpty(nil), "Nil is empty")
335         assert.False(mockAssert.NotEmpty([]string{}), "Empty string array is empty")
336         assert.False(mockAssert.NotEmpty(0), "Zero int value is empty")
337         assert.False(mockAssert.NotEmpty(false), "False value is empty")
338
339         assert.True(mockAssert.NotEmpty("something"), "Non Empty string is not empty")
340         assert.True(mockAssert.NotEmpty(errors.New("something")), "Non nil object is not empty")
341         assert.True(mockAssert.NotEmpty([]string{"something"}), "Non empty string array is not empty")
342         assert.True(mockAssert.NotEmpty(1), "Non-zero int value is not empty")
343         assert.True(mockAssert.NotEmpty(true), "True value is not empty")
344
345 }
346
347 func TestLenWrapper(t *testing.T) {
348         assert := New(t)
349         mockAssert := New(new(testing.T))
350
351         assert.False(mockAssert.Len(nil, 0), "nil does not have length")
352         assert.False(mockAssert.Len(0, 0), "int does not have length")
353         assert.False(mockAssert.Len(true, 0), "true does not have length")
354         assert.False(mockAssert.Len(false, 0), "false does not have length")
355         assert.False(mockAssert.Len('A', 0), "Rune does not have length")
356         assert.False(mockAssert.Len(struct{}{}, 0), "Struct does not have length")
357
358         ch := make(chan int, 5)
359         ch <- 1
360         ch <- 2
361         ch <- 3
362
363         cases := []struct {
364                 v interface{}
365                 l int
366         }{
367                 {[]int{1, 2, 3}, 3},
368                 {[...]int{1, 2, 3}, 3},
369                 {"ABC", 3},
370                 {map[int]int{1: 2, 2: 4, 3: 6}, 3},
371                 {ch, 3},
372
373                 {[]int{}, 0},
374                 {map[int]int{}, 0},
375                 {make(chan int), 0},
376
377                 {[]int(nil), 0},
378                 {map[int]int(nil), 0},
379                 {(chan int)(nil), 0},
380         }
381
382         for _, c := range cases {
383                 assert.True(mockAssert.Len(c.v, c.l), "%#v have %d items", c.v, c.l)
384         }
385 }
386
387 func TestWithinDurationWrapper(t *testing.T) {
388         assert := New(t)
389         mockAssert := New(new(testing.T))
390         a := time.Now()
391         b := a.Add(10 * time.Second)
392
393         assert.True(mockAssert.WithinDuration(a, b, 10*time.Second), "A 10s difference is within a 10s time difference")
394         assert.True(mockAssert.WithinDuration(b, a, 10*time.Second), "A 10s difference is within a 10s time difference")
395
396         assert.False(mockAssert.WithinDuration(a, b, 9*time.Second), "A 10s difference is not within a 9s time difference")
397         assert.False(mockAssert.WithinDuration(b, a, 9*time.Second), "A 10s difference is not within a 9s time difference")
398
399         assert.False(mockAssert.WithinDuration(a, b, -9*time.Second), "A 10s difference is not within a 9s time difference")
400         assert.False(mockAssert.WithinDuration(b, a, -9*time.Second), "A 10s difference is not within a 9s time difference")
401
402         assert.False(mockAssert.WithinDuration(a, b, -11*time.Second), "A 10s difference is not within a 9s time difference")
403         assert.False(mockAssert.WithinDuration(b, a, -11*time.Second), "A 10s difference is not within a 9s time difference")
404 }
405
406 func TestInDeltaWrapper(t *testing.T) {
407         assert := New(new(testing.T))
408
409         True(t, assert.InDelta(1.001, 1, 0.01), "|1.001 - 1| <= 0.01")
410         True(t, assert.InDelta(1, 1.001, 0.01), "|1 - 1.001| <= 0.01")
411         True(t, assert.InDelta(1, 2, 1), "|1 - 2| <= 1")
412         False(t, assert.InDelta(1, 2, 0.5), "Expected |1 - 2| <= 0.5 to fail")
413         False(t, assert.InDelta(2, 1, 0.5), "Expected |2 - 1| <= 0.5 to fail")
414         False(t, assert.InDelta("", nil, 1), "Expected non numerals to fail")
415
416         cases := []struct {
417                 a, b  interface{}
418                 delta float64
419         }{
420                 {uint8(2), uint8(1), 1},
421                 {uint16(2), uint16(1), 1},
422                 {uint32(2), uint32(1), 1},
423                 {uint64(2), uint64(1), 1},
424
425                 {int(2), int(1), 1},
426                 {int8(2), int8(1), 1},
427                 {int16(2), int16(1), 1},
428                 {int32(2), int32(1), 1},
429                 {int64(2), int64(1), 1},
430
431                 {float32(2), float32(1), 1},
432                 {float64(2), float64(1), 1},
433         }
434
435         for _, tc := range cases {
436                 True(t, assert.InDelta(tc.a, tc.b, tc.delta), "Expected |%V - %V| <= %v", tc.a, tc.b, tc.delta)
437         }
438 }
439
440 func TestInEpsilonWrapper(t *testing.T) {
441         assert := New(new(testing.T))
442
443         cases := []struct {
444                 a, b    interface{}
445                 epsilon float64
446         }{
447                 {uint8(2), uint16(2), .001},
448                 {2.1, 2.2, 0.1},
449                 {2.2, 2.1, 0.1},
450                 {-2.1, -2.2, 0.1},
451                 {-2.2, -2.1, 0.1},
452                 {uint64(100), uint8(101), 0.01},
453                 {0.1, -0.1, 2},
454         }
455
456         for _, tc := range cases {
457                 True(t, assert.InEpsilon(tc.a, tc.b, tc.epsilon, "Expected %V and %V to have a relative difference of %v", tc.a, tc.b, tc.epsilon))
458         }
459
460         cases = []struct {
461                 a, b    interface{}
462                 epsilon float64
463         }{
464                 {uint8(2), int16(-2), .001},
465                 {uint64(100), uint8(102), 0.01},
466                 {2.1, 2.2, 0.001},
467                 {2.2, 2.1, 0.001},
468                 {2.1, -2.2, 1},
469                 {2.1, "bla-bla", 0},
470                 {0.1, -0.1, 1.99},
471         }
472
473         for _, tc := range cases {
474                 False(t, assert.InEpsilon(tc.a, tc.b, tc.epsilon, "Expected %V and %V to have a relative difference of %v", tc.a, tc.b, tc.epsilon))
475         }
476 }
477
478 func TestRegexpWrapper(t *testing.T) {
479
480         assert := New(new(testing.T))
481
482         cases := []struct {
483                 rx, str string
484         }{
485                 {"^start", "start of the line"},
486                 {"end$", "in the end"},
487                 {"[0-9]{3}[.-]?[0-9]{2}[.-]?[0-9]{2}", "My phone number is 650.12.34"},
488         }
489
490         for _, tc := range cases {
491                 True(t, assert.Regexp(tc.rx, tc.str))
492                 True(t, assert.Regexp(regexp.MustCompile(tc.rx), tc.str))
493                 False(t, assert.NotRegexp(tc.rx, tc.str))
494                 False(t, assert.NotRegexp(regexp.MustCompile(tc.rx), tc.str))
495         }
496
497         cases = []struct {
498                 rx, str string
499         }{
500                 {"^asdfastart", "Not the start of the line"},
501                 {"end$", "in the end."},
502                 {"[0-9]{3}[.-]?[0-9]{2}[.-]?[0-9]{2}", "My phone number is 650.12a.34"},
503         }
504
505         for _, tc := range cases {
506                 False(t, assert.Regexp(tc.rx, tc.str), "Expected \"%s\" to not match \"%s\"", tc.rx, tc.str)
507                 False(t, assert.Regexp(regexp.MustCompile(tc.rx), tc.str))
508                 True(t, assert.NotRegexp(tc.rx, tc.str))
509                 True(t, assert.NotRegexp(regexp.MustCompile(tc.rx), tc.str))
510         }
511 }
512
513 func TestZeroWrapper(t *testing.T) {
514         assert := New(t)
515         mockAssert := New(new(testing.T))
516
517         for _, test := range zeros {
518                 assert.True(mockAssert.Zero(test), "Zero should return true for %v", test)
519         }
520
521         for _, test := range nonZeros {
522                 assert.False(mockAssert.Zero(test), "Zero should return false for %v", test)
523         }
524 }
525
526 func TestNotZeroWrapper(t *testing.T) {
527         assert := New(t)
528         mockAssert := New(new(testing.T))
529
530         for _, test := range zeros {
531                 assert.False(mockAssert.NotZero(test), "Zero should return true for %v", test)
532         }
533
534         for _, test := range nonZeros {
535                 assert.True(mockAssert.NotZero(test), "Zero should return false for %v", test)
536         }
537 }
538
539 func TestJSONEqWrapper_EqualSONString(t *testing.T) {
540         assert := New(new(testing.T))
541         if !assert.JSONEq(`{"hello": "world", "foo": "bar"}`, `{"hello": "world", "foo": "bar"}`) {
542                 t.Error("JSONEq should return true")
543         }
544
545 }
546
547 func TestJSONEqWrapper_EquivalentButNotEqual(t *testing.T) {
548         assert := New(new(testing.T))
549         if !assert.JSONEq(`{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`) {
550                 t.Error("JSONEq should return true")
551         }
552
553 }
554
555 func TestJSONEqWrapper_HashOfArraysAndHashes(t *testing.T) {
556         assert := New(new(testing.T))
557         if !assert.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}",
558                 "{\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}") {
559                 t.Error("JSONEq should return true")
560         }
561 }
562
563 func TestJSONEqWrapper_Array(t *testing.T) {
564         assert := New(new(testing.T))
565         if !assert.JSONEq(`["foo", {"hello": "world", "nested": "hash"}]`, `["foo", {"nested": "hash", "hello": "world"}]`) {
566                 t.Error("JSONEq should return true")
567         }
568
569 }
570
571 func TestJSONEqWrapper_HashAndArrayNotEquivalent(t *testing.T) {
572         assert := New(new(testing.T))
573         if assert.JSONEq(`["foo", {"hello": "world", "nested": "hash"}]`, `{"foo": "bar", {"nested": "hash", "hello": "world"}}`) {
574                 t.Error("JSONEq should return false")
575         }
576 }
577
578 func TestJSONEqWrapper_HashesNotEquivalent(t *testing.T) {
579         assert := New(new(testing.T))
580         if assert.JSONEq(`{"foo": "bar"}`, `{"foo": "bar", "hello": "world"}`) {
581                 t.Error("JSONEq should return false")
582         }
583 }
584
585 func TestJSONEqWrapper_ActualIsNotJSON(t *testing.T) {
586         assert := New(new(testing.T))
587         if assert.JSONEq(`{"foo": "bar"}`, "Not JSON") {
588                 t.Error("JSONEq should return false")
589         }
590 }
591
592 func TestJSONEqWrapper_ExpectedIsNotJSON(t *testing.T) {
593         assert := New(new(testing.T))
594         if assert.JSONEq("Not JSON", `{"foo": "bar", "hello": "world"}`) {
595                 t.Error("JSONEq should return false")
596         }
597 }
598
599 func TestJSONEqWrapper_ExpectedAndActualNotJSON(t *testing.T) {
600         assert := New(new(testing.T))
601         if assert.JSONEq("Not JSON", "Not JSON") {
602                 t.Error("JSONEq should return false")
603         }
604 }
605
606 func TestJSONEqWrapper_ArraysOfDifferentOrder(t *testing.T) {
607         assert := New(new(testing.T))
608         if assert.JSONEq(`["foo", {"hello": "world", "nested": "hash"}]`, `[{ "hello": "world", "nested": "hash"}, "foo"]`) {
609                 t.Error("JSONEq should return false")
610         }
611 }