Skip to content

Commit 719b0e5

Browse files
authored
[bugfix] fix Nil, NotNil to check nil functions (#12)
* add test cases for nil pointer, nil map, nil slice, nil chan and nil func
1 parent 2f9cabd commit 719b0e5

File tree

2 files changed

+200
-8
lines changed

2 files changed

+200
-8
lines changed

assert_equality.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func isNil(value interface{}) bool {
5555
}
5656

5757
switch reflect.TypeOf(value).Kind() {
58-
case reflect.Ptr, reflect.Map, reflect.Array, reflect.Chan, reflect.Slice:
58+
case reflect.Ptr, reflect.Map, reflect.Slice, reflect.Chan, reflect.Func:
5959
return reflect.ValueOf(value).IsNil()
6060
}
6161

assert_equality_test.go

Lines changed: 199 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,67 @@ func Test_NotEqualShouldFail_WhenActualMatchesExpected(t *testing.T) {
5454
}
5555
}
5656

57-
func Test_NilShouldPass_GivenNilValue(t *testing.T) {
57+
func Test_NilShouldPass_GivenNilPointer(t *testing.T) {
5858
tester := new(testing.T)
5959

6060
var valuePtr *string = nil
6161

6262
Nil(tester, valuePtr)
6363

6464
if tester.Failed() {
65-
t.Error("Nil did not pass when nil value was given")
65+
t.Error("Nil did not pass when nil pointer was given")
6666
}
6767
}
6868

69-
func Test_NilShouldFail_GivenNonNilValue(t *testing.T) {
69+
func Test_NilShouldPass_GivenNilMap(t *testing.T) {
70+
tester := new(testing.T)
71+
72+
var nilMap map[int]int = nil
73+
74+
Nil(tester, nilMap)
75+
76+
if tester.Failed() {
77+
t.Error("Nil did not pass when nil map was given")
78+
}
79+
}
80+
81+
func Test_NilShouldPass_GivenNilSlice(t *testing.T) {
82+
tester := new(testing.T)
83+
84+
var nilSlice []int = nil
85+
86+
Nil(tester, nilSlice)
87+
88+
if tester.Failed() {
89+
t.Error("Nil did not pass when nil slice was given")
90+
}
91+
}
92+
93+
func Test_NilShouldPass_GivenNilChannel(t *testing.T) {
94+
tester := new(testing.T)
95+
96+
var nilChan chan int = nil
97+
98+
Nil(tester, nilChan)
99+
100+
if tester.Failed() {
101+
t.Error("Nil did not pass when nil channel was given")
102+
}
103+
}
104+
105+
func Test_NilShouldPass_GivenNilFunction(t *testing.T) {
106+
tester := new(testing.T)
107+
108+
var nilFunc func() = nil
109+
110+
Nil(tester, nilFunc)
111+
112+
if tester.Failed() {
113+
t.Error("Nil did not pass when nil function was given")
114+
}
115+
}
116+
117+
func Test_NilShouldFail_GivenNonNilPointer(t *testing.T) {
70118
tester := new(testing.T)
71119

72120
value := "value"
@@ -79,7 +127,55 @@ func Test_NilShouldFail_GivenNonNilValue(t *testing.T) {
79127
}
80128
}
81129

82-
func Test_NotNilShouldPass_GivenNonNilValue(t *testing.T) {
130+
func Test_NilShouldFail_GivenNonNilMap(t *testing.T) {
131+
tester := new(testing.T)
132+
133+
nonNilMap := make(map[int]int, 0)
134+
135+
Nil(tester, nonNilMap)
136+
137+
if !tester.Failed() {
138+
t.Error("Nil did not fail when non-nil map was given")
139+
}
140+
}
141+
142+
func Test_NilShouldFail_GivenNonNilSlice(t *testing.T) {
143+
tester := new(testing.T)
144+
145+
nonNilSlice := []int{10}
146+
147+
Nil(tester, nonNilSlice)
148+
149+
if !tester.Failed() {
150+
t.Error("Nil did not fail when non-nil slice was given")
151+
}
152+
}
153+
154+
func Test_NilShouldFail_GivenNonNilChannel(t *testing.T) {
155+
tester := new(testing.T)
156+
157+
nonNilChan := make(chan int)
158+
159+
Nil(tester, nonNilChan)
160+
161+
if !tester.Failed() {
162+
t.Error("Nil did not fail when non-nil channel was given")
163+
}
164+
}
165+
166+
func Test_NilShouldFail_GivenNonNilFunction(t *testing.T) {
167+
tester := new(testing.T)
168+
169+
nonNilFunc := func() {}
170+
171+
Nil(tester, nonNilFunc)
172+
173+
if !tester.Failed() {
174+
t.Error("Nil did not fail when non-nil function was given")
175+
}
176+
}
177+
178+
func Test_NotNilShouldPass_GivenNonNilPointer(t *testing.T) {
83179
tester := new(testing.T)
84180

85181
value := "value"
@@ -88,19 +184,115 @@ func Test_NotNilShouldPass_GivenNonNilValue(t *testing.T) {
88184
NotNil(tester, valuePtr)
89185

90186
if tester.Failed() {
91-
t.Error("NotNil did not pass when non-nil value was given")
187+
t.Error("NotNil did not pass when non-nil pointer was given")
92188
}
93189
}
94190

95-
func Test_NotNilShouldFail_GivenNilValue(t *testing.T) {
191+
func Test_NotNilShouldPass_GivenNonNilMap(t *testing.T) {
192+
tester := new(testing.T)
193+
194+
nonNilMap := make(map[int]int, 0)
195+
196+
NotNil(tester, nonNilMap)
197+
198+
if tester.Failed() {
199+
t.Error("NotNil did not pass when non-nil map was given")
200+
}
201+
}
202+
203+
func Test_NotNilShouldPass_GivenNonNilSlice(t *testing.T) {
204+
tester := new(testing.T)
205+
206+
nonNilSlice := []int{10}
207+
208+
NotNil(tester, nonNilSlice)
209+
210+
if tester.Failed() {
211+
t.Error("NotNil did not pass when non-nil slice was given")
212+
}
213+
}
214+
215+
func Test_NotNilShouldPass_GivenNonNilChan(t *testing.T) {
216+
tester := new(testing.T)
217+
218+
nonNilChan := make(chan int)
219+
220+
NotNil(tester, nonNilChan)
221+
222+
if tester.Failed() {
223+
t.Error("NotNil did not pass when non-nil channel was given")
224+
}
225+
}
226+
227+
func Test_NotNilShouldPass_GivenNonNilFunction(t *testing.T) {
228+
tester := new(testing.T)
229+
230+
nonNilFunc := func() {}
231+
232+
NotNil(tester, nonNilFunc)
233+
234+
if tester.Failed() {
235+
t.Error("NotNil did not pass when non-nil function was given")
236+
}
237+
}
238+
239+
func Test_NotNilShouldFail_GivenNilPointer(t *testing.T) {
96240
tester := new(testing.T)
97241

98242
var valuePtr *string = nil
99243

100244
NotNil(tester, valuePtr)
101245

102246
if !tester.Failed() {
103-
t.Error("NotNil did not fail when nil value was given")
247+
t.Error("NotNil did not fail when nil pointer was given")
248+
}
249+
}
250+
251+
func Test_NotNilShouldFail_GivenNilMap(t *testing.T) {
252+
tester := new(testing.T)
253+
254+
var nilMap map[int]int = nil
255+
256+
NotNil(tester, nilMap)
257+
258+
if !tester.Failed() {
259+
t.Error("NotNil did not fail when nil map was given")
260+
}
261+
}
262+
263+
func Test_NotNilShouldFail_GivenNilSlice(t *testing.T) {
264+
tester := new(testing.T)
265+
266+
var nilSlice []int = nil
267+
268+
NotNil(tester, nilSlice)
269+
270+
if !tester.Failed() {
271+
t.Error("NotNil did not fail when nil slice was given")
272+
}
273+
}
274+
275+
func Test_NotNilShouldFail_GivenNilChannel(t *testing.T) {
276+
tester := new(testing.T)
277+
278+
var nilChan chan int = nil
279+
280+
NotNil(tester, nilChan)
281+
282+
if !tester.Failed() {
283+
t.Error("NotNil did not fail when nil channel was given")
284+
}
285+
}
286+
287+
func Test_NotNilShouldFail_GivenNilFunction(t *testing.T) {
288+
tester := new(testing.T)
289+
290+
var nilFunc func() = nil
291+
292+
NotNil(tester, nilFunc)
293+
294+
if !tester.Failed() {
295+
t.Error("NotNil did not fail when nil function was given")
104296
}
105297
}
106298

0 commit comments

Comments
 (0)