-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassert_test.go
More file actions
178 lines (166 loc) · 3.74 KB
/
Copy pathassert_test.go
File metadata and controls
178 lines (166 loc) · 3.74 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
package tester
import (
"fmt"
"io"
"testing"
)
// testT implements the [testingT] interface
// and replaces [mocT] for testing.
type testT struct{}
func (t *testT) Error(args ...any) {}
func TestAssertEqual(t *testing.T) {
t.Cleanup(func() { mocT = nil })
t.Run("string", func(t *testing.T) {
t.Run("equal", func(t *testing.T) {
tt := &testT{}
mocT = tt
equal := AssertEqual(t, "foo", "foo")
if !equal {
t.Error("values should be equal")
}
})
t.Run("not equal", func(t *testing.T) {
tt := &testT{}
mocT = tt
equal := AssertEqual(t, "foo", "bar")
if equal {
t.Error("values should not be equal")
}
})
})
t.Run("pointer", func(t *testing.T) {
t.Run("equal", func(t *testing.T) {
v1 := &struct{ s string }{"foo"}
tt := &testT{}
mocT = tt
equal := AssertEqual(t, v1, v1)
if !equal {
t.Error("values should be equal")
}
})
t.Run("not equal", func(t *testing.T) {
v1 := &struct{ s string }{"foo"}
v2 := &struct{ s string }{"foo"}
tt := &testT{}
mocT = tt
equal := AssertEqual(t, v1, v2)
if equal {
t.Error("values should not be equal")
}
})
})
}
func TestAssertDeepEqual(t *testing.T) {
t.Cleanup(func() { mocT = nil })
t.Run("string", func(t *testing.T) {
t.Run("equal", func(t *testing.T) {
tt := &testT{}
mocT = tt
equal := AssertDeepEqual(t, "foo", "foo")
if !equal {
t.Error("values should be equal")
}
})
t.Run("not equal", func(t *testing.T) {
tt := &testT{}
mocT = tt
equal := AssertDeepEqual(t, "foo", "bar")
if equal {
t.Error("values should not be equal")
}
})
})
t.Run("pointer", func(t *testing.T) {
t.Run("equal", func(t *testing.T) {
v1 := &struct{ s string }{"foo"}
v2 := &struct{ s string }{"foo"}
tt := &testT{}
mocT = tt
equal := AssertDeepEqual(t, v1, v2)
if !equal {
t.Error("values should be equal")
}
})
t.Run("not equal", func(t *testing.T) {
v1 := &struct{ s string }{"foo"}
v2 := &struct{ s string }{"bar"}
tt := &testT{}
mocT = tt
equal := AssertDeepEqual(t, v1, v2)
if equal {
t.Error("values should not be equal")
}
})
})
}
func TestAssertEqualErr(t *testing.T) {
t.Cleanup(func() { mocT = nil })
t.Run("equal", func(t *testing.T) {
tt := &testT{}
mocT = tt
equal := AssertEqualErr(t, io.EOF, io.EOF)
if !equal {
t.Error("values should be equal")
}
})
t.Run("wrapped got", func(t *testing.T) {
tt := &testT{}
mocT = tt
equal := AssertEqualErr(t, io.EOF, fmt.Errorf("wrap [%w]", io.EOF))
if !equal {
t.Error("values should be equal")
}
})
t.Run("wrapped want", func(t *testing.T) {
tt := &testT{}
mocT = tt
equal := AssertEqualErr(t, fmt.Errorf("wrap [%w]", io.EOF), io.EOF)
if equal {
t.Error("values should not be equal")
}
})
t.Run("not equal", func(t *testing.T) {
tt := &testT{}
mocT = tt
equal := AssertDeepEqual(t, io.EOF, io.ErrUnexpectedEOF)
if equal {
t.Error("values should not be equal")
}
})
}
func TestAssertPanic(t *testing.T) {
t.Cleanup(func() { mocT = nil })
t.Run("no panic", func(t *testing.T) {
tt := &testT{}
mocT = tt
r, panicked := AssertPanic(t, func() {})
if panicked {
t.Error("test should not panic")
}
if r != nil {
t.Error("recovered value should nil")
}
})
t.Run("panic error", func(t *testing.T) {
tt := &testT{}
mocT = tt
r, panicked := AssertPanic(t, func() { panic(io.EOF) })
if !panicked {
t.Error("test should panic")
}
if r != io.EOF {
t.Error("recovered value not match")
}
})
t.Run("panic string", func(t *testing.T) {
tt := &testT{}
mocT = tt
r, panicked := AssertPanic(t, func() { panic("string") })
if !panicked {
t.Error("test should panic")
}
if r != "string" {
t.Error("recovered value not match")
}
})
}