Add tests for custom type that panics in Stringer.

This commit is contained in:
Dave Collins 2013-01-20 12:31:42 -06:00
parent 5c8d842977
commit ad1d81b355
3 changed files with 54 additions and 0 deletions

View File

@ -50,6 +50,7 @@ base test element are also tested to ensure proper indirection across all types.
- Struct that is circular through self referencing - Struct that is circular through self referencing
- Structs that are circular through cross referencing - Structs that are circular through cross referencing
- Structs that are indirectly circular - Structs that are indirectly circular
- Type that panics in its Stringer interface
*/ */
package spew_test package spew_test
@ -748,6 +749,21 @@ func addCircularDumpTests() {
addDumpTest(&pv3, "(**"+v3t+")("+pv3Addr+"->"+v3Addr+")("+v3s2+")\n") addDumpTest(&pv3, "(**"+v3t+")("+pv3Addr+"->"+v3Addr+")("+v3s2+")\n")
} }
func addPanicDumpTests() {
// Type that panics in its Stringer interface.
v := panicer(127)
nv := (*panicer)(nil)
pv := &v
vAddr := fmt.Sprintf("%p", pv)
pvAddr := fmt.Sprintf("%p", &pv)
vt := "spew_test.panicer"
vs := "(PANIC=test panic)127"
addDumpTest(v, "("+vt+") "+vs+"\n")
addDumpTest(pv, "(*"+vt+")("+vAddr+")("+vs+")\n")
addDumpTest(&pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")("+vs+")\n")
addDumpTest(nv, "(*"+vt+")(<nil>)\n")
}
// TestDump executes all of the tests described by dumpTests. // TestDump executes all of the tests described by dumpTests.
func TestDump(t *testing.T) { func TestDump(t *testing.T) {
// Setup tests. // Setup tests.
@ -767,6 +783,7 @@ func TestDump(t *testing.T) {
addChanDumpTests() addChanDumpTests()
addFuncDumpTests() addFuncDumpTests()
addCircularDumpTests() addCircularDumpTests()
addPanicDumpTests()
t.Logf("Running %d tests", len(dumpTests)) t.Logf("Running %d tests", len(dumpTests))
for i, test := range dumpTests { for i, test := range dumpTests {

View File

@ -50,6 +50,7 @@ base test element are also tested to ensure proper indirection across all types.
- Struct that is circular through self referencing - Struct that is circular through self referencing
- Structs that are circular through cross referencing - Structs that are circular through cross referencing
- Structs that are indirectly circular - Structs that are indirectly circular
- Type that panics in its Stringer interface
*/ */
package spew_test package spew_test
@ -1222,6 +1223,33 @@ func addCircularFormatterTests() {
addFormatterTest("%#+v", &pv3, "(**"+v3t+")("+pv3Addr+"->"+v3Addr+")"+v3s8) addFormatterTest("%#+v", &pv3, "(**"+v3t+")("+pv3Addr+"->"+v3Addr+")"+v3s8)
} }
func addPanicFormatterTests() {
// Type that panics in its Stringer interface.
v := panicer(127)
nv := (*panicer)(nil)
pv := &v
vAddr := fmt.Sprintf("%p", pv)
pvAddr := fmt.Sprintf("%p", &pv)
vt := "spew_test.panicer"
vs := "(PANIC=test panic)127"
addFormatterTest("%v", v, vs)
addFormatterTest("%v", pv, "<*>"+vs)
addFormatterTest("%v", &pv, "<**>"+vs)
addFormatterTest("%v", nv, "<nil>")
addFormatterTest("%+v", v, vs)
addFormatterTest("%+v", pv, "<*>("+vAddr+")"+vs)
addFormatterTest("%+v", &pv, "<**>("+pvAddr+"->"+vAddr+")"+vs)
addFormatterTest("%+v", nv, "<nil>")
addFormatterTest("%#v", v, "("+vt+")"+vs)
addFormatterTest("%#v", pv, "(*"+vt+")"+vs)
addFormatterTest("%#v", &pv, "(**"+vt+")"+vs)
addFormatterTest("%#v", nv, "(*"+vt+")"+"<nil>")
addFormatterTest("%#+v", v, "("+vt+")"+vs)
addFormatterTest("%#+v", pv, "(*"+vt+")("+vAddr+")"+vs)
addFormatterTest("%#+v", &pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")"+vs)
addFormatterTest("%#+v", nv, "(*"+vt+")"+"<nil>")
}
// TestFormatter executes all of the tests described by formatterTests. // TestFormatter executes all of the tests described by formatterTests.
func TestFormatter(t *testing.T) { func TestFormatter(t *testing.T) {
// Setup tests. // Setup tests.
@ -1241,6 +1269,7 @@ func TestFormatter(t *testing.T) {
addChanFormatterTests() addChanFormatterTests()
addFuncFormatterTests() addFuncFormatterTests()
addCircularFormatterTests() addCircularFormatterTests()
addPanicFormatterTests()
t.Logf("Running %d tests", len(formatterTests)) t.Logf("Running %d tests", len(formatterTests))
for i, test := range formatterTests { for i, test := range formatterTests {

View File

@ -50,6 +50,14 @@ func testFailed(result string, wants []string) bool {
return true return true
} }
// panicer is used to intentionally cause a panic for testing spew properly
// handles them
type panicer int
func (p panicer) String() string {
panic("test panic")
}
// spewFunc is used to identify which public function of the spew package or // spewFunc is used to identify which public function of the spew package or
// ConfigState a test applies to. // ConfigState a test applies to.
type spewFunc int type spewFunc int