Skip to content

Commit 7f9782a

Browse files
committed
Refactor for use Go generics, requirement Go 1.18+
1 parent 5b81c61 commit 7f9782a

File tree

7 files changed

+198
-31
lines changed

7 files changed

+198
-31
lines changed

.github/workflows/go.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88
- name: Set up Go
99
uses: actions/setup-go@v1
1010
with:
11-
go-version: 1.14
11+
go-version: 1.18
1212
id: go
1313

1414
- name: Check out code into the Go module directory

CHANGELOG.md

Lines changed: 0 additions & 3 deletions
This file was deleted.

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
[![Go](https://github.com/longbridgeapp/assert/actions/workflows/go.yml/badge.svg)](https://github.com/longbridgeapp/assert/actions/workflows/go.yml)
44

5+
> Requirement Go 1.18+, lower Go version please use 0.x
6+
57
Extends [stretchr/testify/assert](https://github.com/stretchr/testify/tree/master/assert) for add more useful methods.
68

79
- `assert.Equal` - asserts values equal, but ignore type.

assert.go

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ type tHelper interface {
2525
//
2626
// assert.StrictEqual(t, 123, int64(123))
2727
//
28-
func Equal(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool {
28+
func Equal[A any, B any](t TestingT, expected A, actual B, msgAndArgs ...any) bool {
2929
if h, ok := t.(tHelper); ok {
3030
h.Helper()
3131
}
32+
3233
return testifyAssert.EqualValues(t, expected, actual, msgAndArgs...)
3334
}
3435

@@ -37,10 +38,11 @@ func Equal(t TestingT, expected, actual interface{}, msgAndArgs ...interface{})
3738
// assert.StrictEqual(t, 123, 123)
3839
// assert.StrictEqual(t, 123, int64(123)) // fail
3940
//
40-
func StrictEqual(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool {
41+
func StrictEqual[A any, B any](t TestingT, expected A, actual B, msgAndArgs ...any) bool {
4142
if h, ok := t.(tHelper); ok {
4243
h.Helper()
4344
}
45+
4446
return testifyAssert.Equal(t, expected, actual, msgAndArgs...)
4547
}
4648

@@ -50,7 +52,7 @@ func StrictEqual(t TestingT, expected, actual interface{}, msgAndArgs ...interfa
5052
// assert.NotEqual(t, 12, int32(12)) // fail
5153
// assert.NotEqual(t, 12, int32(13)) // ok
5254
//
53-
func NotEqual(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool {
55+
func NotEqual[A any, B any](t TestingT, expected A, actual B, msgAndArgs ...any) bool {
5456
if h, ok := t.(tHelper); ok {
5557
h.Helper()
5658
}
@@ -63,7 +65,7 @@ func NotEqual(t TestingT, expected, actual interface{}, msgAndArgs ...interface{
6365
//
6466
// Both arguments must be pointer variables. Pointer variable sameness is
6567
// determined based on the equality of both type and value.
66-
func Same(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool {
68+
func Same[A any, B any](t TestingT, expected A, actual B, msgAndArgs ...any) bool {
6769
if h, ok := t.(tHelper); ok {
6870
h.Helper()
6971
}
@@ -76,7 +78,7 @@ func Same(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) b
7678
//
7779
// Both arguments must be pointer variables. Pointer variable sameness is
7880
// determined based on the equality of both type and value.
79-
func NotSame(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool {
81+
func NotSame[A any, B any](t TestingT, expected A, actual B, msgAndArgs ...any) bool {
8082
if h, ok := t.(tHelper); ok {
8183
h.Helper()
8284
}
@@ -90,7 +92,7 @@ func NotSame(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}
9092
// assert.Contains(t, ["Hello", "World"], "World")
9193
// assert.Contains(t, {"Hello": "World"}, "Hello")
9294
//
93-
func Contains(t TestingT, s, contains interface{}, msgAndArgs ...interface{}) bool {
95+
func Contains[A any, B any](t TestingT, s A, contains B, msgAndArgs ...any) bool {
9496
if h, ok := t.(tHelper); ok {
9597
h.Helper()
9698
}
@@ -104,7 +106,7 @@ func Contains(t TestingT, s, contains interface{}, msgAndArgs ...interface{}) bo
104106
// assert.NotContains(t, ["Hello", "World"], "Earth")
105107
// assert.NotContains(t, {"Hello": "World"}, "Earth")
106108
//
107-
func NotContains(t TestingT, s, contains interface{}, msgAndArgs ...interface{}) bool {
109+
func NotContains[A any, B any](t TestingT, s A, contains B, msgAndArgs ...any) bool {
108110
if h, ok := t.(tHelper); ok {
109111
h.Helper()
110112
}
@@ -117,7 +119,7 @@ func NotContains(t TestingT, s, contains interface{}, msgAndArgs ...interface{})
117119
//
118120
// assert.ElementsMatch(t, [1, 3, 2, 3], [1, 3, 3, 2])
119121
//
120-
func ElementsMatch(t TestingT, listA, listB interface{}, msgAndArgs ...interface{}) (ok bool) {
122+
func ElementsMatch[T any](t TestingT, listA, listB T, msgAndArgs ...any) (ok bool) {
121123
if h, ok := t.(tHelper); ok {
122124
h.Helper()
123125
}
@@ -128,7 +130,7 @@ func ElementsMatch(t TestingT, listA, listB interface{}, msgAndArgs ...interface
128130
//
129131
// assert.Panics(t, func(){ GoCrazy() })
130132
//
131-
func Panics(t TestingT, f testifyAssert.PanicTestFunc, msgAndArgs ...interface{}) bool {
133+
func Panics(t TestingT, f testifyAssert.PanicTestFunc, msgAndArgs ...any) bool {
132134
if h, ok := t.(tHelper); ok {
133135
h.Helper()
134136
}
@@ -139,7 +141,7 @@ func Panics(t TestingT, f testifyAssert.PanicTestFunc, msgAndArgs ...interface{}
139141
//
140142
// assert.NotPanics(t, func(){ GoCrazy() })
141143
//
142-
func NotPanics(t TestingT, f testifyAssert.PanicTestFunc, msgAndArgs ...interface{}) bool {
144+
func NotPanics(t TestingT, f testifyAssert.PanicTestFunc, msgAndArgs ...any) bool {
143145
if h, ok := t.(tHelper); ok {
144146
h.Helper()
145147
}
@@ -150,7 +152,7 @@ func NotPanics(t TestingT, f testifyAssert.PanicTestFunc, msgAndArgs ...interfac
150152
//
151153
// assert.WithinDuration(t, time.Now(), time.Now(), 10*time.Second)
152154
//
153-
func WithinDuration(t TestingT, expected, actual time.Time, delta time.Duration, msgAndArgs ...interface{}) bool {
155+
func WithinDuration(t TestingT, expected, actual time.Time, delta time.Duration, msgAndArgs ...any) bool {
154156
if h, ok := t.(tHelper); ok {
155157
h.Helper()
156158
}
@@ -164,7 +166,7 @@ func WithinDuration(t TestingT, expected, actual time.Time, delta time.Duration,
164166
// assert.Equal(t, expectedObj, actualObj)
165167
// }
166168
//
167-
func NoError(t TestingT, err error, msgAndArgs ...interface{}) bool {
169+
func NoError(t TestingT, err error, msgAndArgs ...any) bool {
168170
if h, ok := t.(tHelper); ok {
169171
h.Helper()
170172
}
@@ -178,7 +180,7 @@ func NoError(t TestingT, err error, msgAndArgs ...interface{}) bool {
178180
// assert.Equal(t, expectedObj, actualObj)
179181
// }
180182
//
181-
func Error(t TestingT, err error, msgAndArgs ...interface{}) bool {
183+
func Error(t TestingT, err error, msgAndArgs ...any) bool {
182184
if h, ok := t.(tHelper); ok {
183185
h.Helper()
184186
}
@@ -191,7 +193,7 @@ func Error(t TestingT, err error, msgAndArgs ...interface{}) bool {
191193
// actualObj, err := SomeFunction()
192194
// assert.EqualError(t, err, expectedErrorString)
193195
//
194-
func EqualError(t TestingT, theError error, errString string, msgAndArgs ...interface{}) bool {
196+
func EqualError(t TestingT, theError error, errString string, msgAndArgs ...any) bool {
195197
if h, ok := t.(tHelper); ok {
196198
h.Helper()
197199
}
@@ -202,7 +204,7 @@ func EqualError(t TestingT, theError error, errString string, msgAndArgs ...inte
202204
//
203205
// assert.True(t, myBool)
204206
//
205-
func True(t TestingT, value bool, msgAndArgs ...interface{}) bool {
207+
func True(t TestingT, value bool, msgAndArgs ...any) bool {
206208
if h, ok := t.(tHelper); ok {
207209
h.Helper()
208210
}
@@ -213,7 +215,7 @@ func True(t TestingT, value bool, msgAndArgs ...interface{}) bool {
213215
//
214216
// assert.False(t, myBool)
215217
//
216-
func False(t TestingT, value bool, msgAndArgs ...interface{}) bool {
218+
func False(t TestingT, value bool, msgAndArgs ...any) bool {
217219
if h, ok := t.(tHelper); ok {
218220
h.Helper()
219221
}
@@ -224,7 +226,7 @@ func False(t TestingT, value bool, msgAndArgs ...interface{}) bool {
224226
//
225227
// assert.Nil(t, err)
226228
//
227-
func Nil(t TestingT, object interface{}, msgAndArgs ...interface{}) bool {
229+
func Nil(t TestingT, object any, msgAndArgs ...any) bool {
228230
if h, ok := t.(tHelper); ok {
229231
h.Helper()
230232
}

assert_test.go

Lines changed: 167 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,175 @@ import (
44
"testing"
55
)
66

7+
func testAssert(t *testing.T, expected bool, block func(t *testing.T) bool) {
8+
t.Helper()
9+
10+
t1 := &testing.T{}
11+
12+
actual := block(t1)
13+
14+
if actual != expected {
15+
t.Errorf("expected: %v, actual: %v", expected, actual)
16+
}
17+
}
18+
719
func Test_AssertEqual(t *testing.T) {
8-
Equal(t, 1, int(1))
9-
Equal(t, 123, uint(123))
10-
Equal(t, 321, uint16(321))
11-
Equal(t, int32(100), int64(100))
12-
Equal(t, 100, uint64(100))
20+
testAssert(t, true, func(t *testing.T) bool {
21+
return Equal(t, 1, 1)
22+
})
23+
24+
testAssert(t, false, func(t *testing.T) bool {
25+
return Equal(t, 1, 2)
26+
})
27+
28+
testAssert(t, true, func(t *testing.T) bool {
29+
return Equal(t, 1, int(1))
30+
})
31+
32+
testAssert(t, true, func(t *testing.T) bool {
33+
return Equal(t, 123, uint(123))
34+
})
35+
36+
testAssert(t, true, func(t *testing.T) bool {
37+
return Equal(t, 321, uint16(321))
38+
})
39+
40+
testAssert(t, true, func(t *testing.T) bool {
41+
return Equal(t, int32(100), int64(100))
42+
})
43+
44+
testAssert(t, true, func(t *testing.T) bool {
45+
return Equal(t, 100, uint64(100))
46+
})
1347
}
1448

1549
func Test_NotEqual(t *testing.T) {
16-
NotEqual(t, 2, 3)
17-
NotEqual(t, 2, int32(3))
50+
testAssert(t, true, func(t *testing.T) bool {
51+
return NotEqual(t, 2, 3)
52+
})
53+
testAssert(t, false, func(t *testing.T) bool {
54+
return NotEqual(t, 3, 3)
55+
})
56+
57+
testAssert(t, true, func(t *testing.T) bool {
58+
return NotEqual(t, 2, int32(3))
59+
})
60+
61+
testAssert(t, true, func(t *testing.T) bool {
62+
return NotEqual(t, int64(2), int32(3))
63+
})
64+
}
65+
66+
func Test_StrictEqual(t *testing.T) {
67+
testAssert(t, true, func(t *testing.T) bool {
68+
return StrictEqual(t, 1, 1)
69+
})
70+
testAssert(t, false, func(t *testing.T) bool {
71+
return StrictEqual(t, 2, 1)
72+
})
73+
74+
testAssert(t, false, func(t *testing.T) bool {
75+
return StrictEqual(t, int32(1), 1)
76+
})
77+
78+
testAssert(t, false, func(t *testing.T) bool {
79+
return StrictEqual(t, 1, int64(1))
80+
})
81+
}
82+
83+
func ptr(i int) *int {
84+
return &i
85+
}
86+
87+
func Test_Same(t *testing.T) {
88+
testAssert(t, false, func(t *testing.T) bool {
89+
return Same(t, ptr(1), ptr(1))
90+
})
91+
testAssert(t, false, func(t *testing.T) bool {
92+
return Same(t, 1, 1)
93+
})
94+
}
95+
96+
func Test_NotSame(t *testing.T) {
97+
testAssert(t, true, func(t *testing.T) bool {
98+
return NotSame(t, ptr(1), ptr(1))
99+
})
100+
testAssert(t, true, func(t *testing.T) bool {
101+
return NotSame(t, 1, 1)
102+
})
103+
}
104+
105+
func Test_Contains_NotContains(t *testing.T) {
106+
testAssert(t, true, func(t *testing.T) bool {
107+
return Contains(t, []int{1, 2, 3}, 1)
108+
})
109+
testAssert(t, false, func(t *testing.T) bool {
110+
return Contains(t, []int{1, 2, 3}, 4)
111+
})
112+
testAssert(t, true, func(t *testing.T) bool {
113+
return Contains(t, []string{"Hello", "World"}, "Hello")
114+
})
115+
testAssert(t, true, func(t *testing.T) bool {
116+
return Contains(t, "Hello World", "World")
117+
})
118+
119+
// NotContains
120+
testAssert(t, false, func(t *testing.T) bool {
121+
return NotContains(t, []int{1, 2, 3}, 1)
122+
})
123+
testAssert(t, true, func(t *testing.T) bool {
124+
return NotContains(t, []int{1, 2, 3}, 4)
125+
})
126+
testAssert(t, false, func(t *testing.T) bool {
127+
return NotContains(t, []string{"Hello", "World"}, "Hello")
128+
})
129+
testAssert(t, false, func(t *testing.T) bool {
130+
return NotContains(t, "Hello World", "World")
131+
})
132+
}
133+
134+
func Test_ElementsMatch(t *testing.T) {
135+
testAssert(t, true, func(t *testing.T) bool {
136+
return ElementsMatch(t, []int{1, 2, 3}, []int{3, 1, 2})
137+
})
138+
testAssert(t, false, func(t *testing.T) bool {
139+
return ElementsMatch(t, []int{1, 2, 3}, []int{3, 1})
140+
})
141+
142+
testAssert(t, true, func(t *testing.T) bool {
143+
return ElementsMatch(t, []string{"A", "B", "C"}, []string{"B", "C", "A"})
144+
})
145+
testAssert(t, false, func(t *testing.T) bool {
146+
return ElementsMatch(t, []string{"A", "B", "C"}, []string{"B", "D", "C", "A"})
147+
})
148+
}
149+
150+
func Test_AssertNil(t *testing.T) {
151+
testAssert(t, true, func(t *testing.T) bool {
152+
return Nil(t, nil)
153+
})
154+
155+
testAssert(t, false, func(t *testing.T) bool {
156+
return Nil(t, []int{})
157+
})
158+
159+
testAssert(t, false, func(t *testing.T) bool {
160+
return Nil(t, 1)
161+
})
162+
163+
testAssert(t, false, func(t *testing.T) bool {
164+
return Nil(t, "hello")
165+
})
166+
167+
testAssert(t, false, func(t *testing.T) bool {
168+
return Nil(t, []int{1})
169+
})
170+
171+
testAssert(t, false, func(t *testing.T) bool {
172+
return Nil(t, []string{"hello"})
173+
})
174+
175+
testAssert(t, false, func(t *testing.T) bool {
176+
return Nil(t, []any{"hello"})
177+
})
18178
}

go.mod

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
module github.com/longbridgeapp/assert
22

3-
go 1.16
3+
go 1.18
44

5-
require github.com/stretchr/testify v1.7.0 // indirect
5+
require github.com/stretchr/testify v1.7.0
6+
7+
require (
8+
github.com/davecgh/go-spew v1.1.0 // indirect
9+
github.com/pmezard/go-difflib v1.0.0 // indirect
10+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
11+
)

go.sum

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8
22
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
33
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
44
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5-
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
65
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
76
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
87
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
8+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
99
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
1010
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
1111
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)