-
-
Notifications
You must be signed in to change notification settings - Fork 58
/
is.go
403 lines (373 loc) · 8.81 KB
/
is.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
// Package is provides a lightweight extension to the
// standard library's testing capabilities.
//
// Comments on the assertion lines are used to add
// a description.
//
// The following failing test:
//
// func Test(t *testing.T) {
// is := is.New(t)
// a, b := 1, 2
// is.Equal(a, b) // expect to be the same
// }
//
// Will output:
//
// your_test.go:123: 1 != 2 // expect to be the same
//
// # Usage
//
// The following code shows a range of useful ways you can use
// the helper methods:
//
// func Test(t *testing.T) {
// // always start tests with this
// is := is.New(t)
//
// signedin, err := isSignedIn(ctx)
// is.NoErr(err) // isSignedIn error
// is.Equal(signedin, true) // must be signed in
//
// body := readBody(r)
// is.True(strings.Contains(body, "Hi there"))
// }
package is
import (
"bufio"
"bytes"
"flag"
"fmt"
"io"
"os"
"path/filepath"
"reflect"
"strconv"
"strings"
)
// T reports when failures occur.
// testing.T implements this interface.
type T interface {
// Fail indicates that the test has failed but
// allowed execution to continue.
// Fail is called in relaxed mode (via NewRelaxed).
Fail()
// FailNow indicates that the test has failed and
// aborts the test.
// FailNow is called in strict mode (via New).
FailNow()
}
// I is the test helper harness.
type I struct {
t T
fail func()
out io.Writer
colorful bool
helpers map[string]struct{} // functions to be skipped when writing file/line info
}
var noColorFlag bool
func init() {
var envNoColor bool
// prefer https://no-color.org (with any value)
if _, ok := os.LookupEnv("NO_COLOR"); ok {
envNoColor = true
}
if v, ok := os.LookupEnv("IS_NO_COLOR"); ok {
if b, err := strconv.ParseBool(v); err == nil {
envNoColor = b
}
}
flag.BoolVar(&noColorFlag, "nocolor", envNoColor, "turns off colors")
}
// New makes a new testing helper using the specified
// T through which failures will be reported.
// In strict mode, failures call T.FailNow causing the test
// to be aborted. See NewRelaxed for alternative behavior.
func New(t T) *I {
return &I{t, t.FailNow, os.Stdout, !noColorFlag, map[string]struct{}{}}
}
// NewRelaxed makes a new testing helper using the specified
// T through which failures will be reported.
// In relaxed mode, failures call T.Fail allowing
// multiple failures per test.
func NewRelaxed(t T) *I {
return &I{t, t.Fail, os.Stdout, !noColorFlag, map[string]struct{}{}}
}
func (is *I) log(args ...interface{}) {
s := is.decorate(fmt.Sprint(args...))
fmt.Fprintf(is.out, s)
is.fail()
}
func (is *I) logf(format string, args ...interface{}) {
is.log(fmt.Sprintf(format, args...))
}
// Fail immediately fails the test.
//
// func Test(t *testing.T) {
// is := is.New(t)
// is.Fail() // TODO: write this test
// }
//
// In relaxed mode, execution will continue after a call to
// Fail, but that test will still fail.
func (is *I) Fail() {
is.log("failed")
}
// True asserts that the expression is true. The expression
// code itself will be reported if the assertion fails.
//
// func Test(t *testing.T) {
// is := is.New(t)
// val := method()
// is.True(val != nil) // val should never be nil
// }
//
// Will output:
//
// your_test.go:123: not true: val != nil
func (is *I) True(expression bool) {
if !expression {
is.log("not true: $ARGS")
}
}
// Equal asserts that a and b are equal.
//
// func Test(t *testing.T) {
// is := is.New(t)
// a := greet("Mat")
// is.Equal(a, "Hi Mat") // greeting
// }
//
// Will output:
//
// your_test.go:123: Hey Mat != Hi Mat // greeting
func (is *I) Equal(a, b interface{}) {
if areEqual(a, b) {
return
}
if isNil(a) || isNil(b) {
is.logf("%s != %s", is.valWithType(a), is.valWithType(b))
} else if reflect.ValueOf(a).Type() == reflect.ValueOf(b).Type() {
is.logf("%v != %v", a, b)
} else {
is.logf("%s != %s", is.valWithType(a), is.valWithType(b))
}
}
// New is a method wrapper around the New function.
// It allows you to write subtests using a similar
// pattern:
//
// func Test(t *testing.T) {
// is := is.New(t)
// t.Run("sub", func(t *testing.T) {
// is := is.New(t)
// // TODO: test
// })
// }
func (is *I) New(t T) *I {
return New(t)
}
// NewRelaxed is a method wrapper around the NewRelaxed
// method. It allows you to write subtests using a similar
// pattern:
//
// func Test(t *testing.T) {
// is := is.NewRelaxed(t)
// t.Run("sub", func(t *testing.T) {
// is := is.NewRelaxed(t)
// // TODO: test
// })
// }
func (is *I) NewRelaxed(t T) *I {
return NewRelaxed(t)
}
func (is *I) valWithType(v interface{}) string {
if isNil(v) {
return "<nil>"
}
if is.colorful {
return fmt.Sprintf("%[1]s%[3]T(%[2]s%[3]v%[1]s)%[2]s", colorType, colorNormal, v)
}
return fmt.Sprintf("%[1]T(%[1]v)", v)
}
// NoErr asserts that err is nil.
//
// func Test(t *testing.T) {
// is := is.New(t)
// val, err := getVal()
// is.NoErr(err) // getVal error
// is.True(len(val) > 10) // val cannot be short
// }
//
// Will output:
//
// your_test.go:123: err: not found // getVal error
func (is *I) NoErr(err error) {
if err != nil {
is.logf("err: %s", err.Error())
}
}
// isNil gets whether the object is nil or not.
func isNil(object interface{}) bool {
if object == nil {
return true
}
value := reflect.ValueOf(object)
kind := value.Kind()
if kind >= reflect.Chan && kind <= reflect.Slice && value.IsNil() {
return true
}
return false
}
// areEqual gets whether a equals b or not.
func areEqual(a, b interface{}) bool {
if isNil(a) && isNil(b) {
return true
}
if isNil(a) || isNil(b) {
return false
}
if reflect.DeepEqual(a, b) {
return true
}
aValue := reflect.ValueOf(a)
bValue := reflect.ValueOf(b)
return aValue == bValue
}
// loadComment gets the Go comment from the specified line
// in the specified file.
func loadComment(path string, line int) (string, bool) {
f, err := os.Open(path)
if err != nil {
return "", false
}
defer f.Close()
s := bufio.NewScanner(f)
i := 1
for s.Scan() {
if i != line {
i++
continue
}
text := s.Text()
commentI := strings.Index(text, "// ")
if commentI == -1 {
return "", false // no comment
}
text = text[commentI+2:]
text = strings.TrimSpace(text)
return text, true
}
return "", false
}
// loadArguments gets the arguments from the function call
// on the specified line of the file.
func loadArguments(path string, line int) (string, bool) {
f, err := os.Open(path)
if err != nil {
return "", false
}
defer f.Close()
s := bufio.NewScanner(f)
i := 1
for s.Scan() {
if i != line {
i++
continue
}
text := s.Text()
braceI := strings.Index(text, "(")
if braceI == -1 {
return "", false
}
text = text[braceI+1:]
cs := bufio.NewScanner(strings.NewReader(text))
cs.Split(bufio.ScanBytes)
j := 0
c := 1
for cs.Scan() {
switch cs.Text() {
case ")":
c--
case "(":
c++
}
if c == 0 {
break
}
j++
}
text = text[:j]
return text, true
}
return "", false
}
// decorate prefixes the string with the file and line of the call site
// and inserts the final newline if needed and indentation tabs for formatting.
// this function was copied from the testing framework and modified.
func (is *I) decorate(s string) string {
path, lineNumber, ok := is.callerinfo() // decorate + log + public function.
file := filepath.Base(path)
if ok {
// Truncate file name at last file name separator.
if index := strings.LastIndex(file, "/"); index >= 0 {
file = file[index+1:]
} else if index = strings.LastIndex(file, "\\"); index >= 0 {
file = file[index+1:]
}
} else {
file = "???"
lineNumber = 1
}
buf := new(bytes.Buffer)
// Every line is indented at least one tab.
buf.WriteByte('\t')
if is.colorful {
buf.WriteString(colorFile)
}
fmt.Fprintf(buf, "%s:%d: ", file, lineNumber)
if is.colorful {
buf.WriteString(colorNormal)
}
s = escapeFormatString(s)
lines := strings.Split(s, "\n")
if l := len(lines); l > 1 && lines[l-1] == "" {
lines = lines[:l-1]
}
for i, line := range lines {
if i > 0 {
// Second and subsequent lines are indented an extra tab.
buf.WriteString("\n\t\t")
}
// expand arguments (if $ARGS is present)
if strings.Contains(line, "$ARGS") {
args, _ := loadArguments(path, lineNumber)
line = strings.Replace(line, "$ARGS", args, -1)
}
buf.WriteString(line)
}
comment, ok := loadComment(path, lineNumber)
if ok {
if is.colorful {
buf.WriteString(colorComment)
}
buf.WriteString(" // ")
comment = escapeFormatString(comment)
buf.WriteString(comment)
if is.colorful {
buf.WriteString(colorNormal)
}
}
buf.WriteString("\n")
return buf.String()
}
// escapeFormatString escapes strings for use in formatted functions like Sprintf.
func escapeFormatString(fmt string) string {
return strings.Replace(fmt, "%", "%%", -1)
}
const (
colorNormal = "\u001b[00m"
colorComment = "\u001b[31m"
colorFile = "\u001b[02m"
colorType = "\u001b[02m"
)