-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgt_test.go
193 lines (184 loc) · 5.12 KB
/
gt_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
package gt
import (
"errors"
"fmt"
"reflect"
"testing"
)
type mockTestingT struct {
called map[string]int
logFunc func(...any)
}
func (m *mockTestingT) Helper() {
}
func (m *mockTestingT) FailNow() {
m.called["FailNow"]++
}
func (m *mockTestingT) Fail() {
m.called["Fail"]++
}
func (m *mockTestingT) Log(args ...any) {
m.called["Log"]++
m.logFunc(args...)
}
func TestEqual(t *testing.T) {
errUnknown := errors.New("unknown")
tests := map[string]struct {
wantLog []any
wantCalled map[string]int
wantResult bool
execute func(testingT) bool
}{
"not equal": {
wantCalled: map[string]int{},
wantLog: []any{""},
wantResult: true,
execute: func(t testingT) bool {
return NotEqual(t, "prefix", "a", "b")
},
},
"not equal with equal values": {
wantCalled: map[string]int{"FailNow": 1, "Log": 1},
wantLog: []any{`prefix is equals`},
wantResult: false,
execute: func(t testingT) bool {
return NotEqual(t, "prefix", "a", "a")
},
},
"equal": {
wantCalled: map[string]int{},
wantLog: []any{""},
wantResult: true,
execute: func(t testingT) bool {
return Equal(t, "prefix", "a", "a")
},
},
"equal with custom diff func": {
wantCalled: map[string]int{},
wantLog: []any{""},
wantResult: true,
execute: func(t testingT) bool {
return Equal(t, "prefix2", "a", "b", WithDiffFunc(func(want, got any) string {
return ""
}))
},
},
"equal: not equal struct": {
wantCalled: map[string]int{"FailNow": 1, "Log": 1},
wantLog: []any{`struct
want: struct { name string; age int }{name:"no name", age:39}
got: struct { name string; age int }{name:"anyone", age:39}`},
wantResult: false,
execute: func(t testingT) bool {
return Equal(t,
"struct",
struct {
name string
age int
}{
name: "no name",
age: 39,
},
struct {
name string
age int
}{
name: "anyone",
age: 39,
},
)
},
},
"equal: not equal": {
wantCalled: map[string]int{"FailNow": 1, "Log": 1},
wantLog: []any{`not equals
want: "a"
got: "b"`},
wantResult: false,
execute: func(t testingT) bool {
return Equal(t, "not equals", "a", "b")
},
},
"iserror fail": {
wantCalled: map[string]int{"FailNow": 1, "Log": 1},
wantLog: []any{"iserror fail: want error \"unknown\" got \"test\""},
wantResult: false,
execute: func(t testingT) bool {
return ErrorIs(t, "iserror fail", fmt.Errorf("test"), errUnknown)
},
},
"iserror success": {
wantCalled: map[string]int{},
wantLog: []any{},
wantResult: true,
execute: func(t testingT) bool {
return ErrorIs(t, "iserror fail", fmt.Errorf("test: %w", errUnknown), errUnknown)
},
},
"iserror nil": {
wantCalled: map[string]int{"FailNow": 1, "Log": 1},
wantLog: []any{`iserror fail: want no error got "test: unknown"`},
wantResult: false,
execute: func(t testingT) bool {
return ErrorIs(t, "iserror fail", fmt.Errorf("test: %w", errUnknown), nil)
},
},
"with json": {
wantCalled: map[string]int{"FailNow": 1, "Log": 1},
wantLog: []any{`with json
want: map[string]interface {}{"id":1, "name":"test"}
got: map[string]interface {}{"id":2, "name":"test"}`},
wantResult: false,
execute: func(t testingT) bool {
return Equal(t, "with json", []byte(`{"id":1,"name": "test"}`), []byte(`{"id":2,"name": "test"}`), WithJSONDiff)
},
},
"with json invalid type": {
wantCalled: map[string]int{"FailNow": 1, "Log": 1},
wantLog: []any{`with json invalid type
error on parse {"id":1,"name": "test"} want []byte and got string`},
wantResult: false,
execute: func(t testingT) bool {
return Equal(t, "with json invalid type", `{"id":1,"name": "test"}`, []byte(`{"id":2,"name": "test"}`), WithJSONDiff)
},
},
"with json invalid type 2": {
wantCalled: map[string]int{"FailNow": 1, "Log": 1},
wantLog: []any{`with json invalid type 2
error on parse {"id":2,"name": "test"} want []byte and got string`},
wantResult: false,
execute: func(t testingT) bool {
return Equal(t, "with json invalid type 2", []byte(`{"id":1,"name": "test"}`), `{"id":2,"name": "test"}`, WithJSONDiff)
},
},
"with json invalid json": {
wantCalled: map[string]int{"FailNow": 1, "Log": 1},
wantLog: []any{`with json invalid json
invalid character '}' looking for beginning of object key string`},
wantResult: false,
execute: func(t testingT) bool {
return Equal(t, "with json invalid json", []byte(`{"id":1,"name": "test",}`), []byte(`{"id":2,"name": "test"}`), WithJSONDiff)
},
},
}
for name, tc := range tests {
tc := tc
t.Run(name, func(t *testing.T) {
mockedTestingT := &mockTestingT{
called: make(map[string]int, 5),
logFunc: func(args ...any) {
if !reflect.DeepEqual(tc.wantLog, args) {
t.Errorf("want log '%v' got '%v'", tc.wantLog, args)
}
},
}
result := tc.execute(mockedTestingT)
if result != tc.wantResult {
t.Errorf("\n++want %v\n--got %v", tc.wantResult, result)
}
if !reflect.DeepEqual(tc.wantCalled, mockedTestingT.called) {
t.Errorf("want %v got %v", tc.wantCalled, mockedTestingT.called)
}
})
}
}