Merge 10fccbfd0b
into d8f796af33
This commit is contained in:
commit
4b5dfa8d41
|
@ -76,6 +76,10 @@ type ConfigState struct {
|
||||||
// data structures in tests.
|
// data structures in tests.
|
||||||
DisableCapacities bool
|
DisableCapacities bool
|
||||||
|
|
||||||
|
// DisableUnexported specifies whether to disable the unexported fields of
|
||||||
|
// struct. This is useful for debugging APIs.
|
||||||
|
DisableUnexported 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
|
||||||
|
|
|
@ -26,6 +26,7 @@ import (
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"unicode"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -413,8 +414,11 @@ func (d *dumpState) dump(v reflect.Value) {
|
||||||
vt := v.Type()
|
vt := v.Type()
|
||||||
numFields := v.NumField()
|
numFields := v.NumField()
|
||||||
for i := 0; i < numFields; i++ {
|
for i := 0; i < numFields; i++ {
|
||||||
d.indent()
|
|
||||||
vtf := vt.Field(i)
|
vtf := vt.Field(i)
|
||||||
|
if d.cs.DisableUnexported && !unicode.IsUpper([]rune(vtf.Name)[0]) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
d.indent()
|
||||||
d.w.Write([]byte(vtf.Name))
|
d.w.Write([]byte(vtf.Name))
|
||||||
d.w.Write(colonSpaceBytes)
|
d.w.Write(colonSpaceBytes)
|
||||||
d.ignoreNextIndent = true
|
d.ignoreNextIndent = true
|
||||||
|
|
|
@ -44,6 +44,7 @@ base test element are also tested to ensure proper indirection across all types.
|
||||||
- Struct that contains custom type with Stringer pointer interface via both
|
- Struct that contains custom type with Stringer pointer interface via both
|
||||||
exported and unexported fields
|
exported and unexported fields
|
||||||
- Struct that contains embedded struct and field to same struct
|
- Struct that contains embedded struct and field to same struct
|
||||||
|
- Struct that contains unexported field and exported field
|
||||||
- Uintptr to 0 (null pointer)
|
- Uintptr to 0 (null pointer)
|
||||||
- Uintptr address of real variable
|
- Uintptr address of real variable
|
||||||
- Unsafe.Pointer to 0 (null pointer)
|
- Unsafe.Pointer to 0 (null pointer)
|
||||||
|
@ -1040,3 +1041,61 @@ func TestDumpSortedKeys(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDumpUnexportedFields(t *testing.T) {
|
||||||
|
dumpTests = make([]dumpTest, 0)
|
||||||
|
|
||||||
|
// Struct with both exported and unexported field.
|
||||||
|
type s struct {
|
||||||
|
ExportedVarA int8
|
||||||
|
ExportedVarB int8
|
||||||
|
unExportedVarC int8
|
||||||
|
unExportedVarD int8
|
||||||
|
}
|
||||||
|
v := s{10, 20, 30, 40}
|
||||||
|
nv := (*s)(nil)
|
||||||
|
pv := &v
|
||||||
|
vAddr := fmt.Sprintf("%p", pv)
|
||||||
|
pvAddr := fmt.Sprintf("%p", &pv)
|
||||||
|
vt := "spew_test.s"
|
||||||
|
vt2 := "int8"
|
||||||
|
vs := "{\n ExportedVarA: (" + vt2 + ") 10,\n ExportedVarB: (" + vt2 + ") 20,\n}"
|
||||||
|
|
||||||
|
addDumpTest(v, "("+vt+") "+vs+"\n")
|
||||||
|
addDumpTest(pv, "(*"+vt+")("+vAddr+")("+vs+")\n")
|
||||||
|
addDumpTest(&pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")("+vs+")\n")
|
||||||
|
addDumpTest(nv, "(*"+vt+")(<nil>)\n")
|
||||||
|
|
||||||
|
type s2 struct {
|
||||||
|
unExportedVarA int8
|
||||||
|
unExportedVarB int8
|
||||||
|
ExportedVarC int8
|
||||||
|
ExportedVarD int8
|
||||||
|
}
|
||||||
|
v2 := s2{10, 20, 30, 40}
|
||||||
|
nv2 := (*s2)(nil)
|
||||||
|
pv2 := &v2
|
||||||
|
v2Addr := fmt.Sprintf("%p", pv2)
|
||||||
|
pv2Addr := fmt.Sprintf("%p", &pv2)
|
||||||
|
v2t := "spew_test.s2"
|
||||||
|
v2t2 := "int8"
|
||||||
|
v2s := "{\n ExportedVarC: (" + v2t2 + ") 30,\n ExportedVarD: (" + v2t2 + ") 40\n}"
|
||||||
|
|
||||||
|
addDumpTest(v2, "("+v2t+") "+v2s+"\n")
|
||||||
|
addDumpTest(pv2, "(*"+v2t+")("+v2Addr+")("+v2s+")\n")
|
||||||
|
addDumpTest(&pv2, "(**"+v2t+")("+pv2Addr+"->"+v2Addr+")("+v2s+")\n")
|
||||||
|
addDumpTest(nv2, "(*"+v2t+")(<nil>)\n")
|
||||||
|
|
||||||
|
spew.Config.DisableUnexported = true
|
||||||
|
|
||||||
|
t.Logf("Running %d tests", len(dumpTests))
|
||||||
|
for i, test := range dumpTests {
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
spew.Fdump(buf, test.in)
|
||||||
|
s := buf.String()
|
||||||
|
if testFailed(s, test.wants) {
|
||||||
|
t.Errorf("Dump #%d\n got: %s %s", i, s, stringizeWants(test.wants))
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue