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