@@ -146,8 +146,10 @@ var (
146
146
)
147
147
148
148
const (
149
- assertionMsg = "assertion violation"
150
- gotWantFmt = ": got '%v', want '%v'"
149
+ assertionMsg = "assertion violation"
150
+ gotWantFmt = ": got '%v', want '%v'"
151
+ gotWantLongerFmt = ": got '%v', should be longer than '%v'"
152
+ gotWantShorterFmt = ": got '%v', should be shorter than '%v'"
151
153
)
152
154
153
155
// PushTester sets the current testing context for default asserter. This must
@@ -400,6 +402,32 @@ func Len(obj string, length int, a ...any) {
400
402
}
401
403
}
402
404
405
+ // Longer asserts that the length of the string is longer to the given. If not
406
+ // it panics/errors (current Asserter) with the given message. Note! This is
407
+ // reasonably fast but not as fast as 'That' because of lacking inlining for the
408
+ // current implementation of Go's type parametric functions.
409
+ func Longer (obj string , length int , a ... any ) {
410
+ l := len (obj )
411
+
412
+ if l > length {
413
+ defMsg := fmt .Sprintf (assertionMsg + gotWantLongerFmt , l , length )
414
+ Default ().reportAssertionFault (defMsg , a ... )
415
+ }
416
+ }
417
+
418
+ // Shorter asserts that the length of the string is shorter to the given. If not
419
+ // it panics/errors (current Asserter) with the given message. Note! This is
420
+ // reasonably fast but not as fast as 'That' because of lacking inlining for the
421
+ // current implementation of Go's type parametric functions.
422
+ func Shorter (obj string , length int , a ... any ) {
423
+ l := len (obj )
424
+
425
+ if l <= length {
426
+ defMsg := fmt .Sprintf (assertionMsg + gotWantShorterFmt , l , length )
427
+ Default ().reportAssertionFault (defMsg , a ... )
428
+ }
429
+ }
430
+
403
431
// SLen asserts that the length of the slice is equal to the given. If not it
404
432
// panics/errors (current Asserter) with the given message. Note! This is
405
433
// reasonably fast but not as fast as 'That' because of lacking inlining for the
@@ -413,6 +441,32 @@ func SLen[S ~[]T, T any](obj S, length int, a ...any) {
413
441
}
414
442
}
415
443
444
+ // SLonger asserts that the length of the slice is equal to the given. If not it
445
+ // panics/errors (current Asserter) with the given message. Note! This is
446
+ // reasonably fast but not as fast as 'That' because of lacking inlining for the
447
+ // current implementation of Go's type parametric functions.
448
+ func SLonger [S ~ []T , T any ](obj S , length int , a ... any ) {
449
+ l := len (obj )
450
+
451
+ if l <= length {
452
+ defMsg := fmt .Sprintf (assertionMsg + gotWantLongerFmt , l , length )
453
+ Default ().reportAssertionFault (defMsg , a ... )
454
+ }
455
+ }
456
+
457
+ // SShorter asserts that the length of the slice is equal to the given. If not it
458
+ // panics/errors (current Asserter) with the given message. Note! This is
459
+ // reasonably fast but not as fast as 'That' because of lacking inlining for the
460
+ // current implementation of Go's type parametric functions.
461
+ func SShorter [S ~ []T , T any ](obj S , length int , a ... any ) {
462
+ l := len (obj )
463
+
464
+ if l >= length {
465
+ defMsg := fmt .Sprintf (assertionMsg + gotWantShorterFmt , l , length )
466
+ Default ().reportAssertionFault (defMsg , a ... )
467
+ }
468
+ }
469
+
416
470
// MLen asserts that the length of the map is equal to the given. If not it
417
471
// panics/errors (current Asserter) with the given message. Note! This is
418
472
// reasonably fast but not as fast as 'That' because of lacking inlining for the
@@ -426,6 +480,71 @@ func MLen[M ~map[T]U, T comparable, U any](obj M, length int, a ...any) {
426
480
}
427
481
}
428
482
483
+ // MLonger asserts that the length of the map is longer to the given. If not it
484
+ // panics/errors (current Asserter) with the given message. Note! This is
485
+ // reasonably fast but not as fast as 'That' because of lacking inlining for the
486
+ // current implementation of Go's type parametric functions.
487
+ func MLonger [M ~ map [T ]U , T comparable , U any ](obj M , length int , a ... any ) {
488
+ l := len (obj )
489
+
490
+ if l <= length {
491
+ defMsg := fmt .Sprintf (assertionMsg + gotWantLongerFmt , l , length )
492
+ Default ().reportAssertionFault (defMsg , a ... )
493
+ }
494
+ }
495
+
496
+ // MShorter asserts that the length of the map is shorter to the given. If not
497
+ // it panics/errors (current Asserter) with the given message. Note! This is
498
+ // reasonably fast but not as fast as 'That' because of lacking inlining for the
499
+ // current implementation of Go's type parametric functions.
500
+ func MShorter [M ~ map [T ]U , T comparable , U any ](obj M , length int , a ... any ) {
501
+ l := len (obj )
502
+
503
+ if l >= length {
504
+ defMsg := fmt .Sprintf (assertionMsg + gotWantShorterFmt , l , length )
505
+ Default ().reportAssertionFault (defMsg , a ... )
506
+ }
507
+ }
508
+
509
+ // CLen asserts that the length of the chan is equal to the given. If not it
510
+ // panics/errors (current Asserter) with the given message. Note! This is
511
+ // reasonably fast but not as fast as 'That' because of lacking inlining for the
512
+ // current implementation of Go's type parametric functions.
513
+ func CLen [C ~ chan T , T any ](obj C , length int , a ... any ) {
514
+ l := len (obj )
515
+
516
+ if l != length {
517
+ defMsg := fmt .Sprintf (assertionMsg + gotWantFmt , l , length )
518
+ Default ().reportAssertionFault (defMsg , a ... )
519
+ }
520
+ }
521
+
522
+ // CLonger asserts that the length of the chan is longer to the given. If not it
523
+ // panics/errors (current Asserter) with the given message. Note! This is
524
+ // reasonably fast but not as fast as 'That' because of lacking inlining for the
525
+ // current implementation of Go's type parametric functions.
526
+ func CLonger [C ~ chan T , T any ](obj C , length int , a ... any ) {
527
+ l := len (obj )
528
+
529
+ if l <= length {
530
+ defMsg := fmt .Sprintf (assertionMsg + gotWantLongerFmt , l , length )
531
+ Default ().reportAssertionFault (defMsg , a ... )
532
+ }
533
+ }
534
+
535
+ // CShorter asserts that the length of the chan is shorter to the given. If not
536
+ // it panics/errors (current Asserter) with the given message. Note! This is
537
+ // reasonably fast but not as fast as 'That' because of lacking inlining for the
538
+ // current implementation of Go's type parametric functions.
539
+ func CShorter [C ~ chan T , T any ](obj C , length int , a ... any ) {
540
+ l := len (obj )
541
+
542
+ if l >= length {
543
+ defMsg := fmt .Sprintf (assertionMsg + gotWantShorterFmt , l , length )
544
+ Default ().reportAssertionFault (defMsg , a ... )
545
+ }
546
+ }
547
+
429
548
// MKeyExists asserts that the map key exists. If not it panics/errors (current
430
549
// Asserter) with the given message.
431
550
func MKeyExists [M ~ map [T ]U , T comparable , U any ](obj M , key T , a ... any ) (val U ) {
@@ -485,7 +604,9 @@ func MNotEmpty[M ~map[T]U, T comparable, U any](obj M, a ...any) {
485
604
486
605
// NoError asserts that the error is nil. If is not it panics with the given
487
606
// formatting string. Thanks to inlining, the performance penalty is equal to a
488
- // single 'if-statement' that is almost nothing.
607
+ // single 'if-statement' that is almost nothing. Note. We recommend that you
608
+ // prefer try.To every case even in tests because they work exactly the same
609
+ // during the test runs and you can use same code for both: runtime and tests.
489
610
func NoError (err error , a ... any ) {
490
611
if err != nil {
491
612
defMsg := "NoError:" + assertionMsg + ": " + err .Error ()
0 commit comments