forked from gookit/goutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassertions_test.go
79 lines (66 loc) · 1.62 KB
/
assertions_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package assert_test
import (
"errors"
"testing"
"github.com/gookit/goutil/testutil/assert"
)
func TestAssertions_Chain(t *testing.T) {
err := errors.New("error message")
as := assert.New(t).
NotEmpty(err).
NotNil(err).
Err(err).
Error(err).
ErrIs(err, err).
ErrMsg(err, "error message").
ErrSubMsg(err, "message").
Eq("error message", err.Error()).
Neq("message", err.Error()).
Equal("error message", err.Error()).
Contains(err.Error(), "message").
StrContains(err.Error(), "message").
NotContains(err.Error(), "success").
Gt(4, 3).
Lt(2, 3)
assert.True(t, as.IsOk())
assert.False(t, as.IsFail())
iv := 23
ss := []string{"a", "b"}
as = assert.New(t).WithMsg("prefix").
IsType(1, iv).
NotEq(22, iv).
NotEqual(22, iv).
Len(ss, 2).
LenGt(ss, 1).
Lte(iv, 23).
Gte(iv, 23).
Empty(0).
True(true).
False(false).
NoErr(nil).
NoError(nil).
Nil(nil).
ContainsKey(map[string]int{"a": 1}, "a")
assert.True(t, as.IsOk())
// Panics
assert.New(t).
Panics(func() { panic("panic") }).
NotPanics(func() {}).
PanicsMsg(func() { panic("panic") }, "panic").
PanicsErrMsg(func() { panic(errors.New("panic")) }, "panic")
}
func TestAssertions_chain_fail(t *testing.T) {
assert.HideFullPath()
defer func() {
assert.ShowFullPath = true
}()
tc := &tCustomTesting{T: t}
ts := assert.New(tc)
ts.Fail("fail message")
str := tc.ResetGet()
assert.StrContains(t, str, "fail message")
assert.StrContains(t, str, "assertions_test.go")
assert.NotContains(t, str, "testutil/assert/assertions_test.go")
ts.FailNow("fail now message")
assert.StrContains(t, tc.ResetGet(), "fail now message")
}