Browse Source

Adds new config options:

DisablePointerAddresses: Specifies whether to disable the printing of
pointer addresses.

DisableCapacities specifies whether to disable the printing of capacities
for arrays, slices, maps and channels.

These are useful when diffing data structures in tests. Printing pointers
and capacities would otherwise lead to false negatives.
Alexander Staubo 9 years ago
parent
commit
04cdfd4297
3 changed files with 23 additions and 3 deletions
  1. 9 0
      spew/config.go
  2. 3 3
      spew/dump.go
  3. 11 0
      spew/spew_test.go

+ 9 - 0
spew/config.go

@@ -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

+ 3 - 3
spew/dump.go

@@ -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)
 			}
 			}

+ 11 - 0
spew/spew_test.go

@@ -130,12 +130,19 @@ func initSpewTests() {
 	scsNoPmethods := &spew.ConfigState{Indent: " ", DisablePointerMethods: true}
 	scsNoPmethods := &spew.ConfigState{Indent: " ", DisablePointerMethods: true}
 	scsMaxDepth := &spew.ConfigState{Indent: " ", MaxDepth: 1}
 	scsMaxDepth := &spew.ConfigState{Indent: " ", MaxDepth: 1}
 	scsContinue := &spew.ConfigState{Indent: " ", ContinueOnMethod: true}
 	scsContinue := &spew.ConfigState{Indent: " ", ContinueOnMethod: true}
+	scsNoPtrAddr := &spew.ConfigState{DisablePointerAddresses: true}
+	scsNoCap := &spew.ConfigState{DisableCapacities: true}
 
 
 	// Variables for tests on types which implement Stringer interface with and
 	// Variables for tests on types which implement Stringer interface with and
 	// without a pointer receiver.
 	// without a pointer receiver.
 	ts := stringer("test")
 	ts := stringer("test")
 	tps := pstringer("test")
 	tps := pstringer("test")
 
 
+	type ptrTester struct {
+		s *struct{}
+	}
+	tptr := &ptrTester{s: &struct{}{}}
+
 	// depthTester is used to test max depth handling for structs, array, slices
 	// depthTester is used to test max depth handling for structs, array, slices
 	// and maps.
 	// and maps.
 	type depthTester struct {
 	type depthTester struct {
@@ -192,6 +199,10 @@ func initSpewTests() {
 		{scsContinue, fCSFprint, "", te, "(error: 10) 10"},
 		{scsContinue, fCSFprint, "", te, "(error: 10) 10"},
 		{scsContinue, fCSFdump, "", te, "(spew_test.customError) " +
 		{scsContinue, fCSFdump, "", te, "(spew_test.customError) " +
 			"(error: 10) 10\n"},
 			"(error: 10) 10\n"},
+		{scsNoPtrAddr, fCSFprint, "", tptr, "<*>{<*>{}}"},
+		{scsNoPtrAddr, fCSSdump, "", tptr, "(*spew_test.ptrTester)({\ns: (*struct {})({\n})\n})\n"},
+		{scsNoCap, fCSSdump, "", make([]string, 0, 10), "([]string) {\n}\n"},
+		{scsNoCap, fCSSdump, "", make([]string, 1, 10), "([]string) (len=1) {\n(string) \"\"\n}\n"},
 	}
 	}
 }
 }