internal_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright (c) 2013 Dave Collins <dave@davec.name>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. /*
  17. This test file is part of the spew package rather than than the spew_test
  18. package because it needs access to internals to properly test certain cases
  19. which are not possible via the public interface since they should never happen.
  20. */
  21. package spew
  22. import (
  23. "bytes"
  24. "reflect"
  25. "testing"
  26. "unsafe"
  27. )
  28. // dummyFmtState implements a fake fmt.State to use for testing invalid
  29. // reflect.Value handling. This is necessary because the fmt package catches
  30. // invalid values before invoking the formatter on them.
  31. type dummyFmtState struct {
  32. bytes.Buffer
  33. }
  34. func (dfs *dummyFmtState) Flag(f int) bool {
  35. if f == int('+') {
  36. return true
  37. }
  38. return false
  39. }
  40. func (dfs *dummyFmtState) Precision() (int, bool) {
  41. return 0, false
  42. }
  43. func (dfs *dummyFmtState) Width() (int, bool) {
  44. return 0, false
  45. }
  46. // TestInvalidReflectValue ensures the dump and formatter code handles an
  47. // invalid reflect value properly. This needs access to internal state since it
  48. // should never happen in real code and therefore can't be tested via the public
  49. // API.
  50. func TestInvalidReflectValue(t *testing.T) {
  51. i := 1
  52. // Dump invalid reflect value.
  53. v := new(reflect.Value)
  54. buf := new(bytes.Buffer)
  55. d := dumpState{w: buf, cs: &Config}
  56. d.dump(*v)
  57. s := buf.String()
  58. want := "<invalid>"
  59. if s != want {
  60. t.Errorf("InvalidReflectValue #%d\n got: %s want: %s", i, s, want)
  61. }
  62. i++
  63. // Formatter invalid reflect value.
  64. buf2 := new(dummyFmtState)
  65. f := formatState{value: *v, cs: &Config, fs: buf2}
  66. f.format(*v)
  67. s = buf2.String()
  68. want = "<invalid>"
  69. if s != want {
  70. t.Errorf("InvalidReflectValue #%d got: %s want: %s", i, s, want)
  71. }
  72. }
  73. // flagRO, flagKindShift and flagKindWidth indicate various bit flags that the
  74. // reflect package uses internally to track kind and state information.
  75. const flagRO = 1 << 0
  76. const flagKindShift = 4
  77. const flagKindWidth = 5
  78. // changeKind uses unsafe to intentionally change the kind of a reflect.Value to
  79. // the maximum kind value which does not exist. This is needed to test the
  80. // fallback code which punts to the standard fmt library for new types that
  81. // might get added to the language.
  82. func changeKind(v *reflect.Value, readOnly bool) {
  83. rvf := (*uintptr)(unsafe.Pointer(uintptr(unsafe.Pointer(v)) + offsetFlag))
  84. *rvf = *rvf | ((1<<flagKindWidth - 1) << flagKindShift)
  85. if readOnly {
  86. *rvf |= flagRO
  87. } else {
  88. *rvf &= ^uintptr(flagRO)
  89. }
  90. }
  91. // TestAddedReflectValue tests functionaly of the dump and formatter code which
  92. // falls back to the standard fmt library for new types that might get added to
  93. // the language.
  94. func TestAddedReflectValue(t *testing.T) {
  95. i := 1
  96. // Dump using a reflect.Value that is exported.
  97. v := reflect.ValueOf(int8(5))
  98. changeKind(&v, false)
  99. buf := new(bytes.Buffer)
  100. d := dumpState{w: buf, cs: &Config}
  101. d.dump(v)
  102. s := buf.String()
  103. want := "(int8) 5"
  104. if s != want {
  105. t.Errorf("TestAddedReflectValue #%d\n got: %s want: %s", i, s, want)
  106. }
  107. i++
  108. // Dump using a reflect.Value that is not exported.
  109. changeKind(&v, true)
  110. buf.Reset()
  111. d.dump(v)
  112. s = buf.String()
  113. want = "(int8) <int8 Value>"
  114. if s != want {
  115. t.Errorf("TestAddedReflectValue #%d\n got: %s want: %s", i, s, want)
  116. }
  117. i++
  118. // Formatter using a reflect.Value that is exported.
  119. changeKind(&v, false)
  120. buf2 := new(dummyFmtState)
  121. f := formatState{value: v, cs: &Config, fs: buf2}
  122. f.format(v)
  123. s = buf2.String()
  124. want = "5"
  125. if s != want {
  126. t.Errorf("TestAddedReflectValue #%d got: %s want: %s", i, s, want)
  127. }
  128. i++
  129. // Formatter using a reflect.Value that is not exported.
  130. changeKind(&v, true)
  131. buf2.Reset()
  132. f = formatState{value: v, cs: &Config, fs: buf2}
  133. f.format(v)
  134. s = buf2.String()
  135. want = "<int8 Value>"
  136. if s != want {
  137. t.Errorf("TestAddedReflectValue #%d got: %s want: %s", i, s, want)
  138. }
  139. }
  140. // SortValues makes the internal sortValues function available to the test
  141. // package.
  142. func SortValues(values []reflect.Value) {
  143. sortValues(values)
  144. }