| 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 |
"testing" |
| 11 |
|
| 12 |
"github.com/maxatome/go-testdeep/internal/test" |
| 13 |
"github.com/maxatome/go-testdeep/td" |
| 14 |
) |
| 15 |
|
| 16 |
func TestPtr(t *testing.T) { |
| 17 |
// |
| 18 |
// Ptr |
| 19 |
num := 12 |
| 20 |
str := "test" |
| 21 |
pNum := &num |
| 22 |
pStr := &str |
| 23 |
pStruct := &struct{}{} |
| 24 |
|
| 25 |
checkOK(t, &num, td.Ptr(12)) |
| 26 |
checkOK(t, &str, td.Ptr("test")) |
| 27 |
checkOK(t, &struct{}{}, td.Ptr(struct{}{})) |
| 28 |
|
| 29 |
checkError(t, &num, td.Ptr(13), |
| 30 |
expectedError{ |
| 31 |
Message: mustBe("values differ"), |
| 32 |
Path: mustBe("*DATA"), |
| 33 |
Got: mustBe("12"), |
| 34 |
Expected: mustBe("13"), |
| 35 |
}) |
| 36 |
checkError(t, nil, td.Ptr(13), |
| 37 |
expectedError{ |
| 38 |
Message: mustBe("values differ"), |
| 39 |
Path: mustBe("DATA"), |
| 40 |
Got: mustBe("nil"), |
| 41 |
Expected: mustBe("*int"), |
| 42 |
}) |
| 43 |
checkError(t, (*int)(nil), td.Ptr(13), |
| 44 |
expectedError{ |
| 45 |
Message: mustBe("values differ"), |
| 46 |
Path: mustBe("*DATA"), // should be DATA, but seems hard to be done |
| 47 |
Got: mustBe("nil"), |
| 48 |
Expected: mustBe("13"), |
| 49 |
}) |
| 50 |
checkError(t, (*int)(nil), td.Ptr((*int)(nil)), |
| 51 |
expectedError{ |
| 52 |
Message: mustBe("type mismatch"), |
| 53 |
Path: mustBe("DATA"), |
| 54 |
Got: mustBe("*int"), |
| 55 |
Expected: mustBe("**int"), |
| 56 |
}) |
| 57 |
checkError(t, &num, td.Ptr("test"), |
| 58 |
expectedError{ |
| 59 |
Message: mustBe("type mismatch"), |
| 60 |
Path: mustBe("DATA"), |
| 61 |
Got: mustBe("*int"), |
| 62 |
Expected: mustBe("*string"), |
| 63 |
}) |
| 64 |
|
| 65 |
checkError(t, &num, td.Ptr(td.Any(11)), |
| 66 |
expectedError{ |
| 67 |
Message: mustBe("comparing with Any"), |
| 68 |
Path: mustBe("*DATA"), |
| 69 |
Got: mustBe("12"), |
| 70 |
Expected: mustBe("Any(11)"), |
| 71 |
}) |
| 72 |
|
| 73 |
checkError(t, &str, td.Ptr("foobar"), |
| 74 |
expectedError{ |
| 75 |
Message: mustBe("values differ"), |
| 76 |
Path: mustBe("*DATA"), |
| 77 |
Got: mustContain(`"test"`), |
| 78 |
Expected: mustContain(`"foobar"`), |
| 79 |
}) |
| 80 |
|
| 81 |
checkError(t, 13, td.Ptr(13), |
| 82 |
expectedError{ |
| 83 |
Message: mustBe("pointer type mismatch"), |
| 84 |
Path: mustBe("DATA"), |
| 85 |
Got: mustBe("int"), |
| 86 |
Expected: mustBe("*int"), |
| 87 |
}) |
| 88 |
checkError(t, &str, td.Ptr(12), |
| 89 |
expectedError{ |
| 90 |
Message: mustBe("type mismatch"), |
| 91 |
Path: mustBe("DATA"), |
| 92 |
Got: mustBe("*string"), |
| 93 |
Expected: mustBe("*int"), |
| 94 |
}) |
| 95 |
|
| 96 |
checkError(t, &pNum, td.Ptr(12), |
| 97 |
expectedError{ |
| 98 |
Message: mustBe("type mismatch"), |
| 99 |
Path: mustBe("DATA"), |
| 100 |
Got: mustBe("**int"), |
| 101 |
Expected: mustBe("*int"), |
| 102 |
}) |
| 103 |
checkError(t, &pStr, td.Ptr("test"), |
| 104 |
expectedError{ |
| 105 |
Message: mustBe("type mismatch"), |
| 106 |
Path: mustBe("DATA"), |
| 107 |
Got: mustBe("**string"), |
| 108 |
Expected: mustBe("*string"), |
| 109 |
}) |
| 110 |
|
| 111 |
// |
| 112 |
// PPtr |
| 113 |
checkOK(t, &pNum, td.PPtr(12)) |
| 114 |
checkOK(t, &pStr, td.PPtr("test")) |
| 115 |
checkOK(t, &pStruct, td.PPtr(struct{}{})) |
| 116 |
|
| 117 |
checkError(t, &pNum, td.PPtr(13), |
| 118 |
expectedError{ |
| 119 |
Message: mustBe("values differ"), |
| 120 |
Path: mustBe("**DATA"), |
| 121 |
Got: mustBe("12"), |
| 122 |
Expected: mustBe("13"), |
| 123 |
}) |
| 124 |
checkError(t, nil, td.PPtr(13), |
| 125 |
expectedError{ |
| 126 |
Message: mustBe("values differ"), |
| 127 |
Path: mustBe("DATA"), |
| 128 |
Got: mustBe("nil"), |
| 129 |
Expected: mustBe("**int"), |
| 130 |
}) |
| 131 |
checkError(t, &num, td.PPtr(13), |
| 132 |
expectedError{ |
| 133 |
Message: mustBe("pointer type mismatch"), |
| 134 |
Path: mustBe("DATA"), |
| 135 |
Got: mustBe("*int"), |
| 136 |
Expected: mustBe("**int"), |
| 137 |
}) |
| 138 |
|
| 139 |
checkError(t, &pStr, td.PPtr("foobar"), |
| 140 |
expectedError{ |
| 141 |
Message: mustBe("values differ"), |
| 142 |
Path: mustBe("**DATA"), |
| 143 |
}) |
| 144 |
checkError(t, &str, td.PPtr("foobar"), |
| 145 |
expectedError{ |
| 146 |
Message: mustBe("pointer type mismatch"), |
| 147 |
Path: mustBe("DATA"), |
| 148 |
Got: mustBe("*string"), |
| 149 |
Expected: mustBe("**string"), |
| 150 |
}) |
| 151 |
|
| 152 |
checkError(t, &pNum, td.PPtr(td.Any(11)), |
| 153 |
expectedError{ |
| 154 |
Message: mustBe("comparing with Any"), |
| 155 |
Path: mustBe("**DATA"), |
| 156 |
Got: mustBe("12"), |
| 157 |
Expected: mustBe("Any(11)"), |
| 158 |
}) |
| 159 |
|
| 160 |
pStruct = nil |
| 161 |
checkError(t, &pStruct, td.PPtr(struct{}{}), |
| 162 |
expectedError{ |
| 163 |
Message: mustBe("values differ"), |
| 164 |
Path: mustBe("**DATA"), // should be *DATA, but seems hard to be done |
| 165 |
Got: mustBe("nil"), |
| 166 |
Expected: mustContain("struct"), |
| 167 |
}) |
| 168 |
|
| 169 |
// |
| 170 |
// Bad usage |
| 171 |
checkError(t, "never tested", |
| 172 |
td.Ptr(nil), |
| 173 |
expectedError{ |
| 174 |
Message: mustBe("bad usage of Ptr operator"), |
| 175 |
Path: mustBe("DATA"), |
| 176 |
Summary: mustContain("usage: Ptr("), |
| 177 |
}) |
| 178 |
|
| 179 |
checkError(t, "never tested", |
| 180 |
td.Ptr(MyInterface(nil)), |
| 181 |
expectedError{ |
| 182 |
Message: mustBe("bad usage of Ptr operator"), |
| 183 |
Path: mustBe("DATA"), |
| 184 |
Summary: mustContain("usage: Ptr("), |
| 185 |
}) |
| 186 |
|
| 187 |
checkError(t, "never tested", |
| 188 |
td.PPtr(nil), |
| 189 |
expectedError{ |
| 190 |
Message: mustBe("bad usage of PPtr operator"), |
| 191 |
Path: mustBe("DATA"), |
| 192 |
Summary: mustContain("usage: PPtr("), |
| 193 |
}) |
| 194 |
|
| 195 |
checkError(t, "never tested", |
| 196 |
td.PPtr(MyInterface(nil)), |
| 197 |
expectedError{ |
| 198 |
Message: mustBe("bad usage of PPtr operator"), |
| 199 |
Path: mustBe("DATA"), |
| 200 |
Summary: mustContain("usage: PPtr("), |
| 201 |
}) |
| 202 |
|
| 203 |
// |
| 204 |
// String |
| 205 |
test.EqualStr(t, td.Ptr(13).String(), "*int") |
| 206 |
test.EqualStr(t, td.PPtr(13).String(), "**int") |
| 207 |
test.EqualStr(t, td.Ptr(td.Ptr(13)).String(), "*<something>") |
| 208 |
test.EqualStr(t, td.PPtr(td.Ptr(13)).String(), "**<something>") |
| 209 |
|
| 210 |
// Erroneous op |
| 211 |
test.EqualStr(t, td.Ptr(nil).String(), "Ptr(<ERROR>)") |
| 212 |
test.EqualStr(t, td.PPtr(nil).String(), "PPtr(<ERROR>)") |
| 213 |
} |
| 214 |
|
| 215 |
func TestPtrTypeBehind(t *testing.T) { |
| 216 |
var num int |
| 217 |
equalTypes(t, td.Ptr(6), &num) |
| 218 |
|
| 219 |
// Another TestDeep operator delegation |
| 220 |
var num64 int64 |
| 221 |
equalTypes(t, td.Ptr(td.Between(int64(1), int64(2))), &num64) |
| 222 |
equalTypes(t, td.Ptr(td.Any(1, 1.2)), nil) |
| 223 |
|
| 224 |
// Erroneous op |
| 225 |
equalTypes(t, td.Ptr(nil), nil) |
| 226 |
} |
| 227 |
|
| 228 |
func TestPPtrTypeBehind(t *testing.T) { |
| 229 |
var pnum *int |
| 230 |
equalTypes(t, td.PPtr(6), &pnum) |
| 231 |
|
| 232 |
// Another TestDeep operator delegation |
| 233 |
var pnum64 *int64 |
| 234 |
equalTypes(t, td.PPtr(td.Between(int64(1), int64(2))), &pnum64) |
| 235 |
equalTypes(t, td.PPtr(td.Any(1, 1.2)), nil) |
| 236 |
|
| 237 |
// Erroneous op |
| 238 |
equalTypes(t, td.PPtr(nil), nil) |
| 239 |
} |