ConfigState: Create 'DisableLengths' option

This commit is contained in:
Harald Nordgren 2019-01-18 23:33:20 +01:00
parent d8f796af33
commit e817bb185a
3 changed files with 26 additions and 18 deletions

View File

@ -76,6 +76,10 @@ type ConfigState struct {
// data structures in tests.
DisableCapacities bool
// DisableLengths specifies whether to disable the printing of lengths
// for strings, arrays, slices, maps and channels.
DisableLengths bool
// ContinueOnMethod specifies whether or not recursion should continue once
// a custom error or Stringer interface is invoked. The default, false,
// means it will print the results of invoking the custom error or Stringer

View File

@ -275,6 +275,7 @@ func (d *dumpState) dump(v reflect.Value) {
// Display length and capacity if the built-in len and cap functions
// work with the value's kind and the len/cap itself is non-zero.
if !d.cs.DisableLengths {
valueLen, valueCap := 0, 0
switch v.Kind() {
case reflect.Array, reflect.Slice, reflect.Chan:
@ -298,6 +299,7 @@ func (d *dumpState) dump(v reflect.Value) {
d.w.Write(closeParenBytes)
d.w.Write(spaceBytes)
}
}
// Call Stringer/error interfaces if they exist and the handle methods flag
// is enabled

View File

@ -132,6 +132,7 @@ func initSpewTests() {
scsContinue := &spew.ConfigState{Indent: " ", ContinueOnMethod: true}
scsNoPtrAddr := &spew.ConfigState{DisablePointerAddresses: true}
scsNoCap := &spew.ConfigState{DisableCapacities: true}
scsNoLen := &spew.ConfigState{DisableLengths: true}
// Variables for tests on types which implement Stringer interface with and
// without a pointer receiver.
@ -203,6 +204,7 @@ func initSpewTests() {
{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"},
{scsNoLen, fCSSdump, "", make([]string, 1, 10), "([]string) {\n(string) \"\"\n}\n"},
}
}