Skip to content

Commit 9c9f594

Browse files
authored
Merge pull request #12 from mastertinner/master
Return true for two nil slices or two nil maps
2 parents be846f6 + 9afb5cf commit 9c9f594

File tree

2 files changed

+60
-37
lines changed

2 files changed

+60
-37
lines changed

is.go

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
//
77
// The following failing test:
88
//
9-
// func Test(t *testing.T) {
10-
// is := is.New(t)
11-
// a, b := 1, 2
12-
// is.Equal(a, b) // expect to be the same
13-
// }
9+
// func Test(t *testing.T) {
10+
// is := is.New(t)
11+
// a, b := 1, 2
12+
// is.Equal(a, b) // expect to be the same
13+
// }
1414
//
1515
// Will output:
1616
//
17-
// your_test.go:123: 1 != 2 // expect to be the same
17+
// your_test.go:123: 1 != 2 // expect to be the same
1818
//
1919
// Usage
2020
//
@@ -33,7 +33,7 @@
3333
// body := readBody(r)
3434
// is.True(strings.Contains(body, "Hi there"))
3535
//
36-
// }
36+
// }
3737
package is
3838

3939
import (
@@ -107,7 +107,7 @@ func (is *I) logf(format string, args ...interface{}) {
107107

108108
// Fail immediately fails the test.
109109
//
110-
// func Test(t *testing.T) {
110+
// func Test(t *testing.T) {
111111
// is := is.New(t)
112112
// is.Fail() // TODO: write this test
113113
// }
@@ -121,15 +121,15 @@ func (is *I) Fail() {
121121
// True asserts that the expression is true. The expression
122122
// code itself will be reported if the assertion fails.
123123
//
124-
// func Test(t *testing.T) {
124+
// func Test(t *testing.T) {
125125
// is := is.New(t)
126126
// val := method()
127127
// is.True(val != nil) // val should never be nil
128128
// }
129129
//
130130
// Will output:
131131
//
132-
// your_test.go:123: not true: val != nil
132+
// your_test.go:123: not true: val != nil
133133
func (is *I) True(expression bool) {
134134
if !expression {
135135
is.log("not true: $ARGS")
@@ -138,15 +138,15 @@ func (is *I) True(expression bool) {
138138

139139
// Equal asserts that a and b are equal.
140140
//
141-
// func Test(t *testing.T) {
141+
// func Test(t *testing.T) {
142142
// is := is.New(t)
143143
// a := greet("Mat")
144144
// is.Equal(a, "Hi Mat") // greeting
145-
// }
145+
// }
146146
//
147147
// Will output:
148148
//
149-
// your_test.go:123: Hey Mat != Hi Mat // greeting
149+
// your_test.go:123: Hey Mat != Hi Mat // greeting
150150
func (is *I) Equal(a, b interface{}) {
151151
if !areEqual(a, b) {
152152
if isNil(a) || isNil(b) {
@@ -173,13 +173,13 @@ func (is *I) Equal(a, b interface{}) {
173173
// It allows you to write subtests using a fimilar
174174
// pattern:
175175
//
176-
// func Test(t *testing.T) {
176+
// func Test(t *testing.T) {
177177
// is := is.New(t)
178-
// t.Run("sub", func(t *testing.T) {
179-
// is := is.New(t)
180-
// // TODO: test
181-
// })
182-
// }
178+
// t.Run("sub", func(t *testing.T) {
179+
// is := is.New(t)
180+
// // TODO: test
181+
// })
182+
// }
183183
func (is *I) New(t *testing.T) *I {
184184
return New(t)
185185
}
@@ -188,13 +188,13 @@ func (is *I) New(t *testing.T) *I {
188188
// method. It allows you to write subtests using a fimilar
189189
// pattern:
190190
//
191-
// func Test(t *testing.T) {
191+
// func Test(t *testing.T) {
192192
// is := is.New(t)
193-
// t.Run("sub", func(t *testing.T) {
194-
// is := is.New(t)
195-
// // TODO: test
196-
// })
197-
// }
193+
// t.Run("sub", func(t *testing.T) {
194+
// is := is.New(t)
195+
// // TODO: test
196+
// })
197+
// }
198198
func (is *I) NewRelaxed(t *testing.T) *I {
199199
return NewRelaxed(t)
200200
}
@@ -208,7 +208,7 @@ func (is *I) valWithType(v interface{}) string {
208208

209209
// NoErr asserts that err is nil.
210210
//
211-
// func Test(t *testing.T) {
211+
// func Test(t *testing.T) {
212212
// is := is.New(t)
213213
// val, err := getVal()
214214
// is.NoErr(err) // getVal error
@@ -217,7 +217,7 @@ func (is *I) valWithType(v interface{}) string {
217217
//
218218
// Will output:
219219
//
220-
// your_test.go:123: err: not found // getVal error
220+
// your_test.go:123: err: not found // getVal error
221221
func (is *I) NoErr(err error) {
222222
if err != nil {
223223
is.logf("err: %s", err.Error())
@@ -246,17 +246,14 @@ func areEqual(a, b interface{}) bool {
246246
if !isNil(a) && isNil(b) {
247247
return false
248248
}
249-
return a == b
249+
return true
250250
}
251251
if reflect.DeepEqual(a, b) {
252252
return true
253253
}
254254
aValue := reflect.ValueOf(a)
255255
bValue := reflect.ValueOf(b)
256-
if aValue == bValue {
257-
return true
258-
}
259-
return false
256+
return aValue == bValue
260257
}
261258

262259
func callerinfo() (path string, line int, ok bool) {
@@ -318,7 +315,7 @@ func loadArguments(path string, line int) (string, bool) {
318315
text = text[braceI+1:]
319316
cs := bufio.NewScanner(strings.NewReader(text))
320317
cs.Split(bufio.ScanBytes)
321-
i := 0
318+
j := 0
322319
c := 1
323320
for cs.Scan() {
324321
switch cs.Text() {
@@ -330,9 +327,9 @@ func loadArguments(path string, line int) (string, bool) {
330327
if c == 0 {
331328
break
332329
}
333-
i++
330+
j++
334331
}
335-
text = text[:i]
332+
text = text[:j]
336333
return text, true
337334
}
338335
i++

is_test.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,33 @@ var tests = []struct {
113113
},
114114
Fail: ` // nil slice`,
115115
},
116+
{
117+
N: "Equal(nil, nil)",
118+
F: func(is *I) {
119+
var s1 []string
120+
var s2 []string
121+
is.Equal(s1, s2) // nil slices
122+
},
123+
Fail: ``,
124+
},
125+
{
126+
N: "Equal(nil, map)",
127+
F: func(is *I) {
128+
var m1 map[string]string
129+
m2 := map[string]string{}
130+
is.Equal(m1, m2) // nil map
131+
},
132+
Fail: ` // nil map`,
133+
},
134+
{
135+
N: "Equal(nil, nil)",
136+
F: func(is *I) {
137+
var m1 map[string]string
138+
var m2 map[string]string
139+
is.Equal(m1, m2) // nil maps
140+
},
141+
Fail: ``,
142+
},
116143

117144
// Fail
118145
{
@@ -244,9 +271,8 @@ func TestLoadArguments(t *testing.T) {
244271
// TestSubtests ensures subtests work as expected.
245272
// https://github.com/matryer/is/issues/1
246273
func TestSubtests(t *testing.T) {
247-
is := New(t)
248274
t.Run("sub1", func(t *testing.T) {
249-
is := is.New(t)
275+
is := New(t)
250276
is.Equal(1+1, 2)
251277
})
252278
}

0 commit comments

Comments
 (0)