Repository navigation
Expand file tree
/
Copy pathassertion_test.go
More file actions
237 lines (189 loc) · 4.6 KB
/
Copy pathassertion_test.go
File metadata and controls
237 lines (189 loc) · 4.6 KB
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
// SPDX-FileCopyrightText: 2014-2026 caixw
//
// SPDX-License-Identifier: MIT
package assert
import (
"bytes"
"database/sql"
"fmt"
"regexp"
"strings"
"testing"
"time"
)
type errorImpl struct {
msg string
}
func (err *errorImpl) Error() string {
return err.msg
}
func TestAssertion_True_False(t *testing.T) {
out := &bytes.Buffer{}
a := newWithLogEnv(t, func(a ...any) { fmt.Fprint(out, a...) }, nil)
if t != a.TB() {
t.Error("a.T 与 t 不相等")
}
t.Run("True", func(t *testing.T) {
out := &bytes.Buffer{}
a := newWithLogEnv(t, func(a ...any) { fmt.Fprint(out, a...) }, nil)
a.True(true).
True(true, "a.True(5==5) failed")
if out.Len() > 0 {
t.Error("断言出错了")
}
a.True(false, "a.True(5==5) failed")
if !strings.Contains(out.String(), "a.True(5==5) failed") {
t.Error("断言出错了")
}
})
t.Run("False", func(t *testing.T) {
out := &bytes.Buffer{}
a := newWithLogEnv(t, func(a ...any) { fmt.Fprint(out, a...) }, nil)
a.False(false, "a.False(false) failed").
False(false, "a.False(4==5) failed")
if out.Len() > 0 {
t.Error("断言出错了")
}
a.False(true, "a.False(true) failed")
if !strings.Contains(out.String(), "a.False(true) failed") {
t.Error("断言出错了")
}
})
}
func TestAssertion_Equal_NotEqual_Nil_NotNil(t *testing.T) {
a := New(t, false)
v1 := 4
v2 := 4
v3 := 5
v4 := "5"
a.Equal(4, 4, "a.Equal(4,4) failed")
a.Equal(v1, v2, "a.Equal(v1,v2) failed")
a.NotEqual(4, 5, "a.NotEqual(4,5) failed").
NotEqual(v1, v3, "a.NotEqual(v1,v3) failed").
NotEqual(v3, v4, "a.NotEqual(v3,v4) failed")
var v5 any
v6 := 0
v7 := []int{}
a.Empty(v5, "a.Empty failed").
Empty(v6, "a.Empty(0) failed").
Empty(v7, "a.Empty(v7) failed")
a.NotEmpty(1, "a.NotEmpty(1) failed")
a.Nil(v5)
a.NotNil(v7, "a.Nil(v7) failed").
NotNil(v6, "a.NotNil(v6) failed")
}
func TestAssertion_Zero_NotZero(t *testing.T) {
a := New(t, false)
var v any
a.Zero(0)
a.Zero(nil)
a.Zero(time.Time{})
a.Zero(v)
a.Zero([2]int{0, 0})
a.Zero([0]int{})
a.Zero(&time.Time{})
a.Zero(sql.NullTime{})
a.NotZero([]int{0, 0})
a.NotZero([]int{})
}
func TestAssertion_Contains(t *testing.T) {
a := New(t, false)
a.Contains([]int{1, 2, 3}, []int8{1, 2}).
NotContains([]int{1, 2, 3}, []int8{1, 3})
}
func TestAssertion_TypeEqual(t *testing.T) {
a := New(t, true)
a.TypeEqual(false, 1, 2)
a.TypeEqual(false, 1, 1)
a.TypeEqual(false, 1.0, 2.0)
v1 := 5
pv1 := &v1
a.TypeEqual(false, 1, v1)
a.TypeEqual(true, 1, &pv1)
v2 := &errorImpl{}
v3 := errorImpl{}
a.TypeEqual(false, v2, v2)
a.TypeEqual(true, v2, v3)
a.TypeEqual(true, v2, &v3)
a.TypeEqual(true, &v2, &v3)
}
func TestAssertion_Same(t *testing.T) {
a := New(t, false)
a.NotSame(5, 5).
NotSame(struct{}{}, struct{}{}).
NotSame(func() {}, func() {})
i := 5
a.NotSame(i, i)
empty := struct{}{}
empty2 := empty
a.NotSame(empty, empty)
a.NotSame(empty, empty2)
a.Same(&empty, &empty)
a.Same(&empty, &empty2)
f := func() {}
f2 := f
a.Same(f, f)
a.Same(f, f2)
a.NotSame(5, 5)
}
func TestAssertion_Match(t *testing.T) {
a := New(t, false)
t.Run("string", func(t *testing.T) {
a.Match(regexp.MustCompile("^[1-9]*$"), "123")
a.NotMatch(regexp.MustCompile("^[1-9]*$"), "x123")
})
t.Run("[]byte", func(t *testing.T) {
a.Match(regexp.MustCompile("^[1-9]*$"), []byte("123"))
a.NotMatch(regexp.MustCompile("^[1-9]*$"), []byte("x123"))
})
t.Run("[]rune", func(t *testing.T) {
a.Match(regexp.MustCompile("^[1-9]*$"), []rune("123"))
a.NotMatch(regexp.MustCompile("^[1-9]*$"), []rune("x123"))
})
}
func TestAssertion_When(t *testing.T) {
a := New(t, false)
a.When(true, func(a *Assertion) {
a.True(true)
})
}
func TestAssertion_Eventually(t *testing.T) {
out := &bytes.Buffer{}
a := newWithLogEnv(t, func(a ...any) { fmt.Fprint(out, a...) }, nil)
cnt := 0
a.Eventually(func() bool {
cnt++
if cnt > 10 {
return true
}
return false
}, 500*time.Microsecond)
if out.Len() > 0 {
t.Error("断言出错了")
}
a.Eventually(func() bool {
return false
}, 500*time.Microsecond, "Eventually always false")
if !strings.Contains(out.String(), "Eventually always false") {
t.Error("断言出错了")
}
}
func TestAssertion_Never(t *testing.T) {
out := &bytes.Buffer{}
a := newWithLogEnv(t, func(a ...any) { fmt.Fprint(out, a...) }, nil)
a.Never(func() bool { return false }, 500*time.Microsecond)
if out.Len() > 0 {
t.Error("断言出错了")
}
cnt := 0
a.Never(func() bool {
cnt++
if cnt > 10 {
return true
}
return false
}, 500*time.Microsecond, "Never return true")
if !strings.Contains(out.String(), "Never return true") {
t.Error("断言出错了")
}
}