-
Notifications
You must be signed in to change notification settings - Fork 191
/
asserts_test.go
322 lines (245 loc) · 8.42 KB
/
asserts_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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package assert_test
import (
"errors"
"fmt"
"reflect"
"testing"
"github.com/gookit/goutil/testutil/assert"
)
type tCustomTesting struct {
*testing.T
// logs []string
errs []string
}
// FailNow marks the function as having failed and stops its execution.
func (tc *tCustomTesting) FailNow() {}
// Error logs args to error
func (tc *tCustomTesting) Error(args ...interface{}) {
tc.errs = append(tc.errs, fmt.Sprint(args...))
}
// First get the first error message
func (tc *tCustomTesting) First() string {
if len(tc.errs) > 0 {
return tc.errs[0]
}
return ""
}
// Reset error messages
func (tc *tCustomTesting) Reset() {
tc.errs = []string{}
}
// ResetGet reset error messages and return first message
func (tc *tCustomTesting) ResetGet() string {
if len(tc.errs) > 0 {
first := tc.errs[0]
tc.errs = []string{}
return first
}
return ""
}
func TestCommon_success(t *testing.T) {
assert.Nil(t, nil)
assert.False(t, false)
assert.True(t, true)
assert.Empty(t, "")
assert.Empty(t, nil)
assert.NotEmpty(t, "abc")
// eq
assert.Eq(t, 1, 1)
assert.Eq(t, nil, nil)
assert.Equal(t, 1, 1)
// neq
assert.Neq(t, 1, 2)
assert.NotEq(t, 1, 2)
assert.NotEqual(t, 1, 2)
// kind
assert.IsType(t, 1, 1)
assert.IsKind(t, reflect.Int, 1)
// same
val := new(int)
*val = 1
assert.Same(t, val, val)
assert.NotSame(t, 2, 2)
assert.NotSame(t, 1, 2)
// panics
assert.Panics(t, func() {
panic("hh")
})
assert.NotPanics(t, func() {})
assert.PanicsMsg(t, func() {
panic("hh")
}, "hh")
assert.PanicsErrMsg(t, func() {
panic(errors.New("hh"))
}, "hh")
}
func TestCommon_fail(t *testing.T) {
assert.DisableColor()
defer func() {
assert.EnableColor = true
}()
tc := &tCustomTesting{T: t}
// nil
assert.Nil(tc, 1)
str := tc.First()
assert.StrContains(t, str, "TestCommon_fail")
assert.StrContains(t, str, "goutil/testutil/assert/asserts_test.go:")
assert.StrContains(t, str, "Expected nil, but got:")
assert.StrNotContains(t, str, "NOT EXIST")
tc.Reset()
assert.NotNil(tc, nil)
assert.StrContains(t, tc.ResetGet(), "Should not nil value")
// false
assert.False(tc, true)
assert.StrContains(t, tc.ResetGet(), "Result should be False")
// true
assert.True(tc, false)
assert.StrContains(t, tc.ResetGet(), "Result should be True")
assert.True(tc, false, "user custom message")
assert.StrContains(t, tc.ResetGet(), "user custom message")
// empty
assert.Empty(tc, "abc")
assert.StrContains(t, tc.ResetGet(), "Should be empty, but was:")
assert.NotEmpty(tc, "")
assert.StrContains(t, tc.ResetGet(), "Should not be empty, but was:")
// error
assert.Error(tc, nil)
assert.StrContains(t, tc.ResetGet(), "An error is expected but got nil.")
err := errors.New("want error msg")
err1 := errors.New("another error msg")
assert.ErrIs(tc, nil, err)
assert.StrContains(t, tc.ResetGet(), "An error is expected but got nil.")
assert.ErrIs(tc, err, err1)
assert.StrContains(t, tc.ResetGet(), "Expect given err is equals")
assert.ErrMsg(tc, nil, "want error msg")
assert.StrContains(t, tc.ResetGet(), "An error is expected but got nil.")
assert.ErrMsg(tc, err1, "want error msg")
assert.StrContains(t, tc.ResetGet(), "Error message not equal:")
assert.ErrSubMsg(tc, nil, "want error msg")
assert.StrContains(t, tc.ResetGet(), "An error is expected but got nil.")
assert.ErrSubMsg(tc, err1, "want error msg")
assert.StrContains(t, tc.ResetGet(), "Error message check fail:")
// eq
assert.Eq(tc, 1, 2)
assert.StrContains(t, tc.ResetGet(), "Not equal:")
assert.Eq(tc, tc.ResetGet, 2)
assert.StrContains(t, tc.ResetGet(), "cannot take func type as argument")
assert.Equal(tc, 1, 2)
assert.StrContains(t, tc.ResetGet(), "Not equal:")
// neq
assert.Neq(tc, 1, 1)
assert.StrContains(t, tc.ResetGet(), "Given should not be: 1")
assert.NotEq(tc, 1, 1)
assert.StrContains(t, tc.ResetGet(), "Given should not be: 1")
assert.NotEq(tc, tc.ResetGet, 2)
assert.StrContains(t, tc.ResetGet(), "cannot take func type as argument")
assert.NotEqual(tc, 1, 1)
assert.StrContains(t, tc.ResetGet(), "Given should not be: 1")
// kind
assert.IsType(tc, 1, "1")
assert.StrContains(t, tc.ResetGet(), "Expected to be of type int, but was string")
assert.IsKind(tc, reflect.Int, "1")
assert.StrContains(t, tc.ResetGet(), "Expected to be of kind int, but was string")
// same
val := new(int)
*val = 1
assert.Same(tc, val, 2)
assert.StrContains(t, tc.ResetGet(), "Not same: ")
assert.NotSame(tc, val, val)
assert.StrContains(t, tc.ResetGet(), "Expect and actual is same object:")
// compare
assert.Lt(tc, 2, 1)
assert.StrContains(t, tc.ResetGet(), "Given 2 should less than 1")
assert.Lte(tc, 2, 1)
assert.StrContains(t, tc.ResetGet(), "Given 2 should less than or equal 1")
assert.Gt(tc, 1, 2)
assert.StrContains(t, tc.ResetGet(), "Given 1 should greater than 2")
assert.Gte(tc, 1, 2)
assert.StrContains(t, tc.ResetGet(), "Given 1 should greater than or equal 2")
// contains
assert.Contains(tc, "abc", "d")
assert.StrContains(t, tc.ResetGet(), "Should contain:")
assert.Contains(tc, nil, "d")
assert.StrContains(t, tc.ResetGet(), "could not be applied builtin len()")
assert.NotContains(tc, "abc", "a")
assert.StrContains(t, tc.ResetGet(), "Should not contain:")
assert.StrContains(tc, "abc", "d")
assert.StrContains(t, tc.ResetGet(), "String check fail:")
// contains key
assert.ContainsKey(tc, map[string]int{"a": 1}, "b")
assert.StrContains(t, tc.ResetGet(), "Map should contains the key:")
assert.NotContainsKey(tc, map[string]int{"a": 1}, "a")
assert.StrContains(t, tc.ResetGet(), "Map should not contains the key:")
// contains keys
assert.ContainsKeys(tc, map[string]int{"a": 1}, []string{"a", "b"})
assert.StrContains(t, tc.ResetGet(), "Map should contains the key:")
assert.ContainsKeys(tc, map[string]int{"a": 1}, "invalid-type")
assert.StrContains(t, tc.ResetGet(), "input param type is invalid")
assert.NotContainsKeys(tc, map[string]int{"a": 1}, []string{"a"})
assert.StrContains(t, tc.ResetGet(), "Map should not contains the key:")
assert.NotContainsKeys(tc, map[string]int{"a": 1}, "invalid-type")
assert.StrContains(t, tc.ResetGet(), "input param type is invalid")
// len
assert.Len(tc, "abc", 4)
assert.StrContains(t, tc.ResetGet(), "should have 4 item(s), but has 3")
assert.Len(tc, tc.ResetGet, 4)
assert.StrContains(t, tc.ResetGet(), "type 'func() string' could not be calc length")
assert.LenGt(tc, "abc", 3)
assert.StrContains(t, tc.ResetGet(), "should have more than 3 item(s), but has 3")
assert.LenGt(tc, tc.ResetGet, 4)
assert.StrContains(t, tc.ResetGet(), "type 'func() string' could not be calc length")
// panics
assert.Panics(tc, func() {})
assert.StrContains(t, tc.ResetGet(), "should panic")
assert.PanicsMsg(tc, func() {}, "custom message")
assert.StrContains(t, tc.ResetGet(), "should panic")
assert.PanicsMsg(tc, func() {
panic("user custom message")
}, "custom message")
assert.StrContains(t, tc.ResetGet(), "custom message")
assert.PanicsErrMsg(tc, func() {}, "custom message")
assert.StrContains(t, tc.ResetGet(), "should panic")
assert.PanicsErrMsg(tc, func() {
panic("user custom message")
}, "user custom message")
assert.StrContains(t, tc.ResetGet(), "should panic and is error type")
assert.PanicsErrMsg(tc, func() {
panic(errors.New("user custom message"))
}, "not custom message")
assert.StrContains(t, tc.ResetGet(), "not custom message")
assert.NotPanics(tc, func() {
panic("user custom message")
})
assert.StrContains(t, tc.ResetGet(), "should not panic")
// fail
assert.Fail(tc, "custom message1")
assert.StrContains(t, tc.ResetGet(), "custom message1")
assert.FailNow(tc, "custom message2")
assert.StrContains(t, tc.ResetGet(), "custom message2")
}
func TestErr(t *testing.T) {
assert.NoErr(t, nil)
assert.NoError(t, nil)
err := errors.New("this is a error")
// assert2.EqualError(t, err, "user custom message")
assert.Err(t, err, "user custom message")
assert.Error(t, err)
assert.ErrMsg(t, err, "this is a error")
}
func TestContains(t *testing.T) {
str := "abc+123"
assert.StrContains(t, str, "123")
assert.Contains(t, str, "123")
assert.NotContains(t, str, "456")
assert.StrCount(t, str, "123", 1)
mp := map[string]any{
"age": 456,
"name": "inhere",
}
assert.ContainsKey(t, mp, "name")
assert.ContainsKeys(t, mp, []string{"name", "age"})
assert.NotContainsKey(t, mp, "addr")
assert.NotContainsKeys(t, mp, []string{"addr"})
assert.ContainsElems(t, []string{"def"}, []string{"def"})
assert.ContainsElems(t, []string{"def", "abc"}, []string{"def"})
}