Skip to content

Commit 420ceda

Browse files
committed
in progress: assert pkg manual inlining generic functions
1 parent d3ea782 commit 420ceda

File tree

3 files changed

+146
-104
lines changed

3 files changed

+146
-104
lines changed

assert/assert.go

Lines changed: 60 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ func tester() (t testing.TB) {
266266
// Note that when [Plain] asserter is used ([SetDefault]), optional arguments
267267
// are used to override the auto-generated assert violation message.
268268
func NotImplemented(a ...any) {
269-
current().reportAssertionFault("not implemented", a)
269+
current().reportAssertionFault(0, "not implemented", a)
270270
}
271271

272272
// ThatNot asserts that the term is NOT true. If is it panics with the given
@@ -278,7 +278,7 @@ func NotImplemented(a ...any) {
278278
func ThatNot(term bool, a ...any) {
279279
if term {
280280
defMsg := assertionMsg
281-
current().reportAssertionFault(defMsg, a)
281+
current().reportAssertionFault(0, defMsg, a)
282282
}
283283
}
284284

@@ -291,7 +291,7 @@ func ThatNot(term bool, a ...any) {
291291
func That(term bool, a ...any) {
292292
if !term {
293293
defMsg := assertionMsg
294-
current().reportAssertionFault(defMsg, a)
294+
current().reportAssertionFault(0, defMsg, a)
295295
}
296296
}
297297

@@ -303,7 +303,7 @@ func That(term bool, a ...any) {
303303
func NotNil[P ~*T, T any](p P, a ...any) {
304304
if p == nil {
305305
defMsg := assertionMsg + ": pointer shouldn't be nil"
306-
current().reportAssertionFault(defMsg, a)
306+
current().reportAssertionFault(0, defMsg, a)
307307
}
308308
}
309309

@@ -315,7 +315,7 @@ func NotNil[P ~*T, T any](p P, a ...any) {
315315
func Nil[T any](p *T, a ...any) {
316316
if p != nil {
317317
defMsg := assertionMsg + ": pointer should be nil"
318-
current().reportAssertionFault(defMsg, a)
318+
current().reportAssertionFault(0, defMsg, a)
319319
}
320320
}
321321

@@ -333,7 +333,7 @@ func Nil[T any](p *T, a ...any) {
333333
func INil(i any, a ...any) {
334334
if i != nil {
335335
defMsg := assertionMsg + ": interface should be nil"
336-
current().reportAssertionFault(defMsg, a)
336+
current().reportAssertionFault(0, defMsg, a)
337337
}
338338
}
339339

@@ -351,7 +351,7 @@ func INil(i any, a ...any) {
351351
func INotNil(i any, a ...any) {
352352
if i == nil {
353353
defMsg := assertionMsg + ": interface shouldn't be nil"
354-
current().reportAssertionFault(defMsg, a)
354+
current().reportAssertionFault(0, defMsg, a)
355355
}
356356
}
357357

@@ -363,7 +363,7 @@ func INotNil(i any, a ...any) {
363363
func SNil[S ~[]T, T any](s S, a ...any) {
364364
if s != nil {
365365
defMsg := assertionMsg + ": slice should be nil"
366-
current().reportAssertionFault(defMsg, a)
366+
current().reportAssertionFault(0, defMsg, a)
367367
}
368368
}
369369

@@ -375,7 +375,7 @@ func SNil[S ~[]T, T any](s S, a ...any) {
375375
func CNil[C ~chan T, T any](c C, a ...any) {
376376
if c != nil {
377377
defMsg := assertionMsg + ": channel shouldn't be nil"
378-
current().reportAssertionFault(defMsg, a)
378+
current().reportAssertionFault(0, defMsg, a)
379379
}
380380
}
381381

@@ -387,7 +387,7 @@ func CNil[C ~chan T, T any](c C, a ...any) {
387387
func MNil[M ~map[T]U, T comparable, U any](m M, a ...any) {
388388
if m != nil {
389389
defMsg := assertionMsg + ": map should be nil"
390-
current().reportAssertionFault(defMsg, a)
390+
current().reportAssertionFault(0, defMsg, a)
391391
}
392392
}
393393

@@ -399,7 +399,7 @@ func MNil[M ~map[T]U, T comparable, U any](m M, a ...any) {
399399
func SNotNil[S ~[]T, T any](s S, a ...any) {
400400
if s == nil {
401401
defMsg := assertionMsg + ": slice shouldn't be nil"
402-
current().reportAssertionFault(defMsg, a)
402+
current().reportAssertionFault(0, defMsg, a)
403403
}
404404
}
405405

@@ -411,7 +411,7 @@ func SNotNil[S ~[]T, T any](s S, a ...any) {
411411
func CNotNil[C ~chan T, T any](c C, a ...any) {
412412
if c == nil {
413413
defMsg := assertionMsg + ": channel shouldn't be nil"
414-
current().reportAssertionFault(defMsg, a)
414+
current().reportAssertionFault(0, defMsg, a)
415415
}
416416
}
417417

@@ -423,7 +423,7 @@ func CNotNil[C ~chan T, T any](c C, a ...any) {
423423
func MNotNil[M ~map[T]U, T comparable, U any](m M, a ...any) {
424424
if m == nil {
425425
defMsg := assertionMsg + ": map shouldn't be nil"
426-
current().reportAssertionFault(defMsg, a)
426+
current().reportAssertionFault(0, defMsg, a)
427427
}
428428
}
429429

@@ -456,12 +456,12 @@ func Equal[T comparable](val, want T, a ...any) {
456456

457457
func doShouldBeEqual[T comparable](val, want T, a []any) {
458458
defMsg := fmt.Sprintf(assertionMsg+gotWantFmt, val, want)
459-
current().newReportAssertionFault(defMsg, a)
459+
current().reportAssertionFault(1, defMsg, a)
460460
}
461461

462462
func doShouldNotBeEqual[T comparable](val, want T, a []any) {
463463
defMsg := fmt.Sprintf(assertionMsg+": got '%v' want (!= '%v')", val, want)
464-
current().reportAssertionFault(defMsg, a)
464+
current().reportAssertionFault(1, defMsg, a)
465465
}
466466

467467
// DeepEqual asserts that the (whatever) values are equal. If not it
@@ -474,7 +474,7 @@ func doShouldNotBeEqual[T comparable](val, want T, a []any) {
474474
func DeepEqual(val, want any, a ...any) {
475475
if !reflect.DeepEqual(val, want) {
476476
defMsg := fmt.Sprintf(assertionMsg+gotWantFmt, val, want)
477-
current().reportAssertionFault(defMsg, a)
477+
current().reportAssertionFault(0, defMsg, a)
478478
}
479479
}
480480

@@ -493,7 +493,7 @@ func DeepEqual(val, want any, a ...any) {
493493
func NotDeepEqual(val, want any, a ...any) {
494494
if reflect.DeepEqual(val, want) {
495495
defMsg := fmt.Sprintf(assertionMsg+": got '%v', want (!= '%v')", val, want)
496-
current().reportAssertionFault(defMsg, a)
496+
current().reportAssertionFault(0, defMsg, a)
497497
}
498498
}
499499

@@ -530,7 +530,7 @@ func Longer(s string, length int, a ...any) {
530530

531531
if l <= length {
532532
defMsg := fmt.Sprintf(assertionMsg+gotWantLongerFmt, l, length)
533-
current().reportAssertionFault(defMsg, a)
533+
current().reportAssertionFault(0, defMsg, a)
534534
}
535535
}
536536

@@ -549,7 +549,7 @@ func Shorter(str string, length int, a ...any) {
549549

550550
if l >= length {
551551
defMsg := fmt.Sprintf(assertionMsg+gotWantShorterFmt, l, length)
552-
current().reportAssertionFault(defMsg, a)
552+
current().reportAssertionFault(0, defMsg, a)
553553
}
554554
}
555555

@@ -586,7 +586,7 @@ func SLonger[S ~[]T, T any](obj S, length int, a ...any) {
586586

587587
if l <= length {
588588
defMsg := fmt.Sprintf(assertionMsg+gotWantLongerFmt, l, length)
589-
current().reportAssertionFault(defMsg, a)
589+
current().reportAssertionFault(0, defMsg, a)
590590
}
591591
}
592592

@@ -605,7 +605,7 @@ func SShorter[S ~[]T, T any](obj S, length int, a ...any) {
605605

606606
if l >= length {
607607
defMsg := fmt.Sprintf(assertionMsg+gotWantShorterFmt, l, length)
608-
current().reportAssertionFault(defMsg, a)
608+
current().reportAssertionFault(0, defMsg, a)
609609
}
610610
}
611611

@@ -642,7 +642,7 @@ func MLonger[M ~map[T]U, T comparable, U any](obj M, length int, a ...any) {
642642

643643
if l <= length {
644644
defMsg := fmt.Sprintf(assertionMsg+gotWantLongerFmt, l, length)
645-
current().reportAssertionFault(defMsg, a)
645+
current().reportAssertionFault(0, defMsg, a)
646646
}
647647
}
648648

@@ -661,7 +661,7 @@ func MShorter[M ~map[T]U, T comparable, U any](obj M, length int, a ...any) {
661661

662662
if l >= length {
663663
defMsg := fmt.Sprintf(assertionMsg+gotWantShorterFmt, l, length)
664-
current().reportAssertionFault(defMsg, a)
664+
current().reportAssertionFault(0, defMsg, a)
665665
}
666666
}
667667

@@ -680,7 +680,7 @@ func CLen[C ~chan T, T any](obj C, length int, a ...any) {
680680

681681
if l != length {
682682
defMsg := fmt.Sprintf(assertionMsg+gotWantFmt, l, length)
683-
current().reportAssertionFault(defMsg, a)
683+
current().reportAssertionFault(0, defMsg, a)
684684
}
685685
}
686686

@@ -699,7 +699,7 @@ func CLonger[C ~chan T, T any](obj C, length int, a ...any) {
699699

700700
if l <= length {
701701
defMsg := fmt.Sprintf(assertionMsg+gotWantLongerFmt, l, length)
702-
current().reportAssertionFault(defMsg, a)
702+
current().reportAssertionFault(0, defMsg, a)
703703
}
704704
}
705705

@@ -718,7 +718,7 @@ func CShorter[C ~chan T, T any](obj C, length int, a ...any) {
718718

719719
if l >= length {
720720
defMsg := fmt.Sprintf(assertionMsg+gotWantShorterFmt, l, length)
721-
current().reportAssertionFault(defMsg, a)
721+
current().reportAssertionFault(0, defMsg, a)
722722
}
723723
}
724724

@@ -732,12 +732,16 @@ func MKeyExists[M ~map[T]U, T comparable, U any](obj M, key T, a ...any) (val U)
732732
val, ok = obj[key]
733733

734734
if !ok {
735-
defMsg := fmt.Sprintf(assertionMsg+": key '%v' doesn't exist", key)
736-
current().reportAssertionFault(defMsg, a)
735+
doMKeyExists(key, a)
737736
}
738737
return val
739738
}
740739

740+
func doMKeyExists[T comparable](key T, a []any) {
741+
defMsg := fmt.Sprintf(assertionMsg+": key '%v' doesn't exist", key)
742+
current().reportAssertionFault(1, defMsg, a)
743+
}
744+
741745
// NotEmpty asserts that the string is not empty. If it is, it panics/errors
742746
// (according the current Asserter) with the auto-generated message. You can
743747
// append the generated got-want message by using optional message arguments.
@@ -747,7 +751,7 @@ func MKeyExists[M ~map[T]U, T comparable, U any](obj M, key T, a ...any) (val U)
747751
func NotEmpty(obj string, a ...any) {
748752
if obj == "" {
749753
defMsg := assertionMsg + ": string shouldn't be empty"
750-
current().reportAssertionFault(defMsg, a)
754+
current().reportAssertionFault(0, defMsg, a)
751755
}
752756
}
753757

@@ -760,7 +764,7 @@ func NotEmpty(obj string, a ...any) {
760764
func Empty(obj string, a ...any) {
761765
if obj != "" {
762766
defMsg := assertionMsg + ": string should be empty"
763-
current().reportAssertionFault(defMsg, a)
767+
current().reportAssertionFault(0, defMsg, a)
764768
}
765769
}
766770

@@ -777,8 +781,7 @@ func SEmpty[S ~[]T, T any](obj S, a ...any) {
777781
l := len(obj)
778782

779783
if l != 0 {
780-
defMsg := assertionMsg + ": slice should be empty"
781-
current().reportAssertionFault(defMsg, a)
784+
doEmptyNamed("", "slice", a)
782785
}
783786
}
784787

@@ -795,8 +798,7 @@ func SNotEmpty[S ~[]T, T any](obj S, a ...any) {
795798
l := len(obj)
796799

797800
if l == 0 {
798-
defMsg := assertionMsg + ": slice shouldn't be empty"
799-
current().reportAssertionFault(defMsg, a)
801+
doEmptyNamed("not", "slice", a)
800802
}
801803
}
802804

@@ -815,8 +817,7 @@ func MEmpty[M ~map[T]U, T comparable, U any](obj M, a ...any) {
815817
l := len(obj)
816818

817819
if l != 0 {
818-
defMsg := assertionMsg + ": map should be empty"
819-
current().reportAssertionFault(defMsg, a)
820+
doEmptyNamed("", "map", a)
820821
}
821822
}
822823

@@ -835,11 +836,16 @@ func MNotEmpty[M ~map[T]U, T comparable, U any](obj M, a ...any) {
835836
l := len(obj)
836837

837838
if l == 0 {
838-
defMsg := assertionMsg + ": map shouldn't be empty"
839-
current().reportAssertionFault(defMsg, a)
839+
doEmptyNamed("not", "map", a)
840840
}
841841
}
842842

843+
func doEmptyNamed(not, name string, a []any) {
844+
not = x.Whom(not == "not", " not ", "")
845+
defMsg := assertionMsg + ": " + name + " should" + not + "be empty"
846+
current().reportAssertionFault(1, defMsg, a)
847+
}
848+
843849
// NoError asserts that the error is nil. If is not it panics with the given
844850
// formatting string. Thanks to inlining, the performance penalty is equal to a
845851
// single 'if-statement' that is almost nothing.
@@ -853,7 +859,7 @@ func MNotEmpty[M ~map[T]U, T comparable, U any](obj M, a ...any) {
853859
func NoError(err error, a ...any) {
854860
if err != nil {
855861
defMsg := assertionMsg + conCatErrStr + err.Error()
856-
current().reportAssertionFault(defMsg, a)
862+
current().reportAssertionFault(0, defMsg, a)
857863
}
858864
}
859865

@@ -866,7 +872,7 @@ func NoError(err error, a ...any) {
866872
func Error(err error, a ...any) {
867873
if err == nil {
868874
defMsg := "Error:" + assertionMsg + ": missing error"
869-
current().reportAssertionFault(defMsg, a)
875+
current().reportAssertionFault(0, defMsg, a)
870876
}
871877
}
872878

@@ -878,11 +884,15 @@ func Error(err error, a ...any) {
878884
// are used to override the auto-generated assert violation message.
879885
func Greater[T Number](val, want T, a ...any) {
880886
if val <= want {
881-
defMsg := fmt.Sprintf(assertionMsg+": got '%v', want <= '%v'", val, want)
882-
current().reportAssertionFault(defMsg, a)
887+
doGreater(val, want, a)
883888
}
884889
}
885890

891+
func doGreater[T Number](val, want T, a []any) {
892+
defMsg := fmt.Sprintf(assertionMsg+": got '%v', want <= '%v'", val, want)
893+
current().reportAssertionFault(1, defMsg, a)
894+
}
895+
886896
// Less asserts that the value is less than want. If it is not it panics and
887897
// builds a violation message. Thanks to inlining, the performance penalty is
888898
// equal to a single 'if-statement' that is almost nothing.
@@ -891,11 +901,15 @@ func Greater[T Number](val, want T, a ...any) {
891901
// are used to override the auto-generated assert violation message.
892902
func Less[T Number](val, want T, a ...any) {
893903
if val >= want {
894-
defMsg := fmt.Sprintf(assertionMsg+": got '%v', want >= '%v'", val, want)
895-
current().reportAssertionFault(defMsg, a)
904+
doLess(val, want, a)
896905
}
897906
}
898907

908+
func doLess[T Number](val, want T, a []any) {
909+
defMsg := fmt.Sprintf(assertionMsg+": got '%v', want >= '%v'", val, want)
910+
current().reportAssertionFault(1, defMsg, a)
911+
}
912+
899913
// Zero asserts that the value is 0. If it is not it panics and builds a
900914
// violation message. Thanks to inlining, the performance penalty is equal to a
901915
// single 'if-statement' that is almost nothing.
@@ -910,7 +924,7 @@ func Zero[T Number](val T, a ...any) {
910924

911925
func doZero[T Number](val T, a []any) {
912926
defMsg := fmt.Sprintf(assertionMsg+": got '%v', want (== '0')", val)
913-
current().newReportAssertionFault(defMsg, a)
927+
current().reportAssertionFault(1, defMsg, a)
914928
}
915929

916930
// NotZero asserts that the value != 0. If it is not it panics and builds a
@@ -927,7 +941,7 @@ func NotZero[T Number](val T, a ...any) {
927941

928942
func doNotZero[T Number](val T, a []any) {
929943
defMsg := fmt.Sprintf(assertionMsg+": got '%v', want (!= 0)", val)
930-
current().newReportAssertionFault(defMsg, a)
944+
current().reportAssertionFault(1, defMsg, a)
931945
}
932946

933947
// current returns a current default asserter used for package-level

0 commit comments

Comments
 (0)