common_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. )
  20. // custom type to test Stinger interface on non-pointer receiver.
  21. type stringer string
  22. // String implements the Stringer interface for testing invocation of custom
  23. // stringers on types with non-pointer receivers.
  24. func (s stringer) String() string {
  25. return "stringer " + string(s)
  26. }
  27. // custom type to test Stinger interface on pointer receiver.
  28. type pstringer string
  29. // String implements the Stringer interface for testing invocation of custom
  30. // stringers on types with only pointer receivers.
  31. func (s *pstringer) String() string {
  32. return "stringer " + string(*s)
  33. }
  34. // xref1 and xref2 are cross referencing structs for testing circular reference
  35. // detection.
  36. type xref1 struct {
  37. ps2 *xref2
  38. }
  39. type xref2 struct {
  40. ps1 *xref1
  41. }
  42. // indirCir1, indirCir2, and indirCir3 are used to generate an indirect circular
  43. // reference for testing detection.
  44. type indirCir1 struct {
  45. ps2 *indirCir2
  46. }
  47. type indirCir2 struct {
  48. ps3 *indirCir3
  49. }
  50. type indirCir3 struct {
  51. ps1 *indirCir1
  52. }
  53. // embed is used to test embedded structures.
  54. type embed struct {
  55. a string
  56. }
  57. // embedwrap is used to test embedded structures.
  58. type embedwrap struct {
  59. *embed
  60. e *embed
  61. }
  62. // panicer is used to intentionally cause a panic for testing spew properly
  63. // handles them
  64. type panicer int
  65. func (p panicer) String() string {
  66. panic("test panic")
  67. }
  68. // customError is used to test custom error interface invocation.
  69. type customError int
  70. func (e customError) Error() string {
  71. return fmt.Sprintf("error: %d", int(e))
  72. }
  73. // stringizeWants converts a slice of wanted test output into a format suitable
  74. // for an test error message.
  75. func stringizeWants(wants []string) string {
  76. s := ""
  77. for i, want := range wants {
  78. if i > 0 {
  79. s += fmt.Sprintf("want%d: %s", i+1, want)
  80. } else {
  81. s += "want: " + want
  82. }
  83. }
  84. return s
  85. }
  86. // testFailed returns whether or not a test failed by checking if the result
  87. // of the test is in the slice of wanted strings.
  88. func testFailed(result string, wants []string) bool {
  89. for _, want := range wants {
  90. if result == want {
  91. return false
  92. }
  93. }
  94. return true
  95. }