10 func TestImplementsWrapper(t *testing.T) {
11 assert := New(new(testing.T))
13 if !assert.Implements((*AssertionTesterInterface)(nil), new(AssertionTesterConformingObject)) {
14 t.Error("Implements method should return true: AssertionTesterConformingObject implements AssertionTesterInterface")
16 if assert.Implements((*AssertionTesterInterface)(nil), new(AssertionTesterNonConformingObject)) {
17 t.Error("Implements method should return false: AssertionTesterNonConformingObject does not implements AssertionTesterInterface")
21 func TestIsTypeWrapper(t *testing.T) {
22 assert := New(new(testing.T))
24 if !assert.IsType(new(AssertionTesterConformingObject), new(AssertionTesterConformingObject)) {
25 t.Error("IsType should return true: AssertionTesterConformingObject is the same type as AssertionTesterConformingObject")
27 if assert.IsType(new(AssertionTesterConformingObject), new(AssertionTesterNonConformingObject)) {
28 t.Error("IsType should return false: AssertionTesterConformingObject is not the same type as AssertionTesterNonConformingObject")
33 func TestEqualWrapper(t *testing.T) {
34 assert := New(new(testing.T))
36 if !assert.Equal("Hello World", "Hello World") {
37 t.Error("Equal should return true")
39 if !assert.Equal(123, 123) {
40 t.Error("Equal should return true")
42 if !assert.Equal(123.5, 123.5) {
43 t.Error("Equal should return true")
45 if !assert.Equal([]byte("Hello World"), []byte("Hello World")) {
46 t.Error("Equal should return true")
48 if !assert.Equal(nil, nil) {
49 t.Error("Equal should return true")
53 func TestEqualValuesWrapper(t *testing.T) {
54 assert := New(new(testing.T))
56 if !assert.EqualValues(uint32(10), int32(10)) {
57 t.Error("EqualValues should return true")
61 func TestNotNilWrapper(t *testing.T) {
62 assert := New(new(testing.T))
64 if !assert.NotNil(new(AssertionTesterConformingObject)) {
65 t.Error("NotNil should return true: object is not nil")
67 if assert.NotNil(nil) {
68 t.Error("NotNil should return false: object is nil")
73 func TestNilWrapper(t *testing.T) {
74 assert := New(new(testing.T))
77 t.Error("Nil should return true: object is nil")
79 if assert.Nil(new(AssertionTesterConformingObject)) {
80 t.Error("Nil should return false: object is not nil")
85 func TestTrueWrapper(t *testing.T) {
86 assert := New(new(testing.T))
88 if !assert.True(true) {
89 t.Error("True should return true")
91 if assert.True(false) {
92 t.Error("True should return false")
97 func TestFalseWrapper(t *testing.T) {
98 assert := New(new(testing.T))
100 if !assert.False(false) {
101 t.Error("False should return true")
103 if assert.False(true) {
104 t.Error("False should return false")
109 func TestExactlyWrapper(t *testing.T) {
110 assert := New(new(testing.T))
117 if assert.Exactly(a, b) {
118 t.Error("Exactly should return false")
120 if assert.Exactly(a, d) {
121 t.Error("Exactly should return false")
123 if !assert.Exactly(a, c) {
124 t.Error("Exactly should return true")
127 if assert.Exactly(nil, a) {
128 t.Error("Exactly should return false")
130 if assert.Exactly(a, nil) {
131 t.Error("Exactly should return false")
136 func TestNotEqualWrapper(t *testing.T) {
138 assert := New(new(testing.T))
140 if !assert.NotEqual("Hello World", "Hello World!") {
141 t.Error("NotEqual should return true")
143 if !assert.NotEqual(123, 1234) {
144 t.Error("NotEqual should return true")
146 if !assert.NotEqual(123.5, 123.55) {
147 t.Error("NotEqual should return true")
149 if !assert.NotEqual([]byte("Hello World"), []byte("Hello World!")) {
150 t.Error("NotEqual should return true")
152 if !assert.NotEqual(nil, new(AssertionTesterConformingObject)) {
153 t.Error("NotEqual should return true")
157 func TestContainsWrapper(t *testing.T) {
159 assert := New(new(testing.T))
160 list := []string{"Foo", "Bar"}
162 if !assert.Contains("Hello World", "Hello") {
163 t.Error("Contains should return true: \"Hello World\" contains \"Hello\"")
165 if assert.Contains("Hello World", "Salut") {
166 t.Error("Contains should return false: \"Hello World\" does not contain \"Salut\"")
169 if !assert.Contains(list, "Foo") {
170 t.Error("Contains should return true: \"[\"Foo\", \"Bar\"]\" contains \"Foo\"")
172 if assert.Contains(list, "Salut") {
173 t.Error("Contains should return false: \"[\"Foo\", \"Bar\"]\" does not contain \"Salut\"")
178 func TestNotContainsWrapper(t *testing.T) {
180 assert := New(new(testing.T))
181 list := []string{"Foo", "Bar"}
183 if !assert.NotContains("Hello World", "Hello!") {
184 t.Error("NotContains should return true: \"Hello World\" does not contain \"Hello!\"")
186 if assert.NotContains("Hello World", "Hello") {
187 t.Error("NotContains should return false: \"Hello World\" contains \"Hello\"")
190 if !assert.NotContains(list, "Foo!") {
191 t.Error("NotContains should return true: \"[\"Foo\", \"Bar\"]\" does not contain \"Foo!\"")
193 if assert.NotContains(list, "Foo") {
194 t.Error("NotContains should return false: \"[\"Foo\", \"Bar\"]\" contains \"Foo\"")
199 func TestConditionWrapper(t *testing.T) {
201 assert := New(new(testing.T))
203 if !assert.Condition(func() bool { return true }, "Truth") {
204 t.Error("Condition should return true")
207 if assert.Condition(func() bool { return false }, "Lie") {
208 t.Error("Condition should return false")
213 func TestDidPanicWrapper(t *testing.T) {
215 if funcDidPanic, _ := didPanic(func() {
218 t.Error("didPanic should return true")
221 if funcDidPanic, _ := didPanic(func() {
223 t.Error("didPanic should return false")
228 func TestPanicsWrapper(t *testing.T) {
230 assert := New(new(testing.T))
232 if !assert.Panics(func() {
235 t.Error("Panics should return true")
238 if assert.Panics(func() {
240 t.Error("Panics should return false")
245 func TestNotPanicsWrapper(t *testing.T) {
247 assert := New(new(testing.T))
249 if !assert.NotPanics(func() {
251 t.Error("NotPanics should return true")
254 if assert.NotPanics(func() {
257 t.Error("NotPanics should return false")
262 func TestNoErrorWrapper(t *testing.T) {
264 mockAssert := New(new(testing.T))
266 // start with a nil error
269 assert.True(mockAssert.NoError(err), "NoError should return True for nil arg")
272 err = errors.New("Some error")
274 assert.False(mockAssert.NoError(err), "NoError with error should return False")
278 func TestErrorWrapper(t *testing.T) {
280 mockAssert := New(new(testing.T))
282 // start with a nil error
285 assert.False(mockAssert.Error(err), "Error should return False for nil arg")
288 err = errors.New("Some error")
290 assert.True(mockAssert.Error(err), "Error with error should return True")
294 func TestEqualErrorWrapper(t *testing.T) {
296 mockAssert := New(new(testing.T))
298 // start with a nil error
300 assert.False(mockAssert.EqualError(err, ""),
301 "EqualError should return false for nil arg")
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")
311 func TestEmptyWrapper(t *testing.T) {
313 mockAssert := New(new(testing.T))
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")
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")
329 func TestNotEmptyWrapper(t *testing.T) {
331 mockAssert := New(new(testing.T))
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")
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")
347 func TestLenWrapper(t *testing.T) {
349 mockAssert := New(new(testing.T))
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")
358 ch := make(chan int, 5)
368 {[...]int{1, 2, 3}, 3},
370 {map[int]int{1: 2, 2: 4, 3: 6}, 3},
378 {map[int]int(nil), 0},
379 {(chan int)(nil), 0},
382 for _, c := range cases {
383 assert.True(mockAssert.Len(c.v, c.l), "%#v have %d items", c.v, c.l)
387 func TestWithinDurationWrapper(t *testing.T) {
389 mockAssert := New(new(testing.T))
391 b := a.Add(10 * time.Second)
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")
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")
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")
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")
406 func TestInDeltaWrapper(t *testing.T) {
407 assert := New(new(testing.T))
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")
420 {uint8(2), uint8(1), 1},
421 {uint16(2), uint16(1), 1},
422 {uint32(2), uint32(1), 1},
423 {uint64(2), uint64(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},
431 {float32(2), float32(1), 1},
432 {float64(2), float64(1), 1},
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)
440 func TestInEpsilonWrapper(t *testing.T) {
441 assert := New(new(testing.T))
447 {uint8(2), uint16(2), .001},
452 {uint64(100), uint8(101), 0.01},
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))
464 {uint8(2), int16(-2), .001},
465 {uint64(100), uint8(102), 0.01},
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))
478 func TestRegexpWrapper(t *testing.T) {
480 assert := New(new(testing.T))
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"},
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))
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"},
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))
513 func TestZeroWrapper(t *testing.T) {
515 mockAssert := New(new(testing.T))
517 for _, test := range zeros {
518 assert.True(mockAssert.Zero(test), "Zero should return true for %v", test)
521 for _, test := range nonZeros {
522 assert.False(mockAssert.Zero(test), "Zero should return false for %v", test)
526 func TestNotZeroWrapper(t *testing.T) {
528 mockAssert := New(new(testing.T))
530 for _, test := range zeros {
531 assert.False(mockAssert.NotZero(test), "Zero should return true for %v", test)
534 for _, test := range nonZeros {
535 assert.True(mockAssert.NotZero(test), "Zero should return false for %v", test)
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")
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")
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")
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")
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")
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")
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")
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")
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")
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")