From 3c4d62a74492d09c23df307bd3e7f73ad8f79f3c Mon Sep 17 00:00:00 2001 From: Tobi Fuhrimann Date: Fri, 1 Jun 2018 12:59:12 +0200 Subject: [PATCH 1/2] Return true for two nil slices or two nil maps --- is.go | 56 +++++++++++++++++++++++++++--------------------------- is_test.go | 27 ++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 28 deletions(-) diff --git a/is.go b/is.go index 8b4de30..06008cb 100644 --- a/is.go +++ b/is.go @@ -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 // @@ -33,7 +33,7 @@ // body := readBody(r) // is.True(strings.Contains(body, "Hi there")) // -// } +// } package is import ( @@ -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 // } @@ -121,7 +121,7 @@ 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 @@ -129,7 +129,7 @@ func (is *I) Fail() { // // 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") @@ -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) { @@ -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) } @@ -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) } @@ -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 @@ -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()) @@ -246,7 +246,7 @@ 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 diff --git a/is_test.go b/is_test.go index 65b093e..2c2ac9d 100644 --- a/is_test.go +++ b/is_test.go @@ -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 { From 9afb5cf4082f945e6153c103d6287666e40f9705 Mon Sep 17 00:00:00 2001 From: Tobi Fuhrimann Date: Fri, 1 Jun 2018 13:07:11 +0200 Subject: [PATCH 2/2] Fix linting issues --- is.go | 11 ++++------- is_test.go | 3 +-- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/is.go b/is.go index 06008cb..848be11 100644 --- a/is.go +++ b/is.go @@ -253,10 +253,7 @@ func areEqual(a, b interface{}) bool { } 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) { @@ -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() { @@ -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++ diff --git a/is_test.go b/is_test.go index 2c2ac9d..b27dcf2 100644 --- a/is_test.go +++ b/is_test.go @@ -271,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) }) }