Skip to content

Commit a4359ce

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

14 files changed

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

0 commit comments

Comments
 (0)