internal_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. // changeKind uses unsafe to intentionally change the kind of a reflect.Value to
  74. // the maximum kind value which does not exist. This is needed to test the
  75. // fallback code which punts to the standard fmt library for new types that
  76. // might get added to the language.
  77. func changeKind(v *reflect.Value, readOnly bool) {
  78. rvf := (*uintptr)(unsafe.Pointer(uintptr(unsafe.Pointer(v)) + offsetFlag))
  79. *rvf = *rvf | ((1<<flagKindWidth - 1) << flagKindShift)
  80. if readOnly {
  81. *rvf |= flagRO
  82. } else {
  83. *rvf &= ^uintptr(flagRO)
  84. }
  85. }
  86. // TestAddedReflectValue tests functionaly of the dump and formatter code which
  87. // falls back to the standard fmt library for new types that might get added to
  88. // the language.
  89. func TestAddedReflectValue(t *testing.T) {
  90. i := 1
  91. // Dump using a reflect.Value that is exported.
  92. v := reflect.ValueOf(int8(5))
  93. changeKind(&v, false)
  94. buf := new(bytes.Buffer)
  95. d := dumpState{w: buf, cs: &Config}
  96. d.dump(v)
  97. s := buf.String()
  98. want := "(int8) 5"
  99. if s != want {
  100. t.Errorf("TestAddedReflectValue #%d\n got: %s want: %s", i, s, want)
  101. }
  102. i++
  103. // Dump using a reflect.Value that is not exported.
  104. changeKind(&v, true)
  105. buf.Reset()
  106. d.dump(v)
  107. s = buf.String()
  108. want = "(int8) <int8 Value>"
  109. if s != want {
  110. t.Errorf("TestAddedReflectValue #%d\n got: %s want: %s", i, s, want)
  111. }
  112. i++
  113. // Formatter using a reflect.Value that is exported.
  114. changeKind(&v, false)
  115. buf2 := new(dummyFmtState)
  116. f := formatState{value: v, cs: &Config, fs: buf2}
  117. f.format(v)
  118. s = buf2.String()
  119. want = "5"
  120. if s != want {
  121. t.Errorf("TestAddedReflectValue #%d got: %s want: %s", i, s, want)
  122. }
  123. i++
  124. // Formatter using a reflect.Value that is not exported.
  125. changeKind(&v, true)
  126. buf2.Reset()
  127. f = formatState{value: v, cs: &Config, fs: buf2}
  128. f.format(v)
  129. s = buf2.String()
  130. want = "<int8 Value>"
  131. if s != want {
  132. t.Errorf("TestAddedReflectValue #%d got: %s want: %s", i, s, want)
  133. }
  134. }
  135. // SortValues makes the internal sortValues function available to the test
  136. // package.
  137. func SortValues(values []reflect.Value) {
  138. sortValues(values)
  139. }