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.
This commit is contained in:
Dave Collins 2013-01-20 12:02:36 -06:00
parent 1c16a20c21
commit 5c8d842977
3 changed files with 65 additions and 31 deletions

View File

@ -94,8 +94,8 @@ type indirCir3 struct {
// dumpTest is used to describe a test to be perfomed against the Dump method.
type dumpTest struct {
in interface{}
want string
in interface{}
wants []string
}
// dumpTests houses all of the tests to be performed against the Dump method.
@ -103,8 +103,8 @@ var dumpTests = make([]dumpTest, 0)
// addDumpTest is a helper method to append the passed input and desired result
// to dumpTests
func addDumpTest(in interface{}, want string) {
test := dumpTest{in, want}
func addDumpTest(in interface{}, wants ...string) {
test := dumpTest{in, wants}
dumpTests = append(dumpTests, test)
}
@ -448,7 +448,7 @@ func addNilInterfaceDumpTests() {
func addMapDumpTests() {
// 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)
@ -456,10 +456,15 @@ func addMapDumpTests() {
vt := "map[string]int"
vt1 := "string"
vt2 := "int"
vs := "{\n (" + vt1 + ") \"one\": (" + vt2 + ") 1\n}"
addDumpTest(v, "("+vt+") "+vs+"\n")
addDumpTest(pv, "(*"+vt+")("+vAddr+")("+vs+")\n")
addDumpTest(&pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")("+vs+")\n")
vs := "{\n (" + vt1 + ") \"one\": (" + vt2 + ") 1,\n (" + vt1 +
") \"two\": (" + vt2 + ") 2\n}"
vs2 := "{\n (" + vt1 + ") \"two\": (" + vt2 + ") 2,\n (" + vt1 +
") \"one\": (" + vt2 + ") 1\n}"
addDumpTest(v, "("+vt+") "+vs+"\n", "("+vt+") "+vs2+"\n")
addDumpTest(pv, "(*"+vt+")("+vAddr+")("+vs+")\n",
"(*"+vt+")("+vAddr+")("+vs2+")\n")
addDumpTest(&pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")("+vs+")\n",
"(**"+vt+")("+pvAddr+"->"+vAddr+")("+vs2+")\n")
addDumpTest(nv, "(*"+vt+")(<nil>)\n")
// Map with custom formatter type on pointer receiver only keys and vals.
@ -768,8 +773,8 @@ func TestDump(t *testing.T) {
buf := new(bytes.Buffer)
spew.Fdump(buf, test.in)
s := buf.String()
if test.want != s {
t.Errorf("Dump #%d\n got: %s want: %s", i, s, test.want)
if testFailed(s, test.wants) {
t.Errorf("Dump #%d\n got: %s %s", i, s, stringizeWants(test.wants))
continue
}
}

View File

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

View File

@ -25,6 +25,31 @@ import (
"testing"
)
// stringizeWants converts a slice of wanted test output into a format suitable
// for an test error message.
func stringizeWants(wants []string) string {
s := ""
for i, want := range wants {
if i > 0 {
s += fmt.Sprintf("want%d: %s", i+1, want)
} else {
s += "want: " + want
}
}
return s
}
// testFailed returns whether or not a test failed by checking if the result
// of the test is in the slice of wanted strings.
func testFailed(result string, wants []string) bool {
for _, want := range wants {
if result == want {
return false
}
}
return true
}
// spewFunc is used to identify which public function of the spew package or
// ConfigState a test applies to.
type spewFunc int