Skip to content

Commit

Permalink
EveryElement now accepts as many expects as len(actual) and checks n …
Browse files Browse the repository at this point in the history
…vs n
  • Loading branch information
jpolack committed Dec 11, 2020
1 parent 06f60a1 commit 60cb449
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 26 deletions.
20 changes: 12 additions & 8 deletions has/haseveryelement.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import (
"github.com/corbym/gocrest"
)

// Checks whether every element of the array/slice matches each expectation passed
// Checks whether the nth element of the array/slice matches the nth expectation passed
// Panics if the actual is not an array/slice
// Panics if the count of the expectations does not match the array's/slice's length
func EveryElement(expects ...*gocrest.Matcher) *gocrest.Matcher {
match := new(gocrest.Matcher)
match.Describe = fmt.Sprintf("elements to match all of (%s)", describe(expects, "and"))
match.Describe = fmt.Sprintf("elements to match %s", describe(expects, "and"))

for _, e := range expects {
match.AppendActual(e.Actual)
Expand All @@ -22,13 +23,16 @@ func EveryElement(expects ...*gocrest.Matcher) *gocrest.Matcher {
actualValue := reflect.ValueOf(actual)
switch actualValue.Kind() {
case reflect.Array, reflect.Slice:

if actualValue.Len() != len(expects) {
panic(fmt.Sprintf("cannot match expectations (length %v) to actuals (length %v)", len(expects), actualValue.Len()))
}

for i := 0; i < actualValue.Len(); i++ {
for _, expect := range expects {
result := expect.Matches(actualValue.Index(i).Interface())
result := expects[i].Matches(actualValue.Index(i).Interface())

if !result {
return false
}
if !result {
return false
}
}

Expand All @@ -45,7 +49,7 @@ func EveryElement(expects ...*gocrest.Matcher) *gocrest.Matcher {
func describe(matchers []*gocrest.Matcher, conjunction string) string {
var description string
for x := 0; x < len(matchers); x++ {
description += matchers[x].Describe
description += fmt.Sprintf("[%v]:%v", x, matchers[x].Describe)
if x+1 < len(matchers) {
description += fmt.Sprintf(" %s ", conjunction)
}
Expand Down
34 changes: 16 additions & 18 deletions matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -657,32 +657,17 @@ func TestEveryElement(t *testing.T) {
}{
{
actual: []string{"test1", "test2"},
expected: []*gocrest.Matcher{has.Prefix("test")},
expected: []*gocrest.Matcher{is.EqualTo("test1"), is.EqualTo("test2")},
shouldFail: false,
},
{
actual: []int{1, 2},
expected: []*gocrest.Matcher{is.GreaterThanOrEqualTo(1)},
shouldFail: false,
},
{
actual: []int{1, 2},
expected: []*gocrest.Matcher{is.GreaterThanOrEqualTo(0), is.GreaterThanOrEqualTo(1)},
expected: []*gocrest.Matcher{is.EqualTo(1), is.EqualTo(2)},
shouldFail: false,
},
{
actual: []string{"test1", "test2"},
expected: []*gocrest.Matcher{has.Prefix("nottest")},
shouldFail: true,
},
{
actual: []int{1, 2},
expected: []*gocrest.Matcher{is.GreaterThanOrEqualTo(2)},
shouldFail: true,
},
{
actual: []int{1, 2},
expected: []*gocrest.Matcher{is.GreaterThanOrEqualTo(1), is.GreaterThanOrEqualTo(2)},
expected: []*gocrest.Matcher{is.EqualTo("test1"), is.EqualTo("nottest")},
shouldFail: true,
},
}
Expand All @@ -704,6 +689,19 @@ func TestEveryElementPanic(t *testing.T) {
is.Empty(),
},
},
{
actual: []int{1, 2},
expected: []*gocrest.Matcher{
is.EqualTo(1),
},
},
{
actual: []int{1},
expected: []*gocrest.Matcher{
is.EqualTo(1),
is.EqualTo(2),
},
},
}

defer func() {
Expand Down

0 comments on commit 60cb449

Please sign in to comment.