| 1 |
// Copyright (c) 2018, Maxime Soulé |
| 2 |
// All rights reserved. |
| 3 |
// |
| 4 |
// This source code is licensed under the BSD-style license found in the |
| 5 |
// LICENSE file in the root directory of this source tree. |
| 6 |
|
| 7 |
package td_test |
| 8 |
|
| 9 |
import ( |
| 10 |
"regexp" |
| 11 |
"testing" |
| 12 |
|
| 13 |
"github.com/maxatome/go-testdeep/internal/test" |
| 14 |
"github.com/maxatome/go-testdeep/td" |
| 15 |
) |
| 16 |
|
| 17 |
func TestShallow(t *testing.T) { |
| 18 |
checkOK(t, nil, nil) |
| 19 |
|
| 20 |
// |
| 21 |
// Slice |
| 22 |
back := [...]int{1, 2, 3, 1, 2, 3} |
| 23 |
as := back[:3] |
| 24 |
bs := back[3:] |
| 25 |
checkError(t, bs, td.Shallow(back[:]), |
| 26 |
expectedError{ |
| 27 |
Message: mustBe("slice pointer mismatch"), |
| 28 |
Path: mustBe("DATA"), |
| 29 |
Got: mustContain("0x"), |
| 30 |
Expected: mustContain("0x"), |
| 31 |
}) |
| 32 |
|
| 33 |
checkOK(t, as, td.Shallow(back[:])) |
| 34 |
checkOK(t, ([]byte)(nil), ([]byte)(nil)) |
| 35 |
|
| 36 |
// |
| 37 |
// Map |
| 38 |
gotMap := map[string]bool{"a": true, "b": false} |
| 39 |
expectedMap := map[string]bool{"a": true, "b": false} |
| 40 |
checkError(t, gotMap, td.Shallow(expectedMap), |
| 41 |
expectedError{ |
| 42 |
Message: mustBe("map pointer mismatch"), |
| 43 |
Path: mustBe("DATA"), |
| 44 |
Got: mustContain("0x"), |
| 45 |
Expected: mustContain("0x"), |
| 46 |
}) |
| 47 |
|
| 48 |
expectedMap = gotMap |
| 49 |
checkOK(t, gotMap, td.Shallow(expectedMap)) |
| 50 |
checkOK(t, (map[string]bool)(nil), (map[string]bool)(nil)) |
| 51 |
|
| 52 |
// |
| 53 |
// Ptr |
| 54 |
type MyStruct struct { |
| 55 |
val int |
| 56 |
} |
| 57 |
gotPtr := &MyStruct{val: 12} |
| 58 |
expectedPtr := &MyStruct{val: 12} |
| 59 |
checkError(t, gotPtr, td.Shallow(expectedPtr), |
| 60 |
expectedError{ |
| 61 |
Message: mustBe("ptr pointer mismatch"), |
| 62 |
Path: mustBe("DATA"), |
| 63 |
Got: mustContain("0x"), |
| 64 |
Expected: mustContain("0x"), |
| 65 |
}) |
| 66 |
|
| 67 |
expectedPtr = gotPtr |
| 68 |
checkOK(t, gotPtr, td.Shallow(expectedPtr)) |
| 69 |
checkOK(t, (*MyStruct)(nil), (*MyStruct)(nil)) |
| 70 |
|
| 71 |
// |
| 72 |
// Func |
| 73 |
gotFunc := func(a int) int { return a * 2 } |
| 74 |
expectedFunc := func(a int) int { return a * 2 } |
| 75 |
checkError(t, gotFunc, td.Shallow(expectedFunc), |
| 76 |
expectedError{ |
| 77 |
Message: mustBe("func pointer mismatch"), |
| 78 |
Path: mustBe("DATA"), |
| 79 |
Got: mustContain("0x"), |
| 80 |
Expected: mustContain("0x"), |
| 81 |
}) |
| 82 |
|
| 83 |
expectedFunc = gotFunc |
| 84 |
checkOK(t, gotFunc, td.Shallow(expectedFunc)) |
| 85 |
checkOK(t, (func(a int) int)(nil), (func(a int) int)(nil)) |
| 86 |
|
| 87 |
// |
| 88 |
// Chan |
| 89 |
gotChan := make(chan int) |
| 90 |
expectedChan := make(chan int) |
| 91 |
checkError(t, gotChan, td.Shallow(expectedChan), |
| 92 |
expectedError{ |
| 93 |
Message: mustBe("chan pointer mismatch"), |
| 94 |
Path: mustBe("DATA"), |
| 95 |
Got: mustContain("0x"), |
| 96 |
Expected: mustContain("0x"), |
| 97 |
}) |
| 98 |
|
| 99 |
expectedChan = gotChan |
| 100 |
checkOK(t, gotChan, td.Shallow(expectedChan)) |
| 101 |
checkOK(t, (chan int)(nil), (chan int)(nil)) |
| 102 |
|
| 103 |
// |
| 104 |
// String |
| 105 |
backStr := "foobarfoobar!" |
| 106 |
a := backStr[:6] |
| 107 |
b := backStr[6:12] |
| 108 |
checkOK(t, a, td.Shallow(backStr)) |
| 109 |
checkOK(t, backStr, td.Shallow(a)) |
| 110 |
checkOK(t, b, td.Shallow(backStr[6:7])) |
| 111 |
|
| 112 |
checkError(t, backStr, td.Shallow(b), |
| 113 |
expectedError{ |
| 114 |
Message: mustBe("string pointer mismatch"), |
| 115 |
Path: mustBe("DATA"), |
| 116 |
Got: mustContain("0x"), |
| 117 |
Expected: mustContain("0x"), |
| 118 |
}) |
| 119 |
checkError(t, b, td.Shallow(backStr), |
| 120 |
expectedError{ |
| 121 |
Message: mustBe("string pointer mismatch"), |
| 122 |
Path: mustBe("DATA"), |
| 123 |
Got: mustContain("0x"), |
| 124 |
Expected: mustContain("0x"), |
| 125 |
}) |
| 126 |
|
| 127 |
// |
| 128 |
// Erroneous mix |
| 129 |
checkError(t, gotMap, td.Shallow(expectedChan), |
| 130 |
expectedError{ |
| 131 |
Message: mustBe("bad kind"), |
| 132 |
Path: mustBe("DATA"), |
| 133 |
Got: mustContain("map"), |
| 134 |
Expected: mustContain("chan"), |
| 135 |
}) |
| 136 |
|
| 137 |
// |
| 138 |
// Bad usage |
| 139 |
checkError(t, "never tested", |
| 140 |
td.Shallow(42), |
| 141 |
expectedError{ |
| 142 |
Message: mustBe("bad usage of Shallow operator"), |
| 143 |
Path: mustBe("DATA"), |
| 144 |
Summary: mustBe("usage: Shallow(CHANNEL|FUNC|MAP|PTR|SLICE|UNSAFE_PTR|STRING), but received int as 1st parameter"), |
| 145 |
}) |
| 146 |
|
| 147 |
// |
| 148 |
// |
| 149 |
reg := regexp.MustCompile(`^\(map\) 0x[a-f0-9]+\z`) |
| 150 |
if !reg.MatchString(td.Shallow(expectedMap).String()) { |
| 151 |
t.Errorf("Shallow().String() failed\n got: %s\nexpected: %s", |
| 152 |
td.Shallow(expectedMap).String(), reg) |
| 153 |
} |
| 154 |
|
| 155 |
// Erroneous op |
| 156 |
test.EqualStr(t, td.Shallow(42).String(), "Shallow(<ERROR>)") |
| 157 |
} |
| 158 |
|
| 159 |
func TestShallowTypeBehind(t *testing.T) { |
| 160 |
equalTypes(t, td.Shallow(t), nil) |
| 161 |
|
| 162 |
// Erroneous op |
| 163 |
equalTypes(t, td.Shallow(42), nil) |
| 164 |
} |