From eba42209a79e61d85602324c3afefe02a29d2afc Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Sun, 20 Jan 2013 14:03:59 -0600 Subject: [PATCH] Add formatter passthrough tests for non %v verbs. --- spew/format_test.go | 47 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/spew/format_test.go b/spew/format_test.go index 847b2f8..cbc3bd7 100644 --- a/spew/format_test.go +++ b/spew/format_test.go @@ -52,6 +52,12 @@ base test element are also tested to ensure proper indirection across all types. - Structs that are indirectly circular - Type that panics in its Stringer interface - Type that has a custom Error interface +- %x passthrough with uint +- %#x passthrough with uint +- %f passthrough with precision +- %f passthrough with width and precision +- %d passthrough with width +- %q passthrough with string */ package spew_test @@ -1278,6 +1284,46 @@ func addErrorFormatterTests() { addFormatterTest("%#+v", nv, "(*"+vt+")"+"") } +func addPassthroughFormatterTests() { + // %x passthrough with uint. + v := uint(4294967295) + pv := &v + vAddr := fmt.Sprintf("%x", pv) + pvAddr := fmt.Sprintf("%x", &pv) + vs := "ffffffff" + addFormatterTest("%x", v, vs) + addFormatterTest("%x", pv, vAddr) + addFormatterTest("%x", &pv, pvAddr) + + // %#x passthrough with uint. + v2 := int(2147483647) + pv2 := &v2 + v2Addr := fmt.Sprintf("%#x", pv2) + pv2Addr := fmt.Sprintf("%#x", &pv2) + v2s := "0x7fffffff" + addFormatterTest("%#x", v2, v2s) + addFormatterTest("%#x", pv2, v2Addr) + addFormatterTest("%#x", &pv2, pv2Addr) + + // %f passthrough with precision. + addFormatterTest("%.2f", 3.1415, "3.14") + addFormatterTest("%.3f", 3.1415, "3.142") + addFormatterTest("%.4f", 3.1415, "3.1415") + + // %f passthrough with width and precision. + addFormatterTest("%5.2f", 3.1415, " 3.14") + addFormatterTest("%6.3f", 3.1415, " 3.142") + addFormatterTest("%7.4f", 3.1415, " 3.1415") + + // %d passthrough with width. + addFormatterTest("%3d", 127, "127") + addFormatterTest("%4d", 127, " 127") + addFormatterTest("%5d", 127, " 127") + + // %q passthrough with string. + addFormatterTest("%q", "test", "\"test\"") +} + // TestFormatter executes all of the tests described by formatterTests. func TestFormatter(t *testing.T) { // Setup tests. @@ -1299,6 +1345,7 @@ func TestFormatter(t *testing.T) { addCircularFormatterTests() addPanicFormatterTests() addErrorFormatterTests() + addPassthroughFormatterTests() t.Logf("Running %d tests", len(formatterTests)) for i, test := range formatterTests {