Skip to content

Commit

Permalink
- fix nil test so that []interface{} when nil works correctly
Browse files Browse the repository at this point in the history
- use switch for haslength.go
  • Loading branch information
mcorbyeaglen committed Dec 17, 2020
1 parent e5a7240 commit 160fd77
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
9 changes: 5 additions & 4 deletions has/haslength.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ func Length(expected interface{}) *gocrest.Matcher {
}
lenOfActual := reflect.ValueOf(actual).Len()
matcher.Actual = fmt.Sprintf("length was %d", lenOfActual)
typeMatcher, ok := expected.(*gocrest.Matcher)
if ok {
matcher.Describe = fmt.Sprintf(description, typeMatcher.Describe)
return typeMatcher.Matches(lenOfActual)
switch expected.(type) {
case *gocrest.Matcher:
m := expected.(*gocrest.Matcher)
matcher.Describe = fmt.Sprintf(description, m.Describe)
return m.Matches(lenOfActual)
}
return lenOfActual == expected
}
Expand Down
23 changes: 20 additions & 3 deletions is/nil.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
package is

import "github.com/corbym/gocrest"
import (
"fmt"
"github.com/corbym/gocrest"
"reflect"
)

//Nil matches if the expected value is nil
//Nil matches if the actual value is nil
func Nil() *gocrest.Matcher {
return EqualTo(nil)
match := new(gocrest.Matcher)
match.Describe = "value that is <nil>"
match.Matches = func(actual interface{}) bool {
match.Actual = fmt.Sprintf("%v", actual)
if actual == nil {
return true
}
switch reflect.TypeOf(actual).Kind() {
case reflect.Ptr, reflect.Map, reflect.Array, reflect.Chan, reflect.Slice:
return reflect.ValueOf(actual).IsNil()
}
return false // anything else is never nil (hopefully)
}
return match
}
12 changes: 11 additions & 1 deletion matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ func TestMatcherDescription(t *testing.T) {
{description: "GreaterThanOrEqual", actual: 1, matcher: is.GreaterThanOrEqualTo(2), expected: "any of (value greater than <2> or value equal to <2>)"},
{description: "LessThan", actual: 2, matcher: is.LessThan(1), expected: "value less than <1>"},
{description: "LessThanOrEqualTo", actual: 2, matcher: is.LessThanOrEqualTo(1), expected: "any of (value less than <1> or value equal to <1>)"},
{description: "Nil", actual: 1, matcher: is.Nil(), expected: "value equal to <<nil>>"},
{description: "Nil", actual: 1, matcher: is.Nil(), expected: "value that is <nil>"},
{description: "ValueContaining", actual: []string{"Foo", "Bar"}, matcher: is.ValueContaining([]string{"Baz", "Bing"}), expected: "something that contains [Baz Bing]"},
{description: "ValueContaining", actual: []string{"Foo", "Bar"}, matcher: is.ValueContaining(is.EqualTo("Baz"), is.EqualTo("Bing")), expected: "something that contains value equal to <Baz> and value equal to <Bing>"},
{description: "MatchesPattern", actual: "blarney stone", matcher: is.MatchForPattern("~123.?.*"), expected: "a value that matches pattern ~123.?.*"},
Expand Down Expand Up @@ -663,6 +663,16 @@ func TestTypeName(t *testing.T) {
}
}

func TestNilArrayInterface(t *testing.T) {
actual := nilResponse()

then.AssertThat(t, actual, is.Nil())
}

func nilResponse() []interface{} {
return nil
}

func TestEveryElement(t *testing.T) {
tests := []struct {
actual interface{}
Expand Down

0 comments on commit 160fd77

Please sign in to comment.