github.com/Aoi-hosizora/ahlib@v1.5.1-0.20230404072829-241b93cf91c7/xtesting/xtesting_test.go (about) 1 package xtesting 2 3 import ( 4 "errors" 5 "fmt" 6 "io/ioutil" 7 "math" 8 "os" 9 "path" 10 "path/filepath" 11 "reflect" 12 "regexp" 13 "runtime" 14 "strings" 15 "testing" 16 "unsafe" 17 ) 18 19 func fail(t *testing.T) { 20 _, file, line, _ := runtime.Caller(1) 21 fmt.Printf("!!! testing on %s:%d is failed !!!\n", path.Base(file), line) 22 t.Fail() 23 } 24 25 type testFlag uint8 26 27 const ( 28 positive testFlag = iota 29 negative 30 abnormal 31 ) 32 33 /* 34 // discard stderr 35 defer func(stdout *os.File) { 36 os.Stderr = stdout 37 }(os.Stderr) 38 os.Stderr = os.NewFile(uintptr(syscall.Stdin), os.DevNull) 39 */ 40 41 func TestEqualNotEqual(t *testing.T) { 42 mockT := &testing.T{} 43 44 type myType string 45 var m map[string]interface{} 46 47 for _, tc := range []struct { 48 giveG, giveW interface{} 49 want testFlag 50 }{ 51 // expect to Equal 52 {"Hello World", "Hello World", positive}, 53 {123, 123, positive}, 54 {123.5, 123.5, positive}, 55 {[]byte("Hello World"), []byte("Hello World"), positive}, 56 {nil, nil, positive}, 57 {int32(123), int32(123), positive}, 58 {uint64(123), uint64(123), positive}, 59 {myType("1"), myType("1"), positive}, 60 {&struct{}{}, &struct{}{}, positive}, 61 62 // expect to NotEqual 63 {"Hello World", "Hello World!", negative}, 64 {123, 1234, negative}, 65 {123.5, 123.55, negative}, 66 {[]byte("Hello World"), []byte("Hello World!"), negative}, 67 {nil, new(struct{}), negative}, 68 {10, uint(10), negative}, 69 {m["bar"], "something", negative}, 70 {myType("1"), myType("2"), negative}, 71 72 // expect to fail in all cases 73 {func() {}, func() {}, abnormal}, 74 {func() int { return 23 }, func() int { return 42 }, abnormal}, 75 } { 76 pos := Equal(mockT, tc.giveG, tc.giveW) 77 if (tc.want == positive && !pos) || (tc.want != positive && pos) { 78 fail(t) 79 } 80 neg := NotEqual(mockT, tc.giveG, tc.giveW) 81 if (tc.want == negative && !neg) || (tc.want != negative && neg) { 82 fail(t) 83 } 84 } 85 } 86 87 func TestEqualValueNotEqualValue(t *testing.T) { 88 mockT := &testing.T{} 89 90 type myType string 91 92 for _, tc := range []struct { 93 giveG, giveW interface{} 94 want testFlag 95 }{ 96 // expect to EqualValue 97 {nil, nil, positive}, 98 {"Hello World", "Hello World", positive}, 99 {123, 123, positive}, 100 {123.5, 123.5, positive}, 101 {[]byte("Hello World"), []byte("Hello World"), positive}, 102 {new(struct{}), new(struct{}), positive}, 103 {&struct{}{}, &struct{}{}, positive}, 104 {10, uint(10), positive}, 105 {struct{}{}, struct{}{}, positive}, 106 {myType("1"), "1", positive}, 107 108 // expect to NotEqualValue 109 {"Hello World", "Hello World!", negative}, 110 {123, 1234, negative}, 111 {123.5, 123.55, negative}, 112 {[]byte("Hello World"), []byte("Hello World!"), negative}, 113 {myType("1"), myType("2"), negative}, 114 {"1", myType("2"), negative}, 115 116 // except to fail in all cases 117 {func() {}, func() {}, abnormal}, 118 {func() int { return 23 }, func() int { return 42 }, abnormal}, 119 } { 120 pos := EqualValue(mockT, tc.giveG, tc.giveW) 121 if (tc.want == positive && !pos) || (tc.want != positive && pos) { 122 fail(t) 123 } 124 neg := NotEqualValue(mockT, tc.giveG, tc.giveW) 125 if (tc.want == negative && !neg) || (tc.want != negative && neg) { 126 fail(t) 127 } 128 } 129 } 130 131 func TestSamePointer(t *testing.T) { 132 mockT := &testing.T{} 133 134 i := 2 135 u32 := 2 136 f64 := 2.0 137 str := "2" 138 p1 := &i 139 p2 := &u32 140 p3 := &f64 141 p4 := &str 142 143 for _, tc := range []struct { 144 giveG, giveW interface{} 145 want testFlag 146 }{ 147 // expect to SamePointer 148 {p1, p1, positive}, 149 {p2, p2, positive}, 150 {p3, p3, positive}, 151 {p4, p4, positive}, 152 {(*int)(nil), (*int)(nil), positive}, 153 154 // expect to NotSamePointer 155 {p1, p2, negative}, 156 {p1, p3, negative}, 157 {p1, p4, negative}, 158 {p2, p3, negative}, 159 {p2, p4, negative}, 160 {new(uint), new(uint), negative}, 161 {new(uint), new(float32), negative}, 162 {new(*int), new(int), negative}, 163 {(*int)(nil), (*uint64)(nil), negative}, 164 {p1, &p1, negative}, 165 {p2, &p2, negative}, 166 {p3, &p3, negative}, 167 {p4, &p4, negative}, 168 169 // expect to fail in all cases 170 {1, 1, abnormal}, 171 {nil, nil, abnormal}, 172 {p1, nil, abnormal}, 173 {nil, p2, abnormal}, 174 {p3, *p3, abnormal}, 175 {*p4, p4, abnormal}, 176 } { 177 pos := SamePointer(mockT, tc.giveG, tc.giveW) 178 if (tc.want == positive && !pos) || (tc.want != positive && pos) { 179 fail(t) 180 } 181 neg := NotSamePointer(mockT, tc.giveG, tc.giveW) 182 if (tc.want == negative && !neg) || (tc.want != negative && neg) { 183 fail(t) 184 } 185 } 186 } 187 188 func TestSameFunction(t *testing.T) { 189 mockT := &testing.T{} 190 191 f1 := func() {} 192 f11 := *(*func() int)(unsafe.Pointer(&f1)) 193 f111 := *(*func() int)(unsafe.Pointer(&f11)) 194 f2 := func() {} 195 f22 := *(*func() int)(unsafe.Pointer(&f2)) 196 f222 := *(*func() int)(unsafe.Pointer(&f22)) 197 198 for _, tc := range []struct { 199 giveG, giveW interface{} 200 want testFlag 201 }{ 202 // expect to SameFunction 203 {f1, f1, positive}, 204 {f2, f2, positive}, 205 {f11, f111, positive}, 206 {f222, f22, positive}, 207 {SameFunction, SameFunction, positive}, 208 {reflect.Value.Kind, reflect.Value.Kind, positive}, 209 210 // expect to NotSameFunction 211 {SameFunction, NotSameFunction, negative}, 212 {reflect.Value.Kind, reflect.Value.String, negative}, 213 {f1, f2, negative}, 214 {f11, f22, negative}, 215 {f1, f222, negative}, 216 {f1, f111, negative}, 217 {f222, f1, negative}, 218 {f222, f2, negative}, 219 220 // expect to fail in all cases 221 {1, 1, abnormal}, 222 {nil, nil, abnormal}, 223 {nil, func() {}, abnormal}, 224 {false, "func", abnormal}, 225 {new(func()), &f1, abnormal}, 226 } { 227 pos := SameFunction(mockT, tc.giveG, tc.giveW) 228 if (tc.want == positive && !pos) || (tc.want != positive && pos) { 229 fail(t) 230 } 231 neg := NotSameFunction(mockT, tc.giveG, tc.giveW) 232 if (tc.want == negative && !neg) || (tc.want != negative && neg) { 233 fail(t) 234 } 235 } 236 } 237 238 func TestTrueFalse(t *testing.T) { 239 mockT := &testing.T{} 240 241 // True 242 if !True(mockT, true) { 243 fail(t) 244 } 245 if True(mockT, false) { 246 fail(t) 247 } 248 249 // False 250 if !False(mockT, false) { 251 fail(t) 252 } 253 if False(mockT, true) { 254 fail(t) 255 } 256 } 257 258 func TestNilNotNil(t *testing.T) { 259 mockT := &testing.T{} 260 261 nils := []interface{}{ 262 nil, (*struct{})(nil), (*int)(nil), (func())(nil), fmt.Stringer(nil), error(nil), 263 []int(nil), map[int]int(nil), (chan int)(nil), (chan<- int)(nil), (<-chan int)(nil), 264 } 265 266 nonNils := []interface{}{ 267 0, "", &struct{}{}, new(interface{}), new(int), func() {}, fmt.Stringer(&strings.Builder{}), errors.New(""), 268 []int{}, map[int]int{}, make(chan int), make(chan<- int), make(<-chan int), 269 } 270 271 // expect to Nil 272 for _, nil_ := range nils { 273 if !Nil(mockT, nil_) { 274 fail(t) 275 } 276 if NotNil(mockT, nil_) { 277 fail(t) 278 } 279 } 280 281 // expect to NotNil 282 for _, nonNil := range nonNils { 283 if !NotNil(mockT, nonNil) { 284 fail(t) 285 } 286 if Nil(mockT, nonNil) { 287 fail(t) 288 } 289 } 290 } 291 292 func TestZeroNotZero(t *testing.T) { 293 mockT := &testing.T{} 294 295 zeros := []interface{}{ 296 nil, "", false, complex64(0), complex128(0), float32(0), float64(0), 297 0, int8(0), int16(0), int32(0), int64(0), byte(0), rune(0), 298 uint(0), uint8(0), uint16(0), uint32(0), uint64(0), uintptr(0), 299 [0]interface{}{}, []interface{}(nil), struct{ x int }{}, (*interface{})(nil), (func())(nil), interface{}(nil), 300 map[interface{}]interface{}(nil), (chan interface{})(nil), (<-chan interface{})(nil), (chan<- interface{})(nil), 301 } 302 303 nonZeros := []interface{}{ 304 "s", true, complex64(1), complex128(1), float32(1), float64(1), 305 1, int8(1), int16(1), int32(1), int64(1), byte(1), rune(1), 306 uint(1), uint8(1), uint16(1), uint32(1), uint64(1), uintptr(1), 307 [1]interface{}{1}, []interface{}{}, struct{ x int }{1}, new(interface{}), func() {}, interface{}(1), 308 map[interface{}]interface{}{}, make(chan interface{}), (<-chan interface{})(make(chan interface{})), (chan<- interface{})(make(chan interface{})), 309 } 310 311 // expect to Zero 312 for _, zero := range zeros { 313 if !Zero(mockT, zero) { 314 fail(t) 315 } 316 if NotZero(mockT, zero) { 317 fail(t) 318 } 319 } 320 321 // expect to NotZero 322 for _, nonZero := range nonZeros { 323 if !NotZero(mockT, nonZero) { 324 fail(t) 325 } 326 if Zero(mockT, nonZero) { 327 fail(t) 328 } 329 } 330 } 331 332 func TestBlankNotBlankString(t *testing.T) { 333 mockT := &testing.T{} 334 335 errs := []interface{}{ 336 nil, 1, uint64(0), 0.01, false, [0]interface{}{}, []interface{}(nil), 337 struct{}{}, struct{ x int }{}, [0]string{}, []string{""}, map[string]string{"": ""}, 338 } 339 340 blanks := []interface{}{ 341 "", " ", "\t", "\n", "\r", "\v", "\f", " ", string([]rune{0x85}), string([]rune{0xA0}), " ", "\t ", "\r\n", "\v\f", 342 "\u2000", "\u2001", "\u2002", "\u2003", "\u2004", "\u2005", "\u2006", "\u2007", 343 "\u2008", "\u2009", "\u200A", "\u2028", "\u2029", "\u202F", "\u205F", "\u3000", 344 } 345 346 nonBlanks := []interface{}{ 347 "1", "\n \t1", "测试", "テスト", "test", " 1", "\u2000@\u205F", 348 } 349 350 // expect to error 351 for _, err := range errs { 352 if BlankString(mockT, err) { 353 fail(t) 354 } 355 if NotBlankString(mockT, err) { 356 fail(t) 357 } 358 } 359 360 for _, zero := range blanks { 361 if !BlankString(mockT, zero) { 362 fail(t) 363 } 364 if NotBlankString(mockT, zero) { 365 fail(t) 366 } 367 } 368 369 // expect to NotBlankString 370 for _, nonBlank := range nonBlanks { 371 if !NotBlankString(mockT, nonBlank) { 372 fail(t) 373 } 374 if BlankString(mockT, nonBlank) { 375 fail(t) 376 } 377 } 378 } 379 380 func TestEmptyCollectionNotEmptyCollection(t *testing.T) { 381 mockT := &testing.T{} 382 383 empties := []interface{}{ 384 "", [0]interface{}{}, []interface{}(nil), []interface{}{}, map[interface{}]interface{}(nil), 385 map[interface{}]interface{}{}, (chan interface{})(nil), (<-chan interface{})(nil), (chan<- interface{})(nil), 386 } 387 388 nonEmpties := []interface{}{ 389 nil, "1", false, complex64(0), complex128(0), float32(0), float64(0), 390 0, int8(0), int16(0), int32(0), int64(0), byte(0), rune(0), 391 uint(0), uint8(0), uint16(0), uint32(0), uint64(0), uintptr(0), 392 [1]interface{}{1}, []interface{}{1}, map[interface{}]interface{}{1: 1}, 393 struct{ x int }{}, (*interface{})(nil), (func())(nil), 394 } 395 396 // expect to EmptyCollection 397 for _, empty := range empties { 398 if !EmptyCollection(mockT, empty) { 399 fail(t) 400 } 401 if NotEmptyCollection(mockT, empty) { 402 fail(t) 403 } 404 } 405 406 // expect to NotEmptyCollection 407 for _, nonEmpty := range nonEmpties { 408 if !NotEmptyCollection(mockT, nonEmpty) { 409 fail(t) 410 } 411 if EmptyCollection(mockT, nonEmpty) { 412 fail(t) 413 } 414 } 415 } 416 417 type customError struct{} 418 419 func (c *customError) Error() string { 420 if c == nil { 421 return "customError (nil)" 422 } 423 return "customError" 424 } 425 426 func TestErrorNilError(t *testing.T) { 427 mockT := &testing.T{} 428 429 for _, tc := range []struct { 430 give error 431 want testFlag 432 }{ 433 // expect to Error 434 {errors.New("some error"), positive}, 435 {func() error { return &customError{} }(), positive}, 436 {func() error { return (*customError)(nil) }(), positive}, 437 438 // expect to NilError 439 {nil, negative}, 440 {func() error { return nil }(), negative}, 441 } { 442 pos := Error(mockT, tc.give) 443 if (tc.want == positive && !pos) || (tc.want != positive && pos) { 444 fail(t) 445 } 446 neg := NilError(mockT, tc.give) 447 if (tc.want == negative && !neg) || (tc.want != negative && neg) { 448 fail(t) 449 } 450 } 451 } 452 453 func TestEqualErrorNotEqualError(t *testing.T) { 454 mockT := &testing.T{} 455 456 for _, tc := range []struct { 457 giveE error 458 giveS string 459 want testFlag 460 }{ 461 // expect to EqualError 462 {errors.New("some error"), "some error", positive}, 463 {func() error { return &customError{} }(), "customError", positive}, 464 {func() error { return (*customError)(nil) }(), "customError (nil)", positive}, 465 466 // expect to NotEqualError 467 {errors.New("some error"), "some errors", negative}, 468 {func() error { return (*customError)(nil) }(), "customError", negative}, 469 470 // expect to fail in all cases 471 {nil, "", abnormal}, 472 {func() error { return nil }(), "", abnormal}, 473 } { 474 pos := EqualError(mockT, tc.giveE, tc.giveS) 475 if (tc.want == positive && !pos) || (tc.want != positive && pos) { 476 fail(t) 477 } 478 neg := NotEqualError(mockT, tc.giveE, tc.giveS) 479 if (tc.want == negative && !neg) || (tc.want != negative && neg) { 480 fail(t) 481 } 482 } 483 } 484 485 func TestMatchRegexpNotMatchRegexp(t *testing.T) { 486 mockT := &testing.T{} 487 488 for _, tc := range []struct { 489 giveR interface{} 490 giveS string 491 want testFlag 492 }{ 493 // expect to MatchRegexp 494 {"^start", "start of the line", positive}, 495 {"end$", "in the end", positive}, 496 {"[0-9]{3}[.-]?[0-9]{2}[.-]?[0-9]{2}", "My phone number is 650.12.34", positive}, 497 {regexp.MustCompile("^start"), "start of the line", positive}, 498 {regexp.MustCompile("end$"), "in the end", positive}, 499 {regexp.MustCompile("[0-9]{3}[.-]?[0-9]{2}[.-]?[0-9]{2}"), "My phone number is 650.12.34", positive}, 500 501 // expect to NotMatchRegexp 502 {"^asdfastart", "Not the start of the line", negative}, 503 {"end$", "in the end.", negative}, 504 {"[0-9]{3}[.-]?[0-9]{2}[.-]?[0-9]{2}", "My phone number is 650.12a.34", negative}, 505 {regexp.MustCompile("^asdfastart"), "Not the start of the line", negative}, 506 {regexp.MustCompile("end$"), "in the end.", negative}, 507 {regexp.MustCompile("[0-9]{3}[.-]?[0-9]{2}[.-]?[0-9]{2}"), "My phone number is 650.12a.34", negative}, 508 509 // expect to fail in all cases 510 {0, "start of the line", abnormal}, 511 {regexp.Regexp{}, "-", abnormal}, 512 {"end[$", "in the end", abnormal}, 513 } { 514 pos := MatchRegexp(mockT, tc.giveR, tc.giveS) 515 if (tc.want == positive && !pos) || (tc.want != positive && pos) { 516 fail(t) 517 } 518 neg := NotMatchRegexp(mockT, tc.giveR, tc.giveS) 519 if (tc.want == negative && !neg) || (tc.want != negative && neg) { 520 fail(t) 521 } 522 } 523 } 524 525 func TestInDeltaNotInDelta(t *testing.T) { 526 mockT := &testing.T{} 527 528 for _, tc := range []struct { 529 giveA, giveB interface{} 530 giveDelta float64 531 want testFlag 532 }{ 533 // expect to InDelta 534 {1.001, 1, 0.01, positive}, 535 {1, 1.001, 0.01, positive}, 536 {1, 2, 1, positive}, 537 {2, 1, 1, positive}, 538 {uint(2), uint(1), 1, positive}, 539 {uint8(2), uint8(1), 1, positive}, 540 {uint16(2), uint16(1), 1, positive}, 541 {uint32(2), uint32(1), 1, positive}, 542 {uint64(2), uint64(1), 1, positive}, 543 {int8(2), int8(1), 1, positive}, 544 {int16(2), int16(1), 1, positive}, 545 {int32(2), int32(1), 1, positive}, 546 {int64(2), int64(1), 1, positive}, 547 {float32(2), float32(1), 1, positive}, 548 {float64(2), float64(1), 1, positive}, 549 550 // expect to NotInDelta 551 {1.001, 1, 0.0001, negative}, 552 {1, 1.001, 0.0001, negative}, 553 {1, 2, 0.5, negative}, 554 {2, 1, 0.5, negative}, 555 {uint(2), uint(1), 0.5, negative}, 556 {uint8(2), uint8(1), 0.5, negative}, 557 {uint16(2), uint16(1), 0.5, negative}, 558 {uint32(2), uint32(1), 0.5, negative}, 559 {uint64(2), uint64(1), 0.5, negative}, 560 {int8(2), int8(1), 0.5, negative}, 561 {int16(2), int16(1), 0.5, negative}, 562 {int32(2), int32(1), 0.5, negative}, 563 {int64(2), int64(1), 0.5, negative}, 564 {float32(2), float32(1), 0.5, negative}, 565 {float64(2), float64(1), 0.5, negative}, 566 567 // expect to fail in all cases 568 {"1", 1, 1, abnormal}, 569 {1, nil, 1, abnormal}, 570 {nil, "1", 1, abnormal}, 571 {42, math.NaN(), 0.01, abnormal}, 572 {math.NaN(), 42, 0.01, abnormal}, 573 {42, 42, math.NaN(), abnormal}, 574 } { 575 pos := InDelta(mockT, tc.giveA, tc.giveB, tc.giveDelta) 576 if (tc.want == positive && !pos) || (tc.want != positive && pos) { 577 fail(t) 578 } 579 neg := NotInDelta(mockT, tc.giveA, tc.giveB, tc.giveDelta) 580 if (tc.want == negative && !neg) || (tc.want != negative && neg) { 581 fail(t) 582 } 583 } 584 } 585 586 func TestInEpsilonNotInEpsilon(t *testing.T) { 587 mockT := &testing.T{} 588 589 for _, tc := range []struct { 590 giveA, giveB interface{} 591 giveDelta float64 592 want testFlag 593 }{ 594 // expect to InEpsilon 595 {uint8(2), uint16(2), .001, positive}, 596 {2.1, 2.2, 0.1, positive}, 597 {2.2, 2.1, 0.1, positive}, 598 {-2.1, -2.2, 0.1, positive}, 599 {-2.2, -2.1, 0.1, positive}, 600 {uint64(100), uint8(101), 0.01, positive}, 601 {0.1, -0.1, 2, positive}, 602 {0, 0.1, 2, positive}, 603 604 // expect to NotInEpsilon 605 {uint8(2), int16(-2), .001, negative}, 606 {uint64(100), uint8(102), 0.01, negative}, 607 {2.1, 2.2, 0.001, negative}, 608 {2.2, 2.1, 0.001, negative}, 609 {2.1, -2.2, 1, negative}, 610 {0.1, -0.1, 1.99, negative}, 611 {0, 0.1, 0.001, negative}, 612 613 // expect to fail in all cases 614 {0.1, 0, 2, abnormal}, 615 {0, math.NaN(), 1, abnormal}, 616 {math.NaN(), 1, 1, abnormal}, 617 {0, 1, math.NaN(), abnormal}, 618 {math.NaN(), math.NaN(), 1, abnormal}, 619 {"bla-bla", 2.1, 0, abnormal}, 620 {2.1, "bla-bla", 0, abnormal}, 621 } { 622 pos := InEpsilon(mockT, tc.giveA, tc.giveB, tc.giveDelta) 623 if (tc.want == positive && !pos) || (tc.want != positive && pos) { 624 fail(t) 625 } 626 neg := NotInEpsilon(mockT, tc.giveA, tc.giveB, tc.giveDelta) 627 if (tc.want == negative && !neg) || (tc.want != negative && neg) { 628 fail(t) 629 } 630 } 631 } 632 633 func TestContainNotContain(t *testing.T) { 634 mockT := &testing.T{} 635 636 type A struct{ Name, Value string } 637 str := "Hello World" 638 array := [2]string{"Foo", "Bar"} 639 slice := []string{"Foo", "Bar"} 640 complexSlice := []*A{{"b", "c"}, {"d", "e"}, {"g", "h"}, {"j", "k"}} 641 map_ := map[interface{}]interface{}{"Foo": "Bar"} 642 var zeroMap map[interface{}]interface{} 643 644 for _, tc := range []struct { 645 giveL, giveE interface{} 646 want testFlag 647 }{ 648 // expect to Contain 649 {str, "Hello", positive}, 650 {array, "Foo", positive}, 651 {slice, "Bar", positive}, 652 {complexSlice, &A{"g", "h"}, positive}, 653 {map_, "Foo", positive}, 654 655 // expect to NotContain 656 {str, "Salut", negative}, 657 {array, "Salut", negative}, 658 {slice, "Salut", negative}, 659 {complexSlice, &A{"g", "e"}, negative}, 660 {complexSlice, (*A)(nil), negative}, 661 {map_, "Bar", negative}, 662 {map_, 111, negative}, 663 {zeroMap, "Bar", negative}, 664 665 // expect to fail in all case 666 {str, 0, abnormal}, 667 {array, 1, abnormal}, 668 {slice, 2, abnormal}, 669 {0, "Hello", abnormal}, 670 {nil, "Hello", abnormal}, 671 {func() {}, "Hello", abnormal}, 672 {struct{}{}, "Hello", abnormal}, 673 } { 674 pos := Contain(mockT, tc.giveL, tc.giveE) 675 if (tc.want == positive && !pos) || (tc.want != positive && pos) { 676 fail(t) 677 } 678 neg := NotContain(mockT, tc.giveL, tc.giveE) 679 if (tc.want == negative && !neg) || (tc.want != negative && neg) { 680 fail(t) 681 } 682 } 683 } 684 685 func TestSubsetNotSubset(t *testing.T) { 686 mockT := &testing.T{} 687 688 for _, tc := range []struct { 689 giveL, giveS interface{} 690 want testFlag 691 }{ 692 // expect to Subset 693 {[]int{}, []int{}, positive}, 694 {[]int{1, 2, 3}, []int{}, positive}, 695 {[]int{1, 2, 3}, []int{1, 2}, positive}, 696 {[]int8{1, 2, 3}, []int8{3, 2}, positive}, 697 {[]uint64{1, 2, 3}, []uint64{1, 3, 2}, positive}, 698 {[]float64{.1, .2, .3}, []float64{.1}, positive}, 699 {[]string{"hello", "world"}, []string{"hello"}, positive}, 700 701 // expect to NotSubset 702 {[]int{1, 2, 3}, []int{4, 5}, negative}, 703 {[]int8{1, 2, 3}, []int8{0}, negative}, 704 {[]uint64{1, 2, 3}, []uint64{1, 5}, negative}, 705 {[]float64{.1, .2, .3}, []float64{.1, .2, .5}, negative}, 706 {[]string{"hello", "world"}, []string{"hello", "x"}, negative}, 707 708 // expect to fail in all case 709 {[]int{1, 2, 3}, nil, abnormal}, 710 {[]int{1, 2, 3}, []int8{}, abnormal}, 711 {[]uint64{1, 2, 3}, []int32{3, 2}, abnormal}, 712 {[]float64{.1, .2, .3}, []float32{.1}, abnormal}, 713 {[]int8{1, 2, 3}, uint8(1), abnormal}, 714 {[]string{"hello", "world"}, []byte{'h'}, abnormal}, 715 {"hello", []byte{'h'}, abnormal}, 716 } { 717 pos := Subset(mockT, tc.giveL, tc.giveS) 718 if (tc.want == positive && !pos) || (tc.want != positive && pos) { 719 fail(t) 720 } 721 neg := NotSubset(mockT, tc.giveL, tc.giveS) 722 if (tc.want == negative && !neg) || (tc.want != negative && neg) { 723 fail(t) 724 } 725 } 726 } 727 728 func TestElementMatchNotElementMatch(t *testing.T) { 729 mockT := &testing.T{} 730 731 for _, tc := range []struct { 732 giveA, giveB interface{} 733 want testFlag 734 }{ 735 // expect to Subset 736 {[]int{}, []int{}, positive}, 737 {[]int{1}, []int{1}, positive}, 738 {[]int{1, 1}, []int{1, 1}, positive}, 739 {[]int{1, 2}, []int{1, 2}, positive}, 740 {[]int{1, 2}, []int{2, 1}, positive}, 741 {[2]int{1, 2}, [2]int{2, 1}, positive}, 742 {[]string{"hello", "world"}, []string{"world", "hello"}, positive}, 743 {[]string{"hello", "hello"}, []string{"hello", "hello"}, positive}, 744 {[]string{"hello", "hello", "world"}, []string{"hello", "world", "hello"}, positive}, 745 {[3]string{"hello", "hello", "world"}, [3]string{"hello", "world", "hello"}, positive}, 746 747 // expect to NotSubset 748 {[]int{1}, []int{1, 1}, negative}, 749 {[]int{1, 2}, []int{2, 2}, negative}, 750 {[]int{1, 1, 1, 2}, []int{2, 1}, negative}, 751 {[]string{"hello", "hello"}, []string{"hello"}, negative}, 752 753 // expect to fail in all case 754 {nil, nil, abnormal}, 755 {[]int{}, nil, abnormal}, 756 {[]int{1, 2, 3}, []int8{1, 2, 3}, abnormal}, 757 {[]uint64{1, 2, 3}, []int32{1, 2, 3}, abnormal}, 758 {[]float64{.1, .2, .3}, []float32{.1, .2, .3}, abnormal}, 759 {[]int8{1, 2, 3}, uint8(1), abnormal}, 760 {[]string{"hello", "world"}, []byte{'h'}, abnormal}, 761 {"hello", []byte{'h'}, abnormal}, 762 } { 763 pos := ElementMatch(mockT, tc.giveA, tc.giveB) 764 if (tc.want == positive && !pos) || (tc.want != positive && pos) { 765 fail(t) 766 } 767 neg := NotElementMatch(mockT, tc.giveA, tc.giveB) 768 if (tc.want == negative && !neg) || (tc.want != negative && neg) { 769 fail(t) 770 } 771 } 772 } 773 774 type testInterface interface { 775 TestMethod() 776 } 777 778 type testStruct struct{} 779 780 func (a *testStruct) TestMethod() {} 781 782 type testStruct2 struct{} 783 784 func TestSameTypeNotSameType(t *testing.T) { 785 mockT := &testing.T{} 786 787 for _, tc := range []struct { 788 giveV, giveW interface{} 789 want testFlag 790 }{ 791 // expect to SameType 792 {1, 2, positive}, 793 {uint8('a'), byte('b'), positive}, 794 {3.14, 3.15, positive}, 795 {struct{ I string }{}, struct{ I string }{}, positive}, 796 {&testStruct{}, &testStruct{}, positive}, 797 {nil, nil, positive}, 798 {new(testInterface), (*testInterface)(nil), positive}, 799 800 // expect to NotSameType 801 {1, uint(2), negative}, 802 {'a', byte('b'), negative}, 803 {3.14, float32(3.15), negative}, 804 {struct{ I string }{}, struct{ J string }{}, negative}, 805 {&testStruct{}, &testStruct2{}, negative}, 806 {testStruct{}, &testStruct{}, negative}, 807 {new(testInterface), testInterface(nil), negative}, 808 } { 809 pos := SameType(mockT, tc.giveV, tc.giveW) 810 if (tc.want == positive && !pos) || (tc.want != positive && pos) { 811 fail(t) 812 } 813 neg := NotSameType(mockT, tc.giveV, tc.giveW) 814 if (tc.want == negative && !neg) || (tc.want != negative && neg) { 815 fail(t) 816 } 817 } 818 } 819 820 func TestImplementNotImplement(t *testing.T) { 821 mockT := &testing.T{} 822 823 for _, tc := range []struct { 824 giveV, giveW interface{} 825 want testFlag 826 }{ 827 // expect to Implement 828 {&testStruct{}, (*testInterface)(nil), positive}, 829 {&testStruct{}, new(testInterface), positive}, 830 {errors.New("test"), (*error)(nil), positive}, 831 {&customError{}, new(error), positive}, 832 {0, (*interface{})(nil), positive}, 833 {uint64(0), new(interface{}), positive}, 834 835 // expect to NotImplement 836 {testStruct{}, (*testInterface)(nil), negative}, 837 {&testStruct2{}, (*testInterface)(nil), negative}, 838 {"test", (*error)(nil), negative}, 839 {struct{}{}, new(error), negative}, 840 841 // fail in all cases 842 {"", nil, abnormal}, 843 {"", 0, abnormal}, 844 {"", new(int), abnormal}, 845 {nil, (*interface{})(nil), abnormal}, 846 } { 847 pos := Implement(mockT, tc.giveV, tc.giveW) 848 if (tc.want == positive && !pos) || (tc.want != positive && pos) { 849 fail(t) 850 } 851 neg := NotImplement(mockT, tc.giveV, tc.giveW) 852 if (tc.want == negative && !neg) || (tc.want != negative && neg) { 853 fail(t) 854 } 855 } 856 } 857 858 func TestPanicNotPanic(t *testing.T) { 859 mockT := &testing.T{} 860 861 for _, tc := range []struct { 862 give func() 863 want testFlag 864 }{ 865 // expect to Panic 866 {func() { panic("Panic!") }, positive}, 867 {func() { panic(0) }, positive}, 868 {func() { panic(nil) }, positive}, 869 870 // expect to NotPanic 871 {func() {}, negative}, 872 } { 873 pos := Panic(mockT, tc.give) 874 if (tc.want == positive && !pos) || (tc.want != positive && pos) { 875 fail(t) 876 } 877 neg := NotPanic(mockT, tc.give) 878 if (tc.want == negative && !neg) || (tc.want != negative && neg) { 879 fail(t) 880 } 881 } 882 } 883 884 func TestPanicWithValuePanicWithError(t *testing.T) { 885 mockT := &testing.T{} 886 887 for _, tc := range []struct { 888 giveF func() 889 giveW interface{} 890 want testFlag 891 }{ 892 // expect to pass PanicWithValue 893 {func() { panic("Panic!") }, "Panic!", positive}, 894 {func() { panic(0) }, 0, positive}, 895 {func() { panic(nil) }, nil, positive}, 896 {func() { panic(errors.New("panic")) }, errors.New("panic"), positive}, 897 898 // expect to fail PanicWithValue 899 {func() {}, nil, negative}, 900 {func() { panic("Panic!") }, "Panic", negative}, 901 {func() { panic(uint8(0)) }, 0, negative}, 902 {func() { panic(errors.New("panic")) }, "panic", negative}, 903 } { 904 pos := PanicWithValue(mockT, tc.giveW, tc.giveF) 905 if (tc.want == positive && !pos) || (tc.want != positive && pos) { 906 fail(t) 907 } 908 } 909 910 for _, tc := range []struct { 911 giveF func() 912 giveW string 913 want testFlag 914 }{ 915 // expect to pass PanicWithError 916 {func() { panic(errors.New("panic")) }, "panic", positive}, 917 {func() { panic((*customError)(nil)) }, "customError (nil)", positive}, 918 {func() { panic(&customError{}) }, "customError", positive}, 919 920 // expect to fail PanicWithError 921 {func() {}, "", negative}, 922 {func() { panic("Panic!") }, "", negative}, 923 {func() { panic(nil) }, "", negative}, 924 } { 925 pos := PanicWithError(mockT, tc.giveW, tc.giveF) 926 if (tc.want == positive && !pos) || (tc.want != positive && pos) { 927 fail(t) 928 } 929 } 930 } 931 932 func TestFileDirExistNotExist(t *testing.T) { 933 mockT := &testing.T{} 934 935 for _, pair := range [][2]string{ 936 {"xtesting.go", "xtesting.go_symlink"}, 937 {"xxx", "xxx_symlink"}, 938 {"../xtesting", "xtesting_symlink"}, 939 } { 940 if err := os.Symlink(pair[0], pair[1]); err != nil { 941 fail(t) 942 t.FailNow() 943 } 944 } 945 defer func() { 946 matches, _ := filepath.Glob("*_symlink") 947 for _, match := range matches { 948 if os.Remove(match) != nil { 949 fail(t) 950 t.FailNow() 951 } 952 } 953 }() 954 955 for _, tc := range []struct { 956 give string 957 wantFile testFlag 958 wantLfile testFlag 959 wantDir testFlag 960 wantLdir testFlag 961 wantLsym testFlag 962 }{ 963 {"xtesting.go", positive, positive, negative, negative, negative}, 964 {"./xtesting.go/", abnormal, abnormal, abnormal, abnormal, abnormal}, 965 {"xtesting.go_symlink", positive, positive, negative, negative, positive}, 966 {"xxx", negative, negative, negative, negative, negative}, 967 {"xxx_symlink", negative, positive, negative, negative, positive}, 968 969 {"../xtesting", negative, negative, positive, positive, negative}, 970 {"../xtesting/", negative, negative, positive, positive, negative}, 971 {"xtesting_symlink", negative, positive, positive, negative, positive}, 972 {".", negative, negative, positive, positive, negative}, 973 {"..", negative, negative, positive, positive, negative}, 974 } { 975 // File 976 pos := FileExist(mockT, tc.give) 977 if (tc.wantFile == positive && !pos) || (tc.wantFile != positive && pos) { 978 fail(t) 979 } 980 neg := FileNotExist(mockT, tc.give) 981 if (tc.wantFile == negative && !neg) || (tc.wantFile != negative && neg) { 982 fail(t) 983 } 984 pos = FileLexist(mockT, tc.give) 985 if (tc.wantLfile == positive && !pos) || (tc.wantLfile != positive && pos) { 986 fail(t) 987 } 988 neg = FileNotLexist(mockT, tc.give) 989 if (tc.wantLfile == negative && !neg) || (tc.wantLfile != negative && neg) { 990 fail(t) 991 } 992 993 // Dir 994 pos = DirExist(mockT, tc.give) 995 if (tc.wantDir == positive && !pos) || (tc.wantDir != positive && pos) { 996 fail(t) 997 } 998 neg = DirNotExist(mockT, tc.give) 999 if (tc.wantDir == negative && !neg) || (tc.wantDir != negative && neg) { 1000 fail(t) 1001 } 1002 pos = DirLexist(mockT, tc.give) 1003 if (tc.wantLdir == positive && !pos) || (tc.wantLdir != positive && pos) { 1004 fail(t) 1005 } 1006 neg = DirNotLexist(mockT, tc.give) 1007 if (tc.wantLdir == negative && !neg) || (tc.wantLdir != negative && neg) { 1008 fail(t) 1009 } 1010 1011 // Symlink 1012 pos = SymlinkLexist(mockT, tc.give) 1013 if (tc.wantLsym == positive && !pos) || (tc.wantLsym != positive && pos) { 1014 fail(t) 1015 } 1016 neg = SymlinkNotLexist(mockT, tc.give) 1017 if (tc.wantLsym == negative && !neg) || (tc.wantLsym != negative && neg) { 1018 fail(t) 1019 } 1020 } 1021 } 1022 1023 // ================================== 1024 // testings for helpers and internals 1025 // ================================== 1026 1027 func TestCombineMsgAndArgs(t *testing.T) { 1028 for _, tc := range []struct { 1029 give []interface{} 1030 want string 1031 }{ 1032 {nil, ""}, 1033 {[]interface{}{"0"}, "0"}, 1034 {[]interface{}{[]int{1, 2}}, "[1 2]"}, 1035 {[]interface{}{nil}, "<nil>"}, 1036 {[]interface{}{"a%sc", "b"}, "abc"}, 1037 } { 1038 s := combineMsgAndArgs(tc.give...) 1039 if s != tc.want { 1040 fail(t) 1041 } 1042 } 1043 } 1044 1045 type mockFinishFlagTestingT struct { 1046 testing.TB 1047 finished bool 1048 } 1049 1050 func (m *mockFinishFlagTestingT) Fail() { m.finished = false } 1051 func (m *mockFinishFlagTestingT) FailNow() { m.finished = true } 1052 func (m *mockFinishFlagTestingT) Fatal(...interface{}) { m.finished = true } 1053 1054 func TestFailTestOptions(t *testing.T) { 1055 captureStderr := func(f func()) string { 1056 stderr := os.Stderr 1057 r, w, _ := os.Pipe() 1058 os.Stderr = w 1059 defer func() { os.Stderr = stderr }() 1060 f() 1061 w.Close() 1062 bs, _ := ioutil.ReadAll(r) 1063 return string(bs) 1064 } 1065 1066 t.Run("SetExtraSkip", func(t *testing.T) { 1067 mockT := &testing.T{} 1068 1069 // 1 1070 SetExtraSkip(1) 1071 result1 := captureStderr(func() { 1072 if failTest(mockT, -1, "a", "") != false { 1073 fail(t) 1074 } 1075 }) 1076 if !strings.HasSuffix(result1, "a\n") { 1077 fail(t) 1078 } 1079 1080 // 2 1081 result2 := captureStderr(func() { 1082 if failTest(mockT, 0, "a", ", %%a%s", "bbb") != false { 1083 fail(t) 1084 } 1085 }) 1086 if !strings.HasSuffix(result2, "a, %abbb\n") { 1087 fail(t) 1088 } 1089 1090 // 3 1091 SetExtraSkip(0) 1092 result3 := captureStderr(func() { 1093 if failTest(mockT, 0, "%s", ", %s%s%03d", "xx", "yy", 3) != false { 1094 fail(t) 1095 } 1096 }) 1097 if !strings.HasSuffix(result3, "%s, xxyy003\n") { 1098 fail(t) 1099 } 1100 }) 1101 1102 t.Run("UseFailNow", func(t *testing.T) { 1103 mockT := &mockFinishFlagTestingT{} 1104 UseFailNow(true) 1105 result1 := captureStderr(func() { 1106 if failTest(mockT, 1, "") != false { 1107 fail(t) 1108 } 1109 }) 1110 if !mockT.finished { 1111 fail(t) 1112 } 1113 if !strings.HasPrefix(result1, "xtesting_test.go") { 1114 fail(t) 1115 } 1116 1117 mockT = &mockFinishFlagTestingT{} 1118 UseFailNow(false) 1119 result2 := captureStderr(func() { 1120 if failTest(mockT, 1, "") != false { 1121 fail(t) 1122 } 1123 }) 1124 if mockT.finished { 1125 fail(t) 1126 } 1127 if !strings.HasPrefix(result2, "xtesting_test.go") { 1128 fail(t) 1129 } 1130 }) 1131 } 1132 1133 func TestAssert(t *testing.T) { 1134 funcDidPanic, _ := checkPanic(func() { 1135 Assert(true, "test %s", "test") 1136 }) 1137 if funcDidPanic { 1138 fail(t) 1139 } 1140 1141 funcDidPanic, panicValue := checkPanic(func() { 1142 Assert(false, "test %s", "test") 1143 }) 1144 if !funcDidPanic { 1145 fail(t) 1146 } 1147 if panicValue != "test test" { 1148 fail(t) 1149 } 1150 } 1151 1152 func TestGoTool(t *testing.T) { 1153 defer _testGoToolFlag.Store(false) 1154 1155 _testGoToolFlag.Store(false) 1156 p, err := GoCommand() 1157 if err != nil { 1158 fail(t) 1159 } 1160 if !strings.HasPrefix(p, filepath.Join(runtime.GOROOT(), "bin")) { 1161 fail(t) 1162 } 1163 1164 _testGoToolFlag.Store(true) 1165 p, err = GoCommand() 1166 if err == nil { 1167 fail(t) 1168 } 1169 if p != "" { 1170 fail(t) 1171 } 1172 }