summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--spew/common_test.go7
-rw-r--r--spew/dump_test.go16
-rw-r--r--spew/format_test.go29
3 files changed, 52 insertions, 0 deletions
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+")(<nil>)\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+")(<nil>)\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+")"+"<nil>")
}
+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, "<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.
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 {