1 // Copyright (c) 2013-2016 Dave Collins <dave@davec.name> 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 // +build !js,!appengine,!safe,!disableunsafe,go1.4 20 21 /* 22 This test file is part of the spew package rather than than the spew_test 23 package because it needs access to internals to properly test certain cases 24 which are not possible via the public interface since they should never happen. 25 */ 26 27 package spew 28 29 import ( 30 "bytes" 31 "reflect" 32 "testing" 33 ) 34 35 // changeKind uses unsafe to intentionally change the kind of a reflect.Value to 36 // the maximum kind value which does not exist. This is needed to test the 37 // fallback code which punts to the standard fmt library for new types that 38 // might get added to the language. 39 func changeKind(v *reflect.Value, readOnly bool) { 40 flags := flagField(v) 41 if readOnly { 42 *flags |= flagRO 43 } else { 44 *flags &^= flagRO 45 } 46 *flags |= flagKindMask 47 } 48 49 // TestAddedReflectValue tests functionaly of the dump and formatter code which 50 // falls back to the standard fmt library for new types that might get added to 51 // the language. 52 func TestAddedReflectValue(t *testing.T) { 53 i := 1 54 55 // Dump using a reflect.Value that is exported. 56 v := reflect.ValueOf(int8(5)) 57 changeKind(&v, false) 58 buf := new(bytes.Buffer) 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 #%d\n got: %s want: %s", i, s, want) 65 } 66 i++ 67 68 // Dump using a reflect.Value that is not exported. 69 changeKind(&v, true) 70 buf.Reset() 71 d.dump(v) 72 s = buf.String() 73 want = "(int8) <int8 Value>" 74 if s != want { 75 t.Errorf("TestAddedReflectValue #%d\n got: %s want: %s", i, s, want) 76 } 77 i++ 78 79 // Formatter using a reflect.Value that is exported. 80 changeKind(&v, false) 81 buf2 := new(dummyFmtState) 82 f := formatState{value: v, cs: &Config, fs: buf2} 83 f.format(v) 84 s = buf2.String() 85 want = "5" 86 if s != want { 87 t.Errorf("TestAddedReflectValue #%d got: %s want: %s", i, s, want) 88 } 89 i++ 90 91 // Formatter using a reflect.Value that is not exported. 92 changeKind(&v, true) 93 buf2.Reset() 94 f = formatState{value: v, cs: &Config, fs: buf2} 95 f.format(v) 96 s = buf2.String() 97 want = "<int8 Value>" 98 if s != want { 99 t.Errorf("TestAddedReflectValue #%d got: %s want: %s", i, s, want) 100 } 101 } 102