| 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 |
"bytes" |
| 11 |
"errors" |
| 12 |
"testing" |
| 13 |
"time" |
| 14 |
|
| 15 |
"github.com/maxatome/go-testdeep/internal/dark" |
| 16 |
"github.com/maxatome/go-testdeep/internal/test" |
| 17 |
"github.com/maxatome/go-testdeep/td" |
| 18 |
) |
| 19 |
|
| 20 |
func TestStruct(t *testing.T) { |
| 21 |
gotStruct := MyStruct{ |
| 22 |
MyStructMid: MyStructMid{ |
| 23 |
MyStructBase: MyStructBase{ |
| 24 |
ValBool: true, |
| 25 |
}, |
| 26 |
ValStr: "foobar", |
| 27 |
}, |
| 28 |
ValInt: 123, |
| 29 |
} |
| 30 |
|
| 31 |
// |
| 32 |
// Using pointer |
| 33 |
checkOK(t, &gotStruct, |
| 34 |
td.Struct(&MyStruct{}, td.StructFields{ |
| 35 |
"ValBool": true, |
| 36 |
"ValStr": "foobar", |
| 37 |
"ValInt": 123, |
| 38 |
"Ptr": nil, |
| 39 |
})) |
| 40 |
|
| 41 |
checkOK(t, &gotStruct, |
| 42 |
td.Struct( |
| 43 |
&MyStruct{ |
| 44 |
MyStructMid: MyStructMid{ |
| 45 |
ValStr: "zip", |
| 46 |
}, |
| 47 |
ValInt: 666, |
| 48 |
}, |
| 49 |
td.StructFields{ |
| 50 |
"ValBool": true, |
| 51 |
"> ValStr": "foobar", |
| 52 |
">ValInt": 123, |
| 53 |
})) |
| 54 |
|
| 55 |
checkOK(t, &gotStruct, |
| 56 |
td.Struct((*MyStruct)(nil), td.StructFields{ |
| 57 |
"ValBool": true, |
| 58 |
"ValStr": "foobar", |
| 59 |
"ValInt": 123, |
| 60 |
"Ptr": nil, |
| 61 |
})) |
| 62 |
|
| 63 |
checkError(t, 123, |
| 64 |
td.Struct(&MyStruct{}, td.StructFields{}), |
| 65 |
expectedError{ |
| 66 |
Message: mustBe("type mismatch"), |
| 67 |
Path: mustBe("DATA"), |
| 68 |
Got: mustContain("int"), |
| 69 |
Expected: mustContain("*td_test.MyStruct"), |
| 70 |
}) |
| 71 |
|
| 72 |
checkError(t, &MyStructBase{}, |
| 73 |
td.Struct(&MyStruct{}, td.StructFields{}), |
| 74 |
expectedError{ |
| 75 |
Message: mustBe("type mismatch"), |
| 76 |
Path: mustBe("DATA"), |
| 77 |
Got: mustContain("*td_test.MyStructBase"), |
| 78 |
Expected: mustContain("*td_test.MyStruct"), |
| 79 |
}) |
| 80 |
|
| 81 |
checkError(t, &gotStruct, |
| 82 |
td.Struct(&MyStruct{}, td.StructFields{ |
| 83 |
"ValBool": false, // ← does not match |
| 84 |
"ValStr": "foobar", |
| 85 |
"ValInt": 123, |
| 86 |
}), |
| 87 |
expectedError{ |
| 88 |
Message: mustBe("values differ"), |
| 89 |
Path: mustBe("DATA.ValBool"), |
| 90 |
Got: mustContain("true"), |
| 91 |
Expected: mustContain("false"), |
| 92 |
}) |
| 93 |
|
| 94 |
checkOK(t, &gotStruct, |
| 95 |
td.Struct(&MyStruct{ |
| 96 |
MyStructMid: MyStructMid{ |
| 97 |
MyStructBase: MyStructBase{ |
| 98 |
ValBool: true, |
| 99 |
}, |
| 100 |
ValStr: "foobar", |
| 101 |
}, |
| 102 |
ValInt: 123, |
| 103 |
}, nil)) |
| 104 |
|
| 105 |
checkError(t, &gotStruct, |
| 106 |
td.Struct(&MyStruct{ |
| 107 |
MyStructMid: MyStructMid{ |
| 108 |
MyStructBase: MyStructBase{ |
| 109 |
ValBool: true, |
| 110 |
}, |
| 111 |
ValStr: "foobax", // ← does not match |
| 112 |
}, |
| 113 |
ValInt: 123, |
| 114 |
}, nil), |
| 115 |
expectedError{ |
| 116 |
Message: mustBe("values differ"), |
| 117 |
Path: mustBe("DATA.ValStr"), |
| 118 |
Got: mustContain("foobar"), |
| 119 |
Expected: mustContain("foobax"), |
| 120 |
}) |
| 121 |
|
| 122 |
// Zero values |
| 123 |
checkOK(t, &MyStruct{}, |
| 124 |
td.Struct(&MyStruct{}, td.StructFields{ |
| 125 |
"ValBool": false, |
| 126 |
"ValStr": "", |
| 127 |
"ValInt": 0, |
| 128 |
})) |
| 129 |
|
| 130 |
// nil cases |
| 131 |
checkError(t, nil, td.Struct(&MyStruct{}, nil), |
| 132 |
expectedError{ |
| 133 |
Message: mustBe("values differ"), |
| 134 |
Path: mustBe("DATA"), |
| 135 |
Got: mustContain("nil"), |
| 136 |
Expected: mustContain("*td_test.MyStruct"), |
| 137 |
}) |
| 138 |
|
| 139 |
checkError(t, (*MyStruct)(nil), td.Struct(&MyStruct{}, nil), |
| 140 |
expectedError{ |
| 141 |
Message: mustBe("values differ"), |
| 142 |
Path: mustBe("DATA"), |
| 143 |
Got: mustContain("nil"), |
| 144 |
Expected: mustBe("non-nil"), |
| 145 |
}) |
| 146 |
|
| 147 |
// |
| 148 |
// Without pointer |
| 149 |
checkOK(t, gotStruct, |
| 150 |
td.Struct(MyStruct{}, td.StructFields{ |
| 151 |
"ValBool": true, |
| 152 |
"ValStr": "foobar", |
| 153 |
"ValInt": 123, |
| 154 |
})) |
| 155 |
|
| 156 |
checkOK(t, gotStruct, |
| 157 |
td.Struct( |
| 158 |
MyStruct{ |
| 159 |
MyStructMid: MyStructMid{ |
| 160 |
ValStr: "zip", |
| 161 |
}, |
| 162 |
ValInt: 666, |
| 163 |
}, |
| 164 |
td.StructFields{ |
| 165 |
"ValBool": true, |
| 166 |
"> ValStr": "foobar", |
| 167 |
">ValInt": 123, |
| 168 |
})) |
| 169 |
|
| 170 |
checkError(t, 123, td.Struct(MyStruct{}, td.StructFields{}), |
| 171 |
expectedError{ |
| 172 |
Message: mustBe("type mismatch"), |
| 173 |
Path: mustBe("DATA"), |
| 174 |
Got: mustContain("int"), |
| 175 |
Expected: mustContain("td_test.MyStruct"), |
| 176 |
}) |
| 177 |
|
| 178 |
checkError(t, gotStruct, |
| 179 |
td.Struct(MyStruct{}, td.StructFields{ |
| 180 |
"ValBool": false, // ← does not match |
| 181 |
"ValStr": "foobar", |
| 182 |
"ValInt": 123, |
| 183 |
}), |
| 184 |
expectedError{ |
| 185 |
Message: mustBe("values differ"), |
| 186 |
Path: mustBe("DATA.ValBool"), |
| 187 |
Got: mustContain("true"), |
| 188 |
Expected: mustContain("false"), |
| 189 |
}) |
| 190 |
|
| 191 |
checkOK(t, gotStruct, |
| 192 |
td.Struct(MyStruct{ |
| 193 |
MyStructMid: MyStructMid{ |
| 194 |
MyStructBase: MyStructBase{ |
| 195 |
ValBool: true, |
| 196 |
}, |
| 197 |
ValStr: "foobar", |
| 198 |
}, |
| 199 |
ValInt: 123, |
| 200 |
}, nil)) |
| 201 |
|
| 202 |
checkError(t, gotStruct, |
| 203 |
td.Struct(MyStruct{ |
| 204 |
MyStructMid: MyStructMid{ |
| 205 |
MyStructBase: MyStructBase{ |
| 206 |
ValBool: true, |
| 207 |
}, |
| 208 |
ValStr: "foobax", // ← does not match |
| 209 |
}, |
| 210 |
ValInt: 123, |
| 211 |
}, nil), |
| 212 |
expectedError{ |
| 213 |
Message: mustBe("values differ"), |
| 214 |
Path: mustBe("DATA.ValStr"), |
| 215 |
Got: mustContain("foobar"), |
| 216 |
Expected: mustContain("foobax"), |
| 217 |
}) |
| 218 |
|
| 219 |
// Zero values |
| 220 |
checkOK(t, MyStruct{}, |
| 221 |
td.Struct(MyStruct{}, td.StructFields{ |
| 222 |
"ValBool": false, |
| 223 |
"ValStr": "", |
| 224 |
"ValInt": 0, |
| 225 |
})) |
| 226 |
|
| 227 |
// nil cases |
| 228 |
checkError(t, nil, td.Struct(MyStruct{}, nil), |
| 229 |
expectedError{ |
| 230 |
Message: mustBe("values differ"), |
| 231 |
Path: mustBe("DATA"), |
| 232 |
Got: mustContain("nil"), |
| 233 |
Expected: mustContain("td_test.MyStruct"), |
| 234 |
}) |
| 235 |
|
| 236 |
checkError(t, (*MyStruct)(nil), td.Struct(MyStruct{}, nil), |
| 237 |
expectedError{ |
| 238 |
Message: mustBe("type mismatch"), |
| 239 |
Path: mustBe("DATA"), |
| 240 |
Got: mustBe("*td_test.MyStruct"), |
| 241 |
Expected: mustBe("td_test.MyStruct"), |
| 242 |
}) |
| 243 |
|
| 244 |
// |
| 245 |
// Be lax... |
| 246 |
type Struct1 struct { |
| 247 |
name string |
| 248 |
age int |
| 249 |
} |
| 250 |
type Struct2 struct { |
| 251 |
name string |
| 252 |
age int |
| 253 |
} |
| 254 |
|
| 255 |
// Without Lax → error |
| 256 |
checkError(t, |
| 257 |
Struct1{name: "Bob", age: 42}, |
| 258 |
td.Struct(Struct2{name: "Bob", age: 42}, nil), |
| 259 |
expectedError{ |
| 260 |
Message: mustBe("type mismatch"), |
| 261 |
}) |
| 262 |
// With Lax → OK |
| 263 |
checkOK(t, |
| 264 |
Struct1{name: "Bob", age: 42}, |
| 265 |
td.Lax(td.Struct(Struct2{name: "Bob", age: 42}, nil))) |
| 266 |
|
| 267 |
// |
| 268 |
// IgnoreUnexported |
| 269 |
t.Run("IgnoreUnexported", func(tt *testing.T) { |
| 270 |
type SType struct { |
| 271 |
Public int |
| 272 |
private string |
| 273 |
} |
| 274 |
got := SType{Public: 42, private: "test"} |
| 275 |
expected := td.Struct(SType{Public: 42, private: "zip"}, nil) |
| 276 |
|
| 277 |
checkError(tt, got, expected, |
| 278 |
expectedError{ |
| 279 |
Message: mustBe("values differ"), |
| 280 |
Path: mustBe("DATA.private"), |
| 281 |
Got: mustBe(`"test"`), |
| 282 |
Expected: mustBe(`"zip"`), |
| 283 |
}) |
| 284 |
|
| 285 |
// Ignore unexported globally |
| 286 |
defer func() { td.DefaultContextConfig.IgnoreUnexported = false }() |
| 287 |
td.DefaultContextConfig.IgnoreUnexported = true |
| 288 |
checkOK(tt, got, expected) |
| 289 |
td.DefaultContextConfig.IgnoreUnexported = false |
| 290 |
|
| 291 |
ttt := test.NewTestingTB(t.Name()) |
| 292 |
t := td.NewT(ttt).IgnoreUnexported(SType{}) // ignore only for SType |
| 293 |
test.IsTrue(tt, t.Cmp(got, expected)) |
| 294 |
}) |
| 295 |
|
| 296 |
// |
| 297 |
// Bad usage |
| 298 |
checkError(t, "never tested", |
| 299 |
td.Struct("test", nil), |
| 300 |
expectedError{ |
| 301 |
Message: mustBe("bad usage of Struct operator"), |
| 302 |
Path: mustBe("DATA"), |
| 303 |
Summary: mustBe("usage: Struct(STRUCT|&STRUCT|nil, EXPECTED_FIELDS), but received string as 1st parameter"), |
| 304 |
}) |
| 305 |
|
| 306 |
i := 12 |
| 307 |
checkError(t, "never tested", |
| 308 |
td.Struct(&i, nil), |
| 309 |
expectedError{ |
| 310 |
Message: mustBe("bad usage of Struct operator"), |
| 311 |
Path: mustBe("DATA"), |
| 312 |
Summary: mustBe("usage: Struct(STRUCT|&STRUCT|nil, EXPECTED_FIELDS), but received *int (ptr) as 1st parameter"), |
| 313 |
}) |
| 314 |
|
| 315 |
checkError(t, "never tested", |
| 316 |
td.Struct(&MyStruct{}, td.StructFields{"UnknownField": 123}), |
| 317 |
expectedError{ |
| 318 |
Message: mustBe("bad usage of Struct operator"), |
| 319 |
Path: mustBe("DATA"), |
| 320 |
Summary: mustBe(`struct td_test.MyStruct has no field "UnknownField"`), |
| 321 |
}) |
| 322 |
|
| 323 |
checkError(t, "never tested", |
| 324 |
td.Struct(&MyStruct{}, td.StructFields{">\tUnknownField": 123}), |
| 325 |
expectedError{ |
| 326 |
Message: mustBe("bad usage of Struct operator"), |
| 327 |
Path: mustBe("DATA"), |
| 328 |
Summary: mustBe(`struct td_test.MyStruct has no field "UnknownField" (from ">\tUnknownField")`), |
| 329 |
}) |
| 330 |
|
| 331 |
checkError(t, &MyStruct{}, |
| 332 |
td.Struct(&MyStruct{}, td.StructFields{"ValBool": 123}), |
| 333 |
expectedError{ |
| 334 |
Message: mustBe("type mismatch"), |
| 335 |
Path: mustBe("DATA.ValBool"), |
| 336 |
Got: mustBe("bool"), |
| 337 |
Expected: mustBe("int"), |
| 338 |
}) |
| 339 |
|
| 340 |
checkError(t, &MyStruct{}, |
| 341 |
td.Struct(&MyStruct{}, td.StructFields{">ValBool": 123}), |
| 342 |
expectedError{ |
| 343 |
Message: mustBe("type mismatch"), |
| 344 |
Path: mustBe(`DATA.ValBool (from ">ValBool")`), |
| 345 |
Got: mustBe("bool"), |
| 346 |
Expected: mustBe("int"), |
| 347 |
}) |
| 348 |
|
| 349 |
checkError(t, &MyStruct{}, |
| 350 |
td.Struct(&MyStruct{}, td.StructFields{"ValBool": nil}), |
| 351 |
expectedError{ |
| 352 |
Message: mustBe("values differ"), |
| 353 |
Path: mustBe("DATA.ValBool"), |
| 354 |
Got: mustBe("false"), |
| 355 |
Expected: mustBe("nil"), |
| 356 |
}) |
| 357 |
|
| 358 |
checkError(t, "never tested", |
| 359 |
td.Struct(&MyStruct{ |
| 360 |
MyStructMid: MyStructMid{ |
| 361 |
MyStructBase: MyStructBase{ |
| 362 |
ValBool: true, |
| 363 |
}, |
| 364 |
}, |
| 365 |
}, |
| 366 |
td.StructFields{"ValBool": false}), |
| 367 |
expectedError{ |
| 368 |
Message: mustBe("bad usage of Struct operator"), |
| 369 |
Path: mustBe("DATA"), |
| 370 |
Summary: mustBe("non zero field ValBool in model already exists in expectedFields"), |
| 371 |
}) |
| 372 |
|
| 373 |
// |
| 374 |
// String |
| 375 |
test.EqualStr(t, |
| 376 |
td.Struct(MyStruct{ |
| 377 |
MyStructMid: MyStructMid{ |
| 378 |
ValStr: "foobar", |
| 379 |
}, |
| 380 |
ValInt: 123, |
| 381 |
}, |
| 382 |
td.StructFields{ |
| 383 |
"ValBool": false, |
| 384 |
}).String(), |
| 385 |
`Struct(td_test.MyStruct{ |
| 386 |
ValBool: false |
| 387 |
ValInt: 123 |
| 388 |
ValStr: "foobar" |
| 389 |
})`) |
| 390 |
|
| 391 |
test.EqualStr(t, |
| 392 |
td.Struct(&MyStruct{ |
| 393 |
MyStructMid: MyStructMid{ |
| 394 |
ValStr: "foobar", |
| 395 |
}, |
| 396 |
ValInt: 123, |
| 397 |
}, |
| 398 |
td.StructFields{ |
| 399 |
"ValBool": false, |
| 400 |
}).String(), |
| 401 |
`Struct(*td_test.MyStruct{ |
| 402 |
ValBool: false |
| 403 |
ValInt: 123 |
| 404 |
ValStr: "foobar" |
| 405 |
})`) |
| 406 |
|
| 407 |
test.EqualStr(t, |
| 408 |
td.Struct(&MyStruct{}, |
| 409 |
td.StructFields{ |
| 410 |
"ValBool": false, |
| 411 |
"= Val*": td.NotZero(), |
| 412 |
}).String(), |
| 413 |
`Struct(*td_test.MyStruct{ |
| 414 |
ValBool: false |
| 415 |
ValInt (from pattern `+"`= Val*`"+`): NotZero() |
| 416 |
ValStr (from pattern `+"`= Val*`"+`): NotZero() |
| 417 |
})`) |
| 418 |
|
| 419 |
test.EqualStr(t, |
| 420 |
td.Struct(&MyStruct{}, td.StructFields{}).String(), |
| 421 |
`Struct(*td_test.MyStruct{})`) |
| 422 |
|
| 423 |
// Erroneous op |
| 424 |
test.EqualStr(t, td.Struct("test", nil).String(), "Struct(<ERROR>)") |
| 425 |
} |
| 426 |
|
| 427 |
func TestStructPrivateFields(t *testing.T) { |
| 428 |
type privateKey struct { |
| 429 |
num int |
| 430 |
name string |
| 431 |
} |
| 432 |
|
| 433 |
type privateValue struct { |
| 434 |
value string |
| 435 |
weight int |
| 436 |
} |
| 437 |
|
| 438 |
type MyTime time.Time |
| 439 |
|
| 440 |
type structPrivateFields struct { |
| 441 |
byKey map[privateKey]*privateValue |
| 442 |
name string |
| 443 |
nameb []byte |
| 444 |
err error |
| 445 |
iface any |
| 446 |
properties []int |
| 447 |
birth time.Time |
| 448 |
birth2 MyTime |
| 449 |
next *structPrivateFields |
| 450 |
} |
| 451 |
|
| 452 |
d := func(rfc3339Date string) (ret time.Time) { |
| 453 |
var err error |
| 454 |
ret, err = time.Parse(time.RFC3339Nano, rfc3339Date) |
| 455 |
if err != nil { |
| 456 |
panic(err) |
| 457 |
} |
| 458 |
return |
| 459 |
} |
| 460 |
|
| 461 |
got := structPrivateFields{ |
| 462 |
byKey: map[privateKey]*privateValue{ |
| 463 |
{num: 1, name: "foo"}: {value: "test", weight: 12}, |
| 464 |
{num: 2, name: "bar"}: {value: "tset", weight: 23}, |
| 465 |
{num: 3, name: "zip"}: {value: "ttse", weight: 34}, |
| 466 |
}, |
| 467 |
name: "foobar", |
| 468 |
nameb: []byte("foobar"), |
| 469 |
err: errors.New("the error"), |
| 470 |
iface: 1234, |
| 471 |
properties: []int{20, 22, 23, 21}, |
| 472 |
birth: d("2018-04-01T10:11:12.123456789Z"), |
| 473 |
birth2: MyTime(d("2018-03-01T09:08:07.987654321Z")), |
| 474 |
next: &structPrivateFields{ |
| 475 |
byKey: map[privateKey]*privateValue{}, |
| 476 |
name: "sub", |
| 477 |
iface: bytes.NewBufferString("buffer!"), |
| 478 |
birth: d("2018-04-02T10:11:12.123456789Z"), |
| 479 |
birth2: MyTime(d("2018-03-02T09:08:07.987654321Z")), |
| 480 |
}, |
| 481 |
} |
| 482 |
|
| 483 |
checkOK(t, got, |
| 484 |
td.Struct(structPrivateFields{}, td.StructFields{ |
| 485 |
"name": "foobar", |
| 486 |
})) |
| 487 |
|
| 488 |
checkOK(t, got, |
| 489 |
td.Struct(structPrivateFields{}, td.StructFields{ |
| 490 |
"name": td.Re("^foo"), |
| 491 |
})) |
| 492 |
|
| 493 |
checkOK(t, got, |
| 494 |
td.Struct(structPrivateFields{}, td.StructFields{ |
| 495 |
"nameb": td.Re("^foo"), |
| 496 |
})) |
| 497 |
|
| 498 |
checkOKOrPanicIfUnsafeDisabled(t, got, |
| 499 |
td.Struct(structPrivateFields{}, td.StructFields{ |
| 500 |
"err": td.Re("error"), |
| 501 |
})) |
| 502 |
|
| 503 |
checkError(t, got, |
| 504 |
td.Struct(structPrivateFields{}, td.StructFields{ |
| 505 |
"iface": td.Re("buffer"), |
| 506 |
}), |
| 507 |
expectedError{ |
| 508 |
Message: mustBe("bad type"), |
| 509 |
Path: mustBe("DATA.iface"), |
| 510 |
Got: mustBe("int"), |
| 511 |
Expected: mustBe("string (convertible) OR fmt.Stringer OR error OR []uint8"), |
| 512 |
}) |
| 513 |
|
| 514 |
checkOKOrPanicIfUnsafeDisabled(t, got, |
| 515 |
td.Struct(structPrivateFields{}, td.StructFields{ |
| 516 |
"next": td.Struct(&structPrivateFields{}, td.StructFields{ |
| 517 |
"iface": td.Re("buffer"), |
| 518 |
}), |
| 519 |
})) |
| 520 |
|
| 521 |
checkOK(t, got, |
| 522 |
td.Struct(structPrivateFields{}, td.StructFields{ |
| 523 |
"properties": []int{20, 22, 23, 21}, |
| 524 |
})) |
| 525 |
|
| 526 |
checkOK(t, got, |
| 527 |
td.Struct(structPrivateFields{}, td.StructFields{ |
| 528 |
"properties": td.ArrayEach(td.Between(20, 23)), |
| 529 |
})) |
| 530 |
|
| 531 |
checkOK(t, got, |
| 532 |
td.Struct(structPrivateFields{}, td.StructFields{ |
| 533 |
"byKey": td.MapEach(td.Struct(&privateValue{}, td.StructFields{ |
| 534 |
"weight": td.Between(12, 34), |
| 535 |
"value": td.Any(td.HasPrefix("t"), td.HasSuffix("e")), |
| 536 |
})), |
| 537 |
})) |
| 538 |
|
| 539 |
checkOK(t, got, |
| 540 |
td.Struct(structPrivateFields{}, td.StructFields{ |
| 541 |
"byKey": td.SuperMapOf( |
| 542 |
map[privateKey]*privateValue{ |
| 543 |
{num: 3, name: "zip"}: {value: "ttse", weight: 34}, |
| 544 |
}, |
| 545 |
td.MapEntries{ |
| 546 |
privateKey{num: 2, name: "bar"}: &privateValue{value: "tset", weight: 23}, |
| 547 |
}), |
| 548 |
})) |
| 549 |
|
| 550 |
expected := td.Struct(structPrivateFields{}, td.StructFields{ |
| 551 |
"birth": td.TruncTime(d("2018-04-01T10:11:12Z"), time.Second), |
| 552 |
"birth2": td.TruncTime(MyTime(d("2018-03-01T09:08:07Z")), time.Second), |
| 553 |
}) |
| 554 |
if !dark.UnsafeDisabled { |
| 555 |
checkOK(t, got, expected) |
| 556 |
} else { |
| 557 |
checkError(t, got, expected, |
| 558 |
expectedError{ |
| 559 |
Message: mustBe("cannot compare"), |
| 560 |
Path: mustBe("DATA.birth"), |
| 561 |
Summary: mustBe("unexported field that cannot be overridden"), |
| 562 |
Next: &expectedError{ |
| 563 |
Message: mustBe("cannot compare"), |
| 564 |
Path: mustMatch(`DATA\.(?:Iface\.)?birth2`), |
| 565 |
Summary: mustBe("unexported field that cannot be overridden"), |
| 566 |
}, |
| 567 |
}) |
| 568 |
} |
| 569 |
|
| 570 |
checkError(t, got, |
| 571 |
td.Struct(structPrivateFields{}, td.StructFields{ |
| 572 |
"next": td.Struct(&structPrivateFields{}, td.StructFields{ |
| 573 |
"name": "sub", |
| 574 |
"birth": td.Code(func(t time.Time) bool { return true }), |
| 575 |
}), |
| 576 |
}), |
| 577 |
expectedError{ |
| 578 |
Message: mustBe("cannot compare unexported field"), |
| 579 |
Path: mustBe("DATA.next.birth"), |
| 580 |
Summary: mustBe("use Code() on surrounding struct instead"), |
| 581 |
}) |
| 582 |
|
| 583 |
checkError(t, got, |
| 584 |
td.Struct(structPrivateFields{}, td.StructFields{ |
| 585 |
"next": td.Struct(&structPrivateFields{}, td.StructFields{ |
| 586 |
"name": "sub", |
| 587 |
"birth": td.Smuggle( |
| 588 |
func(t time.Time) string { return t.String() }, |
| 589 |
"2018-04-01T10:11:12.123456789Z"), |
| 590 |
}), |
| 591 |
}), |
| 592 |
expectedError{ |
| 593 |
Message: mustBe("cannot smuggle unexported field"), |
| 594 |
Path: mustBe("DATA.next.birth"), |
| 595 |
Summary: mustBe("work on surrounding struct instead"), |
| 596 |
}) |
| 597 |
} |
| 598 |
|
| 599 |
func TestStructPatterns(t *testing.T) { |
| 600 |
type paAnon struct { |
| 601 |
alphaNum int |
| 602 |
betaNum int |
| 603 |
} |
| 604 |
type paTest struct { |
| 605 |
paAnon |
| 606 |
Num int |
| 607 |
} |
| 608 |
|
| 609 |
got := paTest{ |
| 610 |
paAnon: paAnon{ |
| 611 |
alphaNum: 1000, |
| 612 |
betaNum: 2000, |
| 613 |
}, |
| 614 |
Num: 666, |
| 615 |
} |
| 616 |
|
| 617 |
t.Run("Shell pattern", func(t *testing.T) { |
| 618 |
checkOK(t, got, |
| 619 |
td.Struct(paTest{Num: 666}, |
| 620 |
td.StructFields{ |
| 621 |
"=*Num": td.Gte(1000), // matches alphaNum & betaNum |
| 622 |
})) |
| 623 |
|
| 624 |
checkOK(t, got, |
| 625 |
td.Struct(paTest{Num: 666}, |
| 626 |
td.StructFields{ |
| 627 |
"=a*Num": td.Lt(0), // no remaining fields to match |
| 628 |
"=*": td.Gte(1000), // first, matches alphaNum & betaNum |
| 629 |
"=b*Num": td.Lt(0), // no remaining fields to match |
| 630 |
}), |
| 631 |
"Default sorting uses patterns") |
| 632 |
|
| 633 |
checkOK(t, got, |
| 634 |
td.Struct(paTest{Num: 666}, |
| 635 |
td.StructFields{ |
| 636 |
"1 = a*Num": td.Between(999, 1001), // matches alphaNum |
| 637 |
"2 = *": td.Gte(2000), // matches betaNum |
| 638 |
"3 = b*Num": td.Gt(3000), // no remaining fields to match |
| 639 |
}), |
| 640 |
"Explicitly sorted") |
| 641 |
|
| 642 |
checkOK(t, got, |
| 643 |
td.Struct(paTest{Num: 666}, |
| 644 |
td.StructFields{ |
| 645 |
"1 ! beta*": 1000, // matches alphaNum |
| 646 |
"2 = *": 2000, // matches betaNum |
| 647 |
}), |
| 648 |
"negative shell pattern") |
| 649 |
|
| 650 |
checkError(t, "never tested", |
| 651 |
td.Struct(paTest{Num: 666}, td.StructFields{"= al[pha": 123}), |
| 652 |
expectedError{ |
| 653 |
Message: mustBe("bad usage of Struct operator"), |
| 654 |
Path: mustBe("DATA"), |
| 655 |
Summary: mustContain("bad shell pattern field `= al[pha`: "), |
| 656 |
}) |
| 657 |
|
| 658 |
checkError(t, paTest{Num: 666}, |
| 659 |
td.Struct(paTest{Num: 666}, td.StructFields{"= alpha*": nil}), |
| 660 |
expectedError{ |
| 661 |
Message: mustBe("values differ"), |
| 662 |
Path: mustBe("DATA.alphaNum (from pattern `= alpha*`)"), |
| 663 |
Got: mustBe("0"), |
| 664 |
Expected: mustBe("nil"), |
| 665 |
}) |
| 666 |
}) |
| 667 |
|
| 668 |
t.Run("Regexp", func(t *testing.T) { |
| 669 |
checkOK(t, got, |
| 670 |
td.Struct(paTest{Num: 666}, |
| 671 |
td.StructFields{ |
| 672 |
"=~Num$": td.Gte(1000), // matches alphaNum & betaNum |
| 673 |
})) |
| 674 |
|
| 675 |
checkOK(t, got, |
| 676 |
td.Struct(paTest{Num: 666}, |
| 677 |
td.StructFields{ |
| 678 |
"=~^a.*Num$": td.Lt(0), // no remaining fields to match |
| 679 |
"=~.": td.Gte(1000), // first, matches alphaNum & betaNum |
| 680 |
"=~^b.*Num$": td.Lt(0), // no remaining fields to match |
| 681 |
}), |
| 682 |
"Default sorting uses patterns") |
| 683 |
|
| 684 |
checkOK(t, got, |
| 685 |
td.Struct(paTest{Num: 666}, |
| 686 |
td.StructFields{ |
| 687 |
"1 =~ ^a.*Num$": td.Between(999, 1001), // matches alphaNum |
| 688 |
"2 =~ .": td.Gte(2000), // matches betaNum |
| 689 |
"3 =~ ^b.*Num$": td.Gt(3000), // no remaining fields to match |
| 690 |
}), |
| 691 |
"Explicitly sorted") |
| 692 |
|
| 693 |
checkOK(t, got, |
| 694 |
td.Struct(paTest{Num: 666}, |
| 695 |
td.StructFields{ |
| 696 |
"1 !~ ^beta": 1000, // matches alphaNum |
| 697 |
"2 =~ .": 2000, // matches betaNum |
| 698 |
}), |
| 699 |
"negative regexp") |
| 700 |
|
| 701 |
checkError(t, "never tested", |
| 702 |
td.Struct(paTest{Num: 666}, td.StructFields{"=~ al(*": 123}), |
| 703 |
expectedError{ |
| 704 |
Message: mustBe("bad usage of Struct operator"), |
| 705 |
Path: mustBe("DATA"), |
| 706 |
Summary: mustContain("bad regexp field `=~ al(*`: "), |
| 707 |
}) |
| 708 |
|
| 709 |
checkError(t, paTest{Num: 666}, |
| 710 |
td.Struct(paTest{Num: 666}, td.StructFields{"=~ alpha": nil}), |
| 711 |
expectedError{ |
| 712 |
Message: mustBe("values differ"), |
| 713 |
Path: mustBe("DATA.alphaNum (from pattern `=~ alpha`)"), |
| 714 |
Got: mustBe("0"), |
| 715 |
Expected: mustBe("nil"), |
| 716 |
}) |
| 717 |
}) |
| 718 |
} |
| 719 |
|
| 720 |
func TestStructTypeBehind(t *testing.T) { |
| 721 |
equalTypes(t, td.Struct(MyStruct{}, nil), MyStruct{}) |
| 722 |
equalTypes(t, td.Struct(&MyStruct{}, nil), &MyStruct{}) |
| 723 |
|
| 724 |
// Erroneous op |
| 725 |
equalTypes(t, td.Struct("test", nil), nil) |
| 726 |
} |
| 727 |
|
| 728 |
func TestSStruct(t *testing.T) { |
| 729 |
gotStruct := MyStruct{ |
| 730 |
MyStructMid: MyStructMid{ |
| 731 |
MyStructBase: MyStructBase{ |
| 732 |
ValBool: true, |
| 733 |
}, |
| 734 |
ValStr: "foobar", |
| 735 |
}, |
| 736 |
ValInt: 123, |
| 737 |
} |
| 738 |
|
| 739 |
// |
| 740 |
// Using pointer |
| 741 |
checkOK(t, &gotStruct, |
| 742 |
td.SStruct(&MyStruct{}, td.StructFields{ |
| 743 |
"ValBool": true, |
| 744 |
"ValStr": "foobar", |
| 745 |
"ValInt": 123, |
| 746 |
// nil Ptr |
| 747 |
})) |
| 748 |
|
| 749 |
checkOK(t, &gotStruct, |
| 750 |
td.SStruct( |
| 751 |
&MyStruct{ |
| 752 |
MyStructMid: MyStructMid{ |
| 753 |
ValStr: "zip", |
| 754 |
}, |
| 755 |
ValInt: 666, |
| 756 |
}, |
| 757 |
td.StructFields{ |
| 758 |
"ValBool": true, |
| 759 |
"> ValStr": "foobar", |
| 760 |
">ValInt": 123, |
| 761 |
})) |
| 762 |
|
| 763 |
checkOK(t, &gotStruct, |
| 764 |
td.SStruct((*MyStruct)(nil), td.StructFields{ |
| 765 |
"ValBool": true, |
| 766 |
"ValStr": "foobar", |
| 767 |
"ValInt": 123, |
| 768 |
// nil Ptr |
| 769 |
})) |
| 770 |
|
| 771 |
checkError(t, 123, |
| 772 |
td.SStruct(&MyStruct{}, td.StructFields{}), |
| 773 |
expectedError{ |
| 774 |
Message: mustBe("type mismatch"), |
| 775 |
Path: mustBe("DATA"), |
| 776 |
Got: mustContain("int"), |
| 777 |
Expected: mustContain("*td_test.MyStruct"), |
| 778 |
}) |
| 779 |
|
| 780 |
checkError(t, &MyStructBase{}, |
| 781 |
td.SStruct(&MyStruct{}, td.StructFields{}), |
| 782 |
expectedError{ |
| 783 |
Message: mustBe("type mismatch"), |
| 784 |
Path: mustBe("DATA"), |
| 785 |
Got: mustContain("*td_test.MyStructBase"), |
| 786 |
Expected: mustContain("*td_test.MyStruct"), |
| 787 |
}) |
| 788 |
|
| 789 |
checkError(t, &gotStruct, |
| 790 |
td.SStruct(&MyStruct{}, td.StructFields{ |
| 791 |
// ValBool false ← does not match |
| 792 |
"ValStr": "foobar", |
| 793 |
"ValInt": 123, |
| 794 |
}), |
| 795 |
expectedError{ |
| 796 |
Message: mustBe("values differ"), |
| 797 |
Path: mustBe("DATA.ValBool"), |
| 798 |
Got: mustContain("true"), |
| 799 |
Expected: mustContain("false"), |
| 800 |
}) |
| 801 |
|
| 802 |
checkOK(t, &gotStruct, |
| 803 |
td.SStruct(&MyStruct{ |
| 804 |
MyStructMid: MyStructMid{ |
| 805 |
MyStructBase: MyStructBase{ |
| 806 |
ValBool: true, |
| 807 |
}, |
| 808 |
ValStr: "foobar", |
| 809 |
}, |
| 810 |
ValInt: 123, |
| 811 |
}, nil)) |
| 812 |
|
| 813 |
checkError(t, &gotStruct, |
| 814 |
td.SStruct(&MyStruct{ |
| 815 |
MyStructMid: MyStructMid{ |
| 816 |
MyStructBase: MyStructBase{ |
| 817 |
ValBool: true, |
| 818 |
}, |
| 819 |
ValStr: "foobax", // ← does not match |
| 820 |
}, |
| 821 |
ValInt: 123, |
| 822 |
}, nil), |
| 823 |
expectedError{ |
| 824 |
Message: mustBe("values differ"), |
| 825 |
Path: mustBe("DATA.ValStr"), |
| 826 |
Got: mustContain("foobar"), |
| 827 |
Expected: mustContain("foobax"), |
| 828 |
}) |
| 829 |
|
| 830 |
// Zero values |
| 831 |
checkOK(t, &MyStruct{}, td.SStruct(&MyStruct{}, nil)) |
| 832 |
checkOK(t, &MyStruct{}, td.SStruct(&MyStruct{}, td.StructFields{})) |
| 833 |
|
| 834 |
// nil cases |
| 835 |
checkError(t, nil, td.SStruct(&MyStruct{}, nil), |
| 836 |
expectedError{ |
| 837 |
Message: mustBe("values differ"), |
| 838 |
Path: mustBe("DATA"), |
| 839 |
Got: mustContain("nil"), |
| 840 |
Expected: mustContain("*td_test.MyStruct"), |
| 841 |
}) |
| 842 |
|
| 843 |
checkError(t, (*MyStruct)(nil), td.SStruct(&MyStruct{}, nil), |
| 844 |
expectedError{ |
| 845 |
Message: mustBe("values differ"), |
| 846 |
Path: mustBe("DATA"), |
| 847 |
Got: mustContain("nil"), |
| 848 |
Expected: mustBe("non-nil"), |
| 849 |
}) |
| 850 |
|
| 851 |
// |
| 852 |
// Without pointer |
| 853 |
checkOK(t, gotStruct, |
| 854 |
td.SStruct(MyStruct{}, td.StructFields{ |
| 855 |
"ValBool": true, |
| 856 |
"ValStr": "foobar", |
| 857 |
"ValInt": 123, |
| 858 |
})) |
| 859 |
|
| 860 |
checkOK(t, gotStruct, |
| 861 |
td.SStruct( |
| 862 |
MyStruct{ |
| 863 |
MyStructMid: MyStructMid{ |
| 864 |
ValStr: "zip", |
| 865 |
}, |
| 866 |
ValInt: 666, |
| 867 |
}, |
| 868 |
td.StructFields{ |
| 869 |
"ValBool": true, |
| 870 |
"> ValStr": "foobar", |
| 871 |
">ValInt": 123, |
| 872 |
})) |
| 873 |
|
| 874 |
checkError(t, 123, td.SStruct(MyStruct{}, td.StructFields{}), |
| 875 |
expectedError{ |
| 876 |
Message: mustBe("type mismatch"), |
| 877 |
Path: mustBe("DATA"), |
| 878 |
Got: mustContain("int"), |
| 879 |
Expected: mustContain("td_test.MyStruct"), |
| 880 |
}) |
| 881 |
|
| 882 |
checkError(t, gotStruct, |
| 883 |
td.SStruct(MyStruct{}, td.StructFields{ |
| 884 |
// "ValBool" false ← does not match |
| 885 |
"ValStr": "foobar", |
| 886 |
"ValInt": 123, |
| 887 |
}), |
| 888 |
expectedError{ |
| 889 |
Message: mustBe("values differ"), |
| 890 |
Path: mustBe("DATA.ValBool"), |
| 891 |
Got: mustContain("true"), |
| 892 |
Expected: mustContain("false"), |
| 893 |
}) |
| 894 |
|
| 895 |
checkOK(t, gotStruct, |
| 896 |
td.SStruct(MyStruct{ |
| 897 |
MyStructMid: MyStructMid{ |
| 898 |
MyStructBase: MyStructBase{ |
| 899 |
ValBool: true, |
| 900 |
}, |
| 901 |
ValStr: "foobar", |
| 902 |
}, |
| 903 |
ValInt: 123, |
| 904 |
}, nil)) |
| 905 |
|
| 906 |
checkError(t, gotStruct, |
| 907 |
td.SStruct(MyStruct{ |
| 908 |
MyStructMid: MyStructMid{ |
| 909 |
MyStructBase: MyStructBase{ |
| 910 |
ValBool: true, |
| 911 |
}, |
| 912 |
ValStr: "foobax", // ← does not match |
| 913 |
}, |
| 914 |
ValInt: 123, |
| 915 |
}, nil), |
| 916 |
expectedError{ |
| 917 |
Message: mustBe("values differ"), |
| 918 |
Path: mustBe("DATA.ValStr"), |
| 919 |
Got: mustContain("foobar"), |
| 920 |
Expected: mustContain("foobax"), |
| 921 |
}) |
| 922 |
|
| 923 |
// Zero values |
| 924 |
checkOK(t, MyStruct{}, td.Struct(MyStruct{}, td.StructFields{})) |
| 925 |
checkOK(t, MyStruct{}, td.Struct(MyStruct{}, nil)) |
| 926 |
|
| 927 |
// nil cases |
| 928 |
checkError(t, nil, td.SStruct(MyStruct{}, nil), |
| 929 |
expectedError{ |
| 930 |
Message: mustBe("values differ"), |
| 931 |
Path: mustBe("DATA"), |
| 932 |
Got: mustContain("nil"), |
| 933 |
Expected: mustContain("td_test.MyStruct"), |
| 934 |
}) |
| 935 |
|
| 936 |
checkError(t, (*MyStruct)(nil), td.SStruct(MyStruct{}, nil), |
| 937 |
expectedError{ |
| 938 |
Message: mustBe("type mismatch"), |
| 939 |
Path: mustBe("DATA"), |
| 940 |
Got: mustBe("*td_test.MyStruct"), |
| 941 |
Expected: mustBe("td_test.MyStruct"), |
| 942 |
}) |
| 943 |
|
| 944 |
// |
| 945 |
// Be lax... |
| 946 |
type Struct1 struct { |
| 947 |
name string |
| 948 |
age int |
| 949 |
} |
| 950 |
type Struct2 struct { |
| 951 |
name string |
| 952 |
age int |
| 953 |
} |
| 954 |
|
| 955 |
// Without Lax → error |
| 956 |
checkError(t, |
| 957 |
Struct1{name: "Bob", age: 42}, |
| 958 |
td.SStruct(Struct2{name: "Bob", age: 42}, nil), |
| 959 |
expectedError{ |
| 960 |
Message: mustBe("type mismatch"), |
| 961 |
}) |
| 962 |
// With Lax → OK |
| 963 |
checkOK(t, |
| 964 |
Struct1{name: "Bob", age: 42}, |
| 965 |
td.Lax(td.SStruct(Struct2{name: "Bob", age: 42}, nil))) |
| 966 |
|
| 967 |
// |
| 968 |
// IgnoreUnexported |
| 969 |
t.Run("IgnoreUnexported", func(tt *testing.T) { |
| 970 |
type SType struct { |
| 971 |
Public int |
| 972 |
private string |
| 973 |
} |
| 974 |
got := SType{Public: 42, private: "test"} |
| 975 |
expected := td.SStruct(SType{Public: 42}, nil) |
| 976 |
|
| 977 |
checkError(tt, got, expected, |
| 978 |
expectedError{ |
| 979 |
Message: mustBe("values differ"), |
| 980 |
Path: mustBe("DATA.private"), |
| 981 |
Got: mustBe(`"test"`), |
| 982 |
Expected: mustBe(`""`), |
| 983 |
}) |
| 984 |
|
| 985 |
// Ignore unexported globally |
| 986 |
defer func() { td.DefaultContextConfig.IgnoreUnexported = false }() |
| 987 |
td.DefaultContextConfig.IgnoreUnexported = true |
| 988 |
checkOK(tt, got, expected) |
| 989 |
td.DefaultContextConfig.IgnoreUnexported = false |
| 990 |
|
| 991 |
ttt := test.NewTestingTB(t.Name()) |
| 992 |
t := td.NewT(ttt).IgnoreUnexported(SType{}) // ignore only for SType |
| 993 |
test.IsTrue(tt, t.Cmp(got, expected)) |
| 994 |
}) |
| 995 |
|
| 996 |
// |
| 997 |
// Bad usage |
| 998 |
checkError(t, "never tested", |
| 999 |
td.SStruct("test", nil), |
| 1000 |
expectedError{ |
| 1001 |
Message: mustBe("bad usage of SStruct operator"), |
| 1002 |
Path: mustBe("DATA"), |
| 1003 |
Summary: mustBe("usage: SStruct(STRUCT|&STRUCT|nil, EXPECTED_FIELDS), but received string as 1st parameter"), |
| 1004 |
}) |
| 1005 |
|
| 1006 |
i := 12 |
| 1007 |
checkError(t, "never tested", |
| 1008 |
td.SStruct(&i, nil), |
| 1009 |
expectedError{ |
| 1010 |
Message: mustBe("bad usage of SStruct operator"), |
| 1011 |
Path: mustBe("DATA"), |
| 1012 |
Summary: mustBe("usage: SStruct(STRUCT|&STRUCT|nil, EXPECTED_FIELDS), but received *int (ptr) as 1st parameter"), |
| 1013 |
}) |
| 1014 |
|
| 1015 |
checkError(t, "never tested", |
| 1016 |
td.SStruct(&MyStruct{}, td.StructFields{"UnknownField": 123}), |
| 1017 |
expectedError{ |
| 1018 |
Message: mustBe("bad usage of SStruct operator"), |
| 1019 |
Path: mustBe("DATA"), |
| 1020 |
Summary: mustBe(`struct td_test.MyStruct has no field "UnknownField"`), |
| 1021 |
}) |
| 1022 |
|
| 1023 |
checkError(t, "never tested", |
| 1024 |
td.SStruct(&MyStruct{}, td.StructFields{">\tUnknownField": 123}), |
| 1025 |
expectedError{ |
| 1026 |
Message: mustBe("bad usage of SStruct operator"), |
| 1027 |
Path: mustBe("DATA"), |
| 1028 |
Summary: mustBe(`struct td_test.MyStruct has no field "UnknownField" (from ">\tUnknownField")`), |
| 1029 |
}) |
| 1030 |
|
| 1031 |
checkError(t, &MyStruct{}, |
| 1032 |
td.SStruct(&MyStruct{}, td.StructFields{"ValBool": 123}), |
| 1033 |
expectedError{ |
| 1034 |
Message: mustBe("type mismatch"), |
| 1035 |
Path: mustBe("DATA.ValBool"), |
| 1036 |
Got: mustBe("bool"), |
| 1037 |
Expected: mustBe("int"), |
| 1038 |
}) |
| 1039 |
|
| 1040 |
checkError(t, &MyStruct{}, |
| 1041 |
td.SStruct(&MyStruct{}, td.StructFields{">ValBool": 123}), |
| 1042 |
expectedError{ |
| 1043 |
Message: mustBe("type mismatch"), |
| 1044 |
Path: mustBe(`DATA.ValBool (from ">ValBool")`), |
| 1045 |
Got: mustBe("bool"), |
| 1046 |
Expected: mustBe("int"), |
| 1047 |
}) |
| 1048 |
|
| 1049 |
checkError(t, &MyStruct{}, |
| 1050 |
td.SStruct(&MyStruct{}, td.StructFields{"ValBool": nil}), |
| 1051 |
expectedError{ |
| 1052 |
Message: mustBe("values differ"), |
| 1053 |
Path: mustBe("DATA.ValBool"), |
| 1054 |
Got: mustBe("false"), |
| 1055 |
Expected: mustBe("nil"), |
| 1056 |
}) |
| 1057 |
|
| 1058 |
checkError(t, "never tested", |
| 1059 |
td.SStruct(&MyStruct{ |
| 1060 |
MyStructMid: MyStructMid{ |
| 1061 |
MyStructBase: MyStructBase{ |
| 1062 |
ValBool: true, |
| 1063 |
}, |
| 1064 |
}, |
| 1065 |
}, |
| 1066 |
td.StructFields{"ValBool": false}), |
| 1067 |
expectedError{ |
| 1068 |
Message: mustBe("bad usage of SStruct operator"), |
| 1069 |
Path: mustBe("DATA"), |
| 1070 |
Summary: mustBe("non zero field ValBool in model already exists in expectedFields"), |
| 1071 |
}) |
| 1072 |
|
| 1073 |
// |
| 1074 |
// String |
| 1075 |
test.EqualStr(t, |
| 1076 |
td.SStruct(MyStruct{ |
| 1077 |
MyStructMid: MyStructMid{ |
| 1078 |
ValStr: "foobar", |
| 1079 |
}, |
| 1080 |
ValInt: 123, |
| 1081 |
}, |
| 1082 |
td.StructFields{ |
| 1083 |
"ValBool": false, |
| 1084 |
}).String(), |
| 1085 |
`SStruct(td_test.MyStruct{ |
| 1086 |
Ptr: (*int)(<nil>) |
| 1087 |
ValBool: false |
| 1088 |
ValInt: 123 |
| 1089 |
ValStr: "foobar" |
| 1090 |
})`) |
| 1091 |
|
| 1092 |
test.EqualStr(t, |
| 1093 |
td.SStruct(&MyStruct{ |
| 1094 |
MyStructMid: MyStructMid{ |
| 1095 |
ValStr: "foobar", |
| 1096 |
}, |
| 1097 |
ValInt: 123, |
| 1098 |
}, |
| 1099 |
td.StructFields{ |
| 1100 |
"ValBool": false, |
| 1101 |
}).String(), |
| 1102 |
`SStruct(*td_test.MyStruct{ |
| 1103 |
Ptr: (*int)(<nil>) |
| 1104 |
ValBool: false |
| 1105 |
ValInt: 123 |
| 1106 |
ValStr: "foobar" |
| 1107 |
})`) |
| 1108 |
|
| 1109 |
test.EqualStr(t, |
| 1110 |
td.SStruct(&MyStruct{}, td.StructFields{}).String(), |
| 1111 |
`SStruct(*td_test.MyStruct{ |
| 1112 |
Ptr: (*int)(<nil>) |
| 1113 |
ValBool: false |
| 1114 |
ValInt: 0 |
| 1115 |
ValStr: "" |
| 1116 |
})`) |
| 1117 |
|
| 1118 |
// Erroneous op |
| 1119 |
test.EqualStr(t, td.SStruct("test", nil).String(), "SStruct(<ERROR>)") |
| 1120 |
} |
| 1121 |
|
| 1122 |
func TestSStructPattern(t *testing.T) { |
| 1123 |
// Patterns are already fully tested in TestStructPatterns |
| 1124 |
|
| 1125 |
type paAnon struct { |
| 1126 |
alphaNum int |
| 1127 |
betaNum int |
| 1128 |
} |
| 1129 |
type paTest struct { |
| 1130 |
paAnon |
| 1131 |
Num int |
| 1132 |
} |
| 1133 |
|
| 1134 |
got := paTest{ |
| 1135 |
paAnon: paAnon{ |
| 1136 |
alphaNum: 1000, |
| 1137 |
betaNum: 2000, |
| 1138 |
}, |
| 1139 |
Num: 666, |
| 1140 |
} |
| 1141 |
|
| 1142 |
checkOK(t, got, |
| 1143 |
td.SStruct(paTest{}, |
| 1144 |
td.StructFields{ |
| 1145 |
"=*Num": td.Gte(666), // matches Num, alphaNum & betaNum |
| 1146 |
})) |
| 1147 |
|
| 1148 |
checkOK(t, got, |
| 1149 |
td.SStruct(paTest{}, |
| 1150 |
td.StructFields{ |
| 1151 |
"=~Num$": td.Gte(666), // matches Num, alphaNum & betaNum |
| 1152 |
})) |
| 1153 |
|
| 1154 |
checkOK(t, paTest{Num: 666}, |
| 1155 |
td.SStruct(paTest{}, |
| 1156 |
td.StructFields{ |
| 1157 |
"=~^Num": 666, // only matches Num |
| 1158 |
// remaining fields are tested as 0 |
| 1159 |
})) |
| 1160 |
} |
| 1161 |
|
| 1162 |
func TestSStructTypeBehind(t *testing.T) { |
| 1163 |
equalTypes(t, td.SStruct(MyStruct{}, nil), MyStruct{}) |
| 1164 |
equalTypes(t, td.SStruct(&MyStruct{}, nil), &MyStruct{}) |
| 1165 |
|
| 1166 |
// Erroneous op |
| 1167 |
equalTypes(t, td.SStruct("test", nil), nil) |
| 1168 |
} |