summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDave Collins <[email protected]>2013-01-20 20:48:14 -0600
committerDave Collins <[email protected]>2013-01-20 20:48:14 -0600
commit23b797ffdfc118c96d3479162fb131ac1891c4eb (patch)
treea3f5a3bafe77b6f5eba3def0b5bafea695b97ca5
parenta9907c15845e8273e99fd14a9f1641020ffd5cac (diff)
Add tests for invalid reflect values.
-rw-r--r--spew/internal_test.go79
1 files changed, 79 insertions, 0 deletions
diff --git a/spew/internal_test.go b/spew/internal_test.go
new file mode 100644
index 0000000..8e6e54d
--- /dev/null
+++ b/spew/internal_test.go
@@ -0,0 +1,79 @@
+/*
+ * Copyright (c) 2013 Dave Collins <[email protected]>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+/*
+This test file is part of the spew package rather than than the spew_test
+package because it needs access to internals to properly test certain cases
+which are not possible via the public interface since they should never happen.
+*/
+
+package spew
+
+import (
+ "bytes"
+ "reflect"
+ "testing"
+)
+
+// dummyFmtState implements a fake fmt.State to use for testing invalid
+// reflect.Value handling. This is necessary because the fmt package catches
+// invalid values before invoking the formatter on them.
+type dummyFmtState struct {
+ bytes.Buffer
+}
+
+func (dfs *dummyFmtState) Flag(f int) bool {
+ return false
+}
+
+func (dfs *dummyFmtState) Precision() (int, bool) {
+ return 0, false
+}
+
+func (dfs *dummyFmtState) Width() (int, bool) {
+ return 0, false
+}
+
+// TestDumpInvalidReflectValue ensures the dump code handles an invalid reflect
+// value properly. This needs access to internal state since it should never
+// happen in real code and therefore can't be tested via the public API.
+func TestDumpInvalidReflectValue(t *testing.T) {
+ v := new(reflect.Value)
+ buf := new(bytes.Buffer)
+ d := dumpState{w: buf, cs: &Config}
+ d.dump(*v)
+ s := buf.String()
+ want := "<invalid>"
+ if s != want {
+ t.Errorf("DumpInvalidReflectValue\n got: %s want: %s", s, want)
+ }
+}
+
+// TestFormatterInvalidReflectValue ensures the formatter code handles an
+// invalid reflect value properly. This needs access to internal state since it
+// should never happen in real code and therefore can't be tested via the public
+// API.
+func TestFormatterInvalidReflectValue(t *testing.T) {
+ v := new(reflect.Value)
+ buf := new(dummyFmtState)
+ f := formatState{value: *v, cs: &Config, fs: buf}
+ f.format(*v)
+ s := buf.String()
+ want := "<invalid>"
+ if s != want {
+ t.Errorf("FormatterInvalidReflectValue\n got: %s want: %s", s, want)
+ }
+}