common_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. // stringizeWants converts a slice of wanted test output into a format suitable
  53. // for an test error message.
  54. func stringizeWants(wants []string) string {
  55. s := ""
  56. for i, want := range wants {
  57. if i > 0 {
  58. s += fmt.Sprintf("want%d: %s", i+1, want)
  59. } else {
  60. s += "want: " + want
  61. }
  62. }
  63. return s
  64. }
  65. // testFailed returns whether or not a test failed by checking if the result
  66. // of the test is in the slice of wanted strings.
  67. func testFailed(result string, wants []string) bool {
  68. for _, want := range wants {
  69. if result == want {
  70. return false
  71. }
  72. }
  73. return true
  74. }