Skip to content

Commit

Permalink
Merge pull request #12 from mastertinner/master
Browse files Browse the repository at this point in the history
Return true for two nil slices or two nil maps
  • Loading branch information
matryer committed Jun 8, 2018
2 parents be846f6 + 9afb5cf commit 9c9f594
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 37 deletions.
67 changes: 32 additions & 35 deletions is.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
//
// The following failing test:
//
// func Test(t *testing.T) {
// is := is.New(t)
// a, b := 1, 2
// is.Equal(a, b) // expect to be the same
// }
// func Test(t *testing.T) {
// is := is.New(t)
// a, b := 1, 2
// is.Equal(a, b) // expect to be the same
// }
//
// Will output:
//
// your_test.go:123: 1 != 2 // expect to be the same
// your_test.go:123: 1 != 2 // expect to be the same
//
// Usage
//
Expand All @@ -33,7 +33,7 @@
// body := readBody(r)
// is.True(strings.Contains(body, "Hi there"))
//
// }
// }
package is

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

// Fail immediately fails the test.
//
// func Test(t *testing.T) {
// func Test(t *testing.T) {
// is := is.New(t)
// is.Fail() // TODO: write this test
// }
Expand All @@ -121,15 +121,15 @@ func (is *I) Fail() {
// True asserts that the expression is true. The expression
// code itself will be reported if the assertion fails.
//
// func Test(t *testing.T) {
// func Test(t *testing.T) {
// is := is.New(t)
// val := method()
// is.True(val != nil) // val should never be nil
// }
//
// Will output:
//
// your_test.go:123: not true: val != nil
// your_test.go:123: not true: val != nil
func (is *I) True(expression bool) {
if !expression {
is.log("not true: $ARGS")
Expand All @@ -138,15 +138,15 @@ func (is *I) True(expression bool) {

// Equal asserts that a and b are equal.
//
// func Test(t *testing.T) {
// func Test(t *testing.T) {
// is := is.New(t)
// a := greet("Mat")
// is.Equal(a, "Hi Mat") // greeting
// }
// }
//
// Will output:
//
// your_test.go:123: Hey Mat != Hi Mat // greeting
// your_test.go:123: Hey Mat != Hi Mat // greeting
func (is *I) Equal(a, b interface{}) {
if !areEqual(a, b) {
if isNil(a) || isNil(b) {
Expand All @@ -173,13 +173,13 @@ func (is *I) Equal(a, b interface{}) {
// It allows you to write subtests using a fimilar
// pattern:
//
// func Test(t *testing.T) {
// func Test(t *testing.T) {
// is := is.New(t)
// t.Run("sub", func(t *testing.T) {
// is := is.New(t)
// // TODO: test
// })
// }
// t.Run("sub", func(t *testing.T) {
// is := is.New(t)
// // TODO: test
// })
// }
func (is *I) New(t *testing.T) *I {
return New(t)
}
Expand All @@ -188,13 +188,13 @@ func (is *I) New(t *testing.T) *I {
// method. It allows you to write subtests using a fimilar
// pattern:
//
// func Test(t *testing.T) {
// func Test(t *testing.T) {
// is := is.New(t)
// t.Run("sub", func(t *testing.T) {
// is := is.New(t)
// // TODO: test
// })
// }
// t.Run("sub", func(t *testing.T) {
// is := is.New(t)
// // TODO: test
// })
// }
func (is *I) NewRelaxed(t *testing.T) *I {
return NewRelaxed(t)
}
Expand All @@ -208,7 +208,7 @@ func (is *I) valWithType(v interface{}) string {

// NoErr asserts that err is nil.
//
// func Test(t *testing.T) {
// func Test(t *testing.T) {
// is := is.New(t)
// val, err := getVal()
// is.NoErr(err) // getVal error
Expand All @@ -217,7 +217,7 @@ func (is *I) valWithType(v interface{}) string {
//
// Will output:
//
// your_test.go:123: err: not found // getVal error
// your_test.go:123: err: not found // getVal error
func (is *I) NoErr(err error) {
if err != nil {
is.logf("err: %s", err.Error())
Expand Down Expand Up @@ -246,17 +246,14 @@ func areEqual(a, b interface{}) bool {
if !isNil(a) && isNil(b) {
return false
}
return a == b
return true
}
if reflect.DeepEqual(a, b) {
return true
}
aValue := reflect.ValueOf(a)
bValue := reflect.ValueOf(b)
if aValue == bValue {
return true
}
return false
return aValue == bValue
}

func callerinfo() (path string, line int, ok bool) {
Expand Down Expand Up @@ -318,7 +315,7 @@ func loadArguments(path string, line int) (string, bool) {
text = text[braceI+1:]
cs := bufio.NewScanner(strings.NewReader(text))
cs.Split(bufio.ScanBytes)
i := 0
j := 0
c := 1
for cs.Scan() {
switch cs.Text() {
Expand All @@ -330,9 +327,9 @@ func loadArguments(path string, line int) (string, bool) {
if c == 0 {
break
}
i++
j++
}
text = text[:i]
text = text[:j]
return text, true
}
i++
Expand Down
30 changes: 28 additions & 2 deletions is_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,33 @@ var tests = []struct {
},
Fail: ` // nil slice`,
},
{
N: "Equal(nil, nil)",
F: func(is *I) {
var s1 []string
var s2 []string
is.Equal(s1, s2) // nil slices
},
Fail: ``,
},
{
N: "Equal(nil, map)",
F: func(is *I) {
var m1 map[string]string
m2 := map[string]string{}
is.Equal(m1, m2) // nil map
},
Fail: ` // nil map`,
},
{
N: "Equal(nil, nil)",
F: func(is *I) {
var m1 map[string]string
var m2 map[string]string
is.Equal(m1, m2) // nil maps
},
Fail: ``,
},

// Fail
{
Expand Down Expand Up @@ -244,9 +271,8 @@ func TestLoadArguments(t *testing.T) {
// TestSubtests ensures subtests work as expected.
// https://github.com/matryer/is/issues/1
func TestSubtests(t *testing.T) {
is := New(t)
t.Run("sub1", func(t *testing.T) {
is := is.New(t)
is := New(t)
is.Equal(1+1, 2)
})
}

0 comments on commit 9c9f594

Please sign in to comment.