Consolidate tests for invalid reflect values.

This commit is contained in:
Dave Collins 2013-01-20 22:24:52 -06:00
parent 23b797ffdf
commit 3b5249e43e
1 changed files with 19 additions and 17 deletions

View File

@ -36,6 +36,9 @@ type dummyFmtState struct {
} }
func (dfs *dummyFmtState) Flag(f int) bool { func (dfs *dummyFmtState) Flag(f int) bool {
if f == int('+') {
return true
}
return false return false
} }
@ -47,10 +50,14 @@ func (dfs *dummyFmtState) Width() (int, bool) {
return 0, false return 0, false
} }
// TestDumpInvalidReflectValue ensures the dump code handles an invalid reflect // TestInvalidReflectValue ensures the dump and formatter code handles an
// value properly. This needs access to internal state since it should never // invalid reflect value properly. This needs access to internal state since it
// happen in real code and therefore can't be tested via the public API. // should never happen in real code and therefore can't be tested via the public
func TestDumpInvalidReflectValue(t *testing.T) { // API.
func TestInvalidReflectValue(t *testing.T) {
i := 1
// Dump invalid reflect value.
v := new(reflect.Value) v := new(reflect.Value)
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
d := dumpState{w: buf, cs: &Config} d := dumpState{w: buf, cs: &Config}
@ -58,22 +65,17 @@ func TestDumpInvalidReflectValue(t *testing.T) {
s := buf.String() s := buf.String()
want := "<invalid>" want := "<invalid>"
if s != want { if s != want {
t.Errorf("DumpInvalidReflectValue\n got: %s want: %s", s, want) t.Errorf("InvalidReflectValue #%d\n got: %s want: %s", i, s, want)
}
} }
i++
// TestFormatterInvalidReflectValue ensures the formatter code handles an // Formatter invalid reflect value.
// invalid reflect value properly. This needs access to internal state since it buf2 := new(dummyFmtState)
// should never happen in real code and therefore can't be tested via the public f := formatState{value: *v, cs: &Config, fs: buf2}
// API.
func TestFormatterInvalidReflectValue(t *testing.T) {
v := new(reflect.Value)
buf := new(dummyFmtState)
f := formatState{value: *v, cs: &Config, fs: buf}
f.format(*v) f.format(*v)
s := buf.String() s = buf2.String()
want := "<invalid>" want = "<invalid>"
if s != want { if s != want {
t.Errorf("FormatterInvalidReflectValue\n got: %s want: %s", s, want) t.Errorf("InvalidReflectValue #%d got: %s want: %s", i, s, want)
} }
} }