fix: error diff was not being show for large values

If you were comparing values which when formatted were longer than about 64k then the actual, expected and diff messages were not printed to the console.
pull/733/head
Hiram Chirino 2020-01-25 10:46:29 -05:00 committed by Boyan Soubachov
parent b7d60e3a8c
commit 310548cda6
2 changed files with 32 additions and 3 deletions

View File

@ -429,14 +429,27 @@ func samePointers(first, second interface{}) bool {
// to a type conversion in the Go grammar.
func formatUnequalValues(expected, actual interface{}) (e string, a string) {
if reflect.TypeOf(expected) != reflect.TypeOf(actual) {
return fmt.Sprintf("%T(%#v)", expected, expected),
fmt.Sprintf("%T(%#v)", actual, actual)
return fmt.Sprintf("%T(%s)", expected, truncatingFormat(expected)),
fmt.Sprintf("%T(%s)", actual, truncatingFormat(actual))
}
switch expected.(type) {
case time.Duration:
return fmt.Sprintf("%v", expected), fmt.Sprintf("%v", actual)
}
return fmt.Sprintf("%#v", expected), fmt.Sprintf("%#v", actual)
return truncatingFormat(expected), truncatingFormat(actual)
}
// truncatingFormat formats the data and truncates it if it's too long.
//
// This helps keep formatted error messages lines from exceeding the
// bufio.MaxScanTokenSize max line length that the go testing framework imposes.
func truncatingFormat(data interface{}) string {
value := fmt.Sprintf("%#v", data)
max := bufio.MaxScanTokenSize - 100 // Give us some space the type info too if needed.
if len(value) > max {
value = value[0:max] + "<... truncated>"
}
return value
}
// EqualValues asserts that two objects are equal or convertable to the same types

View File

@ -1,6 +1,7 @@
package assert
import (
"bufio"
"bytes"
"encoding/json"
"errors"
@ -2255,3 +2256,18 @@ func Test_validateEqualArgs(t *testing.T) {
t.Error("nil functions are equal")
}
}
func Test_truncatingFormat(t *testing.T) {
original := strings.Repeat("a", bufio.MaxScanTokenSize-102)
result := truncatingFormat(original)
Equal(t, fmt.Sprintf("%#v", original), result, "string should not be truncated")
original = original + "x"
result = truncatingFormat(original)
NotEqual(t, fmt.Sprintf("%#v", original), result, "string should have been truncated.")
if !strings.HasSuffix(result, "<... truncated>") {
t.Error("truncated string should have <... truncated> suffix")
}
}