github.com/Aoi-hosizora/ahlib@v1.5.1-0.20230404072829-241b93cf91c7/xtesting/xtesting.go (about) 1 package xtesting 2 3 import ( 4 "fmt" 5 "github.com/Aoi-hosizora/ahlib/xreflect" 6 "os" 7 "reflect" 8 "testing" 9 "unicode" 10 ) 11 12 // ================= 13 // testing functions 14 // ================= 15 16 // Equal asserts that two objects are deep equal. 17 func Equal(t testing.TB, give, want interface{}, msgAndArgs ...interface{}) bool { 18 if err := validateArgsAreNotFunc(give, want); err != nil { 19 return failTest(t, 1, fmt.Sprintf("Equal: invalid operation `%#v` == `%#v` (%+v)", give, want, err), msgAndArgs...) 20 } 21 22 if !reflect.DeepEqual(give, want) { 23 return failTest(t, 1, fmt.Sprintf("Equal: expect to be `%#v`, but actually was `%#v`", want, give), msgAndArgs...) 24 } 25 26 return true 27 } 28 29 // NotEqual asserts that the specified values are not deep equal. 30 func NotEqual(t testing.TB, give, want interface{}, msgAndArgs ...interface{}) bool { 31 if err := validateArgsAreNotFunc(give, want); err != nil { 32 return failTest(t, 1, fmt.Sprintf("NotEqual: invalid operation `%#v` != `%#v` (%+v)", give, want, err), msgAndArgs...) 33 } 34 35 if reflect.DeepEqual(give, want) { 36 return failTest(t, 1, fmt.Sprintf("NotEqual: expect not to be `%#v`, but actually equaled", want), msgAndArgs...) 37 } 38 39 return true 40 } 41 42 // EqualValue asserts that two objects are equal or convertible to the same types and equal. 43 func EqualValue(t testing.TB, give, want interface{}, msgAndArgs ...interface{}) bool { 44 if err := validateArgsAreNotFunc(give, want); err != nil { 45 return failTest(t, 1, fmt.Sprintf("EqualValue: invalid operation `%#v` == `%#v` (%+v)", give, want, err), msgAndArgs...) 46 } 47 48 if !xreflect.DeepEqualInValue(give, want) { 49 return failTest(t, 1, fmt.Sprintf("EqualValue: expect to be `%#v`, but actually was `%#v`", want, give), msgAndArgs...) 50 } 51 52 return true 53 } 54 55 // NotEqualValue asserts that two objects are not equal even when converted to the same type. 56 func NotEqualValue(t testing.TB, give, want interface{}, msgAndArgs ...interface{}) bool { 57 if err := validateArgsAreNotFunc(give, want); err != nil { 58 return failTest(t, 1, fmt.Sprintf("NotEqualValue: invalid operation `%#v` != `%#v` (%+v)", give, want, err), msgAndArgs...) 59 } 60 61 if xreflect.DeepEqualInValue(give, want) { 62 return failTest(t, 1, fmt.Sprintf("NotEqualValue: expect not to be `%#v`, but actually equaled", want), msgAndArgs...) 63 } 64 65 return true 66 } 67 68 // SamePointer asserts that two pointers have the same pointer type, and point to the same address. 69 func SamePointer(t testing.TB, give, want interface{}, msgAndArgs ...interface{}) bool { 70 if err := validateArgsAreSameKind(give, want, reflect.Ptr); err != nil { 71 return failTest(t, 1, fmt.Sprintf("SamePointer: invalid operation `%#v` == `%#v` (%+v)", give, want, err), msgAndArgs...) 72 } 73 74 giveType, wantType := reflect.TypeOf(give), reflect.TypeOf(want) 75 if giveType != wantType { 76 return failTest(t, 1, fmt.Sprintf("SamePointer: expect to have the same pointer type, but actually differ (%T and %T)", want, give), msgAndArgs...) 77 } 78 if give != want { 79 return failTest(t, 1, fmt.Sprintf("SamePointer: expect to point to `%#v`, but actually pointed to `%#v`", want, give), msgAndArgs...) 80 } 81 82 return true 83 } 84 85 // NotSamePointer asserts that two pointers have different pointer types, or do not point to the same address. 86 func NotSamePointer(t testing.TB, give, want interface{}, msgAndArgs ...interface{}) bool { 87 if err := validateArgsAreSameKind(give, want, reflect.Ptr); err != nil { 88 return failTest(t, 1, fmt.Sprintf("NotSamePointer: invalid operation `%#v` != `%#v` (%+v)", give, want, err), msgAndArgs...) 89 } 90 91 giveType, wantType := reflect.TypeOf(give), reflect.TypeOf(want) 92 if giveType == wantType && give == want { 93 return failTest(t, 1, fmt.Sprintf("NotSamePointer: expect not to point to `%#v` with `%T` type, but actually pointed", want, want), msgAndArgs...) 94 } 95 96 return true 97 } 98 99 // SameFunction asserts that types and underlying pointers of two functions are the same. 100 func SameFunction(t testing.TB, give, want interface{}, msgAndArgs ...interface{}) bool { 101 if err := validateArgsAreSameKind(give, want, reflect.Func); err != nil { 102 return failTest(t, 1, fmt.Sprintf("SameFunction: invalid operation `%#v` == `%#v` (%+v)", give, want, err), msgAndArgs...) 103 } 104 105 if !xreflect.SameUnderlyingPointerWithTypeAndKind(give, want, reflect.Func) { 106 return failTest(t, 1, fmt.Sprintf("SameFunction: expect to be `%p` (%T), but actually `%p` (%T)", want, want, give, give), msgAndArgs...) 107 } 108 109 return true 110 } 111 112 // NotSameFunction asserts that types and underlying pointers of two functions are not the same. 113 func NotSameFunction(t testing.TB, give, want interface{}, msgAndArgs ...interface{}) bool { 114 if err := validateArgsAreSameKind(give, want, reflect.Func); err != nil { 115 return failTest(t, 1, fmt.Sprintf("NotSameFunction: invalid operation `%#v` != `%#v` (%+v)", give, want, err), msgAndArgs...) 116 } 117 118 if xreflect.SameUnderlyingPointerWithTypeAndKind(give, want, reflect.Func) { 119 return failTest(t, 1, fmt.Sprintf("NotSameFunction: expect not to be `%p` (%T), but actually the same", want, want), msgAndArgs...) 120 } 121 122 return true 123 } 124 125 // True asserts that the specified value is true. 126 func True(t testing.TB, value bool, msgAndArgs ...interface{}) bool { 127 if !value { 128 return failTest(t, 1, fmt.Sprintf("True: expect to be `true`, but actually was `%#v`", value), msgAndArgs...) 129 } 130 131 return true 132 } 133 134 // False asserts that the specified value is false. 135 func False(t testing.TB, value bool, msgAndArgs ...interface{}) bool { 136 if value { 137 return failTest(t, 1, fmt.Sprintf("False: expect to be `false`, but actually was `%#v`", value), msgAndArgs...) 138 } 139 140 return true 141 } 142 143 // Nil asserts that the specified object is nil. 144 func Nil(t testing.TB, value interface{}, msgAndArgs ...interface{}) bool { 145 if !xreflect.IsNilValue(value) { 146 return failTest(t, 1, fmt.Sprintf("Nil: expect to be `<nil>`, but actually was `%#v`", value), msgAndArgs...) 147 } 148 149 return true 150 } 151 152 // NotNil asserts that the specified object is not nil. 153 func NotNil(t testing.TB, value interface{}, msgAndArgs ...interface{}) bool { 154 if xreflect.IsNilValue(value) { 155 return failTest(t, 1, fmt.Sprintf("NotNil: expect not to be `<nil>`, but actually was `%#v`", value), msgAndArgs...) 156 } 157 158 return true 159 } 160 161 // Zero asserts that the specified object is the zero value for its type. 162 func Zero(t testing.TB, value interface{}, msgAndArgs ...interface{}) bool { 163 if !xreflect.IsZeroValue(value) { 164 return failTest(t, 1, fmt.Sprintf("Zero: expect to be zero value, but actually was `%#v`", value), msgAndArgs...) 165 } 166 167 return true 168 } 169 170 // NotZero asserts that the specified object is not the zero value for its type. 171 func NotZero(t testing.TB, value interface{}, msgAndArgs ...interface{}) bool { 172 if xreflect.IsZeroValue(value) { 173 return failTest(t, 1, fmt.Sprintf("NotZero: expect not to be zero value, but actually was `%#v`", value), msgAndArgs...) 174 } 175 176 return true 177 } 178 179 // BlankString asserts that the specified object is an empty or black string. 180 func BlankString(t testing.TB, value interface{}, msgAndArgs ...interface{}) bool { 181 s, ok := value.(string) 182 if !ok { 183 return failTest(t, 1, fmt.Sprintf("BlankString: expect string, got `%#v` (%T)", value, value), msgAndArgs...) 184 } 185 186 for _, r := range s { 187 if !unicode.IsSpace(r) { 188 return failTest(t, 1, fmt.Sprintf("BlankString: expect to be blank string, but actually was `%#v`", value), msgAndArgs...) 189 } 190 } 191 192 return true 193 } 194 195 // NotBlankString asserts that the specified object is not an empty or black string. 196 func NotBlankString(t testing.TB, value interface{}, msgAndArgs ...interface{}) bool { 197 s, ok := value.(string) 198 if !ok { 199 return failTest(t, 1, fmt.Sprintf("NotBlankString: expect string, got `%#v` (%T)", value, value), msgAndArgs...) 200 } 201 202 hasNotBlank := false 203 for _, r := range s { 204 if !unicode.IsSpace(r) { 205 hasNotBlank = true 206 break 207 } 208 } 209 if !hasNotBlank { 210 return failTest(t, 1, fmt.Sprintf("NotBlankString: expect not to be blank string, but actually was `%#v`", value), msgAndArgs...) 211 } 212 213 return true 214 } 215 216 // EmptyCollection asserts that the specified object is empty collection value. 217 func EmptyCollection(t testing.TB, value interface{}, msgAndArgs ...interface{}) bool { 218 if !xreflect.IsEmptyCollection(value) { 219 return failTest(t, 1, fmt.Sprintf("EmptyCollection: expect to be empty collection value, but actually was `%#v`", value), msgAndArgs...) 220 } 221 222 return true 223 } 224 225 // NotEmptyCollection asserts that the specified object is not empty collection value. 226 func NotEmptyCollection(t testing.TB, value interface{}, msgAndArgs ...interface{}) bool { 227 if xreflect.IsEmptyCollection(value) { 228 return failTest(t, 1, fmt.Sprintf("NotEmptyCollection: expect not to be empty collection value, but actually was `%#v`", value), msgAndArgs...) 229 } 230 231 return true 232 } 233 234 // Error asserts that a function returned an error. 235 func Error(t testing.TB, err error, msgAndArgs ...interface{}) bool { 236 if err == nil { 237 return failTest(t, 1, fmt.Sprintf("Error: expect not to be nil error, but actually was `%#v`", err), msgAndArgs...) 238 } 239 240 return true 241 } 242 243 // NilError asserts that a function returned no error. 244 func NilError(t testing.TB, err error, msgAndArgs ...interface{}) bool { 245 if err != nil { 246 return failTest(t, 1, fmt.Sprintf("NilError: expect to be nil error, but actually was `%#v`", err), msgAndArgs...) 247 } 248 249 return true 250 } 251 252 // EqualError asserts that a function returned an error and that it is equal to the provided error. 253 func EqualError(t testing.TB, err error, wantString string, msgAndArgs ...interface{}) bool { 254 if err == nil { 255 return failTest(t, 1, fmt.Sprintf("EqualError: expect not to be nil error, but actually was `%#v`", err), msgAndArgs...) 256 } 257 258 if msg := err.Error(); msg != wantString { 259 return failTest(t, 1, fmt.Sprintf("EqualError: expect to be error with message `%#v`, but actually with `%#v`", wantString, msg), msgAndArgs...) 260 } 261 262 return true 263 } 264 265 // NotEqualError asserts that a function returned an error and that it is not equal to the provided error. 266 func NotEqualError(t testing.TB, err error, wantString string, msgAndArgs ...interface{}) bool { 267 if err == nil { 268 return failTest(t, 1, fmt.Sprintf("NotEqualError: expect not to be nil error, but actually was `%#v`", err), msgAndArgs...) 269 } 270 271 if err.Error() == wantString { 272 return failTest(t, 1, fmt.Sprintf("NotEqualError: expect error message not to be `%#v`, but actually equaled", wantString), msgAndArgs...) 273 } 274 275 return true 276 } 277 278 // MatchRegexp asserts that a specified regexp matches a string. 279 func MatchRegexp(t testing.TB, rx interface{}, str string, msgAndArgs ...interface{}) bool { 280 match, re, err := matchRegexp(rx, str) 281 if err != nil { 282 return failTest(t, 1, fmt.Sprintf("MatchRegexp: invalid regular expression value `%#v` (%+v)", rx, err), msgAndArgs...) 283 } 284 285 if !match { 286 return failTest(t, 1, fmt.Sprintf("MatchRegexp: expect `%#v` to match `%#v`, but actually not matched", str, re.String()), msgAndArgs...) 287 } 288 289 return true 290 } 291 292 // NotMatchRegexp asserts that a specified regexp does not match a string. 293 func NotMatchRegexp(t testing.TB, rx interface{}, str string, msgAndArgs ...interface{}) bool { 294 match, re, err := matchRegexp(rx, str) 295 if err != nil { 296 return failTest(t, 1, fmt.Sprintf("NotMatchRegexp: invalid regular expression value `%#v` (%+v)", rx, err), msgAndArgs...) 297 } 298 299 if match { 300 return failTest(t, 1, fmt.Sprintf("NotMatchRegexp: expect `%#v` not to match `%#v`, but actually matched", str, re.String()), msgAndArgs...) 301 } 302 303 return true 304 } 305 306 // InDelta asserts that the two numerics are within delta of each other. 307 func InDelta(t testing.TB, give, want interface{}, delta float64, msgAndArgs ...interface{}) bool { 308 inDelta, actualDiff, err := calcDiffInDelta(give, want, delta) 309 if err != nil { 310 return failTest(t, 1, fmt.Sprintf("InDelta: invalid operation on `%#v`, `%#v` and `%#v` (%+v)", give, want, delta, err), msgAndArgs...) 311 } 312 313 if !inDelta { 314 return failTest(t, 1, fmt.Sprintf("InDelta: expect difference between `%#v` and `%#v` to be less than or equal to `%#v`, but actually was `%#v`", give, want, delta, actualDiff), msgAndArgs...) 315 } 316 317 return true 318 } 319 320 // NotInDelta asserts that the two numerics are not within delta of each other. 321 func NotInDelta(t testing.TB, give, want interface{}, delta float64, msgAndArgs ...interface{}) bool { 322 inDelta, actualDiff, err := calcDiffInDelta(give, want, delta) 323 if err != nil { 324 return failTest(t, 1, fmt.Sprintf("NotInDelta: invalid operation on `%#v`, `%#v` and `%#v` (%+v)", err, give, want, delta), msgAndArgs...) 325 } 326 327 if inDelta { 328 return failTest(t, 1, fmt.Sprintf("NotInDelta: expect difference between `%#v` and `%#v` to be greater than `%#v`, but actually was `%#v`", give, want, delta, actualDiff), msgAndArgs...) 329 } 330 331 return true 332 } 333 334 // InEpsilon asserts that two numerics have a relative error less than epsilon. 335 func InEpsilon(t testing.TB, give, want interface{}, epsilon float64, msgAndArgs ...interface{}) bool { 336 inEps, actualRee, err := calcRelativeError(give, want, epsilon) 337 if err != nil { 338 return failTest(t, 1, fmt.Sprintf("InEpsilon: invalid operation on `%#v`, `%#v` and `%#v` (%+v)", err, give, want, epsilon), msgAndArgs...) 339 } 340 341 if !inEps { 342 return failTest(t, 1, fmt.Sprintf("InEpsilon: expect relative error between `%#v` and `%#v` to be less than or equal to `%#v`, but actually was `%#v`", give, want, epsilon, actualRee), msgAndArgs...) 343 } 344 345 return true 346 } 347 348 // NotInEpsilon asserts that two numerics have a relative error greater than epsilon. 349 func NotInEpsilon(t testing.TB, give, want interface{}, epsilon float64, msgAndArgs ...interface{}) bool { 350 inEps, actualRee, err := calcRelativeError(give, want, epsilon) 351 if err != nil { 352 return failTest(t, 1, fmt.Sprintf("NotInEpsilon: invalid operation on `%#v`, `%#v` and `%#v` (%+v)", err, give, want, epsilon), msgAndArgs...) 353 } 354 355 if inEps { 356 return failTest(t, 1, fmt.Sprintf("NotInEpsilon: expect relative error between `%#v` and `%#v` to be greater than `%#v`, but actually was `%#v`", give, want, epsilon, actualRee), msgAndArgs...) 357 } 358 359 return true 360 } 361 362 // Contain asserts that the specified string, list(array, slice...) or map contains the specified substring or element. 363 func Contain(t testing.TB, container, value interface{}, msgAndArgs ...interface{}) bool { 364 found, err := containElement(container, value) 365 if err != nil { 366 return failTest(t, 1, fmt.Sprintf("Contain: invalid operation on `%#v` and `%#v` (%+v)", container, value, err), msgAndArgs...) 367 } 368 369 if !found { 370 return failTest(t, 1, fmt.Sprintf("Contain: expect `%#v` to contain `%#v`, but actually not contained", container, value), msgAndArgs...) 371 } 372 373 return true 374 } 375 376 // NotContain asserts that the specified string, list(array, slice...) or map does not contain the specified substring or element. 377 func NotContain(t testing.TB, container, value interface{}, msgAndArgs ...interface{}) bool { 378 found, err := containElement(container, value) 379 if err != nil { 380 return failTest(t, 1, fmt.Sprintf("NotContain: invalid operation on `%#v` and `%#v` (%+v)", container, value, err), msgAndArgs...) 381 } 382 383 if found { 384 return failTest(t, 1, fmt.Sprintf("NotContain: expect `%#v` not to contain `%#v`, but actually contained", container, value), msgAndArgs...) 385 } 386 387 return true 388 } 389 390 // Subset asserts that the specified list(array, slice...) contains all elements given in the specified subset(array, slice...). 391 func Subset(t testing.TB, list, subset interface{}, msgAndArgs ...interface{}) bool { 392 if err := validateArgsAreSameList(list, subset); err != nil { 393 return failTest(t, 1, fmt.Sprintf("Subset: invalid operation on `%#v` and `%#v` (%+v)", list, subset, err), msgAndArgs...) 394 } 395 396 allFound, element := containAllElements(list, subset) 397 if !allFound { 398 return failTest(t, 1, fmt.Sprintf("Subset: expect `%#v` to contain `%#v`, but actually not contained", list, element), msgAndArgs...) 399 } 400 401 return true 402 } 403 404 // NotSubset asserts that the specified list(array, slice...) contains not all elements given in the specified subset(array, slice...). 405 func NotSubset(t testing.TB, list, subset interface{}, msgAndArgs ...interface{}) bool { 406 if err := validateArgsAreSameList(list, subset); err != nil { 407 return failTest(t, 1, fmt.Sprintf("NotSubset: invalid operation on `%#v` and `%#v` (%+v)", list, subset, err), msgAndArgs...) 408 } 409 410 allFound, _ := containAllElements(list, subset) 411 if allFound { 412 return failTest(t, 1, fmt.Sprintf("NotSubset: expect `%#v` not to be a subset of `%#v`, but actually was", subset, list), msgAndArgs...) 413 } 414 415 return true 416 } 417 418 // ElementMatch asserts that the specified listA(array, slice...) equals to specified listB(array, slice...) ignoring the order of the elements. 419 // If there are duplicate elements, the number of appearances of each of them in both lists should match. 420 func ElementMatch(t testing.TB, listA, listB interface{}, msgAndArgs ...interface{}) bool { 421 if err := validateArgsAreSameList(listA, listB); err != nil { 422 return failTest(t, 1, fmt.Sprintf("ElementMatch: invalid operation on `%#v` and `%#v` (%+v)", listA, listB, err), msgAndArgs...) 423 } 424 425 extraA, extraB := diffLists(listA, listB) 426 if len(extraA) != 0 || len(extraB) != 0 { 427 return failTest(t, 1, fmt.Sprintf("ElementMatch: expect `%#v` and `%#v` to match each other, but actually not matched", listA, listB), msgAndArgs...) 428 } 429 430 return true 431 } 432 433 // NotElementMatch asserts that the specified listA(array, slice...) does not equal to specified listB(array, slice...) ignoring the order of the elements. 434 func NotElementMatch(t testing.TB, listA, listB interface{}, msgAndArgs ...interface{}) bool { 435 if err := validateArgsAreSameList(listA, listB); err != nil { 436 return failTest(t, 1, fmt.Sprintf("NotElementMatch: invalid operation on `%#v` and `%#v` (%+v)", listA, listB, err), msgAndArgs...) 437 } 438 439 extraA, extraB := diffLists(listA, listB) 440 if len(extraA) == 0 && len(extraB) == 0 { 441 return failTest(t, 1, fmt.Sprintf("NotElementMatch: expect `%#v` and `%#v` not to match each other, but actually matched", listA, listB), msgAndArgs...) 442 } 443 444 return true 445 } 446 447 // SameType asserts that the specified objects are of the same type. 448 func SameType(t testing.TB, value, want interface{}, msgAndArgs ...interface{}) bool { 449 valueType := reflect.TypeOf(value) 450 wantType := reflect.TypeOf(want) 451 452 if !reflect.DeepEqual(valueType, wantType) { 453 return failTest(t, 1, fmt.Sprintf("SameType: expect `%#v` to be of type `%T`, but actually was `%T`", value, want, value), msgAndArgs...) 454 } 455 456 return true 457 } 458 459 // NotSameType asserts that the specified objects are of the different types. 460 func NotSameType(t testing.TB, value, want interface{}, msgAndArgs ...interface{}) bool { 461 valueType := reflect.TypeOf(value) 462 wantType := reflect.TypeOf(want) 463 464 if reflect.DeepEqual(valueType, wantType) { 465 return failTest(t, 1, fmt.Sprintf("NotSameType: expect `%#v` not to be of type `%T`, but actually was the same type", value, want), msgAndArgs...) 466 } 467 468 return true 469 } 470 471 // Implement asserts that an object implements the specified interface. 472 func Implement(t testing.TB, value, interfacePtr interface{}, msgAndArgs ...interface{}) bool { 473 interfaceType, err := validateArgsForImplement(value, interfacePtr) 474 if err != nil { 475 return failTest(t, 1, fmt.Sprintf("Implement: invalid parameters (%+v)", err), msgAndArgs...) 476 } 477 478 if !reflect.TypeOf(value).Implements(interfaceType) { 479 return failTest(t, 1, fmt.Sprintf("Implement: expect type `%T` to implement `%s`, but actually not implemented", value, interfaceType.String()), msgAndArgs...) 480 } 481 482 return true 483 } 484 485 // NotImplement asserts that an object does not implement the specified interface. 486 func NotImplement(t testing.TB, value, interfacePtr interface{}, msgAndArgs ...interface{}) bool { 487 interfaceType, err := validateArgsForImplement(value, interfacePtr) 488 if err != nil { 489 return failTest(t, 1, fmt.Sprintf("Implement: invalid parameters (%+v)", err), msgAndArgs...) 490 } 491 492 if reflect.TypeOf(value).Implements(interfaceType) { 493 return failTest(t, 1, fmt.Sprintf("Implement: expect type `%T` not to implement `%s`, but actually implemented", value, interfaceType.String()), msgAndArgs...) 494 } 495 496 return true 497 } 498 499 // Panic asserts that the code inside the specified function panics. 500 func Panic(t testing.TB, f func(), msgAndArgs ...interface{}) bool { 501 funcDidPanic, _ := checkPanic(f) 502 if !funcDidPanic { 503 return failTest(t, 1, fmt.Sprintf("Panic: expect function `%#v` to panic, but actually did not panic", interface{}(f)), msgAndArgs...) 504 } 505 506 return true 507 } 508 509 // NotPanic asserts that the code inside the specified function does not panic. 510 func NotPanic(t testing.TB, f func(), msgAndArgs ...interface{}) bool { 511 funcDidPanic, panicValue := checkPanic(f) 512 if funcDidPanic { 513 return failTest(t, 1, fmt.Sprintf("NotPanic: expect function `%#v` not to panic, but actually paniced with `%#v`", interface{}(f), panicValue), msgAndArgs...) 514 } 515 516 return true 517 } 518 519 // PanicWithValue asserts that the code inside the specified function panics, and that the recovered panic value equals the wanted panic value. 520 func PanicWithValue(t testing.TB, want interface{}, f func(), msgAndArgs ...interface{}) bool { 521 funcDidPanic, panicValue := checkPanic(f) 522 if !funcDidPanic { 523 return failTest(t, 1, fmt.Sprintf("PanicWithValue: expect function `%#v` to panic, but actually did not panic", interface{}(f)), msgAndArgs...) 524 } 525 526 if !reflect.DeepEqual(panicValue, want) { 527 return failTest(t, 1, fmt.Sprintf("PanicWithValue: expect function `%#v` to panic with `%#v`, but actually with `%#v`", interface{}(f), want, panicValue), msgAndArgs...) 528 } 529 530 return true 531 } 532 533 // PanicWithError asserts that the code inside the specified PanicTestFunc panics, and that the recovered panic value is an error that satisfies the EqualError comparison. 534 func PanicWithError(t testing.TB, wantString string, f func(), msgAndArgs ...interface{}) bool { 535 funcDidPanic, panicValue := checkPanic(f) 536 if !funcDidPanic { 537 return failTest(t, 1, fmt.Sprintf("PanicWithError: expect function `%#v` to panic, but actually did not panic", interface{}(f)), msgAndArgs...) 538 } 539 540 panicErr, ok := panicValue.(error) 541 if !ok || panicErr.Error() != wantString { 542 return failTest(t, 1, fmt.Sprintf("PanicWithError: expect function `%#v` to panic with error message `%#v`, but actually with `%#v`", interface{}(f), wantString, panicValue), msgAndArgs...) 543 } 544 545 return true 546 } 547 548 // FileExist checks whether a file exists in given path. It fails if the path points to a directory, or there is an error when checking whether it exists. 549 func FileExist(t testing.TB, path string, msgAndArgs ...interface{}) bool { 550 info, err := os.Stat(path) 551 if err != nil && !os.IsNotExist(err) { 552 return failTest(t, 1, fmt.Sprintf("FileExist: error when calling os.Stat on `%#v` (%+v)", path, err), msgAndArgs...) 553 } 554 555 if err != nil { 556 return failTest(t, 1, fmt.Sprintf("FileExist: expect file `%s` to exist, but actually not existed", path), msgAndArgs...) 557 } 558 if info.IsDir() { 559 return failTest(t, 1, fmt.Sprintf("FileExist: expect `%s` to be a file, but actually was a directory", path), msgAndArgs...) 560 } 561 562 return true 563 } 564 565 // FileNotExist checks whether a file does not exist in given path. It fails if the path points to an existing file only, or there is an error when checking whether it exists. 566 func FileNotExist(t testing.TB, path string, msgAndArgs ...interface{}) bool { 567 info, err := os.Stat(path) 568 if err != nil && !os.IsNotExist(err) { 569 return failTest(t, 1, fmt.Sprintf("FileNotExist: error when calling os.Stat on `%#v` (%+v)", path, err), msgAndArgs...) 570 } 571 572 if err == nil && !info.IsDir() { 573 return failTest(t, 1, fmt.Sprintf("FileNotExist: expect file `%s` not to exist, but actually was an existing file", path), msgAndArgs...) 574 } 575 576 return true 577 } 578 579 // FileLexist checks whether a file lexists in given path. It fails if the path points to a directory, or there is an error when checking whether it exists. 580 func FileLexist(t testing.TB, path string, msgAndArgs ...interface{}) bool { 581 info, err := os.Lstat(path) 582 if err != nil && !os.IsNotExist(err) { 583 return failTest(t, 1, fmt.Sprintf("FileLexist: error when calling os.Lstat on `%#v` (%+v)", path, err), msgAndArgs...) 584 } 585 586 if err != nil { 587 return failTest(t, 1, fmt.Sprintf("FileLexist: expect file `%s` to exist, but actually not existed", path), msgAndArgs...) 588 } 589 if info.IsDir() { 590 return failTest(t, 1, fmt.Sprintf("FileLexist: expect `%s` to be a file, but actually was a directory", path), msgAndArgs...) 591 } 592 593 return true 594 } 595 596 // FileNotLexist checks whether a file does not lexist in given path. It fails if the path points to an existing file only, or there is an error when checking whether it exists. 597 func FileNotLexist(t testing.TB, path string, msgAndArgs ...interface{}) bool { 598 info, err := os.Lstat(path) 599 if err != nil && !os.IsNotExist(err) { 600 return failTest(t, 1, fmt.Sprintf("FileNotLexist: error when calling os.Lstat on `%#v` (%+v)", path, err), msgAndArgs...) 601 } 602 603 if err == nil && !info.IsDir() { 604 return failTest(t, 1, fmt.Sprintf("FileNotLexist: expect file `%s` not to exist, but actually was an existing file", path), msgAndArgs...) 605 } 606 607 return true 608 } 609 610 // DirExist checks whether a directory exists in given path. It fails if the path is a file rather a directory, or there is an error checking whether it exists. 611 func DirExist(t testing.TB, path string, msgAndArgs ...interface{}) bool { 612 info, err := os.Stat(path) 613 if err != nil && !os.IsNotExist(err) { 614 return failTest(t, 1, fmt.Sprintf("DirExist: error when calling os.Stat on `%#v` (%+v)", path, err), msgAndArgs...) 615 } 616 617 if err != nil { 618 return failTest(t, 1, fmt.Sprintf("DirExist: expect directory `%s` to exist, but actually not existed", path), msgAndArgs...) 619 } 620 if !info.IsDir() { 621 return failTest(t, 1, fmt.Sprintf("DirExist: expect `%s` to be a directory, but actually was a file", path), msgAndArgs...) 622 } 623 624 return true 625 } 626 627 // DirNotExist checks whether a directory does not exist in given path. It fails if the path points to an existing directory only, or there is an error when checking whether it exists. 628 func DirNotExist(t testing.TB, path string, msgAndArgs ...interface{}) bool { 629 info, err := os.Stat(path) 630 if err != nil && !os.IsNotExist(err) { 631 return failTest(t, 1, fmt.Sprintf("DirNotExist: error when calling os.Stat on `%#v` (%+v)", path, err), msgAndArgs...) 632 } 633 634 if err == nil && info.IsDir() { 635 return failTest(t, 1, fmt.Sprintf("DirNotExist: expect directory `%s` not to exist, but actually was an existing directory", path), msgAndArgs...) 636 } 637 638 return true 639 } 640 641 // DirLexist checks whether a directory lexists in given path. It fails if the path is a file rather a directory, or there is an error checking whether it exists. 642 func DirLexist(t testing.TB, path string, msgAndArgs ...interface{}) bool { 643 info, err := os.Lstat(path) 644 if err != nil && !os.IsNotExist(err) { 645 return failTest(t, 1, fmt.Sprintf("DirLexist: error when calling os.Lstat on `%#v` (%+v)", path, err), msgAndArgs...) 646 } 647 648 if err != nil { 649 return failTest(t, 1, fmt.Sprintf("DirLexist: expect directory `%s` to exist, but actually not existed", path), msgAndArgs...) 650 } 651 if !info.IsDir() { 652 return failTest(t, 1, fmt.Sprintf("DirLexist: expect `%s` to be a directory, but actually was a file", path), msgAndArgs...) 653 } 654 655 return true 656 } 657 658 // DirNotLexist checks whether a directory does not lexist in given path. It fails if the path points to an existing directory only, or there is an error when checking whether it exists. 659 func DirNotLexist(t testing.TB, path string, msgAndArgs ...interface{}) bool { 660 info, err := os.Lstat(path) 661 if err != nil && !os.IsNotExist(err) { 662 return failTest(t, 1, fmt.Sprintf("DirNotLexist: error when calling os.Lstat on `%#v` (%+v)", path, err), msgAndArgs...) 663 } 664 665 if err == nil && info.IsDir() { 666 return failTest(t, 1, fmt.Sprintf("DirNotLexist: expect directory `%s` not to exist, but actually was an existing directory", path), msgAndArgs...) 667 } 668 669 return true 670 } 671 672 // SymlinkLexist checks whether a symlink lexists in given path. It fails if the path does not point to an existing symlink, or there is an error checking whether it exists. 673 func SymlinkLexist(t testing.TB, path string, msgAndArgs ...interface{}) bool { 674 info, err := os.Lstat(path) 675 if err != nil && !os.IsNotExist(err) { 676 return failTest(t, 1, fmt.Sprintf("SymlinkLexist: error when calling os.Lstat on `%#v` (%+v)", path, err), msgAndArgs...) 677 } 678 679 if err != nil { 680 return failTest(t, 1, fmt.Sprintf("SymlinkLexist: expect symlink `%s` to exist, but actually not existed", path), msgAndArgs...) 681 } 682 if (info.Mode() & os.ModeSymlink) == 0 { 683 return failTest(t, 1, fmt.Sprintf("SymlinkLexist: expect `%s` to be a symlink, but actually was an existing file or directory", path), msgAndArgs...) 684 } 685 686 return true 687 } 688 689 // SymlinkNotLexist checks whether a symlink does not lexist in given path. It fails if the path points to an existing symlink only, or there is an error when checking whether it exist. 690 func SymlinkNotLexist(t testing.TB, path string, msgAndArgs ...interface{}) bool { 691 info, err := os.Lstat(path) 692 if err != nil && !os.IsNotExist(err) { 693 return failTest(t, 1, fmt.Sprintf("SymlinkNotLexist: error when calling os.Lstat on `%#v` (%+v)", path, err), msgAndArgs...) 694 } 695 696 if err == nil && (info.Mode()&os.ModeSymlink) == os.ModeSymlink { 697 return failTest(t, 1, fmt.Sprintf("SymlinkNotLexist: expect symlink `%s` not to exist, but actually was an existing file or directory", path), msgAndArgs...) 698 } 699 700 return true 701 }