common_test.go 2.4 KB

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