| 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 |
"testing" |
| 21 |
|
| 22 |
"github.com/maxatome/go-testdeep/internal/spew" |
| 23 |
"github.com/maxatome/go-testdeep/internal/test" |
| 24 |
) |
| 25 |
|
| 26 |
// spewTest is used to describe a test to be performed against the public |
| 27 |
// functions of the spew package or ConfigState. |
| 28 |
type spewTest struct { |
| 29 |
cs *spew.ConfigState |
| 30 |
in any |
| 31 |
want string |
| 32 |
} |
| 33 |
|
| 34 |
// spewTests houses the tests to be performed against the public functions of |
| 35 |
// the spew package and ConfigState. |
| 36 |
// |
| 37 |
// These tests are only intended to ensure the public functions are exercised |
| 38 |
// and are intentionally not exhaustive of types. The exhaustive type |
| 39 |
// tests are handled in the dump and format tests. |
| 40 |
var spewTests []spewTest |
| 41 |
|
| 42 |
func initSpewTests() { |
| 43 |
scsNoMethods := &spew.ConfigState{ |
| 44 |
Indent: " ", |
| 45 |
DisableMethods: true, |
| 46 |
DisablePointerAddresses: true, |
| 47 |
} |
| 48 |
scsNoPmethods := &spew.ConfigState{ |
| 49 |
Indent: " ", |
| 50 |
DisablePointerMethods: true, |
| 51 |
DisablePointerAddresses: true, |
| 52 |
} |
| 53 |
scsMaxDepth := &spew.ConfigState{ |
| 54 |
Indent: " ", |
| 55 |
MaxDepth: 1, |
| 56 |
DisablePointerAddresses: true, |
| 57 |
} |
| 58 |
scsNoPtrAddr := &spew.ConfigState{DisablePointerAddresses: true} |
| 59 |
scsCap := &spew.ConfigState{EnableCapacities: true} |
| 60 |
|
| 61 |
// Variables for tests on types which implement fmt.Stringer |
| 62 |
// interface with and without a pointer receiver. |
| 63 |
ts := stringer("test") |
| 64 |
tps := pstringer("test") |
| 65 |
|
| 66 |
type ptrTester struct { |
| 67 |
s *struct{} |
| 68 |
} |
| 69 |
tptr := &ptrTester{s: &struct{}{}} |
| 70 |
|
| 71 |
// depthTester is used to test max depth handling for structs, array, slices |
| 72 |
// and maps. |
| 73 |
type depthTester struct { |
| 74 |
ic indirCir1 |
| 75 |
arr [1]string |
| 76 |
slice []string |
| 77 |
m map[string]int |
| 78 |
} |
| 79 |
dt := depthTester{ |
| 80 |
indirCir1{&indirCir2{}}, |
| 81 |
[1]string{"arr"}, |
| 82 |
[]string{"slice"}, |
| 83 |
map[string]int{"one": 1}, |
| 84 |
} |
| 85 |
dz := depthTester{} |
| 86 |
|
| 87 |
spewTests = []spewTest{ |
| 88 |
{nil, int8(127), "(int8) 127"}, |
| 89 |
{nil, int16(32767), "(int16) 32767"}, |
| 90 |
{nil, 2147483647, "(int) 2147483647"}, |
| 91 |
{nil, int64(9223372036854775807), "(int64) 9223372036854775807"}, |
| 92 |
{nil, uint8(64), "(uint8) 64"}, |
| 93 |
{nil, complex(1, 2), "(complex128) (1+2i)"}, |
| 94 |
{nil, complex(float64(5), 6), "(complex128) (5+6i)"}, |
| 95 |
{nil, float32(3.14), "(float32) 3.14"}, |
| 96 |
{nil, float64(6.28), "(float64) 6.28"}, |
| 97 |
{nil, true, "(bool) true"}, |
| 98 |
{nil, false, "(bool) false"}, |
| 99 |
{nil, complex(-10, -20), "(complex128) (-10-20i)"}, |
| 100 |
{nil, dz, "(spew_test.depthTester) {\n}"}, |
| 101 |
{scsMaxDepth, dt, `(spew_test.depthTester) { |
| 102 |
ic: (spew_test.indirCir1) { |
| 103 |
<max depth reached> |
| 104 |
}, |
| 105 |
arr: ([1]string) (len=1) { |
| 106 |
<max depth reached> |
| 107 |
}, |
| 108 |
slice: ([]string) (len=1) { |
| 109 |
<max depth reached> |
| 110 |
}, |
| 111 |
m: (map[string]int) (len=1) { |
| 112 |
<max depth reached> |
| 113 |
} |
| 114 |
}`}, |
| 115 |
// scsNoMethods |
| 116 |
{scsNoMethods, ts, `(spew_test.stringer) (len=4) "test"`}, |
| 117 |
{scsNoMethods, &ts, `(*spew_test.stringer)((len=4) "test")`}, |
| 118 |
{scsNoMethods, tps, `(spew_test.pstringer) (len=4) "test"`}, |
| 119 |
{scsNoMethods, &tps, `(*spew_test.pstringer)((len=4) "test")`}, |
| 120 |
// scsNoPmethods |
| 121 |
{scsNoPmethods, ts, "(spew_test.stringer) (len=4) stringer test"}, |
| 122 |
{scsNoPmethods, &ts, "(*spew_test.stringer)((len=4) stringer test)"}, |
| 123 |
{scsNoPmethods, tps, `(spew_test.pstringer) (len=4) "test"`}, |
| 124 |
{scsNoPmethods, &tps, "(*spew_test.pstringer)((len=4) stringer test)"}, |
| 125 |
{scsNoPtrAddr, tptr, "(*spew_test.ptrTester)({\ns: (*struct {})({\n})\n})"}, |
| 126 |
{scsCap, make([]string, 0, 10), "([]string) (cap=10) {\n}"}, |
| 127 |
{scsCap, make([]string, 1, 10), "([]string) (len=1 cap=10) {\n(string) \"\"\n}"}, |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
// TestSpew executes all of the tests described by spewTests. |
| 132 |
func TestSpew(t *testing.T) { |
| 133 |
initSpewTests() |
| 134 |
|
| 135 |
t.Logf("Running %d tests", len(spewTests)) |
| 136 |
for i, tc := range spewTests { |
| 137 |
var cs []spew.ConfigState |
| 138 |
if tc.cs != nil { |
| 139 |
cs = append(cs, *tc.cs) |
| 140 |
} |
| 141 |
str := spew.Sdump(tc.in, cs...) |
| 142 |
test.EqualStr(t, str, tc.want, "#%d", i) |
| 143 |
} |
| 144 |
} |