Skip to content

Commit

Permalink
Fix assertion IsNil() failing when passing a nil slice (#94)
Browse files Browse the repository at this point in the history
* Fixed assertion IsNil() failing when passing a nil slice

* Fixed assertion IsNil() failing when passing a nil slice

* Fixed assertion IsNil() failing when passing a nil slice

* Expand Nil checks to every builtin go type
  • Loading branch information
Neirpyc committed Oct 3, 2021
1 parent 85d372a commit 0a4f594
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 20 deletions.
25 changes: 21 additions & 4 deletions assertions.go
Expand Up @@ -91,20 +91,37 @@ func (a *Assertion) IsFalse(messages ...interface{}) {
}
}

// isNil returns whether a.src is nil or not.
func (a *Assertion) isNil() bool {
if !objectsAreEqual(a.src, nil) {
specialKinds := []reflect.Kind{
reflect.Slice, reflect.Chan,
reflect.Map, reflect.Ptr,
reflect.Interface, reflect.Func,
}
t := reflect.TypeOf(a.src).Kind()
for _, kind := range specialKinds {
if t == kind {
return reflect.ValueOf(a.src).IsNil()
}
}
return false
}
return true
}

// IsNil asserts that source is nil.
func (a *Assertion) IsNil(messages ...interface{}) {
if !objectsAreEqual(a.src, nil) {
if !a.isNil() {
message := fmt.Sprintf("%v %s%v", a.src, "expected to be nil", formatMessages(messages...))

a.fail(message)
}
}

// IsNotNil asserts that source is not nil.
func (a *Assertion) IsNotNil(messages ...interface{}) {
if objectsAreEqual(a.src, nil) {
if a.isNil() {
message := fmt.Sprintf("%v %s%v", a.src, "is nil", formatMessages(messages...))

a.fail(message)
}
}
Expand Down
78 changes: 62 additions & 16 deletions assertions_test.go
Expand Up @@ -147,15 +147,38 @@ func TestIsTrueWithMessage(t *testing.T) {
}

func TestIsNil(t *testing.T) {
verifier := AssertionVerifier{ShouldPass: true}
a := Assertion{src: nil, fail: verifier.FailFunc}
a.IsNil()
verifier.Verify(t)
check := func(isNil interface{}, isNotNil interface{}) {
verifier := AssertionVerifier{ShouldPass: true}
a := Assertion{src: isNil, fail: verifier.FailFunc}
a.IsNil()
verifier.Verify(t)

verifier = AssertionVerifier{ShouldPass: false}
a = Assertion{src: isNotNil, fail: verifier.FailFunc}
a.IsNil()
verifier.Verify(t)
}

verifier = AssertionVerifier{ShouldPass: false}
a = Assertion{src: struct{}{}, fail: verifier.FailFunc}
a.IsNil()
verifier.Verify(t)
check(nil, struct {}{})

var s []struct{}
check(s, make([]struct{}, 0))

var c chan struct{}
check(c, make(chan struct{}, 0))

var m map[struct{}]struct{}
check(m, make(map[struct{}]struct{}, 0))

var p *struct{}
check(p, &s)

var ni interface{} = nil
var i interface{} = struct {}{}
check(ni, i)

var f func()
check(f, check)
}

func TestIsNilWithMessage(t *testing.T) {
Expand All @@ -167,15 +190,38 @@ func TestIsNilWithMessage(t *testing.T) {
}

func TestIsNotNil(t *testing.T) {
verifier := AssertionVerifier{ShouldPass: true}
a := Assertion{src: 1, fail: verifier.FailFunc}
a.IsNotNil()
verifier.Verify(t)
check := func(isNil interface{}, isNotNil interface{}) {
verifier := AssertionVerifier{ShouldPass: false}
a := Assertion{src: isNil, fail: verifier.FailFunc}
a.IsNotNil()
verifier.Verify(t)

verifier = AssertionVerifier{ShouldPass: true}
a = Assertion{src: isNotNil, fail: verifier.FailFunc}
a.IsNotNil()
verifier.Verify(t)
}

verifier = AssertionVerifier{ShouldPass: false}
a = Assertion{src: nil, fail: verifier.FailFunc}
a.IsNotNil()
verifier.Verify(t)
check(nil, struct {}{})

var s []struct{}
check(s, make([]struct{}, 0))

var c chan struct{}
check(c, make(chan struct{}, 0))

var m map[struct{}]struct{}
check(m, make(map[struct{}]struct{}, 0))

var p *struct{}
check(p, &s)

var ni interface{} = nil
var i interface{} = struct {}{}
check(ni, i)

var f func()
check(f, check)
}

func TestIsNotNilWithMessage(t *testing.T) {
Expand Down

0 comments on commit 0a4f594

Please sign in to comment.