Skip to content

Commit

Permalink
* anyof and all of should carry actuals over
Browse files Browse the repository at this point in the history
  • Loading branch information
mattcorbyeaglen committed Jan 21, 2018
1 parent 0668f4a commit 1fc6722
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 10 deletions.
19 changes: 14 additions & 5 deletions is/allof.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,23 @@ import (
//Returns a matcher that performs the the test on the input matchers.
func AllOf(allMatchers ...*gocrest.Matcher) *gocrest.Matcher {
matcher := new(gocrest.Matcher)
matcher.Matches = func(actual interface{}) bool {
matcher.Describe = fmt.Sprintf("all of (%s)", describe(allMatchers, "and"))
matcher.Describe = fmt.Sprintf("all of (%s)", describe(allMatchers, "and"))
matcher.Matches = matchAll(allMatchers, matcher)
return matcher
}

func matchAll(allMatchers []*gocrest.Matcher, allOf *gocrest.Matcher) func(actual interface{}) bool {
return func(actual interface{}) bool {
var actualStrings []string
matches := true
allOf.AppendActual(fmt.Sprintf("actual <%v>", actual))
for x := 0; x < len(allMatchers); x++ {
if !allMatchers[x].Matches(actual) {
return false
matches = false
}
allOf.AppendActual(allMatchers[x].Actual)
actualStrings = append(actualStrings, allMatchers[x].Actual)
}
return true
return matches
}
return matcher
}
15 changes: 11 additions & 4 deletions is/anyof.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,20 @@ import (
func AnyOf(allMatchers ...*gocrest.Matcher) *gocrest.Matcher {
matcher := new(gocrest.Matcher)
matcher.Describe = fmt.Sprintf("any of (%s)", describe(allMatchers, "or"))
matcher.Matches = func(actual interface{}) bool {
matcher.Matches = anyMatcherMatches(allMatchers, matcher)
return matcher
}

func anyMatcherMatches(allMatchers []*gocrest.Matcher, anyOf *gocrest.Matcher) func(actual interface{}) bool {
return func(actual interface{}) bool {
matches := false
anyOf.AppendActual(fmt.Sprintf("actual <%v>", actual))
for x := 0; x < len(allMatchers); x++ {
if allMatchers[x].Matches(actual) {
return true
matches = true
}
anyOf.AppendActual(allMatchers[x].Actual)
}
return false
return matches
}
return matcher
}
10 changes: 9 additions & 1 deletion matcher.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package gocrest

import "fmt"
import (
"fmt"
"strings"
)

//Matcher provides the structure for matcher operations.
type Matcher struct {
Expand All @@ -27,3 +30,8 @@ func (matcher *Matcher) Reason(r string) *Matcher {
func (matcher *Matcher) Reasonf(format string, args ...interface{}) *Matcher {
return matcher.Reason(fmt.Sprintf(format, args...))
}

func (matcher *Matcher) AppendActual(actualAsString string) {
matcher.Actual += " " + actualAsString
matcher.Actual = strings.TrimSpace(matcher.Actual)
}
14 changes: 14 additions & 0 deletions matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,20 @@ func TestNotReturnsTheSubMatcherActual(testing *testing.T) {
is.EqualTo("length was 1"))
}

func TestAnyofReturnsTheSubMatcherActual(testing *testing.T) {
anyOf := is.AnyOf(has.Length(1), is.EqualTo("a"))
anyOf.Matches("a")
then.AssertThat(testing, anyOf.Actual,
is.EqualTo("actual <a> length was 1"))
}

func TestAllofReturnsTheSubMatcherActual(testing *testing.T) {
anyOf := is.AllOf(has.Length(1), is.EqualTo("a"))
anyOf.Matches("a")
then.AssertThat(testing, anyOf.Actual,
is.EqualTo("actual <a> length was 1"))
}

func TestIsNilMatches(testing *testing.T) {
then.AssertThat(testing, nil, is.Nil())
}
Expand Down

0 comments on commit 1fc6722

Please sign in to comment.