forked from go-openapi/testify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassert_assertions.go
More file actions
1752 lines (1667 loc) · 49.3 KB
/
assert_assertions.go
File metadata and controls
1752 lines (1667 loc) · 49.3 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
// SPDX-FileCopyrightText: Copyright 2025 go-swagger maintainers
// SPDX-License-Identifier: Apache-2.0
// Code generated with github.com/go-openapi/testify/codegen/v2; DO NOT EDIT.
// Generated on 2026-01-11 (version ca82e58) using codegen version v2.1.9-0.20260111184010-ca82e58db12c+dirty [sha: ca82e58db12cbb61bfcae58c3684b3add9599d10]
package assert
import (
"net/http"
"net/url"
"reflect"
"time"
"github.com/go-openapi/testify/v2/internal/assertions"
)
// Condition uses a [Comparison] to assert a complex condition.
//
// # Usage
//
// assertions.Condition(t, func() bool { return myCondition })
//
// # Examples
//
// success: func() bool { return true }
// failure: func() bool { return false }
//
// Upon failure, the test [T] is marked as failed and continues execution.
func Condition(t T, comp Comparison, msgAndArgs ...any) bool {
if h, ok := t.(H); ok {
h.Helper()
}
return assertions.Condition(t, comp, msgAndArgs...)
}
// Contains asserts that the specified string, list(array, slice...) or map contains the
// specified substring or element.
//
// # Usage
//
// assertions.Contains(t, "Hello World", "World")
// assertions.Contains(t, []string{"Hello", "World"}, "World")
// assertions.Contains(t, map[string]string{"Hello": "World"}, "Hello")
//
// # Examples
//
// success: []string{"A","B"}, "A"
// failure: []string{"A","B"}, "C"
//
// Upon failure, the test [T] is marked as failed and continues execution.
func Contains(t T, s any, contains any, msgAndArgs ...any) bool {
if h, ok := t.(H); ok {
h.Helper()
}
return assertions.Contains(t, s, contains, msgAndArgs...)
}
// DirExists checks whether a directory exists in the given path. It also fails
// if the path is a file rather a directory or there is an error checking whether it exists.
//
// # Usage
//
// assertions.DirExists(t, "path/to/directory")
//
// # Examples
//
// success: filepath.Join(testDataPath(),"existing_dir")
// failure: filepath.Join(testDataPath(),"non_existing_dir")
//
// Upon failure, the test [T] is marked as failed and continues execution.
func DirExists(t T, path string, msgAndArgs ...any) bool {
if h, ok := t.(H); ok {
h.Helper()
}
return assertions.DirExists(t, path, msgAndArgs...)
}
// ElementsMatch asserts that the specified listA(array, slice...) is equal to specified
// listB(array, slice...) ignoring the order of the elements. If there are duplicate elements,
// the number of appearances of each of them in both lists should match.
//
// # Usage
//
// assertions.ElementsMatch(t, [1, 3, 2, 3], [1, 3, 3, 2])
//
// # Examples
//
// success: []int{1, 3, 2, 3}, []int{1, 3, 3, 2}
// failure: []int{1, 2, 3}, []int{1, 2, 4}
//
// Upon failure, the test [T] is marked as failed and continues execution.
func ElementsMatch(t T, listA any, listB any, msgAndArgs ...any) (ok bool) {
if h, ok := t.(H); ok {
h.Helper()
}
return assertions.ElementsMatch(t, listA, listB, msgAndArgs...)
}
// Empty asserts that the given value is "empty".
//
// Zero values are "empty".
//
// Arrays are "empty" if every element is the zero value of the type (stricter than "empty").
//
// Slices, maps and channels with zero length are "empty".
//
// Pointer values are "empty" if the pointer is nil or if the pointed value is "empty".
//
// # Usage
//
// assertions.Empty(t, obj)
//
// # Examples
//
// success: ""
// failure: "not empty"
//
// Upon failure, the test [T] is marked as failed and continues execution.
//
// [Zero values]: https://go.dev/ref/spec#The_zero_value
func Empty(t T, object any, msgAndArgs ...any) bool {
if h, ok := t.(H); ok {
h.Helper()
}
return assertions.Empty(t, object, msgAndArgs...)
}
// Equal asserts that two objects are equal.
//
// Pointer variable equality is determined based on the equality of the
// referenced values (as opposed to the memory addresses).
//
// Function equality cannot be determined and will always fail.
//
// # Usage
//
// assertions.Equal(t, 123, 123)
//
// # Examples
//
// success: 123, 123
// failure: 123, 456
//
// Upon failure, the test [T] is marked as failed and continues execution.
func Equal(t T, expected any, actual any, msgAndArgs ...any) bool {
if h, ok := t.(H); ok {
h.Helper()
}
return assertions.Equal(t, expected, actual, msgAndArgs...)
}
// EqualError asserts that a function returned a non-nil error (i.e. an error)
// and that it is equal to the provided error.
//
// # Usage
//
// actualObj, err := SomeFunction()
// assertions.EqualError(t, err, expectedErrorString)
//
// # Examples
//
// success: ErrTest, "assert.ErrTest general error for testing"
// failure: ErrTest, "wrong error message"
//
// Upon failure, the test [T] is marked as failed and continues execution.
func EqualError(t T, theError error, errString string, msgAndArgs ...any) bool {
if h, ok := t.(H); ok {
h.Helper()
}
return assertions.EqualError(t, theError, errString, msgAndArgs...)
}
// EqualExportedValues asserts that the types of two objects are equal and their public
// fields are also equal. This is useful for comparing structs that have private fields
// that could potentially differ.
//
// # Usage
//
// type S struct {
// Exported int
// notExported int
// }
// assertions.EqualExportedValues(t, S{1, 2}, S{1, 3}) => true
// assertions.EqualExportedValues(t, S{1, 2}, S{2, 3}) => false
//
// # Examples
//
// success: &dummyStruct{A: "a", b: 1}, &dummyStruct{A: "a", b: 2}
// failure: &dummyStruct{A: "a", b: 1}, &dummyStruct{A: "b", b: 1}
//
// Upon failure, the test [T] is marked as failed and continues execution.
func EqualExportedValues(t T, expected any, actual any, msgAndArgs ...any) bool {
if h, ok := t.(H); ok {
h.Helper()
}
return assertions.EqualExportedValues(t, expected, actual, msgAndArgs...)
}
// EqualValues asserts that two objects are equal or convertible to the larger
// type and equal.
//
// # Usage
//
// assertions.EqualValues(t, uint32(123), int32(123))
//
// # Examples
//
// success: uint32(123), int32(123)
// failure: uint32(123), int32(456)
//
// Upon failure, the test [T] is marked as failed and continues execution.
func EqualValues(t T, expected any, actual any, msgAndArgs ...any) bool {
if h, ok := t.(H); ok {
h.Helper()
}
return assertions.EqualValues(t, expected, actual, msgAndArgs...)
}
// Error asserts that a function returned a non-nil error (ie. an error).
//
// # Usage
//
// actualObj, err := SomeFunction()
// assertions.Error(t, err)
//
// # Examples
//
// success: ErrTest
// failure: nil
//
// Upon failure, the test [T] is marked as failed and continues execution.
func Error(t T, err error, msgAndArgs ...any) bool {
if h, ok := t.(H); ok {
h.Helper()
}
return assertions.Error(t, err, msgAndArgs...)
}
// ErrorAs asserts that at least one of the errors in err's chain matches target, and if so, sets target to that error value.
//
// This is a wrapper for [errors.As].
//
// # Usage
//
// assertions.ErrorAs(t, err, &target)
//
// # Examples
//
// success: fmt.Errorf("wrap: %w", &dummyError{}), new(*dummyError)
// failure: ErrTest, new(*dummyError)
//
// Upon failure, the test [T] is marked as failed and continues execution.
func ErrorAs(t T, err error, target any, msgAndArgs ...any) bool {
if h, ok := t.(H); ok {
h.Helper()
}
return assertions.ErrorAs(t, err, target, msgAndArgs...)
}
// ErrorContains asserts that a function returned a non-nil error (i.e. an
// error) and that the error contains the specified substring.
//
// # Usage
//
// actualObj, err := SomeFunction()
// assertions.ErrorContains(t, err, expectedErrorSubString)
//
// # Examples
//
// success: ErrTest, "general error"
// failure: ErrTest, "not in message"
//
// Upon failure, the test [T] is marked as failed and continues execution.
func ErrorContains(t T, theError error, contains string, msgAndArgs ...any) bool {
if h, ok := t.(H); ok {
h.Helper()
}
return assertions.ErrorContains(t, theError, contains, msgAndArgs...)
}
// ErrorIs asserts that at least one of the errors in err's chain matches target.
//
// This is a wrapper for [errors.Is].
//
// # Usage
//
// assertions.ErrorIs(t, err, io.EOF)
//
// # Examples
//
// success: fmt.Errorf("wrap: %w", io.EOF), io.EOF
// failure: ErrTest, io.EOF
//
// Upon failure, the test [T] is marked as failed and continues execution.
func ErrorIs(t T, err error, target error, msgAndArgs ...any) bool {
if h, ok := t.(H); ok {
h.Helper()
}
return assertions.ErrorIs(t, err, target, msgAndArgs...)
}
// Eventually asserts that the given condition will be met in waitFor time,
// periodically checking the target function on each tick.
//
// [Eventually] waits until the condition returns true, for at most waitFor,
// or until the parent context of the test is cancelled.
//
// If the condition takes longer than waitFor to complete, [Eventually] fails
// but waits for the current condition execution to finish before returning.
//
// For long-running conditions to be interrupted early, check [testing.T.Context]
// which is cancelled on test failure.
//
// # Usage
//
// assertions.Eventually(t, func() bool { return true }, time.Second, 10*time.Millisecond)
//
// # Concurrency
//
// The condition function is never executed in parallel: only one goroutine executes it.
// It may write to variables outside its scope without triggering race conditions.
//
// A blocking condition will cause [Eventually] to hang until it returns.
//
// # Examples
//
// success: func() bool { return true }, 100*time.Millisecond, 20*time.Millisecond
// failure: func() bool { return false }, 100*time.Millisecond, 20*time.Millisecond
//
// Upon failure, the test [T] is marked as failed and continues execution.
func Eventually(t T, condition func() bool, waitFor time.Duration, tick time.Duration, msgAndArgs ...any) bool {
if h, ok := t.(H); ok {
h.Helper()
}
return assertions.Eventually(t, condition, waitFor, tick, msgAndArgs...)
}
// EventuallyWithT asserts that the given condition will be met in waitFor time,
// periodically checking the target function at each tick.
//
// In contrast to [Eventually], the condition function is supplied with a [CollectT]
// to accumulate errors from calling other assertions.
//
// The condition is considered "met" if no errors are raised in a tick.
// The supplied [CollectT] collects all errors from one tick.
//
// If the condition is not met before waitFor, the collected errors from the
// last tick are copied to t.
//
// Calling [CollectT.FailNow] cancels the condition immediately and fails the assertion.
//
// # Usage
//
// externalValue := false
// go func() {
// time.Sleep(8*time.Second)
// externalValue = true
// }()
//
// assertions.EventuallyWithT(t, func(c *assertions.CollectT) {
// // add assertions as needed; any assertion failure will fail the current tick
// assertions.True(c, externalValue, "expected 'externalValue' to be true")
// }, 10*time.Second, 1*time.Second, "external state has not changed to 'true'; still false")
//
// # Concurrency
//
// The condition function is never executed in parallel: only one goroutine executes it.
// It may write to variables outside its scope without triggering race conditions.
//
// # Examples
//
// success: func(c *CollectT) { True(c,true) }, 100*time.Millisecond, 20*time.Millisecond
// failure: func(c *CollectT) { False(c,true) }, 100*time.Millisecond, 20*time.Millisecond
//
// Upon failure, the test [T] is marked as failed and continues execution.
func EventuallyWithT(t T, condition func(collect *CollectT), waitFor time.Duration, tick time.Duration, msgAndArgs ...any) bool {
if h, ok := t.(H); ok {
h.Helper()
}
return assertions.EventuallyWithT(t, condition, waitFor, tick, msgAndArgs...)
}
// Exactly asserts that two objects are equal in value and type.
//
// # Usage
//
// assertions.Exactly(t, int32(123), int64(123))
//
// # Examples
//
// success: int32(123), int32(123)
// failure: int32(123), int64(123)
//
// Upon failure, the test [T] is marked as failed and continues execution.
func Exactly(t T, expected any, actual any, msgAndArgs ...any) bool {
if h, ok := t.(H); ok {
h.Helper()
}
return assertions.Exactly(t, expected, actual, msgAndArgs...)
}
// Fail reports a failure through.
//
// # Usage
//
// assertions.Fail(t, "failed")
//
// # Examples
//
// failure: "failed"
//
// Upon failure, the test [T] is marked as failed and continues execution.
func Fail(t T, failureMessage string, msgAndArgs ...any) bool {
if h, ok := t.(H); ok {
h.Helper()
}
return assertions.Fail(t, failureMessage, msgAndArgs...)
}
// FailNow fails test.
//
// # Usage
//
// assertions.FailNow(t, "failed")
//
// # Examples
//
// failure: "failed"
//
// Upon failure, the test [T] is marked as failed and continues execution.
func FailNow(t T, failureMessage string, msgAndArgs ...any) bool {
if h, ok := t.(H); ok {
h.Helper()
}
return assertions.FailNow(t, failureMessage, msgAndArgs...)
}
// False asserts that the specified value is false.
//
// # Usage
//
// assertions.False(t, myBool)
//
// # Examples
//
// success: 1 == 0
// failure: 1 == 1
//
// Upon failure, the test [T] is marked as failed and continues execution.
func False(t T, value bool, msgAndArgs ...any) bool {
if h, ok := t.(H); ok {
h.Helper()
}
return assertions.False(t, value, msgAndArgs...)
}
// FileEmpty checks whether a file exists in the given path and is empty.
// It fails if the file is not empty, if the path points to a directory or there is an error when trying to check the file.
//
// # Usage
//
// assertions.FileEmpty(t, "path/to/file")
//
// # Examples
//
// success: filepath.Join(testDataPath(),"empty_file")
// failure: filepath.Join(testDataPath(),"existing_file")
//
// Upon failure, the test [T] is marked as failed and continues execution.
func FileEmpty(t T, path string, msgAndArgs ...any) bool {
if h, ok := t.(H); ok {
h.Helper()
}
return assertions.FileEmpty(t, path, msgAndArgs...)
}
// FileExists checks whether a file exists in the given path. It also fails if
// the path points to a directory or there is an error when trying to check the file.
//
// # Usage
//
// assertions.FileExists(t, "path/to/file")
//
// # Examples
//
// success: filepath.Join(testDataPath(),"existing_file")
// failure: filepath.Join(testDataPath(),"non_existing_file")
//
// Upon failure, the test [T] is marked as failed and continues execution.
func FileExists(t T, path string, msgAndArgs ...any) bool {
if h, ok := t.(H); ok {
h.Helper()
}
return assertions.FileExists(t, path, msgAndArgs...)
}
// FileNotEmpty checks whether a file exists in the given path and is not empty.
// It fails if the file is empty, if the path points to a directory or there is an error when trying to check the file.
//
// # Usage
//
// assertions.FileNotEmpty(t, "path/to/file")
//
// # Examples
//
// success: filepath.Join(testDataPath(),"existing_file")
// failure: filepath.Join(testDataPath(),"empty_file")
//
// Upon failure, the test [T] is marked as failed and continues execution.
func FileNotEmpty(t T, path string, msgAndArgs ...any) bool {
if h, ok := t.(H); ok {
h.Helper()
}
return assertions.FileNotEmpty(t, path, msgAndArgs...)
}
// Greater asserts that the first element is strictly greater than the second.
//
// # Usage
//
// assertions.Greater(t, 2, 1)
// assertions.Greater(t, float64(2), float64(1))
// assertions.Greater(t, "b", "a")
//
// # Examples
//
// success: 2, 1
// failure: 1, 2
//
// Upon failure, the test [T] is marked as failed and continues execution.
func Greater(t T, e1 any, e2 any, msgAndArgs ...any) bool {
if h, ok := t.(H); ok {
h.Helper()
}
return assertions.Greater(t, e1, e2, msgAndArgs...)
}
// GreaterOrEqual asserts that the first element is greater than or equal to the second.
//
// # Usage
//
// assertions.GreaterOrEqual(t, 2, 1)
// assertions.GreaterOrEqual(t, 2, 2)
// assertions.GreaterOrEqual(t, "b", "a")
// assertions.GreaterOrEqual(t, "b", "b")
//
// # Examples
//
// success: 2, 1
// failure: 1, 2
//
// Upon failure, the test [T] is marked as failed and continues execution.
func GreaterOrEqual(t T, e1 any, e2 any, msgAndArgs ...any) bool {
if h, ok := t.(H); ok {
h.Helper()
}
return assertions.GreaterOrEqual(t, e1, e2, msgAndArgs...)
}
// HTTPBodyContains asserts that a specified handler returns a body that contains a string.
//
// Returns whether the assertion was successful (true) or not (false).
//
// # Usage
//
// assertions.HTTPBodyContains(t, myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky")
//
// # Examples
//
// success: httpBody, "GET", "/", url.Values{"name": []string{"World"}}, "Hello, World!"
// failure: httpBody, "GET", "/", url.Values{"name": []string{"Bob"}}, "Hello, World!"
//
// Upon failure, the test [T] is marked as failed and continues execution.
func HTTPBodyContains(t T, handler http.HandlerFunc, method string, url string, values url.Values, str any, msgAndArgs ...any) bool {
if h, ok := t.(H); ok {
h.Helper()
}
return assertions.HTTPBodyContains(t, handler, method, url, values, str, msgAndArgs...)
}
// HTTPBodyNotContains asserts that a specified handler returns a
// body that does not contain a string.
//
// Returns whether the assertion was successful (true) or not (false).
//
// # Usage
//
// assertions.HTTPBodyNotContains(t, myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky")
//
// # Examples
//
// success: httpBody, "GET", "/", url.Values{"name": []string{"World"}}, "Hello, Bob!"
// failure: httpBody, "GET", "/", url.Values{"name": []string{"Bob"}}, "Hello, Bob!"
//
// Upon failure, the test [T] is marked as failed and continues execution.
func HTTPBodyNotContains(t T, handler http.HandlerFunc, method string, url string, values url.Values, str any, msgAndArgs ...any) bool {
if h, ok := t.(H); ok {
h.Helper()
}
return assertions.HTTPBodyNotContains(t, handler, method, url, values, str, msgAndArgs...)
}
// HTTPError asserts that a specified handler returns an error status code.
//
// Returns whether the assertion was successful (true) or not (false).
//
// # Usage
//
// assertions.HTTPError(t, myHandler, "POST", "/a/b/c", url.Values{"a": []string{"b", "c"}}
//
// # Examples
//
// success: httpError, "GET", "/", nil
// failure: httpOK, "GET", "/", nil
//
// Upon failure, the test [T] is marked as failed and continues execution.
func HTTPError(t T, handler http.HandlerFunc, method string, url string, values url.Values, msgAndArgs ...any) bool {
if h, ok := t.(H); ok {
h.Helper()
}
return assertions.HTTPError(t, handler, method, url, values, msgAndArgs...)
}
// HTTPRedirect asserts that a specified handler returns a redirect status code.
//
// Returns whether the assertion was successful (true) or not (false).
//
// # Usage
//
// assertions.HTTPRedirect(t, myHandler, "GET", "/a/b/c", url.Values{"a": []string{"b", "c"}}
//
// # Examples
//
// success: httpRedirect, "GET", "/", nil
// failure: httpError, "GET", "/", nil
//
// Upon failure, the test [T] is marked as failed and continues execution.
func HTTPRedirect(t T, handler http.HandlerFunc, method string, url string, values url.Values, msgAndArgs ...any) bool {
if h, ok := t.(H); ok {
h.Helper()
}
return assertions.HTTPRedirect(t, handler, method, url, values, msgAndArgs...)
}
// HTTPStatusCode asserts that a specified handler returns a specified status code.
//
// Returns whether the assertion was successful (true) or not (false).
//
// # Usage
//
// assertions.HTTPStatusCode(t, myHandler, "GET", "/notImplemented", nil, 501)
//
// # Examples
//
// success: httpOK, "GET", "/", nil, http.StatusOK
// failure: httpError, "GET", "/", nil, http.StatusOK
//
// Upon failure, the test [T] is marked as failed and continues execution.
func HTTPStatusCode(t T, handler http.HandlerFunc, method string, url string, values url.Values, statuscode int, msgAndArgs ...any) bool {
if h, ok := t.(H); ok {
h.Helper()
}
return assertions.HTTPStatusCode(t, handler, method, url, values, statuscode, msgAndArgs...)
}
// HTTPSuccess asserts that a specified handler returns a success status code.
//
// Returns whether the assertion was successful (true) or not (false).
//
// # Usage
//
// assertions.HTTPSuccess(t, myHandler, "POST", "http://www.google.com", nil)
//
// # Examples
//
// success: httpOK, "GET", "/", nil
// failure: httpError, "GET", "/", nil
//
// Upon failure, the test [T] is marked as failed and continues execution.
func HTTPSuccess(t T, handler http.HandlerFunc, method string, url string, values url.Values, msgAndArgs ...any) bool {
if h, ok := t.(H); ok {
h.Helper()
}
return assertions.HTTPSuccess(t, handler, method, url, values, msgAndArgs...)
}
// Implements asserts that an object is implemented by the specified interface.
//
// # Usage
//
// assertions.Implements(t, (*MyInterface)(nil), new(MyObject))
//
// # Examples
//
// success: ptr(dummyInterface), new(testing.T)
// failure: (*error)(nil), new(testing.T)
//
// Upon failure, the test [T] is marked as failed and continues execution.
func Implements(t T, interfaceObject any, object any, msgAndArgs ...any) bool {
if h, ok := t.(H); ok {
h.Helper()
}
return assertions.Implements(t, interfaceObject, object, msgAndArgs...)
}
// InDelta asserts that the two numerals are within delta of each other.
//
// # Usage
//
// assertions.InDelta(t, math.Pi, 22/7.0, 0.01)
//
// # Examples
//
// success: 1.0, 1.01, 0.02
// failure: 1.0, 1.1, 0.05
//
// Upon failure, the test [T] is marked as failed and continues execution.
func InDelta(t T, expected any, actual any, delta float64, msgAndArgs ...any) bool {
if h, ok := t.(H); ok {
h.Helper()
}
return assertions.InDelta(t, expected, actual, delta, msgAndArgs...)
}
// InDeltaMapValues is the same as InDelta, but it compares all values between two maps. Both maps must have exactly the same keys.
//
// # Usage
//
// assertions.InDeltaMapValues(t, map[string]float64{"a": 1.0}, map[string]float64{"a": 1.01}, 0.02)
//
// # Examples
//
// success: map[string]float64{"a": 1.0}, map[string]float64{"a": 1.01}, 0.02
// failure: map[string]float64{"a": 1.0}, map[string]float64{"a": 1.1}, 0.05
//
// Upon failure, the test [T] is marked as failed and continues execution.
func InDeltaMapValues(t T, expected any, actual any, delta float64, msgAndArgs ...any) bool {
if h, ok := t.(H); ok {
h.Helper()
}
return assertions.InDeltaMapValues(t, expected, actual, delta, msgAndArgs...)
}
// InDeltaSlice is the same as InDelta, except it compares two slices.
//
// # Usage
//
// assertions.InDeltaSlice(t, []float64{1.0, 2.0}, []float64{1.01, 2.01}, 0.02)
//
// # Examples
//
// success: []float64{1.0, 2.0}, []float64{1.01, 2.01}, 0.02
// failure: []float64{1.0, 2.0}, []float64{1.1, 2.1}, 0.05
//
// Upon failure, the test [T] is marked as failed and continues execution.
func InDeltaSlice(t T, expected any, actual any, delta float64, msgAndArgs ...any) bool {
if h, ok := t.(H); ok {
h.Helper()
}
return assertions.InDeltaSlice(t, expected, actual, delta, msgAndArgs...)
}
// InEpsilon asserts that expected and actual have a relative error less than epsilon.
//
// # Usage
//
// assertions.InEpsilon(t, 100.0, 101.0, 0.02)
//
// # Examples
//
// success: 100.0, 101.0, 0.02
// failure: 100.0, 110.0, 0.05
//
// Upon failure, the test [T] is marked as failed and continues execution.
func InEpsilon(t T, expected any, actual any, epsilon float64, msgAndArgs ...any) bool {
if h, ok := t.(H); ok {
h.Helper()
}
return assertions.InEpsilon(t, expected, actual, epsilon, msgAndArgs...)
}
// InEpsilonSlice is the same as InEpsilon, except it compares each value from two slices.
//
// # Usage
//
// assertions.InEpsilonSlice(t, []float64{100.0, 200.0}, []float64{101.0, 202.0}, 0.02)
//
// # Examples
//
// success: []float64{100.0, 200.0}, []float64{101.0, 202.0}, 0.02
// failure: []float64{100.0, 200.0}, []float64{110.0, 220.0}, 0.05
//
// Upon failure, the test [T] is marked as failed and continues execution.
func InEpsilonSlice(t T, expected any, actual any, epsilon float64, msgAndArgs ...any) bool {
if h, ok := t.(H); ok {
h.Helper()
}
return assertions.InEpsilonSlice(t, expected, actual, epsilon, msgAndArgs...)
}
// IsDecreasing asserts that the collection is decreasing.
//
// # Usage
//
// assertions.IsDecreasing(t, []int{2, 1, 0})
// assertions.IsDecreasing(t, []float{2, 1})
// assertions.IsDecreasing(t, []string{"b", "a"})
//
// # Examples
//
// success: []int{3, 2, 1}
// failure: []int{1, 2, 3}
//
// Upon failure, the test [T] is marked as failed and continues execution.
func IsDecreasing(t T, object any, msgAndArgs ...any) bool {
if h, ok := t.(H); ok {
h.Helper()
}
return assertions.IsDecreasing(t, object, msgAndArgs...)
}
// IsIncreasing asserts that the collection is increasing.
//
// # Usage
//
// assertions.IsIncreasing(t, []int{1, 2, 3})
// assertions.IsIncreasing(t, []float{1, 2})
// assertions.IsIncreasing(t, []string{"a", "b"})
//
// # Examples
//
// success: []int{1, 2, 3}
// failure: []int{1, 1, 2}
//
// Upon failure, the test [T] is marked as failed and continues execution.
func IsIncreasing(t T, object any, msgAndArgs ...any) bool {
if h, ok := t.(H); ok {
h.Helper()
}
return assertions.IsIncreasing(t, object, msgAndArgs...)
}
// IsNonDecreasing asserts that the collection is not decreasing.
//
// # Usage
//
// assertions.IsNonDecreasing(t, []int{1, 1, 2})
// assertions.IsNonDecreasing(t, []float{1, 2})
// assertions.IsNonDecreasing(t, []string{"a", "b"})
//
// # Examples
//
// success: []int{1, 1, 2}
// failure: []int{2, 1, 1}
//
// Upon failure, the test [T] is marked as failed and continues execution.
func IsNonDecreasing(t T, object any, msgAndArgs ...any) bool {
if h, ok := t.(H); ok {
h.Helper()
}
return assertions.IsNonDecreasing(t, object, msgAndArgs...)
}
// IsNonIncreasing asserts that the collection is not increasing.
//
// # Usage
//
// assertions.IsNonIncreasing(t, []int{2, 1, 1})
// assertions.IsNonIncreasing(t, []float{2, 1})
// assertions.IsNonIncreasing(t, []string{"b", "a"})
//
// # Examples
//
// success: []int{2, 1, 1}
// failure: []int{1, 2, 3}
//
// Upon failure, the test [T] is marked as failed and continues execution.
func IsNonIncreasing(t T, object any, msgAndArgs ...any) bool {
if h, ok := t.(H); ok {
h.Helper()
}
return assertions.IsNonIncreasing(t, object, msgAndArgs...)
}
// IsNotType asserts that the specified objects are not of the same type.
//
// # Usage
//
// assertions.IsNotType(t, &NotMyStruct{}, &MyStruct{})
//
// # Examples
//
// success: int32(123), int64(456)
// failure: 123, 456
//
// Upon failure, the test [T] is marked as failed and continues execution.
func IsNotType(t T, theType any, object any, msgAndArgs ...any) bool {
if h, ok := t.(H); ok {
h.Helper()
}
return assertions.IsNotType(t, theType, object, msgAndArgs...)
}
// IsType asserts that the specified objects are of the same type.
//
// # Usage
//
// assertions.IsType(t, &MyStruct{}, &MyStruct{})
//
// # Examples
//
// success: 123, 456
// failure: int32(123), int64(456)
//
// Upon failure, the test [T] is marked as failed and continues execution.
func IsType(t T, expectedType any, object any, msgAndArgs ...any) bool {
if h, ok := t.(H); ok {
h.Helper()
}
return assertions.IsType(t, expectedType, object, msgAndArgs...)
}
// JSONEq asserts that two JSON strings are equivalent.
//
// # Usage
//
// assertions.JSONEq(t, `{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`)
//
// # Examples
//
// success: `{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`
// failure: `{"hello": "world", "foo": "bar"}`, `[{"foo": "bar"}, {"hello": "world"}]`
//
// Upon failure, the test [T] is marked as failed and continues execution.
func JSONEq(t T, expected string, actual string, msgAndArgs ...any) bool {
if h, ok := t.(H); ok {
h.Helper()
}
return assertions.JSONEq(t, expected, actual, msgAndArgs...)
}
// JSONEqBytes asserts that two JSON byte slices are equivalent.
//
// # Usage
//
// assertions.JSONEqBytes(t, []byte(`{"hello": "world", "foo": "bar"}`), []byte(`{"foo": "bar", "hello": "world"}`))
//
// # Examples
//
// success: []byte(`{"hello": "world", "foo": "bar"}`), []byte(`{"foo": "bar", "hello": "world"}`)
// failure: []byte(`{"hello": "world", "foo": "bar"}`), []byte(`[{"foo": "bar"}, {"hello": "world"}]`)
//
// Upon failure, the test [T] is marked as failed and continues execution.
func JSONEqBytes(t T, expected []byte, actual []byte, msgAndArgs ...any) bool {
if h, ok := t.(H); ok {
h.Helper()
}
return assertions.JSONEqBytes(t, expected, actual, msgAndArgs...)
}
// Kind asserts that the [reflect.Kind] of a given object matches the expected [reflect.Kind].
//
// Kind reflects the concrete value stored in the object. The nil value (or interface with nil value)
// are comparable to [reflect.Invalid]. See also [reflect.Value.Kind].
//
// # Usage
//
// assertions.Kind(t, reflect.String, "Hello World")
//
// # Examples
//
// success: reflect.String, "hello"
// failure: reflect.String, 0
//
// Upon failure, the test [T] is marked as failed and continues execution.
func Kind(t T, expectedKind reflect.Kind, object any, msgAndArgs ...any) bool {
if h, ok := t.(H); ok {
h.Helper()
}
return assertions.Kind(t, expectedKind, object, msgAndArgs...)
}
// Len asserts that the specified object has specific length.
//
// Len also fails if the object has a type that len() does not accept.
//
// The asserted object can be a string, a slice, a map, an array or a channel.
//
// See also [reflect.Len].
//
// # Usage
//
// assertions.Len(t, mySlice, 3)
// assertions.Len(t, myString, 4)
// assertions.Len(t, myMap, 5)
//
// # Examples
//
// success: []string{"A","B"}, 2