Skip to content

Commit 30daf59

Browse files
committed
Replace testify assertions with custom testing helpers
1 parent b94b78f commit 30daf59

14 files changed

Lines changed: 249 additions & 35 deletions

accessors_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"testing"
55

66
"github.com/stretchr/objx"
7-
"github.com/stretchr/testify/assert"
87
)
98

109
func TestAccessorsAccessGetSingleField(t *testing.T) {

conversions_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import (
55
"testing"
66

77
"github.com/stretchr/objx"
8-
"github.com/stretchr/testify/assert"
9-
"github.com/stretchr/testify/require"
108
)
119

1210
func TestConversionJSON(t *testing.T) {

fixture_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"testing"
55

66
"github.com/stretchr/objx"
7-
"github.com/stretchr/testify/assert"
87
)
98

109
var fixtures = []struct {

go.mod

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
11
module github.com/stretchr/objx
22

33
go 1.20
4-
5-
require github.com/stretchr/testify v1.11.1
6-
7-
require (
8-
github.com/davecgh/go-spew v1.1.1 // indirect
9-
github.com/pmezard/go-difflib v1.0.0 // indirect
10-
gopkg.in/yaml.v3 v3.0.1 // indirect
11-
)
12-
13-
exclude github.com/stretchr/testify v1.8.0

go.sum

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +0,0 @@
1-
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2-
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3-
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4-
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5-
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
6-
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
7-
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
8-
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
9-
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
10-
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

map_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"testing"
55

66
"github.com/stretchr/objx"
7-
"github.com/stretchr/testify/assert"
8-
"github.com/stretchr/testify/require"
97
)
108

119
var TestMap = objx.Map{

mutations_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import (
55
"testing"
66

77
"github.com/stretchr/objx"
8-
"github.com/stretchr/testify/assert"
9-
"github.com/stretchr/testify/require"
108
)
119

1210
func TestExclude(t *testing.T) {

security_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"testing"
55

66
"github.com/stretchr/objx"
7-
"github.com/stretchr/testify/assert"
87
)
98

109
func TestHashWithKey(t *testing.T) {

simple_example_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"testing"
55

66
"github.com/stretchr/objx"
7-
"github.com/stretchr/testify/assert"
8-
"github.com/stretchr/testify/require"
97
)
108

119
func TestSimpleExample(t *testing.T) {

std_assert_test.go

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

0 commit comments

Comments
 (0)