Best Is code snippet using is.NewRelaxed
functions_test.go
Source:functions_test.go
...45 },46 }47 for name, test := range tt {48 t.Run(name, func(t *testing.T) {49 is = is.NewRelaxed(t)50 is.Equal(test.expErr, StrLength(test.s, test.minLen, test.maxLen)())51 })52 }53}54func TestMinInt(t *testing.T) {55 t.Parallel()56 is := is.New(t)57 tt := map[string]struct {58 i int59 min int60 expErr error61 }{62 "int larger than min should pass": {63 i: 10,64 min: 5,65 },66 "int equal to min should pass": {67 i: 5,68 min: 5,69 },70 "int smaller than min should fail": {71 i: 5,72 min: 50,73 expErr: fmt.Errorf(validateMin, 5, 50),74 },75 }76 for name, test := range tt {77 t.Run(name, func(t *testing.T) {78 is = is.NewRelaxed(t)79 is.Equal(test.expErr, MinInt(test.i, test.min)())80 })81 }82}83func TestMaxInt(t *testing.T) {84 t.Parallel()85 is := is.New(t)86 tt := map[string]struct {87 i int88 max int89 expErr error90 }{91 "int smaller than max should pass": {92 i: 100,93 max: 101,94 },95 "int equal to max should pass": {96 i: 5,97 max: 5,98 },99 "int larger than max should fail": {100 i: 51,101 max: 50,102 expErr: fmt.Errorf(validateMax, 51, 50),103 },104 }105 for name, test := range tt {106 t.Run(name, func(t *testing.T) {107 is = is.NewRelaxed(t)108 is.Equal(test.expErr, MaxInt(test.i, test.max)())109 })110 }111}112func TestBetweenInt(t *testing.T) {113 t.Parallel()114 is := is.New(t)115 tt := map[string]struct {116 i int117 min int118 max int119 expErr error120 }{121 "int between min and max should pass": {122 i: 100,123 min: 50,124 max: 101,125 },126 "int equal to max should pass": {127 i: 100,128 min: 50,129 max: 100,130 },131 "int equal to min should pass": {132 i: 50,133 min: 50,134 max: 100,135 },136 "int larger than max should fail": {137 i: 51,138 max: 50,139 expErr: fmt.Errorf(validateNumBetween, 51, 0, 50),140 },141 "int smaller than min should fail": {142 i: 5,143 min: 6,144 max: 50,145 expErr: fmt.Errorf(validateNumBetween, 5, 6, 50),146 },147 }148 for name, test := range tt {149 t.Run(name, func(t *testing.T) {150 is = is.NewRelaxed(t)151 is.Equal(test.expErr, BetweenInt(test.i, test.min, test.max)())152 })153 }154}155func TestMinInt64(t *testing.T) {156 t.Parallel()157 is := is.New(t)158 tt := map[string]struct {159 i int64160 min int64161 expErr error162 }{163 "int larger than min should pass": {164 i: 10,165 min: 5,166 },167 "int equal to min should pass": {168 i: 5,169 min: 5,170 },171 "int smaller than min should fail": {172 i: 5,173 min: 50,174 expErr: fmt.Errorf(validateMin, 5, 50),175 },176 }177 for name, test := range tt {178 t.Run(name, func(t *testing.T) {179 is = is.NewRelaxed(t)180 is.Equal(test.expErr, MinInt64(test.i, test.min)())181 })182 }183}184func TestMaxInt64(t *testing.T) {185 t.Parallel()186 is := is.New(t)187 tt := map[string]struct {188 i int64189 max int64190 expErr error191 }{192 "int smaller than max should pass": {193 i: 100,194 max: 101,195 },196 "int equal to max should pass": {197 i: 5,198 max: 5,199 },200 "int larger than max should fail": {201 i: 51,202 max: 50,203 expErr: fmt.Errorf(validateMax, 51, 50),204 },205 }206 for name, test := range tt {207 t.Run(name, func(t *testing.T) {208 is = is.NewRelaxed(t)209 is.Equal(test.expErr, MaxInt64(test.i, test.max)())210 })211 }212}213func TestBetweenInt64(t *testing.T) {214 t.Parallel()215 is := is.New(t)216 tt := map[string]struct {217 i int64218 min int64219 max int64220 expErr error221 }{222 "int between min and max should pass": {223 i: 100,224 min: 50,225 max: 101,226 },227 "int equal to max should pass": {228 i: 100,229 min: 50,230 max: 100,231 },232 "int equal to min should pass": {233 i: 50,234 min: 50,235 max: 100,236 },237 "int larger than max should fail": {238 i: 51,239 max: 50,240 expErr: fmt.Errorf(validateNumBetween, 51, 0, 50),241 },242 "int smaller than min should fail": {243 i: 5,244 min: 6,245 max: 50,246 expErr: fmt.Errorf(validateNumBetween, 5, 6, 50),247 },248 }249 for name, test := range tt {250 t.Run(name, func(t *testing.T) {251 is = is.NewRelaxed(t)252 is.Equal(test.expErr, BetweenInt64(test.i, test.min, test.max)())253 })254 }255}256func TestPositiveInt(t *testing.T) {257 t.Parallel()258 is := is.New(t)259 tt := map[string]struct {260 i int261 expErr error262 }{263 "int greater than 0 should pass": {264 i: 100,265 },266 "int max should pass": {267 i: 2147483647,268 },269 "int smaller than 0 should fail": {270 i: -1,271 expErr: fmt.Errorf(validatePositive, -1),272 },273 }274 for name, test := range tt {275 t.Run(name, func(t *testing.T) {276 is = is.NewRelaxed(t)277 is.Equal(test.expErr, PositiveInt(test.i)())278 })279 }280}281func TestPositiveInt64(t *testing.T) {282 t.Parallel()283 is := is.New(t)284 tt := map[string]struct {285 i int64286 expErr error287 }{288 "int64 greater than 0 should pass": {289 i: 100,290 },291 "int64 max should pass": {292 i: 9223372036854775807,293 },294 "int64 smaller than 0 should fail": {295 i: -1,296 expErr: fmt.Errorf(validatePositive, -1),297 },298 }299 for name, test := range tt {300 t.Run(name, func(t *testing.T) {301 is = is.NewRelaxed(t)302 is.Equal(test.expErr, PositiveInt64(test.i)())303 })304 }305}306func TestMatchString(t *testing.T) {307 t.Parallel()308 is := is.New(t)309 tt := map[string]struct {310 s string311 r *regexp.Regexp312 expErr error313 }{314 "string that matches should pass": {315 s: "hi there",316 r: regexp.MustCompile("[a-z ]*"),317 },318 "string that matches list should pass": {319 s: "pass",320 r: regexp.MustCompile(`(pass|fail)`),321 },322 "string that doesn't match should fail": {323 s: "oops",324 r: regexp.MustCompile(`(pass|fail)`),325 expErr: fmt.Errorf(validateRegex, "oops"),326 },327 }328 for name, test := range tt {329 t.Run(name, func(t *testing.T) {330 is = is.NewRelaxed(t)331 is.Equal(test.expErr, MatchString(test.s, test.r)())332 })333 }334}335func TestMatchBytes(t *testing.T) {336 t.Parallel()337 is := is.New(t)338 tt := map[string]struct {339 s []byte340 r *regexp.Regexp341 expErr error342 }{343 "string that matches should pass": {344 s: []byte("hi there"),345 r: regexp.MustCompile("[a-z ]*"),346 },347 "string that matches list should pass": {348 s: []byte("pass"),349 r: regexp.MustCompile(`(pass|fail)`),350 },351 "string that doesn't match should fail": {352 s: []byte("oops"),353 r: regexp.MustCompile(`(pass|fail)`),354 expErr: fmt.Errorf(validateRegex, "oops"),355 },356 }357 for name, test := range tt {358 t.Run(name, func(t *testing.T) {359 is = is.NewRelaxed(t)360 is.Equal(test.expErr, MatchBytes(test.s, test.r)())361 })362 }363}364func TestBool(t *testing.T) {365 t.Parallel()366 is := is.New(t)367 tt := map[string]struct {368 val bool369 exp bool370 expErr error371 }{372 "val matching exp should pass": {373 val: true,374 exp: true,375 }, "val matching false exp should pass": {376 val: false,377 exp: false,378 }, "val not matching exp should fail": {379 val: true,380 exp: false,381 expErr: fmt.Errorf(validateBool, true, false),382 },383 }384 for name, test := range tt {385 t.Run(name, func(t *testing.T) {386 is = is.NewRelaxed(t)387 is.Equal(test.expErr, Bool(test.val, test.exp)())388 })389 }390}391func TestDateEqual(t *testing.T) {392 t.Parallel()393 is := is.New(t)394 tt := map[string]struct {395 val time.Time396 exp time.Time397 expErr error398 }{399 "date matching should pass": {400 val: time.Date(2021, 1, 1, 1, 1, 1, 1, time.UTC),401 exp: time.Date(2021, 1, 1, 1, 1, 1, 1, time.UTC),402 },403 "date not matching should fail": {404 val: time.Date(2021, 1, 1, 1, 1, 1, 1, time.UTC),405 exp: time.Date(2021, 1, 1, 1, 1, 1, 2, time.UTC),406 expErr: fmt.Errorf(validateDateEqual,407 time.Date(2021, 1, 1, 1, 1, 1, 1, time.UTC),408 time.Date(2021, 1, 1, 1, 1, 1, 2, time.UTC)),409 },410 }411 for name, test := range tt {412 t.Run(name, func(t *testing.T) {413 is = is.NewRelaxed(t)414 is.Equal(test.expErr, DateEqual(test.val, test.exp)())415 })416 }417}418func TestDateBefore(t *testing.T) {419 t.Parallel()420 is := is.New(t)421 tt := map[string]struct {422 val time.Time423 exp time.Time424 expErr error425 }{426 "date before should pass": {427 val: time.Date(2020, 1, 1, 1, 1, 1, 1, time.UTC),428 exp: time.Date(2021, 1, 1, 1, 1, 1, 1, time.UTC),429 },430 "date matching exp should fail": {431 val: time.Date(2021, 1, 1, 1, 1, 1, 1, time.UTC),432 exp: time.Date(2021, 1, 1, 1, 1, 1, 1, time.UTC),433 expErr: fmt.Errorf(validateDateBefore,434 time.Date(2021, 1, 1, 1, 1, 1, 1, time.UTC),435 time.Date(2021, 1, 1, 1, 1, 1, 1, time.UTC)),436 },437 "date after exp should fail": {438 val: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC),439 exp: time.Date(2021, 1, 1, 1, 1, 1, 1, time.UTC),440 expErr: fmt.Errorf(validateDateBefore,441 time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC),442 time.Date(2021, 1, 1, 1, 1, 1, 1, time.UTC)),443 },444 }445 for name, test := range tt {446 t.Run(name, func(t *testing.T) {447 is = is.NewRelaxed(t)448 is.Equal(test.expErr, DateBefore(test.val, test.exp)())449 })450 }451}452func TestDateAfter(t *testing.T) {453 t.Parallel()454 is := is.New(t)455 tt := map[string]struct {456 val time.Time457 exp time.Time458 expErr error459 }{460 "date after should pass": {461 val: time.Date(2021, 1, 1, 1, 1, 1, 2, time.UTC),462 exp: time.Date(2021, 1, 1, 1, 1, 1, 1, time.UTC),463 },464 "date matching exp should fail": {465 val: time.Date(2021, 1, 1, 1, 1, 1, 1, time.UTC),466 exp: time.Date(2021, 1, 1, 1, 1, 1, 1, time.UTC),467 expErr: fmt.Errorf(validateDateAfter,468 time.Date(2021, 1, 1, 1, 1, 1, 1, time.UTC),469 time.Date(2021, 1, 1, 1, 1, 1, 1, time.UTC)),470 },471 "date before exp should fail": {472 val: time.Date(2020, 1, 1, 1, 1, 1, 1, time.UTC),473 exp: time.Date(2021, 1, 1, 1, 1, 1, 1, time.UTC),474 expErr: fmt.Errorf(validateDateAfter,475 time.Date(2020, 1, 1, 1, 1, 1, 1, time.UTC),476 time.Date(2021, 1, 1, 1, 1, 1, 1, time.UTC)),477 },478 }479 for name, test := range tt {480 t.Run(name, func(t *testing.T) {481 is = is.NewRelaxed(t)482 is.Equal(test.expErr, DateAfter(test.val, test.exp)())483 })484 }485}486func TestIsNumeric(t *testing.T) {487 t.Parallel()488 is := is.New(t)489 tt := map[string]struct {490 val string491 expErr error492 }{493 "valid number should pass": {494 val: "12345",495 },496 "valid negative number should pass": {497 val: "-12345",498 },499 "invalid number should fail": {500 val: "12345a",501 expErr: fmt.Errorf(validateIsNumeric, "12345a"),502 },503 }504 for name, test := range tt {505 t.Run(name, func(t *testing.T) {506 is = is.NewRelaxed(t)507 is.Equal(test.expErr, IsNumeric(test.val)())508 })509 }510}511func TestUKPostCode(t *testing.T) {512 t.Parallel()513 is := is.New(t)514 tt := map[string]struct {515 val []string516 expErr error517 }{518 "Valid postcodes should pass": {519 val: []string{"bt13 4GH", "NW1A 1AA", "A9A 9AA", "A9 9AA", "A99 9AA"},520 },521 "Invalid postcodes should fail": {522 val: []string{"GGG 7GH", "NW1A 1A", "N1 GF", "N11 DDD"},523 expErr: fmt.Errorf(validateUkPostCode, "GGG 7GH"),524 },525 }526 for name, test := range tt {527 t.Run(name, func(t *testing.T) {528 is = is.NewRelaxed(t)529 for _, p := range test.val {530 err := UKPostCode(p)()531 if test.expErr == nil {532 is.NoErr(err)533 continue534 }535 is.Equal(err != nil, true)536 }537 })538 }539}540func TestZipCode(t *testing.T) {541 t.Parallel()542 is := is.New(t)543 tt := map[string]struct {544 val []string545 expErr error546 }{547 "Valid zipcodes should pass": {548 val: []string{"57501", "17101", "12201-7050", "99750-0077"},549 },550 "Invalid zipcodes should fail": {551 val: []string{"GGG 7GH", "99750-00", "99750-0", "99750-", "1111"},552 expErr: fmt.Errorf(validateUkPostCode, "GGG 7GH"),553 },554 }555 for name, test := range tt {556 t.Run(name, func(t *testing.T) {557 is = is.NewRelaxed(t)558 for _, p := range test.val {559 err := USZipCode(p)()560 if test.expErr == nil {561 is.NoErr(err)562 continue563 }564 is.Equal(err != nil, true)565 }566 })567 }568}569func TestEmail(t *testing.T) {570 t.Parallel()571 is := is.New(t)572 tt := map[string]struct {573 val string574 expErr error575 }{576 "email without a domain should fail": {577 val: "test@",578 expErr: fmt.Errorf(validateEmail),579 },580 "email without a prefix": {581 val: "@test.com",582 expErr: fmt.Errorf(validateEmail),583 },584 "emails are not required to have a tld so will pass": {585 val: "test@mail",586 },587 }588 for name, test := range tt {589 t.Run(name, func(t *testing.T) {590 is = is.NewRelaxed(t)591 is.Equal(test.expErr, Email(test.val)())592 })593 }594}595func TestNotEmpty(t *testing.T) {596 t.Parallel()597 is := is.New(t)598 tt := map[string]struct {599 val interface{}600 expErr error601 }{602 "nil ptr": {603 val: nil,604 expErr: errors.New(validateEmpty),605 },606 "non-empty string": {607 val: "hello",608 },609 "empty string": {610 val: "",611 expErr: errors.New(validateEmpty),612 },613 "non-empty time": {614 val: time.Now(),615 },616 "empty time": {617 val: time.Time{},618 expErr: errors.New(validateEmpty),619 },620 "non-empty int": {621 val: 235,622 },623 "empty int": {624 val: 0,625 expErr: errors.New(validateEmpty),626 },627 "non-empty int8": {628 val: int8(5),629 },630 "empty int8": {631 val: int8(0),632 expErr: errors.New(validateEmpty),633 },634 "non-empty int16": {635 val: int16(5),636 },637 "empty int16": {638 val: int16(0),639 expErr: errors.New(validateEmpty),640 },641 "non-empty int32": {642 val: int32(5),643 },644 "empty int32": {645 val: int32(0),646 expErr: errors.New(validateEmpty),647 },648 "non-empty int64": {649 val: int64(5),650 },651 "empty int64": {652 val: int64(0),653 expErr: errors.New(validateEmpty),654 },655 "non-empty uint": {656 val: 235,657 },658 "empty uint": {659 val: 0,660 expErr: errors.New(validateEmpty),661 },662 "non-empty uint8": {663 val: uint8(5),664 },665 "empty uint8": {666 val: uint8(0),667 expErr: errors.New(validateEmpty),668 },669 "non-empty uint16": {670 val: uint16(5),671 },672 "empty uint16": {673 val: uint16(0),674 expErr: errors.New(validateEmpty),675 },676 "non-empty uint32": {677 val: uint32(5),678 },679 "empty uint32": {680 val: uint32(0),681 expErr: errors.New(validateEmpty),682 },683 "non-empty uint64": {684 val: uint64(5),685 },686 "empty uint64": {687 val: uint64(0),688 expErr: errors.New(validateEmpty),689 },690 "non-empty float32": {691 val: float32(5),692 },693 "empty float32": {694 val: float32(0),695 expErr: errors.New(validateEmpty),696 },697 "non-empty float64": {698 val: float64(5),699 },700 "empty float64": {701 val: float64(0),702 expErr: errors.New(validateEmpty),703 },704 "non-empty array": {705 val: [2]string{"hello", "there"},706 },707 "empty array": {708 val: [2]string{"", ""},709 expErr: errors.New(validateEmpty),710 },711 "non-empty slice": {712 val: []string{"hello", "there"},713 },714 "empty slice": {715 val: []string{},716 expErr: errors.New(validateEmpty),717 },718 "non-empty map": {719 val: map[string]string{"hello": "there"},720 },721 "empty map": {722 val: map[string]string{},723 expErr: errors.New(validateEmpty),724 },725 }726 for name, test := range tt {727 t.Run(name, func(t *testing.T) {728 is = is.NewRelaxed(t)729 is.Equal(test.expErr, NotEmpty(test.val)())730 })731 }732}733func TestEmpty(t *testing.T) {734 t.Parallel()735 is := is.New(t)736 tt := map[string]struct {737 val interface{}738 expErr error739 }{740 "nil ptr": {741 val: nil,742 },743 "non-empty string": {744 val: "hello",745 expErr: errors.New(validateNotEmpty),746 },747 "empty string": {748 val: "",749 },750 "non-empty time": {751 val: time.Now(),752 expErr: errors.New(validateNotEmpty),753 },754 "empty time": {755 val: time.Time{},756 },757 "non-empty int": {758 val: 235,759 expErr: errors.New(validateNotEmpty),760 },761 "empty int": {762 val: 0,763 },764 "non-empty int8": {765 val: int8(5),766 expErr: errors.New(validateNotEmpty),767 },768 "empty int8": {769 val: int8(0),770 },771 "non-empty int16": {772 val: int16(5),773 expErr: errors.New(validateNotEmpty),774 },775 "empty int16": {776 val: int16(0),777 },778 "non-empty int32": {779 val: int32(5),780 expErr: errors.New(validateNotEmpty),781 },782 "empty int32": {783 val: int32(0),784 },785 "non-empty int64": {786 val: int64(5),787 expErr: errors.New(validateNotEmpty),788 },789 "empty int64": {790 val: int64(0),791 },792 "non-empty uint": {793 val: 235,794 expErr: errors.New(validateNotEmpty),795 },796 "empty uint": {797 val: 0,798 },799 "non-empty uint8": {800 val: uint8(5),801 expErr: errors.New(validateNotEmpty),802 },803 "empty uint8": {804 val: uint8(0),805 },806 "non-empty uint16": {807 val: uint16(5),808 expErr: errors.New(validateNotEmpty),809 },810 "empty uint16": {811 val: uint16(0),812 },813 "non-empty uint32": {814 val: uint32(5),815 expErr: errors.New(validateNotEmpty),816 },817 "empty uint32": {818 val: uint32(0),819 },820 "non-empty uint64": {821 val: uint64(5),822 expErr: errors.New(validateNotEmpty),823 },824 "empty uint64": {825 val: uint64(0),826 },827 "non-empty float32": {828 val: float32(5),829 expErr: errors.New(validateNotEmpty),830 },831 "empty float32": {832 val: float32(0),833 },834 "non-empty float64": {835 val: float64(5),836 expErr: errors.New(validateNotEmpty),837 },838 "empty float64": {839 val: float64(0),840 },841 "non-empty array": {842 val: [2]string{"hello", "there"},843 expErr: errors.New(validateNotEmpty),844 },845 "empty array": {846 val: [2]string{"", ""},847 },848 "non-empty slice": {849 val: []string{"hello", "there"},850 expErr: errors.New(validateNotEmpty),851 },852 "empty slice": {853 val: []string{},854 },855 "non-empty map": {856 val: map[string]string{"hello": "there"},857 expErr: errors.New(validateNotEmpty),858 },859 "empty map": {860 val: map[string]string{},861 },862 }863 for name, test := range tt {864 t.Run(name, func(t *testing.T) {865 is = is.NewRelaxed(t)866 is.Equal(test.expErr, Empty(test.val)())867 })868 }869}870func TestAnyString(t *testing.T) {871 t.Parallel()872 is := is.New(t)873 tt := map[string]struct {874 val string875 list []string876 expErr error877 }{878 "matching item": {879 val: "hello",880 list: []string{"wow", "hello", "ohwow"},881 },882 "missing item": {883 val: "hello",884 list: []string{"wow", "goodbye", "ohwow"},885 expErr: errors.New("value not found in allowed values"),886 },887 "empty string found": {888 val: "",889 list: []string{"wow", "", "ohwow"},890 },891 "empty string not found": {892 val: "",893 list: []string{"wow", "ohwow"},894 expErr: errors.New("value not found in allowed values"),895 },896 }897 for name, test := range tt {898 t.Run(name, func(t *testing.T) {899 is = is.NewRelaxed(t)900 is.Equal(test.expErr, AnyString(test.val, test.list...)())901 })902 }903}...
is.go
Source:is.go
...52// testing.T implements this interface.53type T interface {54 // Fail indicates that the test has failed but55 // allowed execution to continue.56 // Fail is called in relaxed mode (via NewRelaxed).57 Fail()58 // FailNow indicates that the test has failed and59 // aborts the test.60 // FailNow is called in strict mode (via New).61 FailNow()62}63// I is the test helper harness.64type I struct {65 t T66 fail func()67 out io.Writer68 colorful bool69 helpers map[string]struct{} // functions to be skipped when writing file/line info70}71var noColorFlag bool72func init() {73 envNoColor := os.Getenv("IS_NO_COLOR") == "true"74 flag.BoolVar(&noColorFlag, "nocolor", envNoColor, "turns off colors")75}76// New makes a new testing helper using the specified77// T through which failures will be reported.78// In strict mode, failures call T.FailNow causing the test79// to be aborted. See NewRelaxed for alternative behavior.80func New(t T) *I {81 return &I{t, t.FailNow, os.Stdout, !noColorFlag, map[string]struct{}{}}82}83// NewRelaxed makes a new testing helper using the specified84// T through which failures will be reported.85// In relaxed mode, failures call T.Fail allowing86// multiple failures per test.87func NewRelaxed(t T) *I {88 return &I{t, t.Fail, os.Stdout, !noColorFlag, map[string]struct{}{}}89}90func (is *I) log(args ...interface{}) {91 s := is.decorate(fmt.Sprint(args...))92 fmt.Fprintf(is.out, s)93 is.fail()94}95func (is *I) logf(format string, args ...interface{}) {96 is.log(fmt.Sprintf(format, args...))97}98// Fail immediately fails the test.99//100// func Test(t *testing.T) {101// is := is.New(t)102// is.Fail() // TODO: write this test103// }104//105// In relaxed mode, execution will continue after a call to106// Fail, but that test will still fail.107func (is *I) Fail() {108 is.log("failed")109}110// True asserts that the expression is true. The expression111// code itself will be reported if the assertion fails.112//113// func Test(t *testing.T) {114// is := is.New(t)115// val := method()116// is.True(val != nil) // val should never be nil117// }118//119// Will output:120//121// your_test.go:123: not true: val != nil122func (is *I) True(expression bool) {123 if !expression {124 is.log("not true: $ARGS")125 }126}127// Equal asserts that a and b are equal.128//129// func Test(t *testing.T) {130// is := is.New(t)131// a := greet("Mat")132// is.Equal(a, "Hi Mat") // greeting133// }134//135// Will output:136//137// your_test.go:123: Hey Mat != Hi Mat // greeting138func (is *I) Equal(a, b interface{}) {139 if areEqual(a, b) {140 return141 }142 if isNil(a) || isNil(b) {143 is.logf("%s != %s", is.valWithType(a), is.valWithType(b))144 } else if reflect.ValueOf(a).Type() == reflect.ValueOf(b).Type() {145 is.logf("%v != %v", a, b)146 } else {147 is.logf("%s != %s", is.valWithType(a), is.valWithType(b))148 }149}150// New is a method wrapper around the New function.151// It allows you to write subtests using a similar152// pattern:153//154// func Test(t *testing.T) {155// is := is.New(t)156// t.Run("sub", func(t *testing.T) {157// is := is.New(t)158// // TODO: test159// })160// }161func (is *I) New(t *testing.T) *I {162 return New(t)163}164// NewRelaxed is a method wrapper around the NewRelaxed165// method. It allows you to write subtests using a similar166// pattern:167//168// func Test(t *testing.T) {169// is := is.NewRelaxed(t)170// t.Run("sub", func(t *testing.T) {171// is := is.NewRelaxed(t)172// // TODO: test173// })174// }175func (is *I) NewRelaxed(t *testing.T) *I {176 return NewRelaxed(t)177}178func (is *I) valWithType(v interface{}) string {179 if isNil(v) {180 return "<nil>"181 }182 if is.colorful {183 return fmt.Sprintf("%[1]s%[3]T(%[2]s%[3]v%[1]s)%[2]s", colorType, colorNormal, v)184 }185 return fmt.Sprintf("%[1]T(%[1]v)", v)186}187// NoErr asserts that err is nil.188//189// func Test(t *testing.T) {190// is := is.New(t)...
base83_test.go
Source:base83_test.go
...21}22func TestDecodeEncode(t *testing.T) {23 for _, test := range tests {24 t.Run(test.str, func(t *testing.T) {25 is := is.NewRelaxed(t)26 val, err := base83.Decode(test.str)27 is.NoErr(err) // Decode returned unexpected error28 is.Equal(val, test.val) // Decode got unexpected result29 })30 }31}32func TestEncode(t *testing.T) {33 for _, test := range tests {34 t.Run(test.str, func(t *testing.T) {35 is := is.NewRelaxed(t)36 str, err := base83.Encode(test.val, len(test.str))37 is.NoErr(err) // Encode returned unexpected error38 is.Equal(str, test.str) // Encode got unexpected result39 })40 }41}42func TestDecodeInvalidInput(t *testing.T) {43 tests := []struct {44 str string45 val int46 err error47 }{48 {"&", 0, base83.ErrInvalidInput},49 }50 for _, test := range tests {51 t.Run(test.str, func(t *testing.T) {52 is := is.NewRelaxed(t)53 val, err := base83.Decode(test.str)54 is.True(err != nil) // Decode should've returned error for invalid input55 is.True(strings.Contains(err.Error(), test.err.Error())) // Decode returned wrong error56 is.Equal(val, test.val) // Decode got unexpected result57 })58 }59}60func TestEncodeInvalidLength(t *testing.T) {61 tests := []struct {62 val int63 length int64 str string65 }{66 {255172974336, 3, "%%%"},67 {255172974336, 6, "%%%%%%"},68 {255172974336, 9, "000%%%%%%"},69 }70 for _, test := range tests {71 t.Run(test.str, func(t *testing.T) {72 is := is.NewRelaxed(t)73 output, err := base83.Encode(test.val, test.length)74 is.NoErr(err) // Encode should've returned error for invalid input75 is.Equal(output, test.str) // Encode got unexpected result76 })77 }78}79func BenchmarkDecode(b *testing.B) {80 for _, test := range tests {81 b.Run(test.str, func(b *testing.B) {82 b.ReportAllocs()83 for i := 0; i < b.N; i++ {84 _, _ = base83.Decode("~$")85 }86 })...
NewRelaxed
Using AI Code Generation
1import "fmt"2func main() {3 fmt.Println("hello world")4}5import "fmt"6func main() {7 fmt.Println("hello world")8}9import "fmt"10func main() {11 fmt.Println("hello world")12}13import "fmt"14func main() {15 fmt.Println("hello world")16}17import "fmt"18func main() {19 fmt.Println("hello world")20}21import "fmt"22func main() {23 fmt.Println("hello world")24}25import "fmt"26func main() {27 fmt.Println("hello world")28}29import "fmt"30func main() {31 fmt.Println("hello world")32}33import "fmt"34func main() {35 fmt.Println("hello world")36}37import "fmt"38func main() {39 fmt.Println("hello world")40}41import "fmt"42func main() {43 fmt.Println("hello world")44}45import "fmt"46func main() {47 fmt.Println("hello world")48}49import "fmt"50func main() {51 fmt.Println("hello world")52}53import "fmt"54func main() {
NewRelaxed
Using AI Code Generation
1import (2func main() {3 canvas := svg.New(os.Stdout)4 canvas.Start(200, 200)5 canvas.Circle(100, 100, 50, "fill:blue")6 canvas.End()7}8import (9func main() {10 canvas := svg.New(os.Stdout)11 canvas.Start(200, 200)12 canvas.Circle(100, 100, 50, "fill:blue")13 canvas.End()14}15import (16func main() {17 canvas := svg.New(os.Stdout)18 canvas.Start(200, 200)19 canvas.Circle(100, 100, 50, "fill:blue")20 canvas.End()21}22import (23func main() {24 canvas := svg.New(os.Stdout)25 canvas.Start(200, 200)26 canvas.Circle(100, 100, 50, "fill:blue")27 canvas.End()28}29import (30func main() {31 canvas := svg.New(os.Stdout)32 canvas.Start(200, 200)33 canvas.Circle(100, 100, 50, "fill:blue")34 canvas.End()35}36import (37func main() {38 canvas := svg.New(os.Stdout)39 canvas.Start(200, 200)40 canvas.Circle(100, 100, 50, "fill:blue")41 canvas.End()42}
NewRelaxed
Using AI Code Generation
1func main() {2 fmt.Println(is.NewRelaxed().Is("foo", "foo"))3}4func main() {5 fmt.Println(is.NewStrict().Is("foo", "foo"))6}
NewRelaxed
Using AI Code Generation
1import (2func main() {3 g := graph.New(6)4 g.Add(0, 1)5 g.Add(1, 2)6 g.Add(2, 3)7 g.Add(3, 4)8 g.Add(4, 5)9 g.Add(5, 0)10 b, p := g.IsBipartite()11 fmt.Println(b, p)12}13Check whether a graph is a tree or not | Set 2 (Using DFS)14Check whether a graph is a tree or not | Set 1 (Using BFS)15Check whether a given graph is Bipartite or not using BFS | Set 3 (Using Queue)16Check whether a given graph is Bipartite or not using DFS | Set 3 (Using Stack)17Check whether a given graph is Bipartite or not using BFS | Set 4 (Using Queue)18Check whether a given graph is Bipartite or not using DFS | Set 4 (Using Stack)19Check whether a given graph is Bipartite or not using BFS | Set 5 (Using Queue)20Check whether a given graph is Bipartite or not using DFS | Set 5 (Using Stack)21Check whether a given graph is Bipartite or not using BFS | Set 6 (Using Queue
NewRelaxed
Using AI Code Generation
1import "fmt"2func main() {3 fmt.Println("Hello, playground")4 is := is.NewRelaxed()5 fmt.Println(is.Email("email"))6}7import "fmt"8func main() {9 fmt.Println("Hello, playground")10 is := is.New()11 fmt.Println(is.Email("email"))12}13import "fmt"14func main() {15 fmt.Println("Hello, playground")16 is := is.New()17 fmt.Println(is.Email("email"))18}19import "fmt"20func main() {21 fmt.Println("Hello, playground")22 is := is.New()23 fmt.Println(is.Email("email"))24}25import "fmt"26func main() {27 fmt.Println("Hello, playground")28 is := is.New()29 fmt.Println(is.Email("email"))30}31import "fmt"32func main() {33 fmt.Println("Hello, playground")34 is := is.New()35 fmt.Println(is.Email("email"))36}37import "fmt"38func main() {39 fmt.Println("Hello, playground")40 is := is.New()41 fmt.Println(is.Email("email"))42}43import "fmt"44func main() {45 fmt.Println("Hello, playground")46 is := is.New()47 fmt.Println(is.Email("email"))48}49import "fmt"50func main() {51 fmt.Println("Hello, playground")52 is := is.New()53 fmt.Println(is.Email("email"))54}55import "fmt"56func main() {57 fmt.Println("Hello, playground")58 is := is.New()59 fmt.Println(is.Email("email"))60}
NewRelaxed
Using AI Code Generation
1import (2func main() {3 fmt.Println("Hello, playground")4 var c clock.Clock = clock.NewMock()5 fmt.Println(c.Now())6 fmt.Println(c.Now())
NewRelaxed
Using AI Code Generation
1import (2func main() {3 fmt.Println(is.NewRelaxed("abcde").Matches("abcde"))4 fmt.Println(is.NewRelaxed("abcde").Matches("abcde"))5 fmt.Println(is.NewRelaxed("abcde").Matches("abcde"))6}
NewRelaxed
Using AI Code Generation
1import (2func main() {3 re := regexp.MustCompilePOSIX("a(.*)b", regexp.IgnoreCase)4 match := re.FindString("A123b")5 fmt.Println(match)6}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!