From 89496a6569e5cccff78a35628ab4057b11cf85a9 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Sun, 20 Jan 2013 13:41:53 -0600 Subject: [PATCH] Add tests for types with custom Error interface. --- spew/common_test.go | 7 +++++++ spew/dump_test.go | 16 ++++++++++++++++ spew/format_test.go | 29 +++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+) diff --git a/spew/common_test.go b/spew/common_test.go index 78b1f81..f0119e6 100644 --- a/spew/common_test.go +++ b/spew/common_test.go @@ -58,6 +58,13 @@ func (p panicer) String() string { panic("test panic") } +// customError is used to test custom error interface invocation. +type customError int + +func (e customError) Error() string { + return fmt.Sprintf("error: %d", int(e)) +} + // stringizeWants converts a slice of wanted test output into a format suitable // for an test error message. func stringizeWants(wants []string) string { diff --git a/spew/dump_test.go b/spew/dump_test.go index f4de975..0d282c2 100644 --- a/spew/dump_test.go +++ b/spew/dump_test.go @@ -734,6 +734,21 @@ func addPanicDumpTests() { addDumpTest(nv, "(*"+vt+")()\n") } +func addErrorDumpTests() { + // Type that has a custom Error interface. + v := customError(127) + nv := (*customError)(nil) + pv := &v + vAddr := fmt.Sprintf("%p", pv) + pvAddr := fmt.Sprintf("%p", &pv) + vt := "spew_test.customError" + vs := "error: 127" + addDumpTest(v, "("+vt+") "+vs+"\n") + addDumpTest(pv, "(*"+vt+")("+vAddr+")("+vs+")\n") + addDumpTest(&pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")("+vs+")\n") + addDumpTest(nv, "(*"+vt+")()\n") +} + // TestDump executes all of the tests described by dumpTests. func TestDump(t *testing.T) { // Setup tests. @@ -754,6 +769,7 @@ func TestDump(t *testing.T) { addFuncDumpTests() addCircularDumpTests() addPanicDumpTests() + addErrorDumpTests() t.Logf("Running %d tests", len(dumpTests)) for i, test := range dumpTests { diff --git a/spew/format_test.go b/spew/format_test.go index a419c74..847b2f8 100644 --- a/spew/format_test.go +++ b/spew/format_test.go @@ -51,6 +51,7 @@ base test element are also tested to ensure proper indirection across all types. - Structs that are circular through cross referencing - Structs that are indirectly circular - Type that panics in its Stringer interface +- Type that has a custom Error interface */ package spew_test @@ -1250,6 +1251,33 @@ func addPanicFormatterTests() { addFormatterTest("%#+v", nv, "(*"+vt+")"+"") } +func addErrorFormatterTests() { + // Type that has a custom Error interface. + v := customError(127) + nv := (*customError)(nil) + pv := &v + vAddr := fmt.Sprintf("%p", pv) + pvAddr := fmt.Sprintf("%p", &pv) + vt := "spew_test.customError" + vs := "error: 127" + addFormatterTest("%v", v, vs) + addFormatterTest("%v", pv, "<*>"+vs) + addFormatterTest("%v", &pv, "<**>"+vs) + addFormatterTest("%v", nv, "") + addFormatterTest("%+v", v, vs) + addFormatterTest("%+v", pv, "<*>("+vAddr+")"+vs) + addFormatterTest("%+v", &pv, "<**>("+pvAddr+"->"+vAddr+")"+vs) + addFormatterTest("%+v", nv, "") + addFormatterTest("%#v", v, "("+vt+")"+vs) + addFormatterTest("%#v", pv, "(*"+vt+")"+vs) + addFormatterTest("%#v", &pv, "(**"+vt+")"+vs) + addFormatterTest("%#v", nv, "(*"+vt+")"+"") + addFormatterTest("%#+v", v, "("+vt+")"+vs) + addFormatterTest("%#+v", pv, "(*"+vt+")("+vAddr+")"+vs) + addFormatterTest("%#+v", &pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")"+vs) + addFormatterTest("%#+v", nv, "(*"+vt+")"+"") +} + // TestFormatter executes all of the tests described by formatterTests. func TestFormatter(t *testing.T) { // Setup tests. @@ -1270,6 +1298,7 @@ func TestFormatter(t *testing.T) { addFuncFormatterTests() addCircularFormatterTests() addPanicFormatterTests() + addErrorFormatterTests() t.Logf("Running %d tests", len(formatterTests)) for i, test := range formatterTests {