| 1 |
|
| 2 |
package check_test |
| 3 |
|
| 4 |
import ( |
| 5 |
"encoding/json" |
| 6 |
"errors" |
| 7 |
"fmt" |
| 8 |
"io" |
| 9 |
"math" |
| 10 |
"net" |
| 11 |
"os" |
| 12 |
"path/filepath" |
| 13 |
"reflect" |
| 14 |
"regexp" |
| 15 |
"testing" |
| 16 |
"time" |
| 17 |
|
| 18 |
"github.com/powerman/check" |
| 19 |
) |
| 20 |
|
| 21 |
type ( |
| 22 |
myInt int |
| 23 |
myString string |
| 24 |
myStruct struct { |
| 25 |
i int |
| 26 |
s string |
| 27 |
} |
| 28 |
myError struct{ s string } |
| 29 |
plainError struct{ msg string } |
| 30 |
targetErr struct{ msg string } |
| 31 |
ptrFieldErr struct{ msg string } |
| 32 |
fieldError struct { |
| 33 |
ns string |
| 34 |
tag string |
| 35 |
} |
| 36 |
fieldErrors []*fieldError |
| 37 |
causeError struct { |
| 38 |
msg string |
| 39 |
err error |
| 40 |
} |
| 41 |
) |
| 42 |
|
| 43 |
func (e myError) Error() string { return e.s } |
| 44 |
func (e *plainError) Error() string { return e.msg } |
| 45 |
func (e *targetErr) Error() string { return e.msg } |
| 46 |
func (e *ptrFieldErr) Error() string { return e.msg } |
| 47 |
func (e *fieldError) Error() string { return e.ns + ":" + e.tag } |
| 48 |
func (e *fieldError) Namespace() string { return e.ns } |
| 49 |
func (e *fieldError) Tag() string { return e.tag } |
| 50 |
func (fieldErrors) Error() string { return "fieldErrors" } |
| 51 |
|
| 52 |
func (e *causeError) Error() string { |
| 53 |
if e.msg != "" { |
| 54 |
return e.msg |
| 55 |
} |
| 56 |
if e.err != nil { |
| 57 |
return e.err.Error() |
| 58 |
} |
| 59 |
return "" |
| 60 |
} |
| 61 |
func (e *causeError) Cause() error { return e.err } |
| 62 |
func (e *causeError) Unwrap() error { return e.err } |
| 63 |
|
| 64 |
func pkgerrorsNew() error { |
| 65 |
return &plainError{msg: "EOF"} |
| 66 |
} |
| 67 |
|
| 68 |
func pkgerrorsWithStack(err error) error { |
| 69 |
return &causeError{err: err} |
| 70 |
} |
| 71 |
|
| 72 |
func pkgerrorsWrap(err error, msg string) error { |
| 73 |
return &causeError{msg: msg, err: err} |
| 74 |
} |
| 75 |
|
| 76 |
var ( |
| 77 |
|
| 78 |
zBool bool |
| 79 |
zInt int |
| 80 |
zInt8 int8 |
| 81 |
zInt16 int16 |
| 82 |
zInt32 int32 |
| 83 |
zInt64 int64 |
| 84 |
zUint uint |
| 85 |
zUint8 uint8 |
| 86 |
zUint16 uint16 |
| 87 |
zUint32 uint32 |
| 88 |
zUint64 uint64 |
| 89 |
zUintptr uintptr |
| 90 |
zFloat32 float32 |
| 91 |
zFloat64 float64 |
| 92 |
zArray0 [0]int |
| 93 |
zArray1 [1]int |
| 94 |
zChan chan int |
| 95 |
zFunc func() |
| 96 |
zIface any |
| 97 |
zMap map[int]int |
| 98 |
zSlice []int |
| 99 |
zString string |
| 100 |
zStruct struct{} |
| 101 |
|
| 102 |
zBoolPtr *bool |
| 103 |
zIntPtr *int |
| 104 |
zInt8Ptr *int8 |
| 105 |
zInt16Ptr *int16 |
| 106 |
zInt32Ptr *int32 |
| 107 |
zInt64Ptr *int64 |
| 108 |
zUintPtr *uint |
| 109 |
zUint8Ptr *uint8 |
| 110 |
zUint16Ptr *uint16 |
| 111 |
zUint32Ptr *uint32 |
| 112 |
zUint64Ptr *uint64 |
| 113 |
zUintptrPtr *uintptr |
| 114 |
zFloat32Ptr *float32 |
| 115 |
zFloat64Ptr *float64 |
| 116 |
zArray0Ptr *[0]int |
| 117 |
zArray1Ptr *[1]int |
| 118 |
zChanPtr *chan int |
| 119 |
zFuncPtr *func() |
| 120 |
zIfacePtr *any |
| 121 |
zMapPtr *map[int]int |
| 122 |
zSlicePtr *[]int |
| 123 |
zStringPtr *string |
| 124 |
zStructPtr *struct{} |
| 125 |
|
| 126 |
|
| 127 |
zMyInt myInt |
| 128 |
zMyString myString |
| 129 |
zJSON json.RawMessage |
| 130 |
zJSONPtr *json.RawMessage |
| 131 |
zTime time.Time |
| 132 |
|
| 133 |
vChan = make(chan int) |
| 134 |
vFunc = func() {} |
| 135 |
vIface any = zIntPtr |
| 136 |
vMap = make(map[int]int) |
| 137 |
vSlice = make([]int, 0) |
| 138 |
|
| 139 |
xBool = true |
| 140 |
xInt = -42 |
| 141 |
xInt8 int8 = -8 |
| 142 |
xInt16 int16 = -16 |
| 143 |
xInt32 int32 = -32 |
| 144 |
xInt64 int64 = -64 |
| 145 |
xUint uint = 42 |
| 146 |
xUint8 uint8 = 8 |
| 147 |
xUint16 uint16 = 16 |
| 148 |
xUint32 uint32 = 32 |
| 149 |
xUint64 uint64 = 64 |
| 150 |
xUintptr uintptr = 0xDEADBEEF |
| 151 |
xFloat32 float32 = -3.2 |
| 152 |
xFloat64 = 6.4 |
| 153 |
xArray1 = [1]int{-1} |
| 154 |
xChan = make(chan int, 1) |
| 155 |
xFunc = func() { panic(nil) } |
| 156 |
xIface io.Reader = os.Stdin |
| 157 |
xMap = map[int]int{2: -2, 3: -3, 5: -5} |
| 158 |
xSlice = []int{3, 5, 8} |
| 159 |
xString = "<nil>" |
| 160 |
xStruct = myStruct{i: 10, s: "ten"} |
| 161 |
|
| 162 |
xBoolPtr = &xBool |
| 163 |
xIntPtr = &xInt |
| 164 |
xInt8Ptr = &xInt8 |
| 165 |
xInt16Ptr = &xInt16 |
| 166 |
xInt32Ptr = &xInt32 |
| 167 |
xInt64Ptr = &xInt64 |
| 168 |
xUintPtr = &xUint |
| 169 |
xUint8Ptr = &xUint8 |
| 170 |
xUint16Ptr = &xUint16 |
| 171 |
xUint32Ptr = &xUint32 |
| 172 |
xUint64Ptr = &xUint64 |
| 173 |
xUintptrPtr = &xUintptr |
| 174 |
xFloat32Ptr = &xFloat32 |
| 175 |
xFloat64Ptr = &xFloat64 |
| 176 |
xArray1Ptr = &xArray1 |
| 177 |
xChanPtr = &xChan |
| 178 |
xFuncPtr = &xFunc |
| 179 |
xIfacePtr = &xIface |
| 180 |
xMapPtr = &xMap |
| 181 |
xSlicePtr = &xSlice |
| 182 |
xStringPtr = &xString |
| 183 |
xStructPtr = &xStruct |
| 184 |
|
| 185 |
xMyInt myInt = 31337 |
| 186 |
xMyString myString = "xyz" |
| 187 |
xJSON json.RawMessage = []byte(`{"s":"ten","i":10}`) |
| 188 |
xJSONPtr = &xJSON |
| 189 |
xTime = time.Now() |
| 190 |
xTimeEST = xTime.In(func() *time.Location { loc, _ := time.LoadLocation("EST"); return loc }()) |
| 191 |
) |
| 192 |
|
| 193 |
func TestTODO(tt *testing.T) { |
| 194 |
tt.Parallel() |
| 195 |
t := check.T(tt) |
| 196 |
|
| 197 |
t.True(true) |
| 198 |
|
| 199 |
t.TODO().True(false) |
| 200 |
t.True(true) |
| 201 |
|
| 202 |
todo := t.TODO() |
| 203 |
t.True(true) |
| 204 |
todo.True(false) |
| 205 |
t.True(true) |
| 206 |
if todo.True(false) { |
| 207 |
panic("never here") |
| 208 |
} |
| 209 |
|
| 210 |
t = t.TODO() |
| 211 |
t.True(false) |
| 212 |
|
| 213 |
t = t.TODO() |
| 214 |
t.True(false) |
| 215 |
} |
| 216 |
|
| 217 |
func TestError(tt *testing.T) { |
| 218 |
tt.Parallel() |
| 219 |
t := check.T(tt) |
| 220 |
t = t.TODO() |
| 221 |
t.Error() |
| 222 |
t.Error("message") |
| 223 |
t.Error("format: %q", "message") |
| 224 |
} |
| 225 |
|
| 226 |
func TestErrorf(tt *testing.T) { |
| 227 |
tt.Parallel() |
| 228 |
t := check.T(tt) |
| 229 |
t.TODO().Errorf("message %d", 1) |
| 230 |
t.TODO().Errorf("format: %s", "message") |
| 231 |
} |
| 232 |
|
| 233 |
func TestFatal(tt *testing.T) { |
| 234 |
tt.Parallel() |
| 235 |
t := check.T(tt) |
| 236 |
t.TODO().Fatal() |
| 237 |
t.TODO().Fatal("message") |
| 238 |
t.TODO().Fatal("format: %s", "message") |
| 239 |
} |
| 240 |
|
| 241 |
func TestFatalf(tt *testing.T) { |
| 242 |
tt.Parallel() |
| 243 |
t := check.T(tt) |
| 244 |
t.TODO().Fatalf("message") |
| 245 |
t.TODO().Fatalf("format: %s", "message") |
| 246 |
} |
| 247 |
|
| 248 |
func TestMustAll(tt *testing.T) { |
| 249 |
tt.Parallel() |
| 250 |
t := check.T(tt).MustAll() |
| 251 |
t.Nil(nil) |
| 252 |
t.NotNil(false) |
| 253 |
t.TODO().Nil(false) |
| 254 |
t.TODO().NotNil(nil) |
| 255 |
} |
| 256 |
|
| 257 |
func TestMust(tt *testing.T) { |
| 258 |
tt.Parallel() |
| 259 |
t := check.T(tt) |
| 260 |
t.Must(t.Nil(nil)) |
| 261 |
t.Must(t.NotNil(false)) |
| 262 |
} |
| 263 |
|
| 264 |
func bePositive(_ *check.TB, actual any) bool { |
| 265 |
return actual.(int) > 0 |
| 266 |
} |
| 267 |
|
| 268 |
func beEqual(_ *check.TB, actual, expected any) bool { |
| 269 |
return actual == expected |
| 270 |
} |
| 271 |
|
| 272 |
func TestCheckerShould(tt *testing.T) { |
| 273 |
tt.Parallel() |
| 274 |
t := check.T(tt) |
| 275 |
t.Should(bePositive, 42, "custom check!!!") |
| 276 |
t.Panic(func() { t.Should(bePositive, "42", "bad arg type") }) |
| 277 |
t.TODO().Should(func(_ *check.TB, _ any) bool { return false }, 42) |
| 278 |
t.Should(beEqual, 123, 123) |
| 279 |
t.TODO().Should(beEqual, 123, 124) |
| 280 |
t.Panic(func() { t.Should(func() {}, nil) }) |
| 281 |
t.Panic(func() { t.Should(bePositive) }) |
| 282 |
t.Panic(func() { t.Should(beEqual, nil) }) |
| 283 |
} |
| 284 |
|
| 285 |
func TestCheckerNilTrue(tt *testing.T) { |
| 286 |
tt.Parallel() |
| 287 |
t := check.T(tt) |
| 288 |
todo := t.TODO() |
| 289 |
|
| 290 |
|
| 291 |
t.Equal(zBool, false) |
| 292 |
t.True(zInt == 0) |
| 293 |
t.True(zInt8 == 0) |
| 294 |
t.True(zInt16 == 0) |
| 295 |
t.True(zInt32 == 0) |
| 296 |
t.True(zInt64 == 0) |
| 297 |
t.True(zUint == 0) |
| 298 |
t.True(zUint8 == 0) |
| 299 |
t.True(zUint16 == 0) |
| 300 |
t.True(zUint32 == 0) |
| 301 |
t.True(zUint64 == 0) |
| 302 |
t.True(zUintptr == 0) |
| 303 |
t.True(zFloat32 == 0) |
| 304 |
t.True(zFloat64 == 0) |
| 305 |
t.True(zArray0 == [0]int{}) |
| 306 |
t.True(zArray1 == [1]int{}) |
| 307 |
t.True(zChan == nil) |
| 308 |
t.True(zFunc == nil) |
| 309 |
t.True(zIface == nil) |
| 310 |
t.True(zMap == nil) |
| 311 |
t.True(zSlice == nil) |
| 312 |
t.True(zString == "") |
| 313 |
t.True(zStruct == struct{}{}) |
| 314 |
|
| 315 |
t.True(zBoolPtr == nil) |
| 316 |
t.True(zIntPtr == nil) |
| 317 |
t.True(zInt8Ptr == nil) |
| 318 |
t.True(zInt16Ptr == nil) |
| 319 |
t.True(zInt32Ptr == nil) |
| 320 |
t.True(zInt64Ptr == nil) |
| 321 |
t.True(zUintPtr == nil) |
| 322 |
t.True(zUint8Ptr == nil) |
| 323 |
t.True(zUint16Ptr == nil) |
| 324 |
t.True(zUint32Ptr == nil) |
| 325 |
t.True(zUint64Ptr == nil) |
| 326 |
t.True(zUintptrPtr == nil) |
| 327 |
t.True(zFloat32Ptr == nil) |
| 328 |
t.True(zFloat64Ptr == nil) |
| 329 |
t.True(zArray0Ptr == nil) |
| 330 |
t.True(zArray1Ptr == nil) |
| 331 |
t.True(zChanPtr == nil) |
| 332 |
t.True(zFuncPtr == nil) |
| 333 |
t.True(zIfacePtr == nil) |
| 334 |
t.True(zMapPtr == nil) |
| 335 |
t.True(zSlicePtr == nil) |
| 336 |
t.True(zStringPtr == nil) |
| 337 |
t.True(zStructPtr == nil) |
| 338 |
|
| 339 |
t.True(zMyInt == 0) |
| 340 |
t.True(zMyString == "") |
| 341 |
t.True(zJSON == nil) |
| 342 |
t.True(zJSONPtr == nil) |
| 343 |
t.True(zTime.Equal(time.Time{})) |
| 344 |
t.False(vChan == nil) |
| 345 |
t.False(vFunc == nil) |
| 346 |
t.False(vIface == nil) |
| 347 |
t.False(vMap == nil) |
| 348 |
t.False(vSlice == nil) |
| 349 |
|
| 350 |
|
| 351 |
vIfaceNil := vIface |
| 352 |
t.Nil(vIfaceNil) |
| 353 |
t.False(vIfaceNil == nil) |
| 354 |
vIfaceNil = nil |
| 355 |
t.Nil(vIfaceNil) |
| 356 |
t.True(vIfaceNil == nil) |
| 357 |
|
| 358 |
cases := []struct { |
| 359 |
equalNil bool |
| 360 |
isNil bool |
| 361 |
actual any |
| 362 |
}{ |
| 363 |
{true, true, nil}, |
| 364 |
{false, false, zBool}, |
| 365 |
{false, false, zInt}, |
| 366 |
{false, false, zInt8}, |
| 367 |
{false, false, zInt16}, |
| 368 |
{false, false, zInt32}, |
| 369 |
{false, false, zInt64}, |
| 370 |
{false, false, zUint}, |
| 371 |
{false, false, zUint8}, |
| 372 |
{false, false, zUint16}, |
| 373 |
{false, false, zUint32}, |
| 374 |
{false, false, zUint64}, |
| 375 |
{false, false, zUintptr}, |
| 376 |
{false, false, zFloat32}, |
| 377 |
{false, false, zFloat64}, |
| 378 |
{false, false, zArray0}, |
| 379 |
{false, false, zArray1}, |
| 380 |
{false, true, zChan}, |
| 381 |
{false, true, zFunc}, |
| 382 |
{true, true, zIface}, |
| 383 |
{false, true, zMap}, |
| 384 |
{false, true, zSlice}, |
| 385 |
{false, false, zString}, |
| 386 |
{false, false, zStruct}, |
| 387 |
|
| 388 |
{false, true, zBoolPtr}, |
| 389 |
{false, true, zIntPtr}, |
| 390 |
{false, true, zInt8Ptr}, |
| 391 |
{false, true, zInt16Ptr}, |
| 392 |
{false, true, zInt32Ptr}, |
| 393 |
{false, true, zInt64Ptr}, |
| 394 |
{false, true, zUintPtr}, |
| 395 |
{false, true, zUint8Ptr}, |
| 396 |
{false, true, zUint16Ptr}, |
| 397 |
{false, true, zUint32Ptr}, |
| 398 |
{false, true, zUint64Ptr}, |
| 399 |
{false, true, zUintptrPtr}, |
| 400 |
{false, true, zFloat32Ptr}, |
| 401 |
{false, true, zFloat64Ptr}, |
| 402 |
{false, true, zArray0Ptr}, |
| 403 |
{false, true, zArray1Ptr}, |
| 404 |
{false, true, zChanPtr}, |
| 405 |
{false, true, zFuncPtr}, |
| 406 |
{false, true, zIfacePtr}, |
| 407 |
{false, true, zMapPtr}, |
| 408 |
{false, true, zSlicePtr}, |
| 409 |
{false, true, zStringPtr}, |
| 410 |
{false, true, zStructPtr}, |
| 411 |
|
| 412 |
{false, false, zMyInt}, |
| 413 |
{false, false, zMyString}, |
| 414 |
{false, true, zJSON}, |
| 415 |
{false, true, zJSONPtr}, |
| 416 |
{false, false, zTime}, |
| 417 |
{false, false, vChan}, |
| 418 |
{false, false, vFunc}, |
| 419 |
{false, true, vIface}, |
| 420 |
{false, false, vMap}, |
| 421 |
{false, false, vSlice}, |
| 422 |
} |
| 423 |
for i, v := range cases { |
| 424 |
msg := fmt.Sprintf("case %d: %#v", i, v.actual) |
| 425 |
if v.equalNil { |
| 426 |
t.True(v.actual == nil, msg) |
| 427 |
} else { |
| 428 |
t.False(v.actual == nil, msg) |
| 429 |
} |
| 430 |
if v.isNil { |
| 431 |
t.Nil(v.actual, msg) |
| 432 |
todo.NotNil(v.actual, msg) |
| 433 |
} else { |
| 434 |
todo.Nil(v.actual, msg) |
| 435 |
t.NotNil(v.actual, msg) |
| 436 |
} |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
func TestCheckerEqual(tt *testing.T) { |
| 441 |
tt.Parallel() |
| 442 |
t := check.T(tt) |
| 443 |
todo := t.TODO() |
| 444 |
|
| 445 |
cases := []struct { |
| 446 |
comparable bool |
| 447 |
actual any |
| 448 |
actual2 any |
| 449 |
}{ |
| 450 |
{true, zBool, xBool}, |
| 451 |
{true, zInt, xInt}, |
| 452 |
{true, zInt8, xInt8}, |
| 453 |
{true, zInt16, xInt16}, |
| 454 |
{true, zInt32, xInt32}, |
| 455 |
{true, zInt64, xInt64}, |
| 456 |
{true, zUint, xUint}, |
| 457 |
{true, zUint8, xUint8}, |
| 458 |
{true, zUint16, xUint16}, |
| 459 |
{true, zUint32, xUint32}, |
| 460 |
{true, zUint64, xUint64}, |
| 461 |
{true, zUintptr, xUintptr}, |
| 462 |
{true, zFloat32, xFloat32}, |
| 463 |
{true, zFloat64, xFloat64}, |
| 464 |
{true, zArray0, xArray1}, |
| 465 |
{true, zArray1, xArray1}, |
| 466 |
{true, zChan, xChan}, |
| 467 |
{false, zFunc, xFunc}, |
| 468 |
{true, zIface, xIface}, |
| 469 |
{false, zMap, xMap}, |
| 470 |
{false, zSlice, xSlice}, |
| 471 |
{true, zString, xString}, |
| 472 |
{true, zStruct, xStruct}, |
| 473 |
{true, zBoolPtr, xBoolPtr}, |
| 474 |
{true, zIntPtr, xIntPtr}, |
| 475 |
{true, zInt8Ptr, xInt8Ptr}, |
| 476 |
{true, zInt16Ptr, xInt16Ptr}, |
| 477 |
{true, zInt32Ptr, xInt32Ptr}, |
| 478 |
{true, zInt64Ptr, xInt64Ptr}, |
| 479 |
{true, zUintPtr, xUintPtr}, |
| 480 |
{true, zUint8Ptr, xUint8Ptr}, |
| 481 |
{true, zUint16Ptr, xUint16Ptr}, |
| 482 |
{true, zUint32Ptr, xUint32Ptr}, |
| 483 |
{true, zUint64Ptr, xUint64Ptr}, |
| 484 |
{true, zUintptrPtr, xUintptrPtr}, |
| 485 |
{true, zFloat32Ptr, xFloat32Ptr}, |
| 486 |
{true, zFloat64Ptr, xFloat64Ptr}, |
| 487 |
{true, zArray0Ptr, xArray1Ptr}, |
| 488 |
{true, zArray1Ptr, xArray1Ptr}, |
| 489 |
{true, zChanPtr, xChanPtr}, |
| 490 |
{true, zFuncPtr, xFuncPtr}, |
| 491 |
{true, zIfacePtr, xIfacePtr}, |
| 492 |
{true, zMapPtr, xMapPtr}, |
| 493 |
{true, zSlicePtr, xSlicePtr}, |
| 494 |
{true, zStringPtr, xStringPtr}, |
| 495 |
{true, zStructPtr, xStructPtr}, |
| 496 |
{true, zMyInt, xMyInt}, |
| 497 |
{true, zMyString, xMyString}, |
| 498 |
{false, zJSON, xJSON}, |
| 499 |
{true, zJSONPtr, xJSONPtr}, |
| 500 |
{true, zTime, xTime}, |
| 501 |
{true, vChan, xChan}, |
| 502 |
{false, vFunc, xFunc}, |
| 503 |
{true, vIface, xIface}, |
| 504 |
{false, vMap, xMap}, |
| 505 |
{false, vSlice, xSlice}, |
| 506 |
{true, "one\ntwo\nend", "one\nTWO\nend"}, |
| 507 |
{true, io.EOF, io.ErrUnexpectedEOF}, |
| 508 |
{true, t, tt}, |
| 509 |
{true, int64(42), int32(42)}, |
| 510 |
{false, []byte{}, []byte(nil)}, |
| 511 |
} |
| 512 |
for _, v := range cases { |
| 513 |
if v.comparable { |
| 514 |
t.Equal(v.actual, v.actual) |
| 515 |
t.EQ(v.actual, v.actual) |
| 516 |
t.DeepEqual(v.actual, v.actual) |
| 517 |
todo.NotEqual(v.actual, v.actual) |
| 518 |
todo.NE(v.actual, v.actual) |
| 519 |
todo.NotDeepEqual(v.actual, v.actual) |
| 520 |
|
| 521 |
t.Equal(v.actual2, v.actual2) |
| 522 |
t.EQ(v.actual2, v.actual2) |
| 523 |
t.DeepEqual(v.actual2, v.actual2) |
| 524 |
todo.NotEqual(v.actual2, v.actual2) |
| 525 |
todo.NE(v.actual2, v.actual2) |
| 526 |
todo.NotDeepEqual(v.actual2, v.actual2) |
| 527 |
|
| 528 |
todo.Equal(v.actual, v.actual2) |
| 529 |
todo.EQ(v.actual, v.actual2) |
| 530 |
todo.DeepEqual(v.actual, v.actual2) |
| 531 |
t.NotEqual(v.actual, v.actual2) |
| 532 |
t.NE(v.actual, v.actual2) |
| 533 |
t.NotDeepEqual(v.actual, v.actual2) |
| 534 |
} else { |
| 535 |
t.Panic(func() { t.Equal(v.actual, v.actual) }) |
| 536 |
t.Panic(func() { t.EQ(v.actual, v.actual) }) |
| 537 |
t.Panic(func() { t.NotEqual(v.actual, v.actual) }) |
| 538 |
t.Panic(func() { t.NE(v.actual, v.actual) }) |
| 539 |
|
| 540 |
if reflect.TypeOf(v.actual).Kind() != reflect.Func { |
| 541 |
t.DeepEqual(v.actual, v.actual) |
| 542 |
todo.NotDeepEqual(v.actual, v.actual) |
| 543 |
t.DeepEqual(v.actual2, v.actual2) |
| 544 |
todo.NotDeepEqual(v.actual2, v.actual2) |
| 545 |
todo.DeepEqual(v.actual, v.actual2) |
| 546 |
t.NotDeepEqual(v.actual, v.actual2) |
| 547 |
} |
| 548 |
} |
| 549 |
} |
| 550 |
|
| 551 |
|
| 552 |
t.Equal(nil, nil) |
| 553 |
t.EQ(nil, nil) |
| 554 |
t.DeepEqual(nil, nil) |
| 555 |
todo.NotEqual(nil, nil) |
| 556 |
todo.NE(nil, nil) |
| 557 |
todo.NotDeepEqual(nil, nil) |
| 558 |
|
| 559 |
|
| 560 |
t.False(xTime == xTimeEST) |
| 561 |
t.Equal(xTime, xTimeEST) |
| 562 |
t.EQ(xTime, xTimeEST) |
| 563 |
t.DeepEqual(xTime, xTimeEST) |
| 564 |
todo.NotEqual(xTime, xTimeEST) |
| 565 |
todo.NE(xTime, xTimeEST) |
| 566 |
todo.NotDeepEqual(xTime, xTimeEST) |
| 567 |
|
| 568 |
|
| 569 |
type notComparable struct { |
| 570 |
s string |
| 571 |
is []int |
| 572 |
} |
| 573 |
cases = []struct { |
| 574 |
comparable bool |
| 575 |
actual any |
| 576 |
actual2 any |
| 577 |
}{ |
| 578 |
{true, io.EOF, errors.New("EOF")}, |
| 579 |
{true, &testing.T{}, &testing.T{}}, |
| 580 |
{false, []byte{2, 5}, []byte{2, 5}}, |
| 581 |
{false, notComparable{"a", []int{3, 5}}, notComparable{"a", []int{3, 5}}}, |
| 582 |
} |
| 583 |
for _, v := range cases { |
| 584 |
if v.comparable { |
| 585 |
t.False(v.actual == v.actual2) |
| 586 |
todo.Equal(v.actual, v.actual2) |
| 587 |
todo.EQ(v.actual, v.actual2) |
| 588 |
t.NotEqual(v.actual, v.actual2) |
| 589 |
t.NE(v.actual, v.actual2) |
| 590 |
} |
| 591 |
t.DeepEqual(v.actual, v.actual2) |
| 592 |
todo.NotDeepEqual(v.actual, v.actual2) |
| 593 |
} |
| 594 |
} |
| 595 |
|
| 596 |
func TestCheckerBytesEqual(tt *testing.T) { |
| 597 |
tt.Parallel() |
| 598 |
t := check.T(tt) |
| 599 |
todo := t.TODO() |
| 600 |
|
| 601 |
cases := []struct { |
| 602 |
equal bool |
| 603 |
actual []byte |
| 604 |
expected []byte |
| 605 |
}{ |
| 606 |
{true, nil, nil}, |
| 607 |
{true, []byte(nil), []byte(nil)}, |
| 608 |
{true, []byte{}, []byte{}}, |
| 609 |
{true, []byte(nil), nil}, |
| 610 |
{true, []byte{}, nil}, |
| 611 |
{true, []byte(nil), []byte{}}, |
| 612 |
{true, []byte{0}, []byte{0}}, |
| 613 |
{false, []byte{0}, nil}, |
| 614 |
{false, []byte{0}, []byte(nil)}, |
| 615 |
{false, []byte{0}, []byte{}}, |
| 616 |
{false, []byte{0}, []byte{0, 0}}, |
| 617 |
} |
| 618 |
for _, v := range cases { |
| 619 |
if v.equal { |
| 620 |
t.BytesEqual(v.actual, v.expected) |
| 621 |
todo.NotBytesEqual(v.actual, v.expected) |
| 622 |
} else { |
| 623 |
todo.BytesEqual(v.actual, v.expected) |
| 624 |
t.NotBytesEqual(v.actual, v.expected) |
| 625 |
} |
| 626 |
} |
| 627 |
} |
| 628 |
|
| 629 |
func TestCheckerMatch(tt *testing.T) { |
| 630 |
tt.Parallel() |
| 631 |
t := check.T(tt) |
| 632 |
todo := t.TODO() |
| 633 |
|
| 634 |
types := []struct { |
| 635 |
actual bool |
| 636 |
expected bool |
| 637 |
zero any |
| 638 |
}{ |
| 639 |
{true, false, nil}, |
| 640 |
{false, false, zBool}, |
| 641 |
{false, false, zInt}, |
| 642 |
{false, false, zInt8}, |
| 643 |
{false, false, zInt16}, |
| 644 |
{false, false, zInt32}, |
| 645 |
{false, false, zInt64}, |
| 646 |
{false, false, zUint}, |
| 647 |
{false, false, zUint8}, |
| 648 |
{false, false, zUint16}, |
| 649 |
{false, false, zUint32}, |
| 650 |
{false, false, zUint64}, |
| 651 |
{false, false, zUintptr}, |
| 652 |
{false, false, zFloat32}, |
| 653 |
{false, false, zFloat64}, |
| 654 |
{false, false, zArray0}, |
| 655 |
{false, false, zArray1}, |
| 656 |
{false, false, zChan}, |
| 657 |
{false, false, zFunc}, |
| 658 |
{false, false, zIface}, |
| 659 |
{false, false, zMap}, |
| 660 |
{false, false, zSlice}, |
| 661 |
{true, true, zString}, |
| 662 |
{false, false, zStruct}, |
| 663 |
{false, false, zBoolPtr}, |
| 664 |
{false, false, zIntPtr}, |
| 665 |
{false, false, zInt8Ptr}, |
| 666 |
{false, false, zInt16Ptr}, |
| 667 |
{false, false, zInt32Ptr}, |
| 668 |
{false, false, zInt64Ptr}, |
| 669 |
{false, false, zUintPtr}, |
| 670 |
{false, false, zUint8Ptr}, |
| 671 |
{false, false, zUint16Ptr}, |
| 672 |
{false, false, zUint32Ptr}, |
| 673 |
{false, false, zUint64Ptr}, |
| 674 |
{false, false, zUintptrPtr}, |
| 675 |
{false, false, zFloat32Ptr}, |
| 676 |
{false, false, zFloat64Ptr}, |
| 677 |
{false, false, zArray0Ptr}, |
| 678 |
{false, false, zArray1Ptr}, |
| 679 |
{false, false, zChanPtr}, |
| 680 |
{false, false, zFuncPtr}, |
| 681 |
{false, false, zIfacePtr}, |
| 682 |
{false, false, zMapPtr}, |
| 683 |
{false, false, zSlicePtr}, |
| 684 |
{false, false, zStringPtr}, |
| 685 |
{false, false, zStructPtr}, |
| 686 |
{false, false, zMyInt}, |
| 687 |
{true, false, zMyString}, |
| 688 |
{true, false, zJSON}, |
| 689 |
{false, false, zJSONPtr}, |
| 690 |
{true, false, zTime}, |
| 691 |
{true, false, time.Sunday}, |
| 692 |
{true, false, errors.New("")}, |
| 693 |
{true, false, []byte(nil)}, |
| 694 |
{true, false, []rune(nil)}, |
| 695 |
{true, true, regexp.MustCompile("")}, |
| 696 |
{false, false, (*regexp.Regexp)(nil)}, |
| 697 |
{false, false, regexp.Regexp{}}, |
| 698 |
} |
| 699 |
for i, va := range types { |
| 700 |
for j, ve := range types { |
| 701 |
msg := fmt.Sprintf("case %d/%d: %#v, %#v", i, j, va.zero, ve.zero) |
| 702 |
switch va.zero.(type) { |
| 703 |
case nil: |
| 704 |
todo.Match(va.zero, ve.zero, msg) |
| 705 |
default: |
| 706 |
if va.actual && ve.expected { |
| 707 |
t.Match(va.zero, ve.zero, msg) |
| 708 |
} else { |
| 709 |
t.Panic(func() { t.Match(va.zero, ve.zero) }, msg) |
| 710 |
} |
| 711 |
} |
| 712 |
} |
| 713 |
} |
| 714 |
|
| 715 |
cases := []struct { |
| 716 |
actual any |
| 717 |
regexMatch any |
| 718 |
regexNotMatch any |
| 719 |
}{ |
| 720 |
{"", `^$`, `.`}, |
| 721 |
{myString("Test"), regexp.MustCompile(`st$`), regexp.MustCompile(`ST$`)}, |
| 722 |
{[]byte(nil), `^$`, `nil`}, |
| 723 |
{[]byte("Test"), regexp.MustCompile(`st$`), regexp.MustCompile(`ST$`)}, |
| 724 |
{[]rune(nil), `^$`, `nil`}, |
| 725 |
{[]rune("Test"), regexp.MustCompile(`st$`), regexp.MustCompile(`ST$`)}, |
| 726 |
{zTime, `00:00:00`, `01:01:01`}, |
| 727 |
{time.Sunday, regexp.MustCompile(`^Sun`), regexp.MustCompile(`Sun$`)}, |
| 728 |
{errors.New(""), `^$`, `nil`}, |
| 729 |
{io.EOF, regexp.MustCompile(`^EO`), regexp.MustCompile(`EO$`)}, |
| 730 |
} |
| 731 |
for _, v := range cases { |
| 732 |
t.Match(v.actual, v.regexMatch) |
| 733 |
todo.Match(v.actual, v.regexNotMatch) |
| 734 |
} |
| 735 |
|
| 736 |
|
| 737 |
todo.Match(nil, ``) |
| 738 |
todo.Match(nil, regexp.MustCompile(``)) |
| 739 |
t.NotMatch(nil, ``) |
| 740 |
t.NotMatch(nil, regexp.MustCompile(``)) |
| 741 |
} |
| 742 |
|
| 743 |
func TestCheckerContains(tt *testing.T) { |
| 744 |
tt.Parallel() |
| 745 |
t := check.T(tt) |
| 746 |
|
| 747 |
failures := []struct { |
| 748 |
panic bool |
| 749 |
actual any |
| 750 |
expected any |
| 751 |
}{ |
| 752 |
{true, nil, nil}, |
| 753 |
{true, zBool, zBool}, |
| 754 |
{true, zInt, zInt}, |
| 755 |
{true, zInt8, zInt8}, |
| 756 |
{true, zInt16, zInt16}, |
| 757 |
{true, zInt32, zInt32}, |
| 758 |
{true, zInt64, zInt64}, |
| 759 |
{true, zUint, zUint}, |
| 760 |
{true, zUint8, zUint8}, |
| 761 |
{true, zUint16, zUint16}, |
| 762 |
{true, zUint32, zUint32}, |
| 763 |
{true, zUint64, zUint64}, |
| 764 |
{true, zUintptr, zUintptr}, |
| 765 |
{true, zFloat32, zFloat32}, |
| 766 |
{true, zFloat64, zFloat64}, |
| 767 |
{true, zArray0, zBool}, |
| 768 |
{false, zArray0, xInt}, |
| 769 |
{true, zArray1, zBool}, |
| 770 |
{false, zArray1, xInt}, |
| 771 |
{true, zChan, zChan}, |
| 772 |
{true, zFunc, zFunc}, |
| 773 |
{true, zIface, zIface}, |
| 774 |
{true, zMap, zBool}, |
| 775 |
{false, zMap, xInt}, |
| 776 |
{true, zSlice, zBool}, |
| 777 |
{false, zSlice, xInt}, |
| 778 |
{true, zString, zBool}, |
| 779 |
{false, zString, xString}, |
| 780 |
{true, zStruct, zStruct}, |
| 781 |
{true, zBoolPtr, zBoolPtr}, |
| 782 |
{true, zIntPtr, zIntPtr}, |
| 783 |
{true, zInt8Ptr, zInt8Ptr}, |
| 784 |
{true, zInt16Ptr, zInt16Ptr}, |
| 785 |
{true, zInt32Ptr, zInt32Ptr}, |
| 786 |
{true, zInt64Ptr, zInt64Ptr}, |
| 787 |
{true, zUintPtr, zUintPtr}, |
| 788 |
{true, zUint8Ptr, zUint8Ptr}, |
| 789 |
{true, zUint16Ptr, zUint16Ptr}, |
| 790 |
{true, zUint32Ptr, zUint32Ptr}, |
| 791 |
{true, zUint64Ptr, zUint64Ptr}, |
| 792 |
{true, zUintptrPtr, zUintptrPtr}, |
| 793 |
{true, zFloat32Ptr, zFloat32Ptr}, |
| 794 |
{true, zFloat64Ptr, zFloat64Ptr}, |
| 795 |
{true, zArray0Ptr, zArray0Ptr}, |
| 796 |
{true, zArray1Ptr, zArray1Ptr}, |
| 797 |
{true, zChanPtr, zChanPtr}, |
| 798 |
{true, zFuncPtr, zFuncPtr}, |
| 799 |
{true, zIfacePtr, zIfacePtr}, |
| 800 |
{true, zMapPtr, zMapPtr}, |
| 801 |
{true, zSlicePtr, zSlicePtr}, |
| 802 |
{true, zStringPtr, zStringPtr}, |
| 803 |
{true, zStructPtr, zStructPtr}, |
| 804 |
{true, zMyInt, zMyInt}, |
| 805 |
{true, zMyString, zBool}, |
| 806 |
{false, zMyString, xString}, |
| 807 |
{true, zJSON, zBool}, |
| 808 |
{false, zJSON, xUint8}, |
| 809 |
{true, zJSONPtr, zJSONPtr}, |
| 810 |
{true, zTime, zTime}, |
| 811 |
} |
| 812 |
for i, v := range failures { |
| 813 |
msg := fmt.Sprintf("case %d: %#v, %#v", i, v.actual, v.expected) |
| 814 |
if v.panic { |
| 815 |
t.Panic(func() { t.Contains(v.actual, v.expected) }, msg) |
| 816 |
} else { |
| 817 |
t.NotContains(v.actual, v.expected, msg) |
| 818 |
} |
| 819 |
} |
| 820 |
|
| 821 |
t.Contains("", "") |
| 822 |
t.Contains("Test", "") |
| 823 |
t.Contains(myString("Test"), "es") |
| 824 |
t.Contains([...]time.Time{zTime, xTime, xTimeEST}, xTime) |
| 825 |
t.Contains([]*time.Time{&zTime, &xTime, &xTimeEST}, &xTime) |
| 826 |
t.Contains([]byte("Test"), byte('e')) |
| 827 |
t.Contains([]rune("Test"), 'e') |
| 828 |
t.Contains(map[int]string{2: "two", 5: "five", 10: "ten"}, "five") |
| 829 |
t.Contains(map[string]int{"two": 2, "five": 5, "ten": 10}, 5) |
| 830 |
t.NotContains(map[string]int{"two": 2, "five": 5, "ten": 10}, 0) |
| 831 |
} |
| 832 |
|
| 833 |
func TestCheckerHasKey(tt *testing.T) { |
| 834 |
tt.Parallel() |
| 835 |
t := check.T(tt) |
| 836 |
|
| 837 |
failures := []struct { |
| 838 |
panic bool |
| 839 |
actual any |
| 840 |
expected any |
| 841 |
}{ |
| 842 |
{true, nil, nil}, |
| 843 |
{true, zBool, zBool}, |
| 844 |
{true, zInt, zInt}, |
| 845 |
{true, zInt8, zInt8}, |
| 846 |
{true, zInt16, zInt16}, |
| 847 |
{true, zInt32, zInt32}, |
| 848 |
{true, zInt64, zInt64}, |
| 849 |
{true, zUint, zUint}, |
| 850 |
{true, zUint8, zUint8}, |
| 851 |
{true, zUint16, zUint16}, |
| 852 |
{true, zUint32, zUint32}, |
| 853 |
{true, zUint64, zUint64}, |
| 854 |
{true, zUintptr, zUintptr}, |
| 855 |
{true, zFloat32, zFloat32}, |
| 856 |
{true, zFloat64, zFloat64}, |
| 857 |
{true, zArray0, zArray0}, |
| 858 |
{true, zArray1, zArray1}, |
| 859 |
{true, zChan, zChan}, |
| 860 |
{true, zFunc, zFunc}, |
| 861 |
{true, zIface, zIface}, |
| 862 |
{true, zMap, zBool}, |
| 863 |
{false, zMap, zInt}, |
| 864 |
{true, zSlice, zSlice}, |
| 865 |
{true, zString, zString}, |
| 866 |
{true, zStruct, zStruct}, |
| 867 |
{true, zBoolPtr, zBoolPtr}, |
| 868 |
{true, zIntPtr, zIntPtr}, |
| 869 |
{true, zInt8Ptr, zInt8Ptr}, |
| 870 |
{true, zInt16Ptr, zInt16Ptr}, |
| 871 |
{true, zInt32Ptr, zInt32Ptr}, |
| 872 |
{true, zInt64Ptr, zInt64Ptr}, |
| 873 |
{true, zUintPtr, zUintPtr}, |
| 874 |
{true, zUint8Ptr, zUint8Ptr}, |
| 875 |
{true, zUint16Ptr, zUint16Ptr}, |
| 876 |
{true, zUint32Ptr, zUint32Ptr}, |
| 877 |
{true, zUint64Ptr, zUint64Ptr}, |
| 878 |
{true, zUintptrPtr, zUintptrPtr}, |
| 879 |
{true, zFloat32Ptr, zFloat32Ptr}, |
| 880 |
{true, zFloat64Ptr, zFloat64Ptr}, |
| 881 |
{true, zArray0Ptr, zArray0Ptr}, |
| 882 |
{true, zArray1Ptr, zArray1Ptr}, |
| 883 |
{true, zChanPtr, zChanPtr}, |
| 884 |
{true, zFuncPtr, zFuncPtr}, |
| 885 |
{true, zIfacePtr, zIfacePtr}, |
| 886 |
{true, zMapPtr, zMapPtr}, |
| 887 |
{true, zSlicePtr, zSlicePtr}, |
| 888 |
{true, zStringPtr, zStringPtr}, |
| 889 |
{true, zStructPtr, zStructPtr}, |
| 890 |
{true, zMyInt, zMyInt}, |
| 891 |
{true, zMyString, zMyString}, |
| 892 |
{true, zJSON, zJSON}, |
| 893 |
{true, zJSONPtr, zJSONPtr}, |
| 894 |
{true, zTime, zTime}, |
| 895 |
} |
| 896 |
for i, v := range failures { |
| 897 |
msg := fmt.Sprintf("case %d: %#v, %#v", i, v.actual, v.expected) |
| 898 |
if v.panic { |
| 899 |
t.Panic(func() { t.HasKey(v.actual, v.expected) }, msg) |
| 900 |
} else { |
| 901 |
t.NotHasKey(v.actual, v.expected, msg) |
| 902 |
} |
| 903 |
} |
| 904 |
|
| 905 |
t.HasKey(map[int]string{2: "two", 5: "five", 10: "ten"}, 5) |
| 906 |
t.HasKey(map[string]int{"two": 2, "five": 5, "ten": 10}, "five") |
| 907 |
t.NotHasKey(map[string]int{"two": 2, "five": 5, "ten": 10}, "") |
| 908 |
} |
| 909 |
|
| 910 |
func TestCheckerSortEqual(tt *testing.T) { |
| 911 |
tt.Parallel() |
| 912 |
t := check.T(tt) |
| 913 |
todo := t.TODO() |
| 914 |
|
| 915 |
type point struct{ x, y int } |
| 916 |
|
| 917 |
|
| 918 |
t.SortEqual([]int{1, 2, 3}, []int{3, 1, 2}) |
| 919 |
todo.NotSortEqual([]int{1, 2, 3}, []int{3, 1, 2}) |
| 920 |
|
| 921 |
|
| 922 |
todo.SortEqual([]int{1, 1, 2}, []int{1, 2, 2}) |
| 923 |
t.NotSortEqual([]int{1, 1, 2}, []int{1, 2, 2}) |
| 924 |
t.SortEqual([]int{1, 1, 2}, []int{2, 1, 1}) |
| 925 |
todo.NotSortEqual([]int{1, 1, 2}, []int{2, 1, 1}) |
| 926 |
|
| 927 |
|
| 928 |
t.SortEqual([]int(nil), []int{}) |
| 929 |
todo.NotSortEqual([]int(nil), []int{}) |
| 930 |
t.SortEqual([]int(nil), []int(nil)) |
| 931 |
t.SortEqual([]int{}, []int{}) |
| 932 |
|
| 933 |
|
| 934 |
todo.SortEqual([]int{1, 2}, []int{1, 2, 3}) |
| 935 |
t.NotSortEqual([]int{1, 2}, []int{1, 2, 3}) |
| 936 |
|
| 937 |
|
| 938 |
t.SortEqual([3]int{1, 2, 3}, [3]int{3, 2, 1}) |
| 939 |
|
| 940 |
|
| 941 |
t.SortEqual( |
| 942 |
[]point{{1, 2}, {3, 4}}, |
| 943 |
[]point{{3, 4}, {1, 2}}, |
| 944 |
) |
| 945 |
todo.SortEqual( |
| 946 |
[]point{{1, 2}, {3, 4}}, |
| 947 |
[]point{{3, 4}, {1, 3}}, |
| 948 |
) |
| 949 |
t.NotSortEqual( |
| 950 |
[]point{{1, 2}, {3, 4}}, |
| 951 |
[]point{{3, 4}, {1, 3}}, |
| 952 |
) |
| 953 |
|
| 954 |
|
| 955 |
t.SortEqual([]any{1, "a", true}, []any{true, 1, "a"}) |
| 956 |
|
| 957 |
|
| 958 |
t.SortEqual([]time.Time{xTime}, []time.Time{xTimeEST}) |
| 959 |
|
| 960 |
|
| 961 |
t.PanicMatch(func() { t.SortEqual(42, []int{1}) }, "actual is not a slice or array") |
| 962 |
t.PanicMatch(func() { t.SortEqual(nil, []int{1}) }, "actual is not a slice or array") |
| 963 |
t.PanicMatch(func() { t.SortEqual([]int{1}, 42) }, "expected is not a slice or array") |
| 964 |
t.PanicMatch(func() { t.SortEqual([]int{1}, nil) }, "expected is not a slice or array") |
| 965 |
} |
| 966 |
|
| 967 |
func TestCheckerSubset(tt *testing.T) { |
| 968 |
tt.Parallel() |
| 969 |
t := check.T(tt) |
| 970 |
todo := t.TODO() |
| 971 |
|
| 972 |
|
| 973 |
t.Subset([]int{1, 2, 3}, []int{2, 1}) |
| 974 |
todo.NotSubset([]int{1, 2, 3}, []int{2, 1}) |
| 975 |
t.Subset([]int{1, 2, 3}, []int{}) |
| 976 |
t.Subset([]int{1, 2, 3}, []int(nil)) |
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
todo.Subset([]int{1}, []int{1, 1}) |
| 981 |
t.NotSubset([]int{1}, []int{1, 1}) |
| 982 |
t.Subset([]int{1, 1, 2}, []int{1, 1}) |
| 983 |
|
| 984 |
|
| 985 |
todo.Subset([]int{1, 2}, []int{3}) |
| 986 |
t.NotSubset([]int{1, 2}, []int{3}) |
| 987 |
|
| 988 |
|
| 989 |
t.Subset( |
| 990 |
map[string]int{"a": 1, "b": 2, "c": 3}, |
| 991 |
map[string]int{"a": 1, "b": 2}, |
| 992 |
) |
| 993 |
|
| 994 |
todo.Subset( |
| 995 |
map[string]int{"a": 1}, |
| 996 |
map[string]int{"a": 1, "b": 2}, |
| 997 |
) |
| 998 |
t.NotSubset( |
| 999 |
map[string]int{"a": 1}, |
| 1000 |
map[string]int{"a": 1, "b": 2}, |
| 1001 |
) |
| 1002 |
|
| 1003 |
todo.Subset( |
| 1004 |
map[string]int{"a": 1, "b": 99}, |
| 1005 |
map[string]int{"a": 1, "b": 2}, |
| 1006 |
) |
| 1007 |
t.NotSubset( |
| 1008 |
map[string]int{"a": 1, "b": 99}, |
| 1009 |
map[string]int{"a": 1, "b": 2}, |
| 1010 |
) |
| 1011 |
|
| 1012 |
t.Subset(map[string]int{"a": 1}, make(map[string]int)) |
| 1013 |
t.Subset(map[string]int{"a": 1}, map[string]int(nil)) |
| 1014 |
|
| 1015 |
|
| 1016 |
t.PanicMatch(func() { t.Subset(make(map[string]int), []int{1}) }, "actual is not a slice or array") |
| 1017 |
t.PanicMatch(func() { t.Subset([]int{1}, make(map[string]int)) }, "actual is not a map") |
| 1018 |
t.PanicMatch(func() { t.Subset([]int{1}, 42) }, "expected is not a slice, array or map") |
| 1019 |
t.PanicMatch(func() { t.Subset([]int{1}, nil) }, "expected is not a slice, array or map") |
| 1020 |
} |
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
func TestElemEqualUsesRegisteredChecker(tt *testing.T) { |
| 1032 |
t := check.T(tt) |
| 1033 |
t.Cleanup(check.ResetEqualCheckers) |
| 1034 |
|
| 1035 |
type fakeElem struct{ id int } |
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
check.RegisterEqualChecker(func(actual, expected any) (equal, ok bool) { |
| 1040 |
_, okA := actual.(fakeElem) |
| 1041 |
_, okB := expected.(fakeElem) |
| 1042 |
if !okA || !okB { |
| 1043 |
return false, false |
| 1044 |
} |
| 1045 |
return true, true |
| 1046 |
}) |
| 1047 |
|
| 1048 |
t.SortEqual([]fakeElem{{1}, {2}}, []fakeElem{{3}, {4}}) |
| 1049 |
t.Subset([]fakeElem{{1}}, []fakeElem{{99}}) |
| 1050 |
} |
| 1051 |
|
| 1052 |
func TestFileDirExists(tt *testing.T) { |
| 1053 |
tt.Parallel() |
| 1054 |
t := check.T(tt) |
| 1055 |
todo := t.TODO() |
| 1056 |
|
| 1057 |
dir := t.TempDir() |
| 1058 |
file := filepath.Join(dir, "file.txt") |
| 1059 |
t.Must(t.Nil(os.WriteFile(file, nil, 0o600))) |
| 1060 |
missing := filepath.Join(dir, "missing") |
| 1061 |
|
| 1062 |
t.FileExists(file) |
| 1063 |
todo.NotFileExists(file) |
| 1064 |
t.NotFileExists(dir) |
| 1065 |
todo.FileExists(dir) |
| 1066 |
t.NotFileExists(missing) |
| 1067 |
todo.FileExists(missing) |
| 1068 |
|
| 1069 |
t.DirExists(dir) |
| 1070 |
todo.NotDirExists(dir) |
| 1071 |
t.NotDirExists(file) |
| 1072 |
todo.DirExists(file) |
| 1073 |
t.NotDirExists(missing) |
| 1074 |
todo.DirExists(missing) |
| 1075 |
} |
| 1076 |
|
| 1077 |
func TestCheckerZero(tt *testing.T) { |
| 1078 |
tt.Parallel() |
| 1079 |
t := check.T(tt) |
| 1080 |
todo := t.TODO() |
| 1081 |
|
| 1082 |
cases := []struct { |
| 1083 |
zero any |
| 1084 |
notzero any |
| 1085 |
}{ |
| 1086 |
{zBool, xBool}, |
| 1087 |
{zInt, xInt}, |
| 1088 |
{zInt8, xInt8}, |
| 1089 |
{zInt16, xInt16}, |
| 1090 |
{zInt32, xInt32}, |
| 1091 |
{zInt64, xInt64}, |
| 1092 |
{zUint, xUint}, |
| 1093 |
{zUint8, xUint8}, |
| 1094 |
{zUint16, xUint16}, |
| 1095 |
{zUint32, xUint32}, |
| 1096 |
{zUint64, xUint64}, |
| 1097 |
{zUintptr, xUintptr}, |
| 1098 |
{zFloat32, xFloat32}, |
| 1099 |
{zFloat64, xFloat64}, |
| 1100 |
{zArray0, xArray1}, |
| 1101 |
{zArray1, xArray1}, |
| 1102 |
{zChan, xChan}, |
| 1103 |
{zFunc, xFunc}, |
| 1104 |
{zIface, xIface}, |
| 1105 |
{zMap, xMap}, |
| 1106 |
{zSlice, xSlice}, |
| 1107 |
{zString, xString}, |
| 1108 |
{zStruct, xStruct}, |
| 1109 |
{zBoolPtr, xBoolPtr}, |
| 1110 |
{zIntPtr, xIntPtr}, |
| 1111 |
{zInt8Ptr, xInt8Ptr}, |
| 1112 |
{zInt16Ptr, xInt16Ptr}, |
| 1113 |
{zInt32Ptr, xInt32Ptr}, |
| 1114 |
{zInt64Ptr, xInt64Ptr}, |
| 1115 |
{zUintPtr, xUintPtr}, |
| 1116 |
{zUint8Ptr, xUint8Ptr}, |
| 1117 |
{zUint16Ptr, xUint16Ptr}, |
| 1118 |
{zUint32Ptr, xUint32Ptr}, |
| 1119 |
{zUint64Ptr, xUint64Ptr}, |
| 1120 |
{zUintptrPtr, xUintptrPtr}, |
| 1121 |
{zFloat32Ptr, xFloat32Ptr}, |
| 1122 |
{zFloat64Ptr, xFloat64Ptr}, |
| 1123 |
{zArray0Ptr, xArray1Ptr}, |
| 1124 |
{zArray1Ptr, xArray1Ptr}, |
| 1125 |
{zChanPtr, xChanPtr}, |
| 1126 |
{zFuncPtr, xFuncPtr}, |
| 1127 |
{zIfacePtr, xIfacePtr}, |
| 1128 |
{zMapPtr, xMapPtr}, |
| 1129 |
{zSlicePtr, xSlicePtr}, |
| 1130 |
{zStringPtr, xStringPtr}, |
| 1131 |
{zStructPtr, xStructPtr}, |
| 1132 |
{zMyInt, xMyInt}, |
| 1133 |
{zMyString, xMyString}, |
| 1134 |
{zJSON, xJSON}, |
| 1135 |
{zJSONPtr, xJSONPtr}, |
| 1136 |
{zTime, xTime}, |
| 1137 |
{nil, vChan}, |
| 1138 |
{nil, vFunc}, |
| 1139 |
{vIface, xIface}, |
| 1140 |
{nil, vMap}, |
| 1141 |
{nil, vSlice}, |
| 1142 |
{[0][]int{}, [1][]int{{1}}}, |
| 1143 |
{[2][]int{nil, nil}, [2][]int{nil, {}}}, |
| 1144 |
{[2][2][2]int{1: {1: {1: 0}}}, [2][2][2]int{1: {1: {1: 1}}}}, |
| 1145 |
} |
| 1146 |
for i, v := range cases { |
| 1147 |
msg := fmt.Sprintf("case %d: %#v, %#v", i, v.zero, v.notzero) |
| 1148 |
t.Zero(v.zero, msg) |
| 1149 |
todo.Zero(v.notzero, msg) |
| 1150 |
t.NotZero(v.notzero, msg) |
| 1151 |
todo.NotZero(v.zero, msg) |
| 1152 |
} |
| 1153 |
|
| 1154 |
t.Zero(nil) |
| 1155 |
todo.NotZero(nil) |
| 1156 |
} |
| 1157 |
|
| 1158 |
func TestCheckerLen(tt *testing.T) { |
| 1159 |
tt.Parallel() |
| 1160 |
t := check.T(tt) |
| 1161 |
|
| 1162 |
cases := []struct { |
| 1163 |
panic bool |
| 1164 |
actual any |
| 1165 |
len int |
| 1166 |
}{ |
| 1167 |
{true, nil, 0}, |
| 1168 |
{true, zBool, 0}, |
| 1169 |
{true, zInt, 0}, |
| 1170 |
{true, zInt8, 0}, |
| 1171 |
{true, zInt16, 0}, |
| 1172 |
{true, zInt32, 0}, |
| 1173 |
{true, zInt64, 0}, |
| 1174 |
{true, zUint, 0}, |
| 1175 |
{true, zUint8, 0}, |
| 1176 |
{true, zUint16, 0}, |
| 1177 |
{true, zUint32, 0}, |
| 1178 |
{true, zUint64, 0}, |
| 1179 |
{true, zUintptr, 0}, |
| 1180 |
{true, zFloat32, 0}, |
| 1181 |
{true, zFloat64, 0}, |
| 1182 |
{false, zArray0, 1}, |
| 1183 |
{false, zArray1, 0}, |
| 1184 |
{false, zChan, 1}, |
| 1185 |
{true, zFunc, 0}, |
| 1186 |
{true, zIface, 0}, |
| 1187 |
{false, zMap, 1}, |
| 1188 |
{false, zSlice, 1}, |
| 1189 |
{false, zString, 1}, |
| 1190 |
{true, zStruct, 0}, |
| 1191 |
{true, zBoolPtr, 0}, |
| 1192 |
{true, zIntPtr, 0}, |
| 1193 |
{true, zInt8Ptr, 0}, |
| 1194 |
{true, zInt16Ptr, 0}, |
| 1195 |
{true, zInt32Ptr, 0}, |
| 1196 |
{true, zInt64Ptr, 0}, |
| 1197 |
{true, zUintPtr, 0}, |
| 1198 |
{true, zUint8Ptr, 0}, |
| 1199 |
{true, zUint16Ptr, 0}, |
| 1200 |
{true, zUint32Ptr, 0}, |
| 1201 |
{true, zUint64Ptr, 0}, |
| 1202 |
{true, zUintptrPtr, 0}, |
| 1203 |
{true, zFloat32Ptr, 0}, |
| 1204 |
{true, zFloat64Ptr, 0}, |
| 1205 |
|
| 1206 |
|
| 1207 |
{true, zChanPtr, 0}, |
| 1208 |
{true, zFuncPtr, 0}, |
| 1209 |
{true, zIfacePtr, 0}, |
| 1210 |
{true, zMapPtr, 0}, |
| 1211 |
{true, zSlicePtr, 0}, |
| 1212 |
{true, zStringPtr, 0}, |
| 1213 |
{true, zStructPtr, 0}, |
| 1214 |
{true, zMyInt, 0}, |
| 1215 |
{false, zMyString, 1}, |
| 1216 |
{false, zJSON, 1}, |
| 1217 |
{true, zJSONPtr, 0}, |
| 1218 |
{true, zTime, 0}, |
| 1219 |
} |
| 1220 |
for _, v := range cases { |
| 1221 |
t.Run("", func(tt *testing.T) { |
| 1222 |
tt.Parallel() |
| 1223 |
t := check.T(tt) |
| 1224 |
todo := t.TODO() |
| 1225 |
|
| 1226 |
if v.panic { |
| 1227 |
t.Panic(func() { t.Len(v.actual, v.len) }) |
| 1228 |
} else { |
| 1229 |
todo.Len(v.actual, v.len) |
| 1230 |
t.NotLen(v.actual, v.len) |
| 1231 |
} |
| 1232 |
}) |
| 1233 |
} |
| 1234 |
|
| 1235 |
todo := t.TODO() |
| 1236 |
|
| 1237 |
t.Len(zArray0, 0) |
| 1238 |
t.Len(zArray1, 1) |
| 1239 |
|
| 1240 |
c := make(chan int, 5) |
| 1241 |
t.Len(c, 0) |
| 1242 |
todo.NotLen(c, 0) |
| 1243 |
c <- 42 |
| 1244 |
t.Len(c, 1) |
| 1245 |
todo.NotLen(c, 1) |
| 1246 |
|
| 1247 |
m := make(map[string]int, 10) |
| 1248 |
t.Len(m, 0) |
| 1249 |
m["one"] = 1 |
| 1250 |
m["ten"] = 10 |
| 1251 |
t.Len(m, 2) |
| 1252 |
|
| 1253 |
t.Len(json.RawMessage("тест"), 8) |
| 1254 |
t.Len([]rune("тест"), 4) |
| 1255 |
|
| 1256 |
t.Len(myString("test"), 4) |
| 1257 |
t.Len("тест", 8) |
| 1258 |
} |
| 1259 |
|
| 1260 |
func TestCheckerOrdered(t *testing.T) { |
| 1261 |
t.Parallel() |
| 1262 |
cases := []struct { |
| 1263 |
panic bool |
| 1264 |
min any |
| 1265 |
mid any |
| 1266 |
max any |
| 1267 |
}{ |
| 1268 |
{true, nil, nil, nil}, |
| 1269 |
{true, zBool, xBool, xBool}, |
| 1270 |
{false, xInt, xInt + 1, xInt + 2}, |
| 1271 |
{false, xInt8, xInt8 + 1, xInt8 + 2}, |
| 1272 |
{false, xInt16, xInt16 + 1, xInt16 + 2}, |
| 1273 |
{false, xInt32, xInt32 + 1, xInt32 + 2}, |
| 1274 |
{false, xInt64, xInt64 + 1, xInt64 + 2}, |
| 1275 |
{false, xUint, xUint + 1, xUint + 2}, |
| 1276 |
{false, xUint8, xUint8 + 1, xUint8 + 2}, |
| 1277 |
{false, xUint16, xUint16 + 1, xUint16 + 2}, |
| 1278 |
{false, xUint32, xUint32 + 1, xUint32 + 2}, |
| 1279 |
{false, xUint64, xUint64 + 1, xUint64 + 2}, |
| 1280 |
{false, xUintptr, xUintptr + 1, xUintptr + 2}, |
| 1281 |
{false, xFloat32, xFloat32 + 1, xFloat32 + 2}, |
| 1282 |
{false, xFloat64, xFloat64 + 1, xFloat64 + 2}, |
| 1283 |
{true, zArray0, zArray0, zArray0}, |
| 1284 |
{true, zArray1, xArray1, xArray1}, |
| 1285 |
{true, zChan, xChan, xChan}, |
| 1286 |
{true, zFunc, xFunc, xFunc}, |
| 1287 |
{true, zIface, xIface, xIface}, |
| 1288 |
{true, zMap, xMap, xMap}, |
| 1289 |
{true, zSlice, xSlice, xSlice}, |
| 1290 |
{false, xString, xString + "1", xString + "2"}, |
| 1291 |
{true, zStruct, xStruct, xStruct}, |
| 1292 |
{true, zBoolPtr, xBoolPtr, xBoolPtr}, |
| 1293 |
{true, zIntPtr, xIntPtr, xIntPtr}, |
| 1294 |
{true, zInt8Ptr, xInt8Ptr, xInt8Ptr}, |
| 1295 |
{true, zInt16Ptr, xInt16Ptr, xInt16Ptr}, |
| 1296 |
{true, zInt32Ptr, xInt32Ptr, xInt32Ptr}, |
| 1297 |
{true, zInt64Ptr, xInt64Ptr, xInt64Ptr}, |
| 1298 |
{true, zUintPtr, xUintPtr, xUintPtr}, |
| 1299 |
{true, zUint8Ptr, xUint8Ptr, xUint8Ptr}, |
| 1300 |
{true, zUint16Ptr, xUint16Ptr, xUint16Ptr}, |
| 1301 |
{true, zUint32Ptr, xUint32Ptr, xUint32Ptr}, |
| 1302 |
{true, zUint64Ptr, xUint64Ptr, xUint64Ptr}, |
| 1303 |
{true, zUintptrPtr, xUintptrPtr, xUintptrPtr}, |
| 1304 |
{true, zFloat32Ptr, xFloat32Ptr, xFloat32Ptr}, |
| 1305 |
{true, zFloat64Ptr, xFloat64Ptr, xFloat64Ptr}, |
| 1306 |
{true, zArray0Ptr, zArray0Ptr, zArray0Ptr}, |
| 1307 |
{true, zArray1Ptr, xArray1Ptr, xArray1Ptr}, |
| 1308 |
{true, zChanPtr, xChanPtr, xChanPtr}, |
| 1309 |
{true, zFuncPtr, xFuncPtr, xFuncPtr}, |
| 1310 |
{true, zIfacePtr, xIfacePtr, xIfacePtr}, |
| 1311 |
{true, zMapPtr, xMapPtr, xMapPtr}, |
| 1312 |
{true, zSlicePtr, xSlicePtr, xSlicePtr}, |
| 1313 |
{true, zStringPtr, xStringPtr, xStringPtr}, |
| 1314 |
{true, zStructPtr, xStructPtr, xStructPtr}, |
| 1315 |
{false, xMyInt, xMyInt + 1, xMyInt + 2}, |
| 1316 |
{false, xMyString, xMyString + "1", xMyString + "2"}, |
| 1317 |
{true, xJSON, xJSON, xJSON}, |
| 1318 |
{true, xJSONPtr, xJSONPtr, xJSONPtr}, |
| 1319 |
{false, xTime, xTime.Add(time.Millisecond), xTime.Add(time.Second)}, |
| 1320 |
} |
| 1321 |
|
| 1322 |
t.Run("Less", func(tt *testing.T) { |
| 1323 |
tt.Parallel() |
| 1324 |
t := check.T(tt) |
| 1325 |
todo := t.TODO() |
| 1326 |
for _, v := range cases { |
| 1327 |
actual, expected := v.min, v.max |
| 1328 |
if v.panic { |
| 1329 |
t.Panic(func() { t.Less(actual, expected) }) |
| 1330 |
t.Panic(func() { t.LT(actual, expected) }) |
| 1331 |
t.Panic(func() { t.LessOrEqual(actual, expected) }) |
| 1332 |
t.Panic(func() { t.LE(actual, expected) }) |
| 1333 |
} else { |
| 1334 |
t.Less(actual, expected) |
| 1335 |
t.LT(actual, expected) |
| 1336 |
t.LessOrEqual(actual, expected) |
| 1337 |
t.LessOrEqual(actual, actual) |
| 1338 |
t.LE(actual, expected) |
| 1339 |
t.LE(actual, actual) |
| 1340 |
|
| 1341 |
actual, expected = expected, actual |
| 1342 |
todo.Less(actual, expected) |
| 1343 |
todo.LT(actual, expected) |
| 1344 |
todo.LessOrEqual(actual, expected) |
| 1345 |
todo.LE(actual, expected) |
| 1346 |
} |
| 1347 |
} |
| 1348 |
}) |
| 1349 |
|
| 1350 |
t.Run("Greater", func(tt *testing.T) { |
| 1351 |
tt.Parallel() |
| 1352 |
t := check.T(tt) |
| 1353 |
todo := t.TODO() |
| 1354 |
for _, v := range cases { |
| 1355 |
actual, expected := v.min, v.max |
| 1356 |
if v.panic { |
| 1357 |
t.Panic(func() { t.Greater(actual, expected) }) |
| 1358 |
t.Panic(func() { t.GT(actual, expected) }) |
| 1359 |
t.Panic(func() { t.GreaterOrEqual(actual, expected) }) |
| 1360 |
t.Panic(func() { t.GE(actual, expected) }) |
| 1361 |
} else { |
| 1362 |
todo.Greater(actual, expected) |
| 1363 |
todo.GT(actual, expected) |
| 1364 |
todo.GreaterOrEqual(actual, expected) |
| 1365 |
todo.GE(actual, expected) |
| 1366 |
|
| 1367 |
actual, expected = expected, actual |
| 1368 |
t.Greater(actual, expected) |
| 1369 |
t.GT(actual, expected) |
| 1370 |
t.GreaterOrEqual(actual, expected) |
| 1371 |
t.GreaterOrEqual(actual, actual) |
| 1372 |
t.GE(actual, expected) |
| 1373 |
t.GE(actual, actual) |
| 1374 |
} |
| 1375 |
} |
| 1376 |
}) |
| 1377 |
|
| 1378 |
t.Run("Between", func(tt *testing.T) { |
| 1379 |
tt.Parallel() |
| 1380 |
t := check.T(tt) |
| 1381 |
todo := t.TODO() |
| 1382 |
for _, v := range cases { |
| 1383 |
minimum, middle, maximum := v.min, v.mid, v.max |
| 1384 |
if v.panic { |
| 1385 |
t.Panic(func() { t.Between(middle, minimum, maximum) }) |
| 1386 |
t.Panic(func() { t.BetweenOrEqual(middle, minimum, maximum) }) |
| 1387 |
t.Panic(func() { t.NotBetween(minimum, middle, maximum) }) |
| 1388 |
t.Panic(func() { t.NotBetweenOrEqual(minimum, middle, maximum) }) |
| 1389 |
} else { |
| 1390 |
t.Between(middle, minimum, maximum) |
| 1391 |
t.BetweenOrEqual(middle, minimum, maximum) |
| 1392 |
t.BetweenOrEqual(middle, middle, maximum) |
| 1393 |
t.BetweenOrEqual(middle, minimum, middle) |
| 1394 |
todo.NotBetween(middle, minimum, maximum) |
| 1395 |
todo.NotBetweenOrEqual(middle, minimum, maximum) |
| 1396 |
todo.NotBetweenOrEqual(middle, middle, maximum) |
| 1397 |
todo.NotBetweenOrEqual(middle, minimum, middle) |
| 1398 |
t.NotBetween(minimum, middle, maximum) |
| 1399 |
t.NotBetween(maximum, minimum, middle) |
| 1400 |
t.NotBetweenOrEqual(minimum, middle, maximum) |
| 1401 |
t.NotBetweenOrEqual(maximum, minimum, middle) |
| 1402 |
todo.Between(minimum, middle, maximum) |
| 1403 |
todo.Between(maximum, minimum, middle) |
| 1404 |
todo.BetweenOrEqual(minimum, middle, maximum) |
| 1405 |
todo.BetweenOrEqual(maximum, minimum, middle) |
| 1406 |
} |
| 1407 |
} |
| 1408 |
}) |
| 1409 |
} |
| 1410 |
|
| 1411 |
func TestCheckerApprox(t *testing.T) { |
| 1412 |
t.Parallel() |
| 1413 |
cases := []struct { |
| 1414 |
panic bool |
| 1415 |
actual any |
| 1416 |
expected any |
| 1417 |
delta any |
| 1418 |
smape float64 |
| 1419 |
}{ |
| 1420 |
{true, nil, nil, nil, 0}, |
| 1421 |
{true, zBool, xBool, xBool, 0}, |
| 1422 |
{false, xInt, xInt + 5, 7, 10.0}, |
| 1423 |
{false, xInt8, xInt8 + 5, 7, 50.0}, |
| 1424 |
{false, xInt16, xInt16 + 5, 7, 20.0}, |
| 1425 |
{false, xInt32, xInt32 + 5, 7, 10.0}, |
| 1426 |
{false, xInt64, xInt64 + 5, 7, 5.0}, |
| 1427 |
{false, xUint, xUint + 5, uint(7), 6.0}, |
| 1428 |
{false, xUint8, xUint8 + 5, uint(7), 30.0}, |
| 1429 |
{false, xUint16, xUint16 + 5, uint(7), 20.0}, |
| 1430 |
{false, xUint32, xUint32 + 5, uint(7), 10.0}, |
| 1431 |
{false, xUint64, xUint64 + 5, uint(7), 5.0}, |
| 1432 |
{false, xUintptr, xUintptr + 5, uint(7), 0.0000001}, |
| 1433 |
{false, xFloat32, xFloat32 - 5, 7.0, 50.0}, |
| 1434 |
{false, xFloat64, xFloat64 + 5, 7.0, 33.0}, |
| 1435 |
{true, zArray0, zArray0, zArray0, 0}, |
| 1436 |
{true, zArray1, xArray1, xArray1, 0}, |
| 1437 |
{true, zChan, xChan, xChan, 0}, |
| 1438 |
{true, zFunc, xFunc, xFunc, 0}, |
| 1439 |
{true, zIface, xIface, xIface, 0}, |
| 1440 |
{true, zMap, xMap, xMap, 0}, |
| 1441 |
{true, zSlice, xSlice, xSlice, 0}, |
| 1442 |
{true, xString, xString, xString, 0}, |
| 1443 |
{true, zStruct, xStruct, xStruct, 0}, |
| 1444 |
{true, zBoolPtr, xBoolPtr, xBoolPtr, 0}, |
| 1445 |
{true, zIntPtr, xIntPtr, xIntPtr, 0}, |
| 1446 |
{true, zInt8Ptr, xInt8Ptr, xInt8Ptr, 0}, |
| 1447 |
{true, zInt16Ptr, xInt16Ptr, xInt16Ptr, 0}, |
| 1448 |
{true, zInt32Ptr, xInt32Ptr, xInt32Ptr, 0}, |
| 1449 |
{true, zInt64Ptr, xInt64Ptr, xInt64Ptr, 0}, |
| 1450 |
{true, zUintPtr, xUintPtr, xUintPtr, 0}, |
| 1451 |
{true, zUint8Ptr, xUint8Ptr, xUint8Ptr, 0}, |
| 1452 |
{true, zUint16Ptr, xUint16Ptr, xUint16Ptr, 0}, |
| 1453 |
{true, zUint32Ptr, xUint32Ptr, xUint32Ptr, 0}, |
| 1454 |
{true, zUint64Ptr, xUint64Ptr, xUint64Ptr, 0}, |
| 1455 |
{true, zUintptrPtr, xUintptrPtr, xUintptrPtr, 0}, |
| 1456 |
{true, zFloat32Ptr, xFloat32Ptr, xFloat32Ptr, 0}, |
| 1457 |
{true, zFloat64Ptr, xFloat64Ptr, xFloat64Ptr, 0}, |
| 1458 |
{true, zArray0Ptr, zArray0Ptr, zArray0Ptr, 0}, |
| 1459 |
{true, zArray1Ptr, xArray1Ptr, xArray1Ptr, 0}, |
| 1460 |
{true, zChanPtr, xChanPtr, xChanPtr, 0}, |
| 1461 |
{true, zFuncPtr, xFuncPtr, xFuncPtr, 0}, |
| 1462 |
{true, zIfacePtr, xIfacePtr, xIfacePtr, 0}, |
| 1463 |
{true, zMapPtr, xMapPtr, xMapPtr, 0}, |
| 1464 |
{true, zSlicePtr, xSlicePtr, xSlicePtr, 0}, |
| 1465 |
{true, zStringPtr, xStringPtr, xStringPtr, 0}, |
| 1466 |
{true, zStructPtr, xStructPtr, xStructPtr, 0}, |
| 1467 |
{false, xMyInt, xMyInt + 5, 7, 0.01}, |
| 1468 |
{true, xMyString, xMyString, xMyString, 0}, |
| 1469 |
{true, xJSON, xJSON, xJSON, 0}, |
| 1470 |
{true, xJSONPtr, xJSONPtr, xJSONPtr, 0}, |
| 1471 |
{false, xTime, xTime.Add(5 * time.Second), 7 * time.Second, 0}, |
| 1472 |
} |
| 1473 |
|
| 1474 |
t.Run("Delta", func(tt *testing.T) { |
| 1475 |
tt.Parallel() |
| 1476 |
t := check.T(tt) |
| 1477 |
todo := t.TODO() |
| 1478 |
for _, v := range cases { |
| 1479 |
if v.panic { |
| 1480 |
t.Panic(func() { t.InDelta(v.actual, v.expected, v.delta) }) |
| 1481 |
t.Panic(func() { t.NotInDelta(v.actual, v.expected, v.delta) }) |
| 1482 |
} else { |
| 1483 |
t.InDelta(v.actual, v.expected, v.delta) |
| 1484 |
t.InDelta(v.expected, v.actual, v.delta) |
| 1485 |
todo.NotInDelta(v.actual, v.expected, v.delta) |
| 1486 |
todo.NotInDelta(v.expected, v.actual, v.delta) |
| 1487 |
t.NotInDelta(v.actual, v.expected, half(v.delta)) |
| 1488 |
t.NotInDelta(v.expected, v.actual, half(v.delta)) |
| 1489 |
todo.InDelta(v.actual, v.expected, half(v.delta)) |
| 1490 |
todo.InDelta(v.expected, v.actual, half(v.delta)) |
| 1491 |
} |
| 1492 |
} |
| 1493 |
}) |
| 1494 |
|
| 1495 |
t.Run("SMAPE", func(tt *testing.T) { |
| 1496 |
tt.Parallel() |
| 1497 |
t := check.T(tt) |
| 1498 |
todo := t.TODO() |
| 1499 |
for _, v := range cases { |
| 1500 |
if v.panic || v.smape == 0 { |
| 1501 |
t.Panic(func() { t.InSMAPE(v.actual, v.expected, v.smape) }) |
| 1502 |
t.Panic(func() { t.NotInSMAPE(v.actual, v.expected, v.smape) }) |
| 1503 |
} else { |
| 1504 |
t.InSMAPE(v.actual, v.expected, v.smape) |
| 1505 |
t.InSMAPE(v.expected, v.actual, v.smape) |
| 1506 |
todo.NotInSMAPE(v.actual, v.expected, v.smape) |
| 1507 |
todo.NotInSMAPE(v.expected, v.actual, v.smape) |
| 1508 |
t.NotInSMAPE(v.actual, v.expected, half(v.smape).(float64)) |
| 1509 |
t.NotInSMAPE(v.expected, v.actual, half(v.smape).(float64)) |
| 1510 |
todo.InSMAPE(v.actual, v.expected, half(v.smape).(float64)) |
| 1511 |
todo.InSMAPE(v.expected, v.actual, half(v.smape).(float64)) |
| 1512 |
} |
| 1513 |
} |
| 1514 |
|
| 1515 |
t.InSMAPE(0, 0, 0.5) |
| 1516 |
t.InSMAPE(0.0, 0.0, 0.5) |
| 1517 |
}) |
| 1518 |
} |
| 1519 |
|
| 1520 |
func TestCheckerApproxOverflow(t *testing.T) { |
| 1521 |
t.Parallel() |
| 1522 |
|
| 1523 |
|
| 1524 |
|
| 1525 |
|
| 1526 |
type overflowCase struct { |
| 1527 |
actual any |
| 1528 |
expected any |
| 1529 |
delta any |
| 1530 |
inDelta bool |
| 1531 |
} |
| 1532 |
|
| 1533 |
overflowCases := []overflowCase{ |
| 1534 |
|
| 1535 |
{uint(0), uint(0), uint(1), true}, |
| 1536 |
{uint(1), uint(0), uint(1), true}, |
| 1537 |
{uint64(math.MaxUint64), uint64(math.MaxUint64), uint64(1), true}, |
| 1538 |
{uint64(math.MaxUint64), uint64(0), uint64(1), false}, |
| 1539 |
|
| 1540 |
{int64(math.MaxInt64), int64(math.MaxInt64 - 1), int64(2), true}, |
| 1541 |
{int64(math.MinInt64), int64(math.MinInt64 + 1), int64(2), true}, |
| 1542 |
ta |
| 1543 |
{int64(math.MaxInt64), int64(math.MinInt64), int64(1), false}, |
| 1544 |
{int64(math.MinInt64), int64(math.MaxInt64), int64(1), false}, |
| 1545 |
} |
| 1546 |
|
| 1547 |
for _, v := range overflowCases { |
| 1548 |
t.Run("", func(tt *testing.T) { |
| 1549 |
tt.Parallel() |
| 1550 |
c := check.T(tt) |
| 1551 |
if v.inDelta { |
| 1552 |
c.InDelta(v.actual, v.expected, v.delta) |
| 1553 |
c.InDelta(v.expected, v.actual, v.delta) |
| 1554 |
} else { |
| 1555 |
c.NotInDelta(v.actual, v.expected, v.delta) |
| 1556 |
c.NotInDelta(v.expected, v.actual, v.delta) |
| 1557 |
} |
| 1558 |
}) |
| 1559 |
} |
| 1560 |
} |
| 1561 |
|
| 1562 |
func half(v any) any { |
| 1563 |
if v, ok := v.(time.Duration); ok { |
| 1564 |
return v / 2 |
| 1565 |
} |
| 1566 |
switch val := reflect.ValueOf(v); val.Kind() { |
| 1567 |
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: |
| 1568 |
return val.Int() / 2 |
| 1569 |
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: |
| 1570 |
return val.Uint() / 2 |
| 1571 |
case reflect.Float32, reflect.Float64: |
| 1572 |
return val.Float() / 2 |
| 1573 |
case reflect.Complex128, reflect.Complex64, |
| 1574 |
|
| 1575 |
reflect.Array, reflect.Slice, reflect.Map, reflect.Struct, reflect.Bool, reflect.String, |
| 1576 |
reflect.Chan, reflect.Func, reflect.Interface, reflect.Invalid, |
| 1577 |
reflect.Pointer, reflect.UnsafePointer: |
| 1578 |
} |
| 1579 |
panic(fmt.Sprintf("can't get half from %#v", v)) |
| 1580 |
} |
| 1581 |
|
| 1582 |
func TestCheckerSubstring(t *testing.T) { |
| 1583 |
t.Parallel() |
| 1584 |
cases := []struct { |
| 1585 |
panic bool |
| 1586 |
actual any |
| 1587 |
prefix string |
| 1588 |
suffix string |
| 1589 |
}{ |
| 1590 |
{true, xBool, "", ""}, |
| 1591 |
{true, xInt, "", ""}, |
| 1592 |
{true, xInt8, "", ""}, |
| 1593 |
{true, xInt16, "", ""}, |
| 1594 |
{true, xInt32, "", ""}, |
| 1595 |
{true, xInt64, "", ""}, |
| 1596 |
{true, xUint, "", ""}, |
| 1597 |
{true, xUint8, "", ""}, |
| 1598 |
{true, xUint16, "", ""}, |
| 1599 |
{true, xUint32, "", ""}, |
| 1600 |
{true, xUint64, "", ""}, |
| 1601 |
{true, xUintptr, "", ""}, |
| 1602 |
{true, xFloat32, "", ""}, |
| 1603 |
{true, xFloat64, "", ""}, |
| 1604 |
{true, zArray0, "", ""}, |
| 1605 |
{true, xArray1, "", ""}, |
| 1606 |
{true, xChan, "", ""}, |
| 1607 |
{true, xFunc, "", ""}, |
| 1608 |
{true, xIface, "", ""}, |
| 1609 |
{true, xMap, "", ""}, |
| 1610 |
{true, xSlice, "", ""}, |
| 1611 |
{false, xString, "<ni", "il>"}, |
| 1612 |
{true, xStruct, "", ""}, |
| 1613 |
{true, xBoolPtr, "", ""}, |
| 1614 |
{true, xIntPtr, "", ""}, |
| 1615 |
{true, xInt8Ptr, "", ""}, |
| 1616 |
{true, xInt16Ptr, "", ""}, |
| 1617 |
{true, xInt32Ptr, "", ""}, |
| 1618 |
{true, xInt64Ptr, "", ""}, |
| 1619 |
{true, xUintPtr, "", ""}, |
| 1620 |
{true, xUint8Ptr, "", ""}, |
| 1621 |
{true, xUint16Ptr, "", ""}, |
| 1622 |
{true, xUint32Ptr, "", ""}, |
| 1623 |
{true, xUint64Ptr, "", ""}, |
| 1624 |
{true, xUintptrPtr, "", ""}, |
| 1625 |
{true, xFloat32Ptr, "", ""}, |
| 1626 |
{true, xFloat64Ptr, "", ""}, |
| 1627 |
{true, zArray0Ptr, "", ""}, |
| 1628 |
{true, xArray1Ptr, "", ""}, |
| 1629 |
{true, xChanPtr, "", ""}, |
| 1630 |
{true, xFuncPtr, "", ""}, |
| 1631 |
{true, xIfacePtr, "", ""}, |
| 1632 |
{true, xMapPtr, "", ""}, |
| 1633 |
{true, xSlicePtr, "", ""}, |
| 1634 |
{true, xStringPtr, "", ""}, |
| 1635 |
{true, xStructPtr, "", ""}, |
| 1636 |
{true, xMyInt, "", ""}, |
| 1637 |
{false, xMyString, "xy", "yz"}, |
| 1638 |
{false, xJSON, "{", "}"}, |
| 1639 |
{true, xJSONPtr, "", ""}, |
| 1640 |
{false, zTime, "0001-01-01", "UTC"}, |
| 1641 |
{false, []byte("String"), "Str", "ing"}, |
| 1642 |
{false, []rune("Symbol"), "Sym", "bol"}, |
| 1643 |
{false, time.Sunday, "Sun", "day"}, |
| 1644 |
{false, io.EOF, "EO", "OF"}, |
| 1645 |
} |
| 1646 |
|
| 1647 |
substrings := []struct { |
| 1648 |
prefix any |
| 1649 |
suffix any |
| 1650 |
}{ |
| 1651 |
{time.Sunday.String(), time.Monday.String()}, |
| 1652 |
{[]byte(time.Sunday.String()), []byte(time.Monday.String())}, |
| 1653 |
{[]rune(time.Sunday.String()), []rune(time.Monday.String())}, |
| 1654 |
{time.Sunday, time.Monday}, |
| 1655 |
{errors.New(time.Sunday.String()), errors.New(time.Monday.String())}, |
| 1656 |
} |
| 1657 |
|
| 1658 |
t.Run("HasPrefix", func(tt *testing.T) { |
| 1659 |
tt.Parallel() |
| 1660 |
t := check.T(tt) |
| 1661 |
todo := t.TODO() |
| 1662 |
|
| 1663 |
for i, v := range cases { |
| 1664 |
msg := fmt.Sprintf("case %d: %#v, %#v, %#v", i, v.actual, v.prefix, v.suffix) |
| 1665 |
if v.panic { |
| 1666 |
t.Panic(func() { t.HasPrefix(v.actual, v.prefix) }, msg) |
| 1667 |
t.Panic(func() { t.NotHasPrefix(v.actual, v.prefix) }, msg) |
| 1668 |
t.Panic(func() { t.HasPrefix("", v.actual) }, msg) |
| 1669 |
t.Panic(func() { t.NotHasPrefix("", v.actual) }, msg) |
| 1670 |
} else { |
| 1671 |
t.HasPrefix(v.actual, v.prefix, msg) |
| 1672 |
todo.HasPrefix(v.actual, v.suffix, msg) |
| 1673 |
t.NotHasPrefix(v.actual, v.suffix, msg) |
| 1674 |
todo.NotHasPrefix(v.actual, v.prefix, msg) |
| 1675 |
} |
| 1676 |
} |
| 1677 |
|
| 1678 |
for _, v := range substrings { |
| 1679 |
t.HasPrefix("Sunday Monday", v.prefix) |
| 1680 |
todo.NotHasPrefix("Sunday Monday", v.prefix) |
| 1681 |
} |
| 1682 |
|
| 1683 |
todo.HasPrefix(nil, "") |
| 1684 |
t.NotHasPrefix(nil, "") |
| 1685 |
todo.HasPrefix("", nil) |
| 1686 |
t.NotHasPrefix("", nil) |
| 1687 |
|
| 1688 |
t.HasPrefix("", "") |
| 1689 |
todo.NotHasPrefix("", "") |
| 1690 |
t.HasPrefix("x", "") |
| 1691 |
todo.NotHasPrefix("x", "") |
| 1692 |
}) |
| 1693 |
|
| 1694 |
t.Run("HasSuffix", func(tt *testing.T) { |
| 1695 |
tt.Parallel() |
| 1696 |
t := check.T(tt) |
| 1697 |
todo := t.TODO() |
| 1698 |
|
| 1699 |
for i, v := range cases { |
| 1700 |
msg := fmt.Sprintf("case %d: %#v, %#v, %#v", i, v.actual, v.suffix, v.suffix) |
| 1701 |
if v.panic { |
| 1702 |
t.Panic(func() { t.HasSuffix(v.actual, v.suffix) }, msg) |
| 1703 |
t.Panic(func() { t.NotHasSuffix(v.actual, v.suffix) }, msg) |
| 1704 |
t.Panic(func() { t.HasSuffix("", v.actual) }, msg) |
| 1705 |
t.Panic(func() { t.NotHasSuffix("", v.actual) }, msg) |
| 1706 |
} else { |
| 1707 |
t.HasSuffix(v.actual, v.suffix, msg) |
| 1708 |
todo.HasSuffix(v.actual, v.prefix, msg) |
| 1709 |
t.NotHasSuffix(v.actual, v.prefix, msg) |
| 1710 |
todo.NotHasSuffix(v.actual, v.suffix, msg) |
| 1711 |
} |
| 1712 |
} |
| 1713 |
|
| 1714 |
for _, v := range substrings { |
| 1715 |
t.HasSuffix("Sunday Monday", v.suffix) |
| 1716 |
todo.NotHasSuffix("Sunday Monday", v.suffix) |
| 1717 |
} |
| 1718 |
|
| 1719 |
todo.HasSuffix(nil, "") |
| 1720 |
t.NotHasSuffix(nil, "") |
| 1721 |
todo.HasSuffix("", nil) |
| 1722 |
t.NotHasSuffix("", nil) |
| 1723 |
|
| 1724 |
t.HasSuffix("", "") |
| 1725 |
todo.NotHasSuffix("", "") |
| 1726 |
t.HasSuffix("x", "") |
| 1727 |
todo.NotHasSuffix("x", "") |
| 1728 |
}) |
| 1729 |
} |
| 1730 |
|
| 1731 |
func TestJSONEqual(tt *testing.T) { |
| 1732 |
tt.Parallel() |
| 1733 |
t := check.T(tt) |
| 1734 |
todo := t.TODO() |
| 1735 |
|
| 1736 |
cases := []struct { |
| 1737 |
panic bool |
| 1738 |
json any |
| 1739 |
}{ |
| 1740 |
{false, nil}, |
| 1741 |
{true, zBool}, |
| 1742 |
{true, zInt}, |
| 1743 |
{true, zInt8}, |
| 1744 |
{true, zInt16}, |
| 1745 |
{true, zInt32}, |
| 1746 |
{true, zInt64}, |
| 1747 |
{true, zUint}, |
| 1748 |
{true, zUint8}, |
| 1749 |
{true, zUint16}, |
| 1750 |
{true, zUint32}, |
| •1751 |
{true, zUint64}, |
| 1752 |
{true, zUintptr}, |
| 1753 |
{true, zFloat32}, |
| 1754 |
{true, zFloat64}, |
| 1755 |
{true, zArray0}, |
| 1756 |
{true, zArray1}, |
| 1757 |
{true, zChan}, |
| 1758 |
{true, zFunc}, |
| 1759 |
{false, zIface}, |
| 1760 |
{true, zMap}, |
| 1761 |
{true, zSlice}, |
| 1762 |
{false, zString}, |
| 1763 |
{true, zStruct}, |
| 1764 |
{true, zBoolPtr}, |
| 1765 |
{true, zIntPtr}, |
| 1766 |
{true, zInt8Ptr}, |
| 1767 |
{true, zInt16Ptr}, |
| 1768 |
{true, zInt32Ptr}, |
| 1769 |
{true, zInt64Ptr}, |
| 1770 |
{true, zUintPtr}, |
| 1771 |
{true, zUint8Ptr}, |
| 1772 |
{true, zUint16Ptr}, |
| 1773 |
{true, zUint32Ptr}, |
| 1774 |
{true, zUint64Ptr}, |
| 1775 |
{true, zUintptrPtr}, |
| 1776 |
{true, zFloat32Ptr}, |
| 1777 |
{true, zFloat64Ptr}, |
| 1778 |
{true, zArray0Ptr}, |
| 1779 |
{true, zArray1Ptr}, |
| 1780 |
{true, zChanPtr}, |
| 1781 |
{true, zFuncPtr}, |
| 1782 |
{true, zIfacePtr}, |
| 1783 |
{true, zMapPtr}, |
| 1784 |
{true, zSlicePtr}, |
| 1785 |
{true, zStringPtr}, |
| 1786 |
{true, zStructPtr}, |
| 1787 |
{true, zMyInt}, |
| 1788 |
{false, zMyString}, |
| 1789 |
{false, zJSON}, |
| 1790 |
{false, zJSONPtr}, |
| 1791 |
{true, zTime}, |
| 1792 |
{false, []byte(nil)}, |
| 1793 |
{false, []byte{}}, |
| 1794 |
} |
| 1795 |
for i, v := range cases { |
| 1796 |
if v.panic { |
| 1797 |
t.Panic(func() { t.JSONEqual(v.json, `{}`, i) }) |
| 1798 |
t.Panic(func() { t.JSONEqual(`{}`, v.json) }) |
| 1799 |
} else { |
| 1800 |
todo.JSONEqual(v.json, v.json) |
| 1801 |
} |
| 1802 |
} |
| 1803 |
|
| 1804 |
invalid := `{"a":1,"b":[2]` |
| 1805 |
invalidRaw := json.RawMessage(invalid) |
| 1806 |
todo.JSONEqual(invalid, invalid) |
| 1807 |
todo.JSONEqual([]byte(invalid), []byte(invalid)) |
| 1808 |
todo.JSONEqual(&invalidRaw, invalid) |
| 1809 |
todo.JSONEqual(&invalidRaw, invalid+"}") |
| 1810 |
todo.JSONEqual(invalidRaw, []byte(invalid)) |
| 1811 |
t.JSONEqual(invalidRaw, invalidRaw) |
| 1812 |
t.JSONEqual(&invalidRaw, &invalidRaw) |
| 1813 |
t.JSONEqual(&invalidRaw, invalidRaw) |
| 1814 |
t.JSONEqual(invalidRaw, &invalidRaw) |
| 1815 |
|
| 1816 |
validRaw := json.RawMessage(invalid + "}") |
| 1817 |
valid := []any{ |
| 1818 |
`{ "b" : [ 2],"a" :1} `, |
| 1819 |
[]byte(` { "b": [2 ],"a": 1}`), |
| 1820 |
validRaw, |
| 1821 |
&validRaw, |
| 1822 |
} |
| 1823 |
for _, actual := range valid { |
| 1824 |
for _, expected := range valid { |
| 1825 |
t.JSONEqual(actual, expected) |
| 1826 |
} |
| 1827 |
} |
| 1828 |
} |
| 1829 |
|
| 1830 |
func TestHasType(tt *testing.T) { |
| 1831 |
tt.Parallel() |
| 1832 |
t := check.T(tt) |
| 1833 |
todo := t.TODO() |
| 1834 |
|
| 1835 |
vs := []any{ |
| 1836 |
zBool, |
| 1837 |
zInt, |
| 1838 |
zInt8, |
| 1839 |
zInt16, |
| 1840 |
zInt32, |
| 1841 |
zInt64, |
| 1842 |
zUint, |
| 1843 |
zUint8, |
| 1844 |
zUint16, |
| 1845 |
zUint32, |
| 1846 |
zUint64, |
| 1847 |
zUintptr, |
| 1848 |
zFloat32, |
| 1849 |
zFloat64, |
| 1850 |
zArray0, |
| 1851 |
zArray1, |
| 1852 |
zChan, |
| 1853 |
zFunc, |
| 1854 |
zIface, |
| 1855 |
zMap, |
| 1856 |
zSlice, |
| 1857 |
zString, |
| 1858 |
zStruct, |
| 1859 |
zBoolPtr, |
| 1860 |
zIntPtr, |
| 1861 |
zInt8Ptr, |
| 1862 |
zInt16Ptr, |
| 1863 |
zInt32Ptr, |
| 1864 |
zInt64Ptr, |
| 1865 |
zUintPtr, |
| 1866 |
zUint8Ptr, |
| 1867 |
zUint16Ptr, |
| 1868 |
zUint32Ptr, |
| 1869 |
zUint64Ptr, |
| 1870 |
zUintptrPtr, |
| 1871 |
zFloat32Ptr, |
| 1872 |
zFloat64Ptr, |
| 1873 |
zArray0Ptr, |
| 1874 |
zArray1Ptr, |
| 1875 |
zChanPtr, |
| 1876 |
zFuncPtr, |
| 1877 |
zIfacePtr, |
| 1878 |
zMapPtr, |
| 1879 |
zSlicePtr, |
| 1880 |
zStringPtr, |
| 1881 |
zStructPtr, |
| 1882 |
zMyInt, |
| 1883 |
zMyString, |
| 1884 |
zJSON, |
| 1885 |
zJSONPtr, |
| 1886 |
zTime, |
| 1887 |
} |
| 1888 |
for i, actual := range vs { |
| 1889 |
for j, expected := range vs { |
| 1890 |
if i == j { |
| 1891 |
t.HasType(actual, expected) |
| 1892 |
todo.NotHasType(actual, expected) |
| 1893 |
} else { |
| 1894 |
t.NotHasType(actual, expected) |
| 1895 |
todo.HasType(actual, expected) |
| 1896 |
} |
| 1897 |
} |
| 1898 |
} |
| 1899 |
|
| 1900 |
t.HasType(vChan, zChan) |
| 1901 |
t.HasType(vFunc, zFunc) |
| 1902 |
t.HasType(vIface, zIntPtr) |
| 1903 |
t.HasType(vMap, zMap) |
| 1904 |
t.HasType(vSlice, zSlice) |
| 1905 |
var reader io.Reader |
| 1906 |
t.HasType(reader, nil) |
| 1907 |
t.HasType(&reader, (*io.Reader)(nil)) |
| 1908 |
t.NotHasType(&reader, nil) |
| 1909 |
t.HasType(os.Stdin, (*os.File)(nil)) |
| 1910 |
t.NotHasType(os.Stdin, &reader) |
| 1911 |
t.HasType(true, zBool) |
| 1912 |
t.HasType(42, zInt) |
| 1913 |
t.HasType("test", zString) |
| 1914 |
t.HasType([]byte("test"), []byte(nil)) |
| 1915 |
t.HasType([]byte("test"), []byte{}) |
| 1916 |
t.HasType(new(int), zIntPtr) |
| 1917 |
t.NotHasType(json.RawMessage([]byte("test")), []byte("test")) |
| 1918 |
} |
| 1919 |
|
| 1920 |
func TestCheckers(t *testing.T) { |
| 1921 |
t.Parallel() |
| 1922 |
t.Run("Err", func(tt *testing.T) { |
| 1923 |
tt.Parallel() |
| 1924 |
t := check.T(tt) |
| 1925 |
|
| 1926 |
cases := []struct { |
| 1927 |
err bool |
| 1928 |
deepEqual bool |
| 1929 |
equal bool |
| 1930 |
actual error |
| 1931 |
expected error |
| 1932 |
}{ |
| 1933 |
{true, true, true, nil, nil}, |
| 1934 |
|
| 1935 |
|
| 1936 |
{false, false, false, (*net.OpError)(nil), nil}, |
| 1937 |
{false, false, false, nil, (*net.OpError)(nil)}, |
| 1938 |
{true, true, true, (*net.OpError)(nil), (*net.OpError)(nil)}, |
| 1939 |
{true, true, false, &net.OpError{}, &net.OpError{}}, |
| 1940 |
{true, true, true, io.EOF, io.EOF}, |
| 1941 |
{true, true, false, io.EOF, errors.New("EOF")}, |
| 1942 |
{false, false, false, pkgerrorsNew(), io.EOF}, |
| 1943 |
{false, false, false, pkgerrorsNew(), errors.New("EOF")}, |
| 1944 |
{true, true, false, pkgerrorsNew(), pkgerrorsNew()}, |
| 1945 |
{true, false, false, pkgerrorsWithStack(io.EOF), io.EOF}, |
| 1946 |
{true, false, false, pkgerrorsWrap(io.EOF, "wrapped"), io.EOF}, |
| 1947 |
{true, false, false, pkgerrorsWrap(io.EOF, "wrapped"), errors.New("EOF")}, |
| 1948 |
{true, false, false, pkgerrorsWrap(pkgerrorsWrap(io.EOF, "wrapped"), "wrapped2"), io.EOF}, |
| 1949 |
{true, false, false, fmt.Errorf("wrapped: %w", io.EOF), io.EOF}, |
| 1950 |
{true, false, false, fmt.Errorf("wrapped: %w", io.EOF), errors.New("EOF")}, |
| 1951 |
{false, false, false, fmt.Errorf("wrapped: %w", io.EOF), &myError{"EOF"}}, |
| 1952 |
{false, false, false, fmt.Errorf("wrapped: %w", &myError{"EOF"}), io.EOF}, |
| 1953 |
{true, false, false, fmt.Errorf("wrapped[]: %w %w", io.EOF, &myError{"EOF"}), io.EOF}, |
| 1954 |
{true, false, false, fmt.Errorf("wrapped[]: %w %w", &myError{"EOF"}, io.EOF), io.EOF}, |
| 1955 |
{true, false, false, fmt.Errorf("wrapped[]: %w %w", &myError{"EOF"}, io.EOF), &myError{"EOF"}}, |
| 1956 |
{false, false, false, fmt.Errorf("wrapped[]: %w %w", io.EOF, &myError{"EOF"}), &myError{"EOF"}}, |
| 1957 |
{true, false, false, fmt.Errorf("wrapped2: %w", fmt.Errorf("wrapped: %w", io.EOF)), io.EOF}, |
| 1958 |
{true, false, false, fmt.Errorf("wrapped2: %w", pkgerrorsWrap(io.EOF, "wrapped")), io.EOF}, |
| 1959 |
{true, false, false, pkgerrorsWrap(fmt.Errorf("wrapped: %w", io.EOF), "wrapped2"), io.EOF}, |
| 1960 |
{ |
| 1961 |
true, |
| 1962 |
false, |
| 1963 |
false, |
| 1964 |
pkgerrorsWrap( |
| 1965 |
pkgerrorsWrap( |
| 1966 |
fmt.Errorf("wrapped4: %w", fmt.Errorf("wrapped3: %w", pkgerrorsWrap(fmt.Errorf("wrapped: %w", io.EOF), "wrapped2"))), |
| 1967 |
"wrapped5", |
| 1968 |
), |
| 1969 |
"wrapped6", |
| 1970 |
), |
| 1971 |
io.EOF, |
| 1972 |
}, |
| 1973 |
{false, false, false, io.EOF, &myError{"EOF"}}, |
| 1974 |
} |
| 1975 |
for _, v := range cases { |
| 1976 |
t.Run("", func(tt *testing.T) { |
| 1977 |
tt.Parallel() |
| 1978 |
t := check.T(tt) |
| 1979 |
todo := t.TODO() |
| 1980 |
if v.err { |
| 1981 |
t.Err(v.actual, v.expected) |
| 1982 |
todo.NotErr(v.actual, v.expected) |
| 1983 |
} else { |
| 1984 |
todo.Err(v.actual, v.expected) |
| 1985 |
t.NotErr(v.actual, v.expected) |
| 1986 |
} |
| 1987 |
if v.equal { |
| 1988 |
t.Equal(v.actual, v.expected) |
| 1989 |
} else { |
| 1990 |
t.NotEqual(v.actual, v.expected) |
| 1991 |
} |
| 1992 |
if v.deepEqual { |
| 1993 |
t.DeepEqual(v.actual, v.expected) |
| 1994 |
} else { |
| 1995 |
t.NotDeepEqual(v.actual, v.expected) |
| 1996 |
} |
| 1997 |
}) |
| 1998 |
} |
| 1999 |
}) |
| 2000 |
|
| 2001 |
t.Run("Panic", func(tt *testing.T) { |
| 2002 |
tt.Parallel() |
| 2003 |
t := check.T(tt) |
| 2004 |
todo := t.TODO() |
| 2005 |
|
| 2006 |
todo.Panic(func() {}) |
| 2007 |
t.NotPanic(func() {}) |
| 2008 |
|
| 2009 |
t.Panic(func() { panic(nil) }) |
| 2010 |
todo.NotPanic(func() { panic(nil) }) |
| 2011 |
|
| 2012 |
t.Panic(func() { panic("") }) |
| 2013 |
t.Panic(func() { panic("oops") }) |
| 2014 |
t.Panic(func() { panic(t) }) |
| 2015 |
todo.NotPanic(func() { panic("") }) |
| 2016 |
todo.NotPanic(func() { panic("oops") }) |
| 2017 |
todo.NotPanic(func() { panic(t) }) |
| 2018 |
}) |
| 2019 |
|
| 2020 |
t.Run("PanicMatch", func(tt *testing.T) { |
| 2021 |
tt.Parallel() |
| 2022 |
t := check.T(tt) |
| 2023 |
todo := t.TODO() |
| 2024 |
|
| 2025 |
t.Panic(func() { t.PanicMatch(func() { panic(0) }, nil) }) |
| 2026 |
t.Panic(func() { t.PanicMatch(func() { panic(0) }, t) }) |
| 2027 |
t.NotPanic(func() { t.PanicMatch(func() { panic(0) }, `0`) }) |
| 2028 |
|
| 2029 |
todo.PanicMatch(func() {}, ``) |
| 2030 |
todo.PanicNotMatch(func() {}, ``) |
| 2031 |
todo.PanicMatch(func() {}, `test`) |
| 2032 |
todo.PanicNotMatch(func() {}, `test`) |
| 2033 |
|
| 2034 |
t.PanicMatch(func() { panic(nil) }, ``) |
| 2035 |
todo.PanicNotMatch(func() { panic(nil) }, ``) |
| 2036 |
t.PanicMatch(func() { panic(nil) }, `nil`) |
| 2037 |
todo.PanicNotMatch(func() { panic(nil) }, `nil`) |
| 2038 |
t.PanicNotMatch(func() { panic(nil) }, `test`) |
| 2039 |
todo.PanicMatch(func() { panic(nil) }, `test`) |
| 2040 |
|
| 2041 |
t.PanicMatch(func() { panic("") }, regexp.MustCompile(`^$`)) |
| 2042 |
t.PanicMatch(func() { panic("oops") }, `(?i)Oops`) |
| 2043 |
t.PanicMatch(func() { panic(t) }, `^&check.C{`) |
| 2044 |
t.PanicNotMatch(func() { panic("") }, regexp.MustCompile(`.`)) |
| 2045 |
t.PanicNotMatch(func() { panic("oops") }, `(?-i)Oops`) |
| 2046 |
todo.PanicNotMatch(func() { panic(t) }, `^&check.C{`) |
| 2047 |
}) |
| 2048 |
|
| 2049 |
t.Run("Implements", func(tt *testing.T) { |
| 2050 |
tt.Parallel() |
| 2051 |
t := check.T(tt) |
| 2052 |
|
| 2053 |
t.Implements(t, (*testing.TB)(nil)) |
| 2054 |
t.Implements(os.Stdin, (*io.Reader)(nil)) |
| 2055 |
t.Implements(os.Stdin, &xIface) |
| 2056 |
t.Implements(*os.Stdin, (*io.Reader)(nil)) |
| 2057 |
t.Implements(time.Time{}, (*fmt.Stringer)(nil)) |
| 2058 |
t.Implements(&time.Time{}, (*fmt.Stringer)(nil)) |
| 2059 |
t.NotImplements(os.Stdin, (*fmt.Stringer)(nil)) |
| 2060 |
t.NotImplements(&os.Stdin, (*io.Reader)(nil)) |
| 2061 |
t.NotImplements(new(int), (*io.Reader)(nil)) |
| 2062 |
}) |
| 2063 |
|
| 2064 |
t.Run("ErrIs", func(tt *testing.T) { |
| 2065 |
tt.Parallel() |
| 2066 |
t := check.T(tt) |
| 2067 |
todo := t.TODO() |
| 2068 |
|
| 2069 |
errA := errors.New("ERR_A") |
| 2070 |
errB := errors.New("ERR_B") |
| 2071 |
wrappedA := fmt.Errorf("wrapped: %w", errA) |
| 2072 |
|
| 2073 |
|
| 2074 |
t.ErrIs(wrappedA, errA) |
| 2075 |
todo.NotErrIs(wrappedA, errA) |
| 2076 |
t.NotErrIs(wrappedA, errB) |
| 2077 |
todo.ErrIs(wrappedA, errB) |
| 2078 |
|
| 2079 |
todo.ErrIs(errA, errors.New("ERR_A")) |
| 2080 |
t.NotErrIs(errA, errors.New("ERR_A")) |
| 2081 |
|
| 2082 |
t.ErrIs(nil, nil) |
| 2083 |
todo.NotErrIs(nil, nil) |
| 2084 |
todo.ErrIs(nil, io.EOF) |
| 2085 |
t.NotErrIs(nil, io.EOF) |
| 2086 |
todo.ErrIs(io.EOF, nil) |
| 2087 |
t.NotErrIs(io.EOF, nil) |
| 2088 |
}) |
| 2089 |
|
| 2090 |
t.Run("ErrAs", func(tt *testing.T) { |
| 2091 |
tt.Parallel() |
| 2092 |
t := check.T(tt) |
| 2093 |
todo := t.TODO() |
| 2094 |
|
| 2095 |
errA := &targetErr{msg: "ERR_A"} |
| 2096 |
errB := errors.New("ERR_B") |
| 2097 |
wrappedA := fmt.Errorf("wrapped: %w", errA) |
| 2098 |
|
| 2099 |
|
| 2100 |
var got1 *targetErr |
| 2101 |
t.ErrAs(wrappedA, &got1) |
| 2102 |
t.NotNil(got1) |
| 2103 |
t.Equal(got1.msg, "ERR_A") |
| 2104 |
|
| 2105 |
var got2 *targetErr |
| 2106 |
todo.ErrAs(errB, &got2) |
| 2107 |
t.Nil(got2) |
| 2108 |
t.NotErrAs(errB, &got2) |
| 2109 |
|
| 2110 |
|
| 2111 |
t.Panic(func() { t.ErrAs(errA, nil) }) |
| 2112 |
}) |
| 2113 |
|
| 2114 |
t.Run("NotErrAs", func(tt *testing.T) { |
| 2115 |
tt.Parallel() |
| 2116 |
t := check.T(tt) |
| 2117 |
todo := t.TODO() |
| 2118 |
|
| 2119 |
errB := errors.New("ERR_B") |
| 2120 |
|
| 2121 |
|
| 2122 |
var got1 *targetErr |
| 2123 |
todo.ErrAs(errB, &got1) |
| 2124 |
t.Nil(got1) |
| 2125 |
t.NotErrAs(errB, &got1) |
| 2126 |
t.Nil(got1) |
| 2127 |
|
| 2128 |
|
| 2129 |
var got2 *targetErr |
| 2130 |
todo.ErrAs(nil, &got2) |
| 2131 |
t.Nil(got2) |
| 2132 |
}) |
| 2133 |
|
| 2134 |
t.Run("ErrChar", func(tt *testing.T) { |
| 2135 |
tt.Parallel() |
| 2136 |
t := check.T(tt) |
| 2137 |
todo := t.TODO() |
| 2138 |
|
| 2139 |
|
| 2140 |
h. |
| 2141 |
|
| 2142 |
|
| 2143 |
|
| 2144 |
nd |
| 2145 |
|
| 2146 |
t.Err(errors.New("x"), errors.New("x")) |
| 2147 |
todo.NotErr(errors.New("x"), errors.New("x")) |
| 2148 |
|
| 2149 |
). |
| 2150 |
wrapped := fmt.Errorf("outer: %w", errors.New("inner")) |
| 2151 |
t.Err(wrapped, errors.New("inner")) |
| 2152 |
todo.NotErr(wrapped, errors.New("inner")) |
| 2153 |
|
| 2154 |
al |
| 2155 |
|
| 2156 |
p1 := &ptrFieldErr{msg: "same"} |
| 2157 |
p2 := &ptrFieldErr{msg: "same"} |
| 2158 |
t.Err(p1, p2) |
| 2159 |
todo.NotErr(p1, p2) |
| 2160 |
|
| 2161 |
l. |
| 2162 |
todo.Err(&ptrFieldErr{msg: "a"}, &ptrFieldErr{msg: "b"}) |
| 2163 |
t.NotErr(&ptrFieldErr{msg: "a"}, &ptrFieldErr{msg: "b"}) |
| 2164 |
|
| 2165 |
|
| 2166 |
t.Err(nil, nil) |
| 2167 |
todo.NotErr(nil, nil) |
| 2168 |
todo.Err(nil, io.EOF) |
| 2169 |
t.NotErr(nil, io.EOF) |
| 2170 |
}) |
| 2171 |
|
| 2172 |
t.Run("CheckFieldError", func(tt *testing.T) { |
| 2173 |
tt.Parallel() |
| 2174 |
t := check.T(tt) |
| 2175 |
todo := t.TODO() |
| 2176 |
|
| 2177 |
|
| 2178 |
fe1 := &fieldError{ns: "ns", tag: "tag"} |
| 2179 |
fe2 := &fieldError{ns: "ns", tag: "other"} |
| 2180 |
fe3 := &fieldError{ns: "other", tag: "tag"} |
| 2181 |
|
| 2182 |
|
| 2183 |
t.Err(fe1, fe1) |
| 2184 |
todo.NotErr(fe1, fe1) |
| 2185 |
|
| 2186 |
todo.Err(fe1, fe2) |
| 2187 |
t.NotErr(fe1, fe2) |
| 2188 |
|
| 2189 |
todo.Err(fe1, fe3) |
| 2190 |
t.NotErr(fe1, fe3) |
| 2191 |
|
| 2192 |
|
| 2193 |
slice1 := fieldErrors{ |
| 2194 |
{ns: "ns1", tag: "tag1"}, |
| 2195 |
{ns: "ns2", tag: "tag2"}, |
| 2196 |
} |
| 2197 |
slice2 := fieldErrors{ |
| 2198 |
{ns: "ns1", tag: "tag1"}, |
| 2199 |
{ns: "ns2", tag: "tag2"}, |
| 2200 |
} |
| 2201 |
slice3 := fieldErrors{ |
| 2202 |
{ns: "ns1", tag: "tag1"}, |
| 2203 |
} |
| 2204 |
|
| 2205 |
|
| 2206 |
t.Err(slice1, slice2) |
| 2207 |
todo.NotErr(slice1, slice2) |
| 2208 |
|
| 2209 |
t.Err(slice1, fieldErrors{ |
| 2210 |
{ns: "ns2", tag: "tag2"}, |
| 2211 |
{ns: "ns1", tag: "tag1"}, |
| 2212 |
}) |
| 2213 |
todo.NotErr(slice1, fieldErrors{ |
| 2214 |
{ns: "ns2", tag: "tag2"}, |
| 2215 |
{ns: "ns1", tag: "tag1"}, |
| 2216 |
}) |
| 2217 |
|
| 2218 |
t.Err(fieldErrors{ |
| 2219 |
{ns: "n1", tag: "t1"}, |
| 2220 |
{ns: "n2", tag: "t2"}, |
| 2221 |
{ns: "n1", tag: "t1"}, |
| 2222 |
}, fieldErrors{ |
| 2223 |
{ns: "n1", tag: "t1"}, |
| 2224 |
{ns: "n1", tag: "t1"}, |
| 2225 |
{ns: "n2", tag: "t2"}, |
| 2226 |
}) |
| 2227 |
todo.NotErr(fieldErrors{ |
| 2228 |
{ns: "n1", tag: "t1"}, |
| 2229 |
{ns: "n2", tag: "t2"}, |
| 2230 |
{ns: "n1", tag: "t1"}, |
| 2231 |
}, fieldErrors{ |
| 2232 |
{ns: "n1", tag: "t1"}, |
| 2233 |
{ns: "n1", tag: "t1"}, |
| 2234 |
{ns: "n2", tag: "t2"}, |
| 2235 |
}) |
| 2236 |
l. |
| 2237 |
todo.Err(slice1, slice3) |
| 2238 |
t.NotErr(slice1, slice3) |
| 2239 |
l. |
| 2240 |
todo.Err(slice1, fieldErrors{ |
| 2241 |
{ns: "ns1", tag: "tag1"}, |
| 2242 |
{ns: "ns2", tag: "DIFF"}, |
| 2243 |
}) |
| 2244 |
t.NotErr(slice1, fieldErrors{ |
| 2245 |
{ns: "ns1", tag: "tag1"}, |
| 2246 |
{ns: "ns2", tag: "DIFF"}, |
| 2247 |
}) |
| 2248 |
|
| 2249 |
|
| 2250 |
t.Err(fe1, fieldErrors{{ns: "ns", tag: "tag"}}) |
| 2251 |
todo.NotErr(fe1, fieldErrors{{ns: "ns", tag: "tag"}}) |
| 2252 |
|
| 2253 |
|
| 2254 |
t.Err(fieldErrors{}, fieldErrors{}) |
| 2255 |
todo.NotErr(fieldErrors{}, fieldErrors{}) |
| 2256 |
|
| 2257 |
|
| 2258 |
plain := errors.New("plain") |
| 2259 |
|
| 2260 |
|
| 2261 |
wrapped := fmt.Errorf("wrapped: %w", fe1) |
| 2262 |
t.Err(wrapped, fe1) |
| 2263 |
todo.NotErr(wrapped, fe1) |
| 2264 |
). |
| 2265 |
t.NotErr(wrapped, plain) |
| 2266 |
t.NotErr(plain, wrapped) |
| 2267 |
|
| 2268 |
r. |
| 2269 |
|
| 2270 |
t.NotErr(fe1, plain) |
| 2271 |
t.NotErr(plain, fe1) |
| 2272 |
}) |
| 2273 |
} |
| 2274 |
|
| 2275 |
type alwaysMatchChecker struct{} |
| 2276 |
|
| 2277 |
func (alwaysMatchChecker) Error() string { return "always" } |
| 2278 |
|
| 2279 |
|
| 2280 |
func TestResetErrCheckers(tt *testing.T) { |
| 2281 |
always := &alwaysMatchChecker{} |
| 2282 |
|
| 2283 |
|
| 2284 |
check.RegisterErrChecker(func(_, _ error) (equal, ok bool) { |
| 2285 |
return true, true |
| 2286 |
}) |
| 2287 |
|
| 2288 |
|
| 2289 |
tt.Run("custom_claimed", func(tt *testing.T) { |
| 2290 |
t := check.T(tt) |
| 2291 |
t.Err(always, io.EOF) |
| 2292 |
}) |
| 2293 |
|
| 2294 |
|
| 2295 |
check.ResetErrCheckers() |
| 2296 |
|
| 2297 |
l. |
| 2298 |
tt.Run("after_reset", func(tt *testing.T) { |
| 2299 |
t := check.T(tt) |
| 2300 |
todo := t.TODO() |
| 2301 |
todo.Err(always, io.EOF) |
| 2302 |
t.NotErr(always, io.EOF) |
| 2303 |
}) |
| 2304 |
|
| 2305 |
|
| 2306 |
check.RegisterErrChecker(check.CheckFieldError) |
| 2307 |
} |
| 2308 |
|
| 2309 |
us |
| 2310 |
|
| 2311 |
type fakeProto struct{} |
| 2312 |
|
| 2313 |
func (fakeProto) ProtoReflect() int { return 0 } |
| 2314 |
|
| 2315 |
type fakeGRPCErr struct{ msg string } |
| 2316 |
|
| 2317 |
func (f fakeGRPCErr) Error() string { return f.msg } |
| 2318 |
func (fakeGRPCErr) GRPCStatus() int { return 0 } |
| 2319 |
|
| 2320 |
|
| 2321 |
func TestDeepEqualProtoPanic(tt *testing.T) { |
| 2322 |
t := check.T(tt) |
| 2323 |
|
| 2324 |
|
| 2325 |
t.PanicMatch(func() { t.DeepEqual(fakeProto{}, fakeProto{}) }, |
| 2326 |
"import github.com/powerman/checkproto") |
| 2327 |
|
| 2328 |
|
| 2329 |
t.PanicMatch(func() { t.DeepEqual(fakeProto{}, "string") }, |
| 2330 |
"import github.com/powerman/checkproto") |
| 2331 |
t.PanicMatch(func() { t.DeepEqual("string", fakeProto{}) }, |
| 2332 |
"import github.com/powerman/checkproto") |
| 2333 |
|
| 2334 |
|
| 2335 |
t.DeepEqual(nil, nil) |
| 2336 |
} |
| 2337 |
|
| 2338 |
|
| 2339 |
func TestNotDeepEqualProtoPanic(tt *testing.T) { |
| 2340 |
t := check.T(tt) |
| 2341 |
|
| 2342 |
t.PanicMatch(func() { t.NotDeepEqual(fakeProto{}, fakeProto{}) }, |
| 2343 |
"import github.com/powerman/checkproto") |
| 2344 |
} |
| 2345 |
|
| 2346 |
|
| 2347 |
func TestErrGRPCStatusPanic(tt *testing.T) { |
| 2348 |
t := check.T(tt) |
| 2349 |
|
| 2350 |
err := &fakeGRPCErr{msg: "test"} |
| 2351 |
|
| 2352 |
|
| 2353 |
t.PanicMatch(func() { t.Err(err, err) }, |
| 2354 |
"import github.com/powerman/checkgrpc") |
| 2355 |
|
| 2356 |
|
| 2357 |
t.PanicMatch(func() { t.Err(io.EOF, err) }, |
| 2358 |
"import github.com/powerman/checkgrpc") |
| 2359 |
|
| 2360 |
|
| 2361 |
t.Err(nil, nil) |
| 2362 |
} |
| 2363 |
|
| 2364 |
|
| 2365 |
func TestNotErrGRPCStatusPanic(tt *testing.T) { |
| 2366 |
t := check.T(tt) |
| 2367 |
|
| 2368 |
err := &fakeGRPCErr{msg: "test"} |
| 2369 |
|
| 2370 |
t.PanicMatch(func() { t.NotErr(err, err) }, |
| 2371 |
"import github.com/powerman/checkgrpc") |
| 2372 |
} |