Skip to content

Commit ad820b4

Browse files
authored
testutil: Improved trimming when output is too large. (#5)
Now e.g. Equals will print maximum of 1000 chars for each "exp", "act", "msg" and "diff", instead of 10k overall. Printing more is still cluttering and impossible to debug by humans. This allows to at least read beginnings of diff and each act and exp lines. Signed-off-by: bwplotka <[email protected]> Signed-off-by: bwplotka <[email protected]>
1 parent a9e1cee commit ad820b4

File tree

1 file changed

+25
-22
lines changed

1 file changed

+25
-22
lines changed

testutil/testutil.go

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ import (
1717
"github.com/google/go-cmp/cmp"
1818
)
1919

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+
2030
// Assert fails the test if the condition is false.
2131
func Assert(tb testing.TB, condition bool, v ...interface{}) {
2232
tb.Helper()
@@ -29,7 +39,7 @@ func Assert(tb testing.TB, condition bool, v ...interface{}) {
2939
if len(v) > 0 {
3040
msg = fmt.Sprintf(v[0].(string), v[1:]...)
3141
}
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))
3343
}
3444

3545
// Ok fails the test if an err is not nil.
@@ -44,7 +54,7 @@ func Ok(tb testing.TB, err error, v ...interface{}) {
4454
if len(v) > 0 {
4555
msg = fmt.Sprintf(v[0].(string), v[1:]...)
4656
}
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()))
4858
}
4959

5060
// NotOk fails the test if an err is nil.
@@ -59,7 +69,7 @@ func NotOk(tb testing.TB, err error, v ...interface{}) {
5969
if len(v) > 0 {
6070
msg = fmt.Sprintf(v[0].(string), v[1:]...)
6171
}
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))
6373
}
6474

6575
// 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{}) {
6878
if reflect.DeepEqual(exp, act) {
6979
return
7080
}
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)
7286

7387
var msg string
7488
if len(v) > 0 {
7589
msg = fmt.Sprintf(v[0].(string), v[1:]...)
7690
}
77-
tb.Fatal(sprintfWithLimit("\033[31m%s:%d:"+msg+"\n\n\texp: %#v\n\n\tgot: %#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\texp: %s\n\n\tgot: %s%s\033[39m\n\n",
93+
filepath.Base(file), line, withLimitf(msg), withLimitf("%#v", exp), withLimitf("%#v", act), withLimitf(diff(exp, act)),
94+
)
7895
}
7996

8097
type goCmp struct {
@@ -83,7 +100,7 @@ type goCmp struct {
83100

84101
// WithGoCmp allows specifying options and using https://github.com/google/go-cmp
85102
// 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).
87104
func WithGoCmp(opts ...cmp.Option) goCmp {
88105
return goCmp{opts: opts}
89106
}
@@ -95,13 +112,7 @@ func (o goCmp) Equals(tb testing.TB, exp, act interface{}, v ...interface{}) {
95112
if cmp.Equal(exp, act, o.opts) {
96113
return
97114
}
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\texp: %#v\n\n\tgot: %#v%s\033[39m\n\n", filepath.Base(file), line, exp, act, diff(exp, act)))
115+
fatalNotEqual(tb, exp, act, v...)
105116
}
106117

107118
// 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) {
126137
_, file, line, _ := runtime.Caller(1)
127138

128139
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))
130141
}
131142
}
132143

@@ -166,14 +177,6 @@ func contains(haystack, needle []string) bool {
166177
return false
167178
}
168179

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-
177180
func typeAndKind(v interface{}) (reflect.Type, reflect.Kind) {
178181
t := reflect.TypeOf(v)
179182
k := t.Kind()

0 commit comments

Comments
 (0)