Add ConfigState to spewTests.

This paves the way for tests against the ConfigState options.
This commit is contained in:
Dave Collins 2013-01-20 16:30:54 -06:00
parent 3ad8c5b5ee
commit 0cd00fd3ae
1 changed files with 24 additions and 23 deletions

View File

@ -25,6 +25,8 @@ import (
"testing" "testing"
) )
var scsDefault = spew.NewDefaultConfig()
// spewFunc is used to identify which public function of the spew package or // spewFunc is used to identify which public function of the spew package or
// ConfigState a test applies to. // ConfigState a test applies to.
type spewFunc int type spewFunc int
@ -72,6 +74,7 @@ func (f spewFunc) String() string {
// spewTest is used to describe a test to be performed against the public // spewTest is used to describe a test to be performed against the public
// functions of the spew package or ConfigState. // functions of the spew package or ConfigState.
type spewTest struct { type spewTest struct {
cs *spew.ConfigState
f spewFunc f spewFunc
format string format string
in interface{} in interface{}
@ -85,19 +88,19 @@ type spewTest struct {
// and are intentionally not exhaustive of types. The exhaustive type // and are intentionally not exhaustive of types. The exhaustive type
// tests are handled in the dump and format tests. // tests are handled in the dump and format tests.
var spewTests = []spewTest{ var spewTests = []spewTest{
{fCSFdump, "", int8(127), "(int8) 127\n"}, {scsDefault, fCSFdump, "", int8(127), "(int8) 127\n"},
{fCSFprint, "", int16(32767), "32767"}, {scsDefault, fCSFprint, "", int16(32767), "32767"},
{fCSFprintf, "%v", int32(2147483647), "2147483647"}, {scsDefault, fCSFprintf, "%v", int32(2147483647), "2147483647"},
{fCSFprintln, "", int(2147483647), "2147483647\n"}, {scsDefault, fCSFprintln, "", int(2147483647), "2147483647\n"},
{fCSPrint, "", int64(9223372036854775807), "9223372036854775807"}, {scsDefault, fCSPrint, "", int64(9223372036854775807), "9223372036854775807"},
{fCSPrintln, "", uint8(255), "255\n"}, {scsDefault, fCSPrintln, "", uint8(255), "255\n"},
{fCSErrorf, "%#v", uint16(65535), "(uint16)65535"}, {scsDefault, fCSErrorf, "%#v", uint16(65535), "(uint16)65535"},
{fCSNewFormatter, "%v", uint32(4294967295), "4294967295"}, {scsDefault, fCSNewFormatter, "%v", uint32(4294967295), "4294967295"},
{fErrorf, "%v", uint64(18446744073709551615), "18446744073709551615"}, {scsDefault, fErrorf, "%v", uint64(18446744073709551615), "18446744073709551615"},
{fFprint, "", float32(3.14), "3.14"}, {scsDefault, fFprint, "", float32(3.14), "3.14"},
{fFprintln, "", float64(6.28), "6.28\n"}, {scsDefault, fFprintln, "", float64(6.28), "6.28\n"},
{fPrint, "", true, "true"}, {scsDefault, fPrint, "", true, "true"},
{fPrintln, "", false, "false\n"}, {scsDefault, fPrintln, "", false, "false\n"},
} }
// redirStdout is a helper function to return the standard output from f as a // redirStdout is a helper function to return the standard output from f as a
@ -121,26 +124,24 @@ func redirStdout(f func()) ([]byte, error) {
// TestSpew executes all of the tests described by spewTests. // TestSpew executes all of the tests described by spewTests.
func TestSpew(t *testing.T) { func TestSpew(t *testing.T) {
scs := spew.NewDefaultConfig()
t.Logf("Running %d tests", len(spewTests)) t.Logf("Running %d tests", len(spewTests))
for i, test := range spewTests { for i, test := range spewTests {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
switch test.f { switch test.f {
case fCSFdump: case fCSFdump:
scs.Fdump(buf, test.in) test.cs.Fdump(buf, test.in)
case fCSFprint: case fCSFprint:
scs.Fprint(buf, test.in) test.cs.Fprint(buf, test.in)
case fCSFprintf: case fCSFprintf:
scs.Fprintf(buf, test.format, test.in) test.cs.Fprintf(buf, test.format, test.in)
case fCSFprintln: case fCSFprintln:
scs.Fprintln(buf, test.in) test.cs.Fprintln(buf, test.in)
case fCSPrint: case fCSPrint:
b, err := redirStdout(func() { scs.Print(test.in) }) b, err := redirStdout(func() { test.cs.Print(test.in) })
if err != nil { if err != nil {
t.Errorf("%v #%d %v", test.f, i, err) t.Errorf("%v #%d %v", test.f, i, err)
continue continue
@ -148,7 +149,7 @@ func TestSpew(t *testing.T) {
buf.Write(b) buf.Write(b)
case fCSPrintln: case fCSPrintln:
b, err := redirStdout(func() { scs.Println(test.in) }) b, err := redirStdout(func() { test.cs.Println(test.in) })
if err != nil { if err != nil {
t.Errorf("%v #%d %v", test.f, i, err) t.Errorf("%v #%d %v", test.f, i, err)
continue continue
@ -156,11 +157,11 @@ func TestSpew(t *testing.T) {
buf.Write(b) buf.Write(b)
case fCSErrorf: case fCSErrorf:
err := scs.Errorf(test.format, test.in) err := test.cs.Errorf(test.format, test.in)
buf.WriteString(err.Error()) buf.WriteString(err.Error())
case fCSNewFormatter: case fCSNewFormatter:
fmt.Fprintf(buf, test.format, scs.NewFormatter(test.in)) fmt.Fprintf(buf, test.format, test.cs.NewFormatter(test.in))
case fErrorf: case fErrorf:
err := spew.Errorf(test.format, test.in) err := spew.Errorf(test.format, test.in)