-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
assertions_error.go
259 lines (238 loc) · 6.52 KB
/
assertions_error.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
package got
import (
"context"
"strings"
"time"
"github.com/ysmood/gop"
"github.com/ysmood/got/lib/diff"
)
// AssertionErrType enum
type AssertionErrType int
const (
// AssertionEq type
AssertionEq AssertionErrType = iota
// AssertionNeqSame type
AssertionNeqSame
// AssertionNeq type
AssertionNeq
// AssertionGt type
AssertionGt
// AssertionGte type
AssertionGte
// AssertionLt type
AssertionLt
// AssertionLte type
AssertionLte
// AssertionInDelta type
AssertionInDelta
// AssertionTrue type
AssertionTrue
// AssertionFalse type
AssertionFalse
// AssertionNil type
AssertionNil
// AssertionNoArgs type
AssertionNoArgs
// AssertionNotNil type
AssertionNotNil
// AssertionNotNilable type
AssertionNotNilable
// AssertionNotNilableNil type
AssertionNotNilableNil
// AssertionZero type
AssertionZero
// AssertionNotZero type
AssertionNotZero
// AssertionRegex type
AssertionRegex
// AssertionHas type
AssertionHas
// AssertionLen type
AssertionLen
// AssertionErr type
AssertionErr
// AssertionPanic type
AssertionPanic
// AssertionIsInChain type
AssertionIsInChain
// AssertionIsKind type
AssertionIsKind
// AssertionCount type
AssertionCount
// AssertionSnapshot type
AssertionSnapshot
)
// AssertionCtx holds the context of an assertion
type AssertionCtx struct {
Type AssertionErrType
Details []interface{}
File string
Line int
}
// AssertionError handler
type AssertionError interface {
Report(*AssertionCtx) string
}
var _ AssertionError = AssertionErrorReport(nil)
// AssertionErrorReport is used to convert a func to AssertionError
type AssertionErrorReport func(*AssertionCtx) string
// Report interface
func (ae AssertionErrorReport) Report(ac *AssertionCtx) string {
return ae(ac)
}
type defaultAssertionError struct {
fns map[AssertionErrType]func(details ...interface{}) string
}
// NewDefaultAssertionError handler
func NewDefaultAssertionError(theme gop.Theme, diffTheme diff.Theme) AssertionError {
f := func(v interface{}) string {
return gop.Format(gop.Tokenize(v), theme)
}
k := func(s string) string {
return " " + gop.Stylize("⦗"+s+"⦘", theme(gop.Error)) + " "
}
fns := map[AssertionErrType]func(details ...interface{}) string{
AssertionEq: func(details ...interface{}) string {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
x := f(details[0])
y := f(details[1])
if diffTheme == nil {
return j(x, k("not =="), y)
}
if hasNewline(x, y) {
df := diff.Format(diff.Tokenize(ctx, gop.StripANSI(x), gop.StripANSI(y)), diffTheme)
return j(x, k("not =="), y, df)
}
dx, dy := diff.TokenizeLine(ctx, gop.StripANSI(x), gop.StripANSI(y))
return diff.Format(dx, diffTheme) + k("not ==") + diff.Format(dy, diffTheme)
},
AssertionNeqSame: func(details ...interface{}) string {
x := f(details[0])
y := f(details[1])
return j(x, k("=="), y)
},
AssertionNeq: func(details ...interface{}) string {
x := f(details[0])
y := f(details[1])
return j(x, k("=="), y, k("when converted to the same type"))
},
AssertionGt: func(details ...interface{}) string {
x := f(details[0])
y := f(details[1])
return j(x, k("not >"), y)
},
AssertionGte: func(details ...interface{}) string {
x := f(details[0])
y := f(details[1])
return j(x, k("not ≥"), y)
},
AssertionLt: func(details ...interface{}) string {
x := f(details[0])
y := f(details[1])
return j(x, k("not <"), y)
},
AssertionLte: func(details ...interface{}) string {
x := f(details[0])
y := f(details[1])
return j(x, k("not ≤"), y)
},
AssertionInDelta: func(details ...interface{}) string {
x := f(details[0])
y := f(details[1])
delta := f(details[2])
return j(k("delta between"), x, k("and"), y, k("not ≤"), delta)
},
AssertionTrue: func(_ ...interface{}) string {
return k("should be") + f(true)
},
AssertionFalse: func(_ ...interface{}) string {
return k("should be") + f(false)
},
AssertionNil: func(details ...interface{}) string {
last := f(details[0])
return j(k("last argument"), last, k("should be"), f(nil))
},
AssertionNoArgs: func(_ ...interface{}) string {
return k("no arguments received")
},
AssertionNotNil: func(_ ...interface{}) string {
return k("last argument shouldn't be") + f(nil)
},
AssertionNotNilable: func(details ...interface{}) string {
last := f(details[0])
return j(k("last argument"), last, k("is not nilable"))
},
AssertionNotNilableNil: func(details ...interface{}) string {
last := f(details[0])
return j(k("last argument"), last, k("shouldn't be"), f(nil))
},
AssertionZero: func(details ...interface{}) string {
x := f(details[0])
return j(x, k("should be zero value for its type"))
},
AssertionNotZero: func(details ...interface{}) string {
x := f(details[0])
return j(x, k("shouldn't be zero value for its type"))
},
AssertionRegex: func(details ...interface{}) string {
pattern := f(details[0])
str := f(details[1])
return j(pattern, k("should match"), str)
},
AssertionHas: func(details ...interface{}) string {
container := f(details[0])
str := f(details[1])
return j(container, k("should has"), str)
},
AssertionLen: func(details ...interface{}) string {
actual := f(details[0])
l := f(details[1])
return k("expect len") + actual + k("to be") + l
},
AssertionErr: func(details ...interface{}) string {
last := f(details[0])
return j(k("last value"), last, k("should be <error>"))
},
AssertionPanic: func(_ ...interface{}) string {
return k("should panic")
},
AssertionIsInChain: func(details ...interface{}) string {
x := f(details[0])
y := f(details[1])
return j(x, k("should in chain of"), y)
},
AssertionIsKind: func(details ...interface{}) string {
x := f(details[0])
y := f(details[1])
return j(x, k("should be kind of"), y)
},
AssertionCount: func(details ...interface{}) string {
n := f(details[0])
count := f(details[1])
return k("should count") + n + k("times, but got") + count
},
}
return &defaultAssertionError{fns: fns}
}
// Report interface
func (ae *defaultAssertionError) Report(ac *AssertionCtx) string {
return ae.fns[ac.Type](ac.Details...)
}
func j(args ...string) string {
if hasNewline(args...) {
for i := 0; i < len(args); i++ {
args[i] = strings.Trim(args[i], " ")
}
return "\n" + strings.Join(args, "\n\n")
}
return strings.Join(args, "")
}
func hasNewline(args ...string) bool {
for _, arg := range args {
if strings.Contains(arg, "\n") {
return true
}
}
return false
}