-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuiltin.go
More file actions
1074 lines (933 loc) · 38.8 KB
/
builtin.go
File metadata and controls
1074 lines (933 loc) · 38.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package assert
import (
"fmt"
"regexp"
"testing"
)
// NotContainsElement tests whether the array or slice contains the specified element or not, and
// it set the result to fail if the array or slice does not contain the specified element. It'll
// panic if the `source` is not an array or a slice.
//
// assert.ContainsElement(t, []int{1, 2, 3}, 1) // success
// assert.ContainsElement(t, []int{1, 2, 3}, 3) // success
// assert.ContainsElement(t, []int{1, 2, 3}, 4) // fail
func ContainsElement(t *testing.T, source, expect any, message ...any) error {
t.Helper()
return tryContainsElement(t, false, source, expect, message...)
}
// ContainsElementNow tests whether the array or slice contains the specified element or not, and
// it will terminate the execution if the array or slice does not contain the specified element.
// It'll panic if the `source` is not an array or a slice.
//
// assert.ContainsElementNow(t, []int{1, 2, 3}, 1) // success
// assert.ContainsElementNow(t, []int{1, 2, 3}, 3) // success
// assert.ContainsElementNow(t, []int{1, 2, 3}, 4) // fail and stop the execution
// // never runs
func ContainsElementNow(t *testing.T, source, expect any, message ...any) error {
t.Helper()
return tryContainsElement(t, true, source, expect, message...)
}
// NotContainsElement tests whether the array or slice contains the specified element or not, and
// it set the result to fail if the array or slice contains the specified element. It'll panic if
// the `source` is not an array or a slice.
//
// assert.NotContainsElement(t, []int{1, 2, 3}, 4) // success
// assert.NotContainsElement(t, []int{1, 2, 3}, 0) // success
// assert.NotContainsElement(t, []int{1, 2, 3}, 1) // fail
func NotContainsElement(t *testing.T, source, expect any, message ...any) error {
t.Helper()
return tryNotContainsElement(t, false, source, expect, message...)
}
// NotContainsElementNow tests whether the array or slice contains the specified element or not,
// and it will terminate the execution if the array or slice contains the specified element. It'll
// panic if the `source` is not an array or a slice.
//
// assert.NotContainsElementNow(t, []int{1, 2, 3}, 4) // success
// assert.NotContainsElementNow(t, []int{1, 2, 3}, 0) // success
// assert.NotContainsElementNow(t, []int{1, 2, 3}, 1) // fail and stop the execution
// // never runs
func NotContainsElementNow(t *testing.T, source, expect any, message ...any) error {
t.Helper()
return tryNotContainsElement(t, true, source, expect, message...)
}
// ContainsString tests whether the string contains the substring or not, and it set the result to
// fail if the string does not contains the substring.
//
// assert.ContainsString(t, "Hello world", "") // success
// assert.ContainsString(t, "Hello world", "Hello") // success
// assert.ContainsString(t, "Hello world", "world") // success
// assert.ContainsString(t, "Hello world", "hello") // fail
func ContainsString(t *testing.T, str, substr string, message ...any) error {
t.Helper()
return tryContainsString(t, false, str, substr, message...)
}
// ContainsStringNow tests whether the string contains the substring or not, and it will terminate the
// execution if the string does not contains the substring.
//
// assert.ContainsStringNow(t, "Hello world", "") // success
// assert.ContainsStringNow(t, "Hello world", "Hello") // success
// assert.ContainsStringNow(t, "Hello world", "world") // success
// assert.ContainsStringNow(t, "Hello world", "hello") // fail and stop the execution
// // never runs
func ContainsStringNow(t *testing.T, str, substr string, message ...any) error {
t.Helper()
return tryContainsString(t, true, str, substr, message...)
}
// Gt compares the values and sets the result to false if the first value is not greater than to
// the second value.
//
// assert.Gt(t, 2, 1) // success
// assert.Gt(t, 3.14, 1.68) // success
// assert.Gt(t, "BCD", "ABC") // success
// assert.Gt(t, 2, 2) // fail
// assert.Gt(t, 1, 2) // fail
func Gt(t *testing.T, v1, v2 any, message ...string) error {
t.Helper()
return tryCompareOrderableValues(
t,
false,
compareTypeGreater,
v1, v2,
fmt.Sprintf(defaultErrMessageGt, v1, v2),
message...,
)
}
// GtNow compares the values and sets the result to false if the first value is not greater than to
// the second value. It will panic if they do not match the expected result.
//
// assert.GtNow(t, 2, 1) // success
// assert.GtNow(t, 3.14, 1.68) // success
// assert.GtNow(t, "BCD", "ABC") // success
// assert.GtNow(t, 1, 2) // fail and terminate
// // never runs
func GtNow(t *testing.T, v1, v2 any, message ...string) error {
t.Helper()
return tryCompareOrderableValues(
t,
true,
compareTypeGreater,
v1, v2,
fmt.Sprintf(defaultErrMessageGt, v1, v2),
message...,
)
}
// Gte compares the values and sets the result to false if the first value is not greater than or
// equal to the second value.
//
// assert.Gte(t, 2, 1) // success
// assert.Gte(t, 3.14, 1.68) // success
// assert.Gte(t, "BCD", "ABC") // success
// assert.Gte(t, 2, 2) // success
// assert.Gte(t, 1, 2) // fail
func Gte(t *testing.T, v1, v2 any, message ...string) error {
t.Helper()
return tryCompareOrderableValues(
t,
false,
compareTypeEqual|compareTypeGreater,
v1, v2,
fmt.Sprintf(defaultErrMessageGte, v1, v2),
message...,
)
}
// GteNow compares the values and sets the result to false if the first value is not greater than
// or equal to the second value. It will panic if they do not match the expected result.
//
// assert.GteNow(t, 2, 1) // success
// assert.GteNow(t, 3.14, 1.68) // success
// assert.GteNow(t, "BCD", "ABC") // success
// assert.GteNow(t, 2, 2) // success
// assert.GteNow(t, 1, 2) // fail and terminate
// // never runs
func GteNow(t *testing.T, v1, v2 any, message ...string) error {
t.Helper()
return tryCompareOrderableValues(
t,
true,
compareTypeEqual|compareTypeGreater,
v1, v2,
fmt.Sprintf(defaultErrMessageGte, v1, v2),
message...,
)
}
// Lt compares the values and sets the result to false if the first value is not less than the
// second value.
//
// assert.Lt(t, 1, 2) // success
// assert.Lt(t, 1.68, 3.14) // success
// assert.Lt(t, "ABC", "BCD") // success
// assert.Lt(t, 2, 2) // fail
// assert.Lt(t, 2, 1) // fail
func Lt(t *testing.T, v1, v2 any, message ...string) error {
t.Helper()
return tryCompareOrderableValues(
t,
false,
compareTypeLess,
v1, v2,
fmt.Sprintf(defaultErrMessageLt, v1, v2),
message...,
)
}
// LtNow compares the values and sets the result to false if the first value is not less than the
// second value. It will panic if they do not match the expected result.
//
// assert.LtNow(t, 1, 2) // success
// assert.LtNow(t, 1.68, 3.14) // success
// assert.LtNow(t, "ABC", "BCD") // success
// assert.LtNow(t, 2, 1) // fail and terminate
// // never runs
func LtNow(t *testing.T, v1, v2 any, message ...string) error {
t.Helper()
return tryCompareOrderableValues(
t,
true,
compareTypeLess,
v1, v2,
fmt.Sprintf(defaultErrMessageLt, v1, v2),
message...,
)
}
// Lte compares the values and sets the result to false if the first value is not less than or
// equal to the second value.
//
// assert.Lte(t, 1, 2) // success
// assert.Lte(t, 1.68, 3.14) // success
// assert.Lte(t, "ABC", "BCD") // success
// assert.Lte(t, 2, 2) // success
// assert.Lte(t, 2, 1) // fail
func Lte(t *testing.T, v1, v2 any, message ...string) error {
t.Helper()
return tryCompareOrderableValues(
t,
false,
compareTypeEqual|compareTypeLess,
v1, v2,
fmt.Sprintf(defaultErrMessageLte, v1, v2),
message...,
)
}
// LteNow compares the values and sets the result to false if the first value is not less than or
// equal to the second value. It will panic if they do not match the expected result.
//
// assert.LteNow(t, 1, 2) // success
// assert.LteNow(t, 1.68, 3.14) // success
// assert.LteNow(t, "ABC", "BCD") // success
// assert.LteNow(t, 2, 2) // success
// assert.LteNow(t, 2, 1) // fail and terminate
// // never runs
func LteNow(t *testing.T, v1, v2 any, message ...string) error {
t.Helper()
return tryCompareOrderableValues(
t,
true,
compareTypeEqual|compareTypeLess,
v1, v2,
fmt.Sprintf(defaultErrMessageLte, v1, v2),
message...,
)
}
// NotContainsString tests whether the string contains the substring or not, and it set the result
// to fail if the string contains the substring.
//
// assert.NotContainsString(t, "Hello world", "") // fail
// assert.NotContainsString(t, "Hello world", "Hello") // fail
// assert.NotContainsString(t, "Hello world", "world") // fail
// assert.NotContainsString(t, "Hello world", "hello") // success
func NotContainsString(t *testing.T, str, substr string, message ...any) error {
t.Helper()
return tryNotContainsString(t, false, str, substr, message...)
}
// NotContainsStringNow tests whether the string contains the substring or not, and it will terminate the
// execution if the string contains the substring.
//
// assert.NotContainsStringNow(t, "Hello world", "hello") // success
// assert.NotContainsStringNow(t, "Hello world", "Hello") // fail and stop the execution
// // never runs
func NotContainsStringNow(t *testing.T, str, substr string, message ...any) error {
t.Helper()
return tryNotContainsString(t, true, str, substr, message...)
}
// DeepEqual tests the deep equality between actual and expect parameters. It'll set the result to
// fail if they are not deeply equal, and it doesn't stop the execution.
//
// assert.DeepEqual(t, 1, 1) // success
// assert.DeepEqual(t, "ABC", "ABC") // success
// assert.DeepEqual(t, 1, 0) // fail
// assert.DeepEqual(t, 1, int64(1)) // fail
func DeepEqual(t *testing.T, actual, expect any, message ...any) error {
t.Helper()
return tryDeepEqual(t, false, actual, expect, message...)
}
// DeepEqualNow tests the deep equality between actual and expect parameters, and it'll stop the
// execution if they are not deeply equal.
//
// assert.DeepEqualNow(t, 1, 1) // success
// assert.DeepEqualNow(t, "ABC", "ABC") // success
// assert.DeepEqualNow(t, 1, int64(1)) // fail and terminate
// // never run
func DeepEqualNow(t *testing.T, actual, expect any, message ...any) error {
t.Helper()
return tryDeepEqual(t, true, actual, expect, message...)
}
// NotDeepEqual tests the deep inequality between actual and expected parameters. It'll set the
// result to fail if they are deeply equal, but it doesn't stop the execution.
//
// assert.NotDeepEqual(t, 1, 0) // success
// assert.NotDeepEqual(t, 1, int64(1)) // success
// assert.NotDeepEqual(t, 1, 1) // fail
// assert.NotDeepEqual(t, "ABC", "ABC") // fail
func NotDeepEqual(t *testing.T, actual, expect any, message ...any) error {
t.Helper()
return tryNotDeepEqual(t, false, actual, expect, message...)
}
// NotDeepEqualNow tests the deep inequality between actual and expected parameters, and it'll stop
// the execution if they are deeply equal.
//
// assert.NotDeepEqual(t, 1, 0) // success
// assert.NotDeepEqual(t, 1, int64(1)) // success
// assert.NotDeepEqual(t, "ABC", "ABC") // fail and terminate
// // never run
func NotDeepEqualNow(t *testing.T, actual, expect any, message ...any) error {
t.Helper()
return tryNotDeepEqual(t, true, actual, expect, message...)
}
// Equal tests the equality between actual and expect parameters. It'll set the result to fail if
// they are not equal, and it doesn't stop the execution.
//
// assert.Equal(t, 1, 1) // success
// assert.Equal(t, "ABC", "ABC") // success
// assert.Equal(t, 1, int64(1)) // success
// assert.Equal(t, 1, uint64(1)) // fail
// assert.Equal(t, 1, 0) // fail
func Equal(t *testing.T, actual, expect any, message ...any) error {
t.Helper()
return tryEqual(t, false, actual, expect, message...)
}
// EqualNow tests the equality between actual and expect parameters, and it'll stop the execution
// if they are not equal.
//
// assert.EqualNow(t, 1, 1) // success
// assert.EqualNow(t, "ABC", "ABC") // success
// assert.EqualNow(t, 1, int64(1)) // success
// assert.EqualNow(t, 1, 0) // fail and terminate
// never run
func EqualNow(t *testing.T, actual, expect any, message ...any) error {
t.Helper()
return tryEqual(t, true, actual, expect, message...)
}
// NotEqual tests the inequality between actual and expected parameters. It'll set the result to
// fail if they are equal, but it doesn't stop the execution.
//
// assert.NotEqual(t, 1, 0) // success
// assert.NotEqual(t, "ABC", "CBA") // success
// assert.NotEqual(t, 1, uint64(1)) // success
// assert.NotEqual(t, 1, 1) // fail
// assert.NotEqual(t, "ABC", "ABC") // fail
// assert.NotEqual(t, 1, int64(1)) // fail
func NotEqual(t *testing.T, actual, expect any, message ...any) error {
t.Helper()
return tryNotEqual(t, false, actual, expect, message...)
}
// NotEqualNow tests the inequality between actual and expected parameters, and it'll stop the
// execution if they are equal.
//
// assert.NotEqualNow(t, 1, 0) // success
// assert.NotEqualNow(t, "ABC", "CBA") // success
// assert.NotEqualNow(t, 1, 1) // fail and terminate
// // never run
func NotEqualNow(t *testing.T, actual, expect any, message ...any) error {
t.Helper()
return tryNotEqual(t, true, actual, expect, message...)
}
// FloatEqual tests the equality between actual and expect floating numbers with epsilon. It'll
// set the result to fail if they are not equal, and it doesn't stop the execution.
//
// FloatEqual(t, 1.0, 1.0, 0.1) // success
// FloatEqual(t, 1.0, 1.01, 0.1) // success
// FloatEqual(t, 1.0, 1.2, 0.1) // fail
func FloatEqual(t *testing.T, actual, expect, epsilon any, message ...any) error {
t.Helper()
return tryFloatEqual(t, false, actual, expect, epsilon, message...)
}
// FloatEqualNow tests the equality between actual and expect floating numbers with epsilon, and
// it'll stop the execution if they are not equal.
//
// FloatEqualNow(t, 1.0, 1.0, 0.1) // success
// FloatEqualNow(t, 1.0, 1.01, 0.1) // success
// FloatEqualNow(t, 1.0, 1.2, 0.1) // fail and terminate
func FloatEqualNow(t *testing.T, actual, expect, epsilon any, message ...any) error {
t.Helper()
return tryFloatEqual(t, true, actual, expect, epsilon, message...)
}
// FloatNotEqual tests the inequality between actual and expect floating numbers with epsilon. It'll
// set the result to fail if they are equal, but it doesn't stop the execution.
//
// FloatNotEqual(t, 1.0, 1.2, 0.1) // success
// FloatNotEqual(t, 1.0, 1.1, 0.1) // success
// FloatNotEqual(t, 1.0, 1.0, 0.1) // fail
func FloatNotEqual(t *testing.T, actual, expect, epsilon any, message ...any) error {
t.Helper()
return tryFloatNotEqual(t, false, actual, expect, epsilon, message...)
}
// FloatNotEqualNow tests the inequality between actual and expect floating numbers with epsilon,
// and it'll stop the execution if they are equal.
//
// FloatNotEqualNow(t, 1.0, 1.2, 0.1) // success
// FloatNotEqualNow(t, 1.0, 1.1, 0.1) // success
// FloatNotEqualNow(t, 1.0, 1.0, 0.1) // fail and terminate
func FloatNotEqualNow(t *testing.T, actual, expect, epsilon any, message ...any) error {
t.Helper()
return tryFloatNotEqual(t, true, actual, expect, epsilon, message...)
}
// HasPrefixString tests whether the string has the prefix string or not, and it set the result to
// fail if the string does not have the prefix string.
//
// assert.HasPrefixString(t, "Hello world", "") // success
// assert.HasPrefixString(t, "Hello world", "Hello") // success
// assert.HasPrefixString(t, "Hello world", "world") // fail
// assert.HasPrefixString(t, "Hello world", "hello") // fail
func HasPrefixString(t *testing.T, str, prefix string, message ...any) error {
t.Helper()
return tryHasPrefixString(t, false, str, prefix, message...)
}
// HasPrefixStringNow tests whether the string has the prefix string or not, and it will terminate
// the execution if the string does not have the prefix string.
//
// assert.HasPrefixStringNow(t, "Hello world", "") // success
// assert.HasPrefixStringNow(t, "Hello world", "Hello") // success
// assert.HasPrefixStringNow(t, "Hello world", "hello") // fail and stop the execution
// // never runs
func HasPrefixStringNow(t *testing.T, str, prefix string, message ...any) error {
t.Helper()
return tryHasPrefixString(t, true, str, prefix, message...)
}
// NotHasPrefixString tests whether the string has the prefix string or not, and it set the result
// to fail if the string have the prefix string.
//
// assert.NotHasPrefixString(t, "Hello world", "hello") // success
// assert.NotHasPrefixString(t, "Hello world", "world") // success
// assert.NotHasPrefixString(t, "Hello world", "") // fail
// assert.NotHasPrefixString(t, "Hello world", "Hello") // fail
func NotHasPrefixString(t *testing.T, str, prefix string, message ...any) error {
t.Helper()
return tryNotHasPrefixString(t, false, str, prefix, message...)
}
// NotHasPrefixStringNow tests whether the string has the prefix string or not, and it will
// terminate the execution if the string have the prefix string.
//
// assert.NotHasPrefixStringNow(t, "Hello world", "hello") // success
// assert.NotHasPrefixStringNow(t, "Hello world", "world") // success
// assert.NotHasPrefixStringNow(t, "Hello world", "Hello") // fail and stop the execution
// // never runs
func NotHasPrefixStringNow(t *testing.T, str, prefix string, message ...any) error {
t.Helper()
return tryNotHasPrefixString(t, true, str, prefix, message...)
}
// HasSuffixString tests whether the string has the suffix string or not, and it set the result to
// fail if the string does not have the suffix string.
//
// assert.HasSuffixString(t, "Hello world", "") // success
// assert.HasSuffixString(t, "Hello world", "world") // success
// assert.HasSuffixString(t, "Hello world", "World") // fail
// assert.HasSuffixString(t, "Hello world", "hello") // fail
func HasSuffixString(t *testing.T, str, suffix string, message ...any) error {
t.Helper()
return tryHasSuffixString(t, false, str, suffix, message...)
}
// HasSuffixStringNow tests whether the string has the suffix string or not, and it will terminate
// the execution if the string does not have the suffix string.
//
// assert.HasSuffixStringNow(t, "Hello world", "") // success
// assert.HasSuffixStringNow(t, "Hello world", "world") // success
// assert.HasSuffixStringNow(t, "Hello world", "World") // fail and stop the execution
// // never runs
func HasSuffixStringNow(t *testing.T, str, suffix string, message ...any) error {
t.Helper()
return tryHasSuffixString(t, true, str, suffix, message...)
}
// NotHasSuffixString tests whether the string has the suffix string or not, and it set the result
// to fail if the string have the suffix string.
//
// assert.NotHasSuffixString(t, "Hello world", "Hello") // success
// assert.NotHasSuffixString(t, "Hello world", "World") // success
// assert.NotHasSuffixString(t, "Hello world", "") // fail
// assert.NotHasSuffixString(t, "Hello world", "world") // fail
func NotHasSuffixString(t *testing.T, str, suffix string, message ...any) error {
t.Helper()
return tryNotHasSuffixString(t, false, str, suffix, message...)
}
// NotHasSuffixStringNow tests whether the string has the suffix string or not, and it will
// terminate the execution if the string have the suffix string.
//
// assert.NotHasSuffixStringNow(t, "Hello world", "hello") // success
// assert.NotHasSuffixStringNow(t, "Hello world", "World") // success
// assert.NotHasSuffixStringNow(t, "Hello world", "world") // fail and stop the execution
// // never runs
func NotHasSuffixStringNow(t *testing.T, str, suffix string, message ...any) error {
t.Helper()
return tryNotHasSuffixString(t, true, str, suffix, message...)
}
// IsError tests whether the error matches the target or not. It'll set the result to fail if the
// error does not match to the target error, and it doesn't stop the execution.
//
// err1 := errors.New("error 1")
// err2 := errors.New("error 2")
// err3 := errors.New("error 3")
// assert.IsError(t, err1, err1) // success
// assert.IsError(t, err1, err2) // fail
// assert.IsError(t, errors.Join(err1, err2), err1) // success
// assert.IsError(t, errors.Join(err1, err2), err2) // success
// assert.IsError(t, errors.Join(err1, err2), err3) // fail
func IsError(t *testing.T, err, expected error, message ...any) error {
return isError(t, false, err, expected, message...)
}
// IsErrorNow tests whether the error matches the target or not. It'll set the result to fail and
// stop the execution if the error does not match to the target error.
//
// err1 := errors.New("error 1")
// err2 := errors.New("error 2")
// assert.IsErrorNow(t, errors.Join(err1, err2), err1) // success
// assert.IsErrorNow(t, errors.Join(err1, err2), err2) // success
// assert.IsErrorNow(t, err1, err1) // success
// assert.IsErrorNow(t, err1, err2) // fail
// // never runs
func IsErrorNow(t *testing.T, err, expected error, message ...any) error {
return isError(t, true, err, expected, message...)
}
// NotIsError tests whether the error matches the target or not. It'll set the result to fail if
// the error matches to the target error, and it doesn't stop the execution.
//
// err1 := errors.New("error 1")
// err2 := errors.New("error 2")
// err3 := errors.New("error 3")
// assert.NotIsError(t, err1, err2) // success
// assert.NotIsError(t, err1, err1) // fail
// assert.NotIsError(t, errors.Join(err1, err2), err3) // success
// assert.NotIsError(t, errors.Join(err1, err2), err1) // fail
// assert.NotIsError(t, errors.Join(err1, err2), err2) // fail
func NotIsError(t *testing.T, err, unexpected error, message ...any) error {
return notIsError(t, false, err, unexpected, message...)
}
// NotIsErrorNow tests whether the error matches the target or not. It'll set the result to fail
// and stop the execution if the error matches to the target error.
//
// err1 := errors.New("error 1")
// err2 := errors.New("error 2")
// err3 := errors.new("error 3")
// assert.NotIsErrorNow(t, errors.Join(err1, err2), err3) // success
// assert.NotIsErrorNow(t, err1, err2) // fail
// assert.NotIsErrorNow(t, err1, err1) // fail and terminate
// // never runs
func NotIsErrorNow(t *testing.T, err, unexpected error, message ...any) error {
return notIsError(t, true, err, unexpected, message...)
}
// MapHasKey tests whether the map contains the specified key or not, it will fail if the map does
// not contain the key, or the type of the key cannot assign to the type of the key of the map.
//
// assert.MapHasKey(t, map[string]int{"a":1}, "a") // success
// assert.MapHasKey(t, map[string]int{"a":1}, "b") // fail
// assert.MapHasKey(t, map[string]int{"a":1}, 1) // fail
func MapHasKey(t *testing.T, m, key any, message ...any) error {
t.Helper()
return tryMapHasKey(t, false, m, key, message...)
}
// MapHasKeyNow tests whether the map contains the specified key or not, and it will terminate the
// execution if the test fails. It will fail if the map does not contain the key, or the type of
// the key cannot assign to the type of the key of the map.
//
// assert.MapHasKeyNow(t, map[string]int{"a":1}, "a") // success
// assert.MapHasKeyNow(t, map[string]int{"a":1}, "b") // fail and terminate
// // never run
func MapHasKeyNow(t *testing.T, m, key any, message ...any) error {
t.Helper()
return tryMapHasKey(t, true, m, key, message...)
}
// NotMapHasKey tests whether the map contains the specified key or not, it will fail if the map
// contain the key. It will also set the test result to success if the type of the key cannot
// assign to the type of the key of the map.
//
// assert.NotMapHasKey(t, map[string]int{"a":1}, "b") // success
// assert.NotMapHasKey(t, map[string]int{"a":1}, 1) // success
// assert.NotMapHasKey(t, map[string]int{"a":1}, "a") // fail
func NotMapHasKey(t *testing.T, m, key any, message ...any) error {
t.Helper()
return tryNotMapHasKey(t, false, m, key, message...)
}
// NotMapHasKeyNow tests whether the map contains the specified key or not, it will fail if the map
// contain the key, and it will terminate the execution if the test fails. It will also set the
// test result to success if the type of the key cannot assign to the type of the key of the map.
//
// assert.NotMapHasKeyNow(t, map[string]int{"a":1}, "b") // success
// assert.NotMapHasKeyNow(t, map[string]int{"a":1}, 1) // success
// assert.NotMapHasKeyNow(t, map[string]int{"a":1}, "a") // fail and terminate
// // never run
func NotMapHasKeyNow(t *testing.T, m, key any, message ...any) error {
t.Helper()
return tryNotMapHasKey(t, true, m, key, message...)
}
// MapHasValue tests whether the map contains the specified value or not, it will fail if the map
// does not contain the value, or the type of the value cannot assign to the type of the values of
// the map.
//
// assert.MapHasValue(t, map[string]int{"a":1}, 1) // success
// assert.MapHasValue(t, map[string]int{"a":1}, 2) // fail
// assert.MapHasValue(t, map[string]int{"a":1}, "a") // fail
func MapHasValue(t *testing.T, m, value any, message ...any) error {
t.Helper()
return tryMapHasValue(t, false, m, value, message...)
}
// MapHasValueNow tests whether the map contains the specified value or not, and it will terminate
// the execution if the test fails. It will fail if the map does not contain the value, or the type
// of the value cannot assign to the type of the value of the map.
//
// assert.MapHasValueNow(t, map[string]int{"a":1}, 1) // success
// assert.MapHasValueNow(t, map[string]int{"a":1}, 2) // fail and terminate
// // never run
func MapHasValueNow(t *testing.T, m, value any, message ...any) error {
t.Helper()
return tryMapHasValue(t, true, m, value, message...)
}
// NotMapHasValue tests whether the map contains the specified value or not, it will fail if the
// map contain the value. It will also set the test result to success if the type of the value
// cannot assign to the type of the value of the map.
//
// assert.NotMapHasValue(t, map[string]int{"a":1}, 2) // success
// assert.NotMapHasValue(t, map[string]int{"a":1}, "a") // success
// assert.NotMapHasValue(t, map[string]int{"a":1}, 1) // fail
func NotMapHasValue(t *testing.T, m, value any, message ...any) error {
t.Helper()
return tryNotMapHasValue(t, false, m, value, message...)
}
// NotMapHasValueNow tests whether the map contains the specified value or not, it will fail if the
// map contain the value, and it will terminate the execution if the test fails. It will also set
// the test result to success if the type of the value cannot assign to the type of the value of
// the map.
//
// assert.NotMapHasValueNow(t, map[string]int{"a":1}, 2) // success
// assert.NotMapHasValueNow(t, map[string]int{"a":1}, "a") // success
// assert.NotMapHasValueNow(t, map[string]int{"a":1}, 1) // fail and terminate
// // never run
func NotMapHasValueNow(t *testing.T, m, value any, message ...any) error {
t.Helper()
return tryNotMapHasValue(t, true, m, value, message...)
}
// Match tests whether the string matches the regular expression or not.
//
// pattern := regexp.MustCompile(`^https?:\/\/`)
// assert.Match(t, "http://example.com", pattern) // success
// assert.Match(t, "example.com", pattern) // fail
func Match(t *testing.T, val string, pattern *regexp.Regexp, message ...any) error {
t.Helper()
return tryMatchRegexp(t, false, val, pattern, "", message...)
}
// MatchNow tests whether the string matches the regular expression or not, and it will terminate
// the execution if it does not match.
//
// pattern := regexp.MustCompile(`^https?:\/\/`)
// assert.MatchNow(t, "http://example.com", pattern) // success
// assert.MatchNow(t, "example.com", pattern) // fail and terminate
// // never run
func MatchNow(t *testing.T, val string, pattern *regexp.Regexp, message ...any) error {
t.Helper()
return tryMatchRegexp(t, true, val, pattern, "", message...)
}
// MatchString will compile the pattern and test whether the string matches the regular expression
// or not. It will panic if the pattern is not a valid regular expression.
//
// assert.MatchString(t, "http://example.com", `^https?:\/\/`) // success
// assert.MatchString(t, "example.com", `^https?:\/\/`) // fail
func MatchString(t *testing.T, val, pattern string, message ...any) error {
t.Helper()
return tryMatchRegexp(t, false, val, nil, pattern, message...)
}
// MatchStringNow will compile the pattern and test whether the string matches the regular
// expression or not. It will terminate the execution if it does not match, and it will panic if
// the pattern is not a valid regular expression.
//
// assert.MatchStringNow(t, "http://example.com", `^https?:\/\/`) // success
// assert.MatchStringNow(t, "example.com", `^https?:\/\/`) // fail and terminate
// // never run
func MatchStringNow(t *testing.T, val, pattern string, message ...any) error {
t.Helper()
return tryMatchRegexp(t, true, val, nil, pattern, message...)
}
// NotMatch tests whether the string matches the regular expression or not, and it set the result
// to fail if the string matches the pattern.
//
// pattern := regexp.MustCompile(`^https?:\/\/`)
// assert.NotMatch(t, "example.com", pattern) // success
// assert.NotMatch(t, "http://example.com", pattern) // fail
func NotMatch(t *testing.T, val string, pattern *regexp.Regexp, message ...any) error {
t.Helper()
return tryNotMatchRegexp(t, false, val, pattern, "", message...)
}
// NotMatchNow tests whether the string matches the regular expression or not, and it will
// terminate the execution if the string matches the pattern.
//
// pattern := regexp.MustCompile(`^https?:\/\/`)
// assert.NotMatchNow(t, "example.com", pattern) // success
// assert.NotMatchNow(t, "http://example.com", pattern) // fail and terminate
// // never run
func NotMatchNow(t *testing.T, val string, pattern *regexp.Regexp, message ...any) error {
t.Helper()
return tryNotMatchRegexp(t, true, val, pattern, "", message...)
}
// MatchString will compile the pattern and test whether the string matches the regular expression
// or not, and it set the result to fail if the string matches the pattern. It will also panic if
// the pattern is not a valid regular expression.
//
// assert.NotMatchString(t, "example.com", `^https?:\/\/`) // success
// assert.NotMatchString(t, "http://example.com", `^https?:\/\/`) // fail
func NotMatchString(t *testing.T, val, pattern string, message ...any) error {
t.Helper()
return tryNotMatchRegexp(t, false, val, nil, pattern, message...)
}
// NotMatchStringNow will compile the pattern and test whether the string matches the regular
// expression or not, and it set the result to fail if the string matches the pattern. It will
// terminate the execution if the string matches the pattern, and it will panic if the pattern is
// not a valid regular expression.
//
// assert.NotMatchStringNow(t, "example.com", `^https?:\/\/`) // success
// assert.NotMatchStringNow(t, "http://example.com", `^https?:\/\/`) // fail and terminate
// // never run
func NotMatchStringNow(t *testing.T, val, pattern string, message ...any) error {
t.Helper()
return tryNotMatchRegexp(t, true, val, nil, pattern, message...)
}
// Nil tests whether a value is nil or not, and it'll fail when the value is not nil. It will
// always return false if the value is a bool, an integer, a floating number, a complex, or a
// string.
//
// var err error // nil
// assert.Nil(t, err) // success
//
// err = errors.New("some error")
// assert.Nil(t, err) // fail
func Nil(t *testing.T, val any, message ...any) error {
t.Helper()
return tryNil(t, false, val, message...)
}
// NilNow tests whether a value is nil or not, and it'll fail when the value is not nil. It will
// always return false if the value is a bool, an integer, a floating number, a complex, or a
// string.
//
// This function will set the result to fail, and stop the execution if the value is not nil.
//
// var err error // nil
// assert.NilNow(t, err) // success
//
// err = errors.New("some error")
// assert.NilNow(t, err) // fail and terminate
// // never run
func NilNow(t *testing.T, val any, message ...any) error {
t.Helper()
return tryNil(t, true, val, message...)
}
// NotNil tests whether a value is nil or not, and it'll fail when the value is nil. It will
// always return true if the value is a bool, an integer, a floating number, a complex, or a
// string.
//
// var err error // nil
// assert.NotNil(t, err) // fail
//
// err = errors.New("some error")
// assert.NotNil(t, err) // success
func NotNil(t *testing.T, val any, message ...any) error {
t.Helper()
return tryNotNil(t, false, val, message...)
}
// NotNilNow tests whether a value is nil or not, and it'll fail when the value is nil. It will
// always return true if the value is a bool, an integer, a floating number, a complex, or a
// string.
//
// This function will set the result to fail, and stop the execution if the value is nil.
//
// var err error = errors.New("some error")
// assert.NotNilNow(t, err) // success
//
// err = nil
// assert.NotNilNow(t, err) // fail and terminate
// // never run
func NotNilNow(t *testing.T, val any, message ...any) error {
t.Helper()
return tryNotNil(t, true, val, message...)
}
// Panic expects the function fn to panic, and it'll set the result to fail if the function doesn't
// panic.
//
// assert.Panic(t, func() {
// panic("some error")
// }) // success
//
// assert.Panic(t, func() {
// // no panic
// }) // fail
func Panic(t *testing.T, fn func(), message ...any) error {
t.Helper()
return tryPanic(t, false, fn, message...)
}
// PanicNow expects the function fn to panic. It'll set the result to fail if the function doesn't
// panic, and stop the execution.
//
// assert.PanicNow(t, func() {
// panic("some error")
// }) // success
//
// assert.PanicNow(t, func() {
// // no panic
// }) // fail
// // never run
func PanicNow(t *testing.T, fn func(), message ...any) error {
t.Helper()
return tryPanic(t, true, fn, message...)
}
// NotPanic asserts that the function fn does not panic, and it'll set the result to fail if the
// function panic.
//
// assert.NotPanic(t, func() {
// // no panic
// }) // success
//
// assert.NotPanic(t, func() {
// panic("some error")
// }) // fail
func NotPanic(t *testing.T, fn func(), message ...any) error {
t.Helper()
return tryNotPanic(t, false, fn, message...)
}
// NotPanicNow asserts that the function fn does not panic. It'll set the result to fail if the
// function panic, and it also stops the execution.
//
// assert.NotPanicNow(t, func() {
// // no panic
// }) // success
//
// assert.NotPanicNow(t, func() {
// panic("some error")
// }) // fail and terminate
// // never run
func NotPanicNow(t *testing.T, fn func(), message ...any) error {
t.Helper()
return tryNotPanic(t, true, fn, message...)
}
// PanicOf expects the function fn to panic by the expected error. If the function does not panic
// or panic for another reason, it will set the result to fail.
//
// assert.PanicOf(t, func() {
// panic("expected error")
// }, "expected error") // success
// assert.PanicOf(t, func() {
// panic("unexpected error")
// }, "expected error") // fail
// assert.PanicOf(t, func() {
// // ..., no panic
// }, "expected error") // fail
func PanicOf(t *testing.T, fn func(), expectErr any, message ...any) error {
t.Helper()
return tryPanicOf(t, false, fn, expectErr, message...)
}
// PanicOfNow expects the function fn to panic by the expected error. If the function does not
// panic or panic for another reason, it will set the result to fail and terminate the execution.
//
// assert.PanicOfNow(t, func() {
// panic("expected error")
// }, "expected error") // success
// assert.PanicOfNow(t, func() {
// panic("unexpected error")
// }, "expected error") // fail and terminated
// // never runs
func PanicOfNow(t *testing.T, fn func(), expectErr any, message ...any) error {
t.Helper()
return tryPanicOf(t, true, fn, expectErr, message...)
}
// NotPanicOf expects the function fn not panic, or the function does not panic by the unexpected
// error. If the function panics by the unexpected error, it will set the result to fail.
//
// assert.NotPanicOf(t, func() {
// panic("other error")
// }, "unexpected error") // success
// assert.NotPanicOf(t, func() {
// // ..., no panic
// }, "unexpected error") // success
// assert.NotPanicOf(t, func() {
// panic("unexpected error")
// }, "unexpected error") // fail
func NotPanicOf(t *testing.T, fn func(), unexpectedErr any, message ...any) error {
t.Helper()
return tryNotPanicOf(t, false, fn, unexpectedErr, message...)
}
// NotPanicOfNow expects the function fn not panic, or the function does not panic by the
// unexpected error. If the function panics by the unexpected error, it will set the result to fail
// and stop the execution.
//
// assert.NotPanicOfNow(t, func() {
// panic("other error")
// }, "unexpected error") // success
// assert.NotPanicOfNow(t, func() {
// // ..., no panic