-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathassertion_test.go
153 lines (116 loc) · 2.67 KB
/
assertion_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
// SPDX-FileCopyrightText: 2014-2024 caixw
//
// SPDX-License-Identifier: MIT
package assert
import (
"database/sql"
"regexp"
"testing"
"time"
)
type errorImpl struct {
msg string
}
func (err *errorImpl) Error() string {
return err.msg
}
func TestAssertion_True_False(t *testing.T) {
a := New(t, true)
if t != a.TB() {
t.Error("a.T与t不相等")
}
a.True(true)
a.True(true, "a.True(5==5 failed")
a.False(false, "a.False(false) failed")
a.False(false, "a.False(4==5) failed")
}
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 interface{}
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 interface{}
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)
a.NotSame(f, 5)
}
func TestAssertion_Match(t *testing.T) {
a := New(t, false)
a.Match(regexp.MustCompile("^[1-9]*$"), "123")
a.NotMatch(regexp.MustCompile("^[1-9]*$"), "x123")
a.Match(regexp.MustCompile("^[1-9]*$"), []byte("123"))
a.NotMatch(regexp.MustCompile("^[1-9]*$"), []byte("x123"))
}
func TestAssert_When(t *testing.T) {
a := New(t, false)
a.When(true, func(a *Assertion) {
a.True(true)
})
}