14
14
package testing
15
15
16
16
import (
17
- "fmt"
18
- "path/filepath"
19
- "runtime"
20
17
"strings"
21
18
"testing"
22
19
@@ -27,19 +24,19 @@ import (
27
24
// Assert fails the test if the condition is false.
28
25
// Taken from https://github.com/benbjohnson/testing.
29
26
func Assert (tb testing.TB , condition bool , msg string , v ... interface {}) {
27
+ tb .Helper ()
30
28
if ! condition {
31
- _ , file , line , _ := runtime .Caller (1 )
32
- fmt .Printf ("\033 [31m%s:%d: " + msg + "\033 [39m\n \n " , append ([]interface {}{filepath .Base (file ), line }, v ... )... )
29
+ errLog (tb , msg , v ... )
33
30
tb .FailNow ()
34
31
}
35
32
}
36
33
37
34
// Ok fails the test if an err is not nil.
38
35
// Taken from https://github.com/benbjohnson/testing.
39
36
func Ok (tb testing.TB , err error ) {
37
+ tb .Helper ()
40
38
if err != nil {
41
- _ , file , line , _ := runtime .Caller (1 )
42
- fmt .Printf ("\033 [31m%s:%d: unexpected error: %s\033 [39m\n \n " , filepath .Base (file ), line , err .Error ())
39
+ errLog (tb , "unexpected error: %s" , err .Error ())
43
40
tb .FailNow ()
44
41
}
45
42
}
@@ -49,19 +46,21 @@ func Ok(tb testing.TB, err error) {
49
46
func Equals (tb testing.TB , exp , act interface {}) {
50
47
tb .Helper ()
51
48
if diff := deep .Equal (exp , act ); diff != nil {
52
- _ , file , line , _ := runtime . Caller ( 1 )
53
- tb .Fatalf ( " \033 [31m%s:%d: %s \n \n exp: %s****** \n got: %s \033 [39m \n " , filepath . Base ( file ), line , diff , spew . Sdump ( exp ), spew . Sdump ( act ) )
49
+ errLog ( tb , "%s \n \n exp: %s****** \n got: %s" , diff , spew . Sdump ( exp ), spew . Sdump ( act ) )
50
+ tb .FailNow ( )
54
51
}
55
52
}
56
53
57
54
// ErrEquals fails the test if act is nil or act.Error() != exp
58
55
func ErrEquals (tb testing.TB , exp string , act error ) {
59
56
tb .Helper ()
60
57
if act == nil {
61
- tb .Fatalf ("exp err %q but err was nil\n " , exp )
58
+ errLog (tb , "exp err %q but err was nil\n " , exp )
59
+ tb .FailNow ()
62
60
}
63
61
if act .Error () != exp {
64
- tb .Fatalf ("exp err: %q but got: %q\n " , exp , act .Error ())
62
+ errLog (tb , "exp err: %q but got: %q\n " , exp , act .Error ())
63
+ tb .FailNow ()
65
64
}
66
65
}
67
66
@@ -70,21 +69,28 @@ func ErrEquals(tb testing.TB, exp string, act error) {
70
69
func ErrContains (tb testing.TB , substr string , act error ) {
71
70
tb .Helper ()
72
71
if act == nil {
73
- tb .Fatalf ("exp err to contain %q but err was nil" , substr )
72
+ errLog (tb , "exp err to contain %q but err was nil" , substr )
73
+ tb .FailNow ()
74
74
}
75
75
if ! strings .Contains (act .Error (), substr ) {
76
- tb .Fatalf ("exp err %q to contain %q" , act .Error (), substr )
76
+ errLog (tb , "exp err %q to contain %q" , act .Error (), substr )
77
+ tb .FailNow ()
77
78
}
78
79
}
79
80
80
81
// Contains fails the test if the slice doesn't contain the expected element
81
82
func Contains (tb testing.TB , exp interface {}, slice []string ) {
83
+ tb .Helper ()
82
84
for _ , v := range slice {
83
85
if v == exp {
84
86
return
85
87
}
86
88
}
87
- _ , file , line , _ := runtime .Caller (1 )
88
- fmt .Printf ("\033 [31m%s:%d:\n \n \t exp: %#v\n \n \t was not in: %#v\033 [39m\n \n " , filepath .Base (file ), line , exp , slice )
89
+ errLog (tb , "exp: %#v\n \n \t was not in: %#v" , exp , slice )
89
90
tb .FailNow ()
90
91
}
92
+
93
+ func errLog (tb testing.TB , fmt string , args ... interface {}) {
94
+ tb .Helper ()
95
+ tb .Logf ("\033 [31m" + fmt + "\033 [39m" , args ... )
96
+ }
0 commit comments