common_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. package spew_test
  17. import (
  18. "fmt"
  19. "github.com/davecgh/go-spew/spew"
  20. "reflect"
  21. "testing"
  22. )
  23. // custom type to test Stinger interface on non-pointer receiver.
  24. type stringer string
  25. // String implements the Stringer interface for testing invocation of custom
  26. // stringers on types with non-pointer receivers.
  27. func (s stringer) String() string {
  28. return "stringer " + string(s)
  29. }
  30. // custom type to test Stinger interface on pointer receiver.
  31. type pstringer string
  32. // String implements the Stringer interface for testing invocation of custom
  33. // stringers on types with only pointer receivers.
  34. func (s *pstringer) String() string {
  35. return "stringer " + string(*s)
  36. }
  37. // xref1 and xref2 are cross referencing structs for testing circular reference
  38. // detection.
  39. type xref1 struct {
  40. ps2 *xref2
  41. }
  42. type xref2 struct {
  43. ps1 *xref1
  44. }
  45. // indirCir1, indirCir2, and indirCir3 are used to generate an indirect circular
  46. // reference for testing detection.
  47. type indirCir1 struct {
  48. ps2 *indirCir2
  49. }
  50. type indirCir2 struct {
  51. ps3 *indirCir3
  52. }
  53. type indirCir3 struct {
  54. ps1 *indirCir1
  55. }
  56. // embed is used to test embedded structures.
  57. type embed struct {
  58. a string
  59. }
  60. // embedwrap is used to test embedded structures.
  61. type embedwrap struct {
  62. *embed
  63. e *embed
  64. }
  65. // panicer is used to intentionally cause a panic for testing spew properly
  66. // handles them
  67. type panicer int
  68. func (p panicer) String() string {
  69. panic("test panic")
  70. }
  71. // customError is used to test custom error interface invocation.
  72. type customError int
  73. func (e customError) Error() string {
  74. return fmt.Sprintf("error: %d", int(e))
  75. }
  76. // stringizeWants converts a slice of wanted test output into a format suitable
  77. // for a test error message.
  78. func stringizeWants(wants []string) string {
  79. s := ""
  80. for i, want := range wants {
  81. if i > 0 {
  82. s += fmt.Sprintf("want%d: %s", i+1, want)
  83. } else {
  84. s += "want: " + want
  85. }
  86. }
  87. return s
  88. }
  89. // testFailed returns whether or not a test failed by checking if the result
  90. // of the test is in the slice of wanted strings.
  91. func testFailed(result string, wants []string) bool {
  92. for _, want := range wants {
  93. if result == want {
  94. return false
  95. }
  96. }
  97. return true
  98. }
  99. // TestSortValues ensures the sort functionality for relect.Value based sorting
  100. // works as intended.
  101. func TestSortValues(t *testing.T) {
  102. getInterfaces := func(values []reflect.Value) []interface{} {
  103. interfaces := []interface{}{}
  104. for _, v := range values {
  105. interfaces = append(interfaces, v.Interface())
  106. }
  107. return interfaces
  108. }
  109. v := reflect.ValueOf
  110. a := v("a")
  111. b := v("b")
  112. c := v("c")
  113. embedA := v(embed{"a"})
  114. embedB := v(embed{"b"})
  115. embedC := v(embed{"c"})
  116. tests := []struct {
  117. input []reflect.Value
  118. expected []reflect.Value
  119. }{
  120. // No values.
  121. {
  122. []reflect.Value{},
  123. []reflect.Value{},
  124. },
  125. // Bools.
  126. {
  127. []reflect.Value{v(false), v(true), v(false)},
  128. []reflect.Value{v(false), v(false), v(true)},
  129. },
  130. // Ints.
  131. {
  132. []reflect.Value{v(2), v(1), v(3)},
  133. []reflect.Value{v(1), v(2), v(3)},
  134. },
  135. // Uints.
  136. {
  137. []reflect.Value{v(uint8(2)), v(uint8(1)), v(uint8(3))},
  138. []reflect.Value{v(uint8(1)), v(uint8(2)), v(uint8(3))},
  139. },
  140. // Floats.
  141. {
  142. []reflect.Value{v(2.0), v(1.0), v(3.0)},
  143. []reflect.Value{v(1.0), v(2.0), v(3.0)},
  144. },
  145. // Strings.
  146. {
  147. []reflect.Value{b, a, c},
  148. []reflect.Value{a, b, c},
  149. },
  150. // Array
  151. {
  152. []reflect.Value{v([3]int{3, 2, 1}), v([3]int{1, 3, 2}), v([3]int{1, 2, 3})},
  153. []reflect.Value{v([3]int{1, 2, 3}), v([3]int{1, 3, 2}), v([3]int{3, 2, 1})},
  154. },
  155. // Uintptrs.
  156. {
  157. []reflect.Value{v(uintptr(2)), v(uintptr(1)), v(uintptr(3))},
  158. []reflect.Value{v(uintptr(1)), v(uintptr(2)), v(uintptr(3))},
  159. },
  160. // Invalid.
  161. {
  162. []reflect.Value{embedB, embedA, embedC},
  163. []reflect.Value{embedB, embedA, embedC},
  164. },
  165. }
  166. for _, test := range tests {
  167. spew.SortValues(test.input)
  168. // reflect.DeepEqual cannot really make sense of reflect.Value,
  169. // probably because of all the pointer tricks. For instance,
  170. // v(2.0) != v(2.0) on a 32-bits system. Turn them into interface{}
  171. // instead.
  172. input := getInterfaces(test.input)
  173. expected := getInterfaces(test.expected)
  174. if !reflect.DeepEqual(input, expected) {
  175. t.Errorf("Sort mismatch:\n %v != %v", input, expected)
  176. }
  177. }
  178. }