When diffing with spew, use a format that doesn't include pointer addresses (which

generate false negatives). This updates go-spew to 04cdfd42973bb9c8589fd6a731800cf222fde1a9.
pull/365/head
Alexander Staubo 2016-10-29 16:23:25 -04:00
parent 976c720a22
commit cbd71e7dd4
5 changed files with 31 additions and 16 deletions

2
Godeps/Godeps.json generated
View File

@ -9,7 +9,7 @@
{ {
"ImportPath": "github.com/davecgh/go-spew/spew", "ImportPath": "github.com/davecgh/go-spew/spew",
"Comment": "v1.0.0-3-g6d21280", "Comment": "v1.0.0-3-g6d21280",
"Rev": "6d212800a42e8ab5c146b8ace3490ee17e5225f9" "Rev": "04cdfd42973bb9c8589fd6a731800cf222fde1a9"
}, },
{ {
"ImportPath": "github.com/pmezard/go-difflib/difflib", "ImportPath": "github.com/pmezard/go-difflib/difflib",

View File

@ -18,10 +18,6 @@ import (
"github.com/pmezard/go-difflib/difflib" "github.com/pmezard/go-difflib/difflib"
) )
func init() {
spew.Config.SortKeys = true
}
// TestingT is an interface wrapper around *testing.T // TestingT is an interface wrapper around *testing.T
type TestingT interface { type TestingT interface {
Errorf(format string, args ...interface{}) Errorf(format string, args ...interface{})
@ -1043,8 +1039,8 @@ func diff(expected interface{}, actual interface{}) string {
return "" return ""
} }
e := spew.Sdump(expected) e := spewConfig.Sdump(expected)
a := spew.Sdump(actual) a := spewConfig.Sdump(actual)
diff, _ := difflib.GetUnifiedDiffString(difflib.UnifiedDiff{ diff, _ := difflib.GetUnifiedDiffString(difflib.UnifiedDiff{
A: difflib.SplitLines(e), A: difflib.SplitLines(e),
@ -1058,3 +1054,10 @@ func diff(expected interface{}, actual interface{}) string {
return "\n\nDiff:\n" + diff return "\n\nDiff:\n" + diff
} }
var spewConfig = spew.ConfigState{
Indent: " ",
DisablePointerAddresses: true,
DisableCapacities: true,
SortKeys: true,
}

View File

@ -15,10 +15,6 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func inin() {
spew.Config.SortKeys = true
}
// TestingT is an interface wrapper around *testing.T // TestingT is an interface wrapper around *testing.T
type TestingT interface { type TestingT interface {
Logf(format string, args ...interface{}) Logf(format string, args ...interface{})
@ -746,8 +742,8 @@ func diff(expected interface{}, actual interface{}) string {
return "" return ""
} }
e := spew.Sdump(expected) e := spewConfig.Sdump(expected)
a := spew.Sdump(actual) a := spewConfig.Sdump(actual)
diff, _ := difflib.GetUnifiedDiffString(difflib.UnifiedDiff{ diff, _ := difflib.GetUnifiedDiffString(difflib.UnifiedDiff{
A: difflib.SplitLines(e), A: difflib.SplitLines(e),
@ -761,3 +757,10 @@ func diff(expected interface{}, actual interface{}) string {
return diff return diff
} }
var spewConfig = spew.ConfigState{
Indent: " ",
DisablePointerAddresses: true,
DisableCapacities: true,
SortKeys: true,
}

View File

@ -67,6 +67,15 @@ type ConfigState struct {
// Google App Engine or with the "safe" build tag specified. // Google App Engine or with the "safe" build tag specified.
DisablePointerMethods bool DisablePointerMethods bool
// DisablePointerAddresses specifies whether to disable the printing of
// pointer addresses. This is useful when diffing data structures in tests.
DisablePointerAddresses bool
// DisableCapacities specifies whether to disable the printing of capacities
// for arrays, slices, maps and channels. This is useful when diffing
// data structures in tests.
DisableCapacities bool
// ContinueOnMethod specifies whether or not recursion should continue once // ContinueOnMethod specifies whether or not recursion should continue once
// a custom error or Stringer interface is invoked. The default, false, // a custom error or Stringer interface is invoked. The default, false,
// means it will print the results of invoking the custom error or Stringer // means it will print the results of invoking the custom error or Stringer

View File

@ -129,7 +129,7 @@ func (d *dumpState) dumpPtr(v reflect.Value) {
d.w.Write(closeParenBytes) d.w.Write(closeParenBytes)
// Display pointer information. // Display pointer information.
if len(pointerChain) > 0 { if !d.cs.DisablePointerAddresses && len(pointerChain) > 0 {
d.w.Write(openParenBytes) d.w.Write(openParenBytes)
for i, addr := range pointerChain { for i, addr := range pointerChain {
if i > 0 { if i > 0 {
@ -282,13 +282,13 @@ func (d *dumpState) dump(v reflect.Value) {
case reflect.Map, reflect.String: case reflect.Map, reflect.String:
valueLen = v.Len() valueLen = v.Len()
} }
if valueLen != 0 || valueCap != 0 { if valueLen != 0 || !d.cs.DisableCapacities && valueCap != 0 {
d.w.Write(openParenBytes) d.w.Write(openParenBytes)
if valueLen != 0 { if valueLen != 0 {
d.w.Write(lenEqualsBytes) d.w.Write(lenEqualsBytes)
printInt(d.w, int64(valueLen), 10) printInt(d.w, int64(valueLen), 10)
} }
if valueCap != 0 { if !d.cs.DisableCapacities && valueCap != 0 {
if valueLen != 0 { if valueLen != 0 {
d.w.Write(spaceBytes) d.w.Write(spaceBytes)
} }