From 5c8d842977941cdb1663827b8c6a93046e211bbb Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Sun, 20 Jan 2013 12:02:36 -0600 Subject: Add tests for maps with multiple entries. Previously, the tests did not include maps with more than a single entry since the iteration order is randomized and the tests only accepted a single valid expected value. This commit modifies the tests to accept multiple valid expected values and adds tests for a multi-entry map to both Dump and Formatter. --- spew/format_test.go | 44 ++++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 20 deletions(-) (limited to 'spew/format_test.go') diff --git a/spew/format_test.go b/spew/format_test.go index 12ef478..dcd54d9 100644 --- a/spew/format_test.go +++ b/spew/format_test.go @@ -66,7 +66,7 @@ import ( type formatterTest struct { format string in interface{} - want string + wants []string } // formatterTests houses all of the tests to be performed against NewFormatter. @@ -74,8 +74,8 @@ var formatterTests = make([]formatterTest, 0) // addFormatterTest is a helper method to append the passed input and desired // result to formatterTests. -func addFormatterTest(format string, in interface{}, want string) { - test := formatterTest{format, in, want} +func addFormatterTest(format string, in interface{}, wants ...string) { + test := formatterTest{format, in, wants} formatterTests = append(formatterTests, test) } @@ -702,28 +702,32 @@ func addNilInterfaceFormatterTests() { func addMapFormatterTests() { // Map with string keys and int vals. - v := map[string]int{"one": 1} + v := map[string]int{"one": 1, "two": 2} nv := (*map[string]int)(nil) pv := &v vAddr := fmt.Sprintf("%p", pv) pvAddr := fmt.Sprintf("%p", &pv) vt := "map[string]int" - vs := "map[one:1]" - addFormatterTest("%v", v, vs) - addFormatterTest("%v", pv, "<*>"+vs) - addFormatterTest("%v", &pv, "<**>"+vs) + vs := "map[one:1 two:2]" + vs2 := "map[two:2 one:1]" + addFormatterTest("%v", v, vs, vs2) + addFormatterTest("%v", pv, "<*>"+vs, "<*>"+vs2) + addFormatterTest("%v", &pv, "<**>"+vs, "<**>"+vs2) addFormatterTest("%+v", nv, "") - addFormatterTest("%+v", v, vs) - addFormatterTest("%+v", pv, "<*>("+vAddr+")"+vs) - addFormatterTest("%+v", &pv, "<**>("+pvAddr+"->"+vAddr+")"+vs) + addFormatterTest("%+v", v, vs, vs2) + addFormatterTest("%+v", pv, "<*>("+vAddr+")"+vs, "<*>("+vAddr+")"+vs2) + addFormatterTest("%+v", &pv, "<**>("+pvAddr+"->"+vAddr+")"+vs, + "<**>("+pvAddr+"->"+vAddr+")"+vs2) addFormatterTest("%+v", nv, "") - addFormatterTest("%#v", v, "("+vt+")"+vs) - addFormatterTest("%#v", pv, "(*"+vt+")"+vs) - addFormatterTest("%#v", &pv, "(**"+vt+")"+vs) + addFormatterTest("%#v", v, "("+vt+")"+vs, "("+vt+")"+vs2) + addFormatterTest("%#v", pv, "(*"+vt+")"+vs, "(*"+vt+")"+vs2) + addFormatterTest("%#v", &pv, "(**"+vt+")"+vs, "(**"+vt+")"+vs2) addFormatterTest("%#v", nv, "(*"+vt+")"+"") - addFormatterTest("%#+v", v, "("+vt+")"+vs) - addFormatterTest("%#+v", pv, "(*"+vt+")("+vAddr+")"+vs) - addFormatterTest("%#+v", &pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")"+vs) + addFormatterTest("%#+v", v, "("+vt+")"+vs, "("+vt+")"+vs2) + addFormatterTest("%#+v", pv, "(*"+vt+")("+vAddr+")"+vs, + "(*"+vt+")("+vAddr+")"+vs2) + addFormatterTest("%#+v", &pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")"+vs, + "(**"+vt+")("+pvAddr+"->"+vAddr+")"+vs2) addFormatterTest("%#+v", nv, "(*"+vt+")"+"") // Map with custom formatter type on pointer receiver only keys and vals. @@ -1243,9 +1247,9 @@ func TestFormatter(t *testing.T) { buf := new(bytes.Buffer) spew.Fprintf(buf, test.format, test.in) s := buf.String() - if test.want != s { - t.Errorf("Formatter #%d format: %s got: %s want: %s", i, - test.format, s, test.want) + if testFailed(s, test.wants) { + t.Errorf("Formatter #%d format: %s got: %s %s", i, test.format, s, + stringizeWants(test.wants)) continue } } -- cgit v1.2.3