Add tests for types with custom Error interface.

This commit is contained in:
Dave Collins 2013-01-20 13:41:53 -06:00
parent 57a610269f
commit 89496a6569
3 changed files with 52 additions and 0 deletions

View File

@ -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 {

View File

@ -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 {

View File

@ -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 {