ConfigState: Create 'DisableNativeTypes' option

This commit is contained in:
Harald Nordgren 2019-01-19 01:45:25 +01:00
parent d8f796af33
commit 936523cb79
3 changed files with 53 additions and 0 deletions

View File

@ -76,6 +76,10 @@ type ConfigState struct {
// data structures in tests.
DisableCapacities bool
// DisableNativeTypes specifies whether to disable the printing of native
// types.
DisableNativeTypes 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

@ -263,6 +263,20 @@ func (d *dumpState) dump(v reflect.Value) {
return
}
if d.cs.DisableNativeTypes {
switch kind {
case reflect.Array,
reflect.Chan,
reflect.Func,
reflect.Map,
reflect.Slice,
reflect.Struct:
default:
d.indent()
d.ignoreNextType = true
}
}
// Print type information unless already handled elsewhere.
if !d.ignoreNextType {
d.indent()

View File

@ -132,6 +132,7 @@ func initSpewTests() {
scsContinue := &spew.ConfigState{Indent: " ", ContinueOnMethod: true}
scsNoPtrAddr := &spew.ConfigState{DisablePointerAddresses: true}
scsNoCap := &spew.ConfigState{DisableCapacities: true}
scsNoNativeTypes := &spew.ConfigState{Indent: " ", DisableNativeTypes: true}
// Variables for tests on types which implement Stringer interface with and
// without a pointer receiver.
@ -154,6 +155,26 @@ func initSpewTests() {
dt := depthTester{indirCir1{nil}, [1]string{"arr"}, []string{"slice"},
map[string]int{"one": 1}}
// nestedNativeTypesTester and nativeTypesTester are used to test the
// disabling of printing of native types.
type nestedNativeTypesTester struct {
intField int
}
type nativeTypesTester struct {
intField int
stringField string
stringSlice []string
nestedNativeTypesTester nestedNativeTypesTester
nestedNativeTypesTesterSlice []nestedNativeTypesTester
}
nt := nativeTypesTester{
intField: 1,
stringField: "A",
stringSlice: []string{"B"},
nestedNativeTypesTester: nestedNativeTypesTester{intField: 2},
nestedNativeTypesTesterSlice: []nestedNativeTypesTester{{intField: 3}},
}
// Variable for tests on types which implement error interface.
te := customError(10)
@ -203,6 +224,20 @@ 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"},
{scsNoNativeTypes, fCSSdump, "", nt, "(spew_test.nativeTypesTester) {\n" +
" intField: 1,\n" +
" stringField: (len=1) \"A\",\n" +
" stringSlice: ([]string) (len=1 cap=1) {\n" +
" (len=1) \"B\"\n" +
" },\n" +
" nestedNativeTypesTester: (spew_test.nestedNativeTypesTester) {\n" +
" intField: 2\n" +
" },\n" +
" nestedNativeTypesTesterSlice: ([]spew_test.nestedNativeTypesTester) (len=1 cap=1) {\n" +
" (spew_test.nestedNativeTypesTester) {\n" +
" intField: 3\n" +
" }\n" +
" }\n}\n"},
}
}