summaryrefslogtreecommitdiff
path: root/spew/format_test.go
diff options
context:
space:
mode:
authorDave Collins <[email protected]>2013-01-20 12:02:36 -0600
committerDave Collins <[email protected]>2013-01-20 12:02:36 -0600
commit5c8d842977941cdb1663827b8c6a93046e211bbb (patch)
tree2da421934d0d0c93e15c5d1a195945c5f472437b /spew/format_test.go
parent1c16a20c214d354cf84ca9646c73fc78b0620f7e (diff)
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.
Diffstat (limited to 'spew/format_test.go')
-rw-r--r--spew/format_test.go44
1 files changed, 24 insertions, 20 deletions
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, "<nil>")
- 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, "<nil>")
- 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+")"+"<nil>")
- 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+")"+"<nil>")
// 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
}
}