@@ -17,6 +17,16 @@ import (
17
17
"github.com/google/go-cmp/cmp"
18
18
)
19
19
20
+ const limitOfElemChars = 1e3
21
+
22
+ func withLimitf (f string , v ... interface {}) string {
23
+ s := fmt .Sprintf (f , v ... )
24
+ if len (s ) > limitOfElemChars {
25
+ return s [:limitOfElemChars ] + "...(output trimmed)"
26
+ }
27
+ return s
28
+ }
29
+
20
30
// Assert fails the test if the condition is false.
21
31
func Assert (tb testing.TB , condition bool , v ... interface {}) {
22
32
tb .Helper ()
@@ -29,7 +39,7 @@ func Assert(tb testing.TB, condition bool, v ...interface{}) {
29
39
if len (v ) > 0 {
30
40
msg = fmt .Sprintf (v [0 ].(string ), v [1 :]... )
31
41
}
32
- tb .Fatalf ("\033 [31m%s:%d: " + msg + "\033 [39m\n \n " , filepath .Base (file ), line )
42
+ tb .Fatalf ("\033 [31m%s:%d: \" %s \ "\033 [39m\n \n " , filepath .Base (file ), line , withLimitf ( msg ) )
33
43
}
34
44
35
45
// Ok fails the test if an err is not nil.
@@ -44,7 +54,7 @@ func Ok(tb testing.TB, err error, v ...interface{}) {
44
54
if len (v ) > 0 {
45
55
msg = fmt .Sprintf (v [0 ].(string ), v [1 :]... )
46
56
}
47
- tb .Fatalf ("\033 [31m%s:%d:" + msg + "\n \n unexpected error: %s\033 [39m\n \n " , filepath .Base (file ), line , err .Error ())
57
+ tb .Fatalf ("\033 [31m%s:%d: \" %s \ "\n \n unexpected error: %s\033 [39m\n \n " , filepath .Base (file ), line , withLimitf ( msg ), withLimitf ( err .Error () ))
48
58
}
49
59
50
60
// NotOk fails the test if an err is nil.
@@ -59,7 +69,7 @@ func NotOk(tb testing.TB, err error, v ...interface{}) {
59
69
if len (v ) > 0 {
60
70
msg = fmt .Sprintf (v [0 ].(string ), v [1 :]... )
61
71
}
62
- tb .Fatalf ("\033 [31m%s:%d:" + msg + "\n \n expected error, got nothing \033 [39m\n \n " , filepath .Base (file ), line )
72
+ tb .Fatalf ("\033 [31m%s:%d: \" %s \ "\n \n expected error, got nothing \033 [39m\n \n " , filepath .Base (file ), line , withLimitf ( msg ) )
63
73
}
64
74
65
75
// Equals fails the test if exp is not equal to act.
@@ -68,13 +78,20 @@ func Equals(tb testing.TB, exp, act interface{}, v ...interface{}) {
68
78
if reflect .DeepEqual (exp , act ) {
69
79
return
70
80
}
71
- _ , file , line , _ := runtime .Caller (1 )
81
+ fatalNotEqual (tb , exp , act , v ... )
82
+ }
83
+
84
+ func fatalNotEqual (tb testing.TB , exp , act interface {}, v ... interface {}) {
85
+ _ , file , line , _ := runtime .Caller (2 )
72
86
73
87
var msg string
74
88
if len (v ) > 0 {
75
89
msg = fmt .Sprintf (v [0 ].(string ), v [1 :]... )
76
90
}
77
- tb .Fatal (sprintfWithLimit ("\033 [31m%s:%d:" + msg + "\n \n \t exp: %#v\n \n \t got: %#v%s\033 [39m\n \n " , filepath .Base (file ), line , exp , act , diff (exp , act )))
91
+ tb .Fatalf (
92
+ "\033 [31m%s:%d: \" %s\" \n \n \t exp: %s\n \n \t got: %s%s\033 [39m\n \n " ,
93
+ filepath .Base (file ), line , withLimitf (msg ), withLimitf ("%#v" , exp ), withLimitf ("%#v" , act ), withLimitf (diff (exp , act )),
94
+ )
78
95
}
79
96
80
97
type goCmp struct {
@@ -83,7 +100,7 @@ type goCmp struct {
83
100
84
101
// WithGoCmp allows specifying options and using https://github.com/google/go-cmp
85
102
// for equality comparisons. The compatibility guarantee of this function's arguments
86
- // are the same as go-cmp i.e none due to v0.x.
103
+ // are the same as go-cmp (no guarantee due to v0.x) .
87
104
func WithGoCmp (opts ... cmp.Option ) goCmp {
88
105
return goCmp {opts : opts }
89
106
}
@@ -95,13 +112,7 @@ func (o goCmp) Equals(tb testing.TB, exp, act interface{}, v ...interface{}) {
95
112
if cmp .Equal (exp , act , o .opts ) {
96
113
return
97
114
}
98
- _ , file , line , _ := runtime .Caller (1 )
99
-
100
- var msg string
101
- if len (v ) > 0 {
102
- msg = fmt .Sprintf (v [0 ].(string ), v [1 :]... )
103
- }
104
- tb .Fatal (sprintfWithLimit ("\033 [31m%s:%d:" + msg + "\n \n \t exp: %#v\n \n \t got: %#v%s\033 [39m\n \n " , filepath .Base (file ), line , exp , act , diff (exp , act )))
115
+ fatalNotEqual (tb , exp , act , v ... )
105
116
}
106
117
107
118
// FaultOrPanicToErr returns error if panic of fault was triggered during execution of function.
@@ -126,7 +137,7 @@ func ContainsStringSlice(tb testing.TB, haystack, needle []string) {
126
137
_ , file , line , _ := runtime .Caller (1 )
127
138
128
139
if ! contains (haystack , needle ) {
129
- tb .Fatalf (sprintfWithLimit ( "\033 [31m%s:%d: %#v does not contain %#v \033 [39m\n \n " , filepath .Base (file ), line , haystack , needle ))
140
+ tb .Fatalf ("\033 [31m%s:%d: %s does not contain %s \033 [39m\n \n " , filepath .Base (file ), line , withLimitf ( "%#v" , haystack ), withLimitf ( "%#v" , needle ))
130
141
}
131
142
}
132
143
@@ -166,14 +177,6 @@ func contains(haystack, needle []string) bool {
166
177
return false
167
178
}
168
179
169
- func sprintfWithLimit (act string , v ... interface {}) string {
170
- s := fmt .Sprintf (act , v ... )
171
- if len (s ) > 10000 {
172
- return s [:10000 ] + "...(output trimmed)"
173
- }
174
- return s
175
- }
176
-
177
180
func typeAndKind (v interface {}) (reflect.Type , reflect.Kind ) {
178
181
t := reflect .TypeOf (v )
179
182
k := t .Kind ()
0 commit comments