Skip to content

Commit cb06f83

Browse files
Import Emilien Puget version as-is from
stretchr/objx#159 Co-Authored-by: Emilien Puget <emilien-puget@users.noreply.github.com>
1 parent 79da55d commit cb06f83

1 file changed

Lines changed: 247 additions & 0 deletions

File tree

internal/testifail.go

Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
package internal
2+
3+
import (
4+
"fmt"
5+
"reflect"
6+
"testing"
7+
)
8+
9+
type (
10+
nonfatal struct{}
11+
fatal struct{}
12+
)
13+
14+
var (
15+
assert nonfatal
16+
require fatal
17+
)
18+
19+
func (nonfatal) fail(t *testing.T, msg string, msgAndArgs ...any) bool {
20+
t.Helper()
21+
if len(msgAndArgs) > 0 {
22+
msg = fmt.Sprintf(msg, msgAndArgs...)
23+
}
24+
t.Error(msg)
25+
return false
26+
}
27+
28+
func (fatal) fail(t *testing.T, msg string, msgAndArgs ...any) {
29+
t.Helper()
30+
if len(msgAndArgs) > 0 {
31+
msg = fmt.Sprintf(msg, msgAndArgs...)
32+
}
33+
t.Fatal(msg)
34+
}
35+
36+
func (a nonfatal) Equal(t *testing.T, expected, actual any, msgAndArgs ...any) bool {
37+
t.Helper()
38+
if !reflect.DeepEqual(expected, actual) {
39+
return a.fail(t, "not equal:\nexpected: %#v\nactual: %#v", expected, actual)
40+
}
41+
return true
42+
}
43+
44+
func (a fatal) Equal(t *testing.T, expected, actual any, msgAndArgs ...any) {
45+
t.Helper()
46+
if !reflect.DeepEqual(expected, actual) {
47+
a.fail(t, "not equal:\nexpected: %#v\nactual: %#v", expected, actual)
48+
}
49+
}
50+
51+
func (a nonfatal) NotEqual(t *testing.T, expected, actual any, msgAndArgs ...any) bool {
52+
t.Helper()
53+
if reflect.DeepEqual(expected, actual) {
54+
return a.fail(t, "should not be equal: %#v", actual)
55+
}
56+
return true
57+
}
58+
59+
func (a fatal) NotEqual(t *testing.T, expected, actual any, msgAndArgs ...any) {
60+
t.Helper()
61+
if reflect.DeepEqual(expected, actual) {
62+
a.fail(t, "should not be equal: %#v", actual)
63+
}
64+
}
65+
66+
func isNil(i any) bool {
67+
if i == nil {
68+
return true
69+
}
70+
v := reflect.ValueOf(i)
71+
switch v.Kind() {
72+
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
73+
return v.IsNil()
74+
}
75+
return false
76+
}
77+
78+
func (a nonfatal) Nil(t *testing.T, object any, msgAndArgs ...any) bool {
79+
t.Helper()
80+
if !isNil(object) {
81+
return a.fail(t, "expected nil, got: %#v", object)
82+
}
83+
return true
84+
}
85+
86+
func (a fatal) Nil(t *testing.T, object any, msgAndArgs ...any) {
87+
t.Helper()
88+
if !isNil(object) {
89+
a.fail(t, "expected nil, got: %#v", object)
90+
}
91+
}
92+
93+
func (a nonfatal) NotNil(t *testing.T, object any, msgAndArgs ...any) bool {
94+
t.Helper()
95+
if isNil(object) {
96+
return a.fail(t, "expected not nil")
97+
}
98+
return true
99+
}
100+
101+
func (a fatal) NotNil(t *testing.T, object any, msgAndArgs ...any) {
102+
t.Helper()
103+
if isNil(object) {
104+
a.fail(t, "expected not nil")
105+
}
106+
}
107+
108+
func (a nonfatal) True(t *testing.T, value bool, msgAndArgs ...any) bool {
109+
t.Helper()
110+
if !value {
111+
return a.fail(t, "expected true, got false")
112+
}
113+
return true
114+
}
115+
116+
func (a fatal) True(t *testing.T, value bool, msgAndArgs ...any) {
117+
t.Helper()
118+
if !value {
119+
a.fail(t, "expected true, got false")
120+
}
121+
}
122+
123+
func (a nonfatal) False(t *testing.T, value bool, msgAndArgs ...any) bool {
124+
t.Helper()
125+
if value {
126+
return a.fail(t, "expected false, got true")
127+
}
128+
return true
129+
}
130+
131+
func (a fatal) False(t *testing.T, value bool, msgAndArgs ...any) {
132+
t.Helper()
133+
if value {
134+
a.fail(t, "expected false, got true")
135+
}
136+
}
137+
138+
func (a nonfatal) NoError(t *testing.T, err error, msgAndArgs ...any) bool {
139+
t.Helper()
140+
if err != nil {
141+
return a.fail(t, "expected no error, got: %v", err)
142+
}
143+
return true
144+
}
145+
146+
func (a fatal) NoError(t *testing.T, err error, msgAndArgs ...any) {
147+
t.Helper()
148+
if err != nil {
149+
a.fail(t, "expected no error, got: %v", err)
150+
}
151+
}
152+
153+
func (a nonfatal) Error(t *testing.T, err error, msgAndArgs ...any) bool {
154+
t.Helper()
155+
if err == nil {
156+
return a.fail(t, "expected error, got nil")
157+
}
158+
return true
159+
}
160+
161+
func (a fatal) Error(t *testing.T, err error, msgAndArgs ...any) {
162+
t.Helper()
163+
if err == nil {
164+
a.fail(t, "expected error, got nil")
165+
}
166+
}
167+
168+
func (a nonfatal) Panics(t *testing.T, f func(), msgAndArgs ...any) bool {
169+
t.Helper()
170+
defer func() {
171+
if r := recover(); r == nil {
172+
_ = a.fail(t, "expected panic, but function did not panic")
173+
}
174+
}()
175+
f()
176+
return true
177+
}
178+
179+
func (a fatal) Panics(t *testing.T, f func(), msgAndArgs ...any) {
180+
t.Helper()
181+
defer func() {
182+
if r := recover(); r == nil {
183+
a.fail(t, "expected panic, but function did not panic")
184+
}
185+
}()
186+
f()
187+
}
188+
189+
func (a nonfatal) Empty(t *testing.T, object any, msgAndArgs ...any) bool {
190+
t.Helper()
191+
if !isEmpty(object) {
192+
return a.fail(t, "expected empty, got: %#v", object)
193+
}
194+
return true
195+
}
196+
197+
func (a fatal) Empty(t *testing.T, object any, msgAndArgs ...any) {
198+
t.Helper()
199+
if !isEmpty(object) {
200+
a.fail(t, "expected empty, got: %#v", object)
201+
}
202+
}
203+
204+
func isEmpty(object any) bool {
205+
if object == nil {
206+
return true
207+
}
208+
v := reflect.ValueOf(object)
209+
switch v.Kind() {
210+
case reflect.Array, reflect.Slice, reflect.Map, reflect.Chan, reflect.String:
211+
return v.Len() == 0
212+
case reflect.Ptr, reflect.Interface:
213+
if v.IsNil() {
214+
return true
215+
}
216+
return isEmpty(v.Elem().Interface())
217+
}
218+
// numbers and structs are never considered empty here
219+
return false
220+
}
221+
222+
func (a nonfatal) Len(t *testing.T, object any, length int, msgAndArgs ...any) bool {
223+
t.Helper()
224+
v := reflect.ValueOf(object)
225+
switch v.Kind() {
226+
case reflect.Array, reflect.Slice, reflect.Map, reflect.Chan, reflect.String:
227+
if v.Len() != length {
228+
return a.fail(t, "unexpected length, expected %d got %d", length, v.Len())
229+
}
230+
return true
231+
default:
232+
return a.fail(t, "Len not supported for kind %s", v.Kind())
233+
}
234+
}
235+
236+
func (a fatal) Len(t *testing.T, object any, length int, msgAndArgs ...any) {
237+
t.Helper()
238+
v := reflect.ValueOf(object)
239+
switch v.Kind() {
240+
case reflect.Array, reflect.Slice, reflect.Map, reflect.Chan, reflect.String:
241+
if v.Len() != length {
242+
a.fail(t, "unexpected length, expected %d got %d", length, v.Len())
243+
}
244+
default:
245+
a.fail(t, "Len not supported for kind %s", v.Kind())
246+
}
247+
}

0 commit comments

Comments
 (0)