From 2fc049e83c263a23e23f71e1546b2ba662221f81 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Sun, 20 Jan 2013 22:25:51 -0600 Subject: [PATCH] Add tests for unrecognized reflect values. In order to help future proof itself, spew handles unrecognized reflect values by passing them on to the standard fmt library. Since spew handles all current reflect values in the language, this condition has to be manually tested with a bit of hackery by using unsafe to change the kind to a nonexistent value. As of this commit, there is now 100% test coverage. --- spew/internal_test.go | 76 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/spew/internal_test.go b/spew/internal_test.go index 7a872e4..05bab97 100644 --- a/spew/internal_test.go +++ b/spew/internal_test.go @@ -26,6 +26,7 @@ import ( "bytes" "reflect" "testing" + "unsafe" ) // dummyFmtState implements a fake fmt.State to use for testing invalid @@ -79,3 +80,78 @@ func TestInvalidReflectValue(t *testing.T) { t.Errorf("InvalidReflectValue #%d got: %s want: %s", i, s, want) } } + +// flagRO, flagKindShift and flagKindWidth indicate various bit flags that the +// reflect package uses internally to track kind and state information. +const flagRO = 1 << 0 +const flagKindShift = 4 +const flagKindWidth = 5 + +// changeKind uses unsafe to intentionally change the kind of a reflect.Value to +// the maximum kind value which does not exist. This is needed to test the +// fallback code which punts to the standard fmt library for new types that +// might get added to the language. +func changeKind(v *reflect.Value, readOnly bool) { + rvf := (*uintptr)(unsafe.Pointer(uintptr(unsafe.Pointer(v)) + + unsafe.Offsetof(reflectValue.flag))) + *rvf = *rvf | ((1<