Skip to content

Commit b152998

Browse files
Replace testify assertions with custom testing helpers (#159)
Co-authored-by: Ben Schumacher <[email protected]>
1 parent f248e2b commit b152998

14 files changed

+240
-35
lines changed

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

0 commit comments

Comments
 (0)