| 1 |
// Copyright (c) 2013-2016 Dave Collins <[email protected]> |
| 2 |
|
| 3 |
// Permission to use, copy, modify, and distribute this software for any |
| 4 |
// purpose with or without fee is hereby granted, provided that the above |
| 5 |
// copyright notice and this permission notice appear in all copies. |
| 6 |
|
| 7 |
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 |
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 |
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 10 |
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 |
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 12 |
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 13 |
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 14 |
|
| 15 |
// NOTE: Due to the following build constraints, this file will only be compiled |
| 16 |
// when the code is not running on Google App Engine, compiled by GopherJS, and |
| 17 |
// "-tags safe" is not added to the go build command line. The "disableunsafe" |
| 18 |
// tag is deprecated and thus should not be used. |
| 19 |
|
| 20 |
//go:build !js && !appengine && !safe && !disableunsafe |
| 21 |
// +build !js,!appengine,!safe,!disableunsafe |
| 22 |
|
| 23 |
/* |
| 24 |
This test file is part of the spew package rather than than the spew_test |
| 25 |
package because it needs access to internals to properly test certain cases |
| 26 |
which are not possible via the public interface since they should never happen. |
| 27 |
*/ |
| 28 |
|
| 29 |
package spew |
| 30 |
|
| 31 |
import ( |
| 32 |
"reflect" |
| 33 |
"strings" |
| 34 |
"testing" |
| 35 |
) |
| 36 |
|
| 37 |
// changeKind uses unsafe to intentionally change the kind of a reflect.Value to |
| 38 |
// the maximum kind value which does not exist. This is needed to test the |
| 39 |
// fallback code which punts to the standard fmt library for new types that |
| 40 |
// might get added to the language. |
| 41 |
func changeKind(v *reflect.Value, readOnly bool) { |
| 42 |
flags := flagField(v) |
| 43 |
if readOnly { |
| 44 |
*flags |= flagRO |
| 45 |
} else { |
| 46 |
*flags &^= flagRO |
| 47 |
} |
| 48 |
*flags |= flagKindMask |
| 49 |
} |
| 50 |
|
| 51 |
// TestAddedReflectValue tests functionally of the dump and formatter code which |
| 52 |
// falls back to the standard fmt library for new types that might get added to |
| 53 |
// the language. |
| 54 |
func TestAddedReflectValue(t *testing.T) { |
| 55 |
// Dump using a reflect.Value that is exported. |
| 56 |
v := reflect.ValueOf(int8(5)) |
| 57 |
changeKind(&v, false) |
| 58 |
var buf strings.Builder |
| 59 |
d := dumpState{w: &buf, cs: &Config} |
| 60 |
d.dump(v) |
| 61 |
s := buf.String() |
| 62 |
want := "(int8) 5" |
| 63 |
if s != want { |
| 64 |
t.Errorf("TestAddedReflectValue\n got: %s want: %s", s, want) |
| 65 |
} |
| 66 |
|
| 67 |
// Dump using a reflect.Value that is not exported. |
| 68 |
changeKind(&v, true) |
| 69 |
buf.Reset() |
| 70 |
d.dump(v) |
| 71 |
s = buf.String() |
| 72 |
want = "(int8) <int8 Value>" |
| 73 |
if s != want { |
| 74 |
t.Errorf("TestAddedReflectValue\n got: %s want: %s", s, want) |
| 75 |
} |
| 76 |
} |