common_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. v := reflect.ValueOf
  103. a := v("a")
  104. b := v("b")
  105. c := v("c")
  106. tests := []struct {
  107. input []reflect.Value
  108. expected []reflect.Value
  109. }{
  110. {
  111. []reflect.Value{v(2), v(1), v(3)},
  112. []reflect.Value{v(1), v(2), v(3)},
  113. },
  114. {
  115. []reflect.Value{v(2.), v(1.), v(3.)},
  116. []reflect.Value{v(1.), v(2.), v(3.)},
  117. },
  118. {
  119. []reflect.Value{v(false), v(true), v(false)},
  120. []reflect.Value{v(false), v(false), v(true)},
  121. },
  122. {
  123. []reflect.Value{b, a, c},
  124. []reflect.Value{a, b, c},
  125. },
  126. }
  127. for _, test := range tests {
  128. spew.SortValues(test.input)
  129. if !reflect.DeepEqual(test.input, test.expected) {
  130. t.Errorf("Sort mismatch:\n %v != %v", test.input, test.expected)
  131. }
  132. }
  133. }