Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions matchers/have_patterns.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type HavePatternsMatcher struct {
Elements interface{}
missingElements []string
foundElements []string
cachedActual string
}

func HavePatterns(elements interface{}) GossMatcher {
Expand Down Expand Up @@ -76,8 +77,10 @@ func (m *HavePatternsMatcher) Match(actual interface{}) (success bool, err error
scanner := bufio.NewScanner(fh)
scanner.Buffer(nil, maxScanTokenSize)
var found []patternMatcher
var lines []string
for scanner.Scan() {
line := scanner.Text()
lines = append(lines, line)

i := 0
for _, pat := range notfound {
Expand All @@ -99,6 +102,7 @@ func (m *HavePatternsMatcher) Match(actual interface{}) (success bool, err error
if err := scanner.Err(); err != nil {
return false, err
}
m.cachedActual = strings.Join(lines, "\n")

for _, pat := range notfound {
// Didn't find it, but we didn't want to.. so we mark it as found
Expand All @@ -123,7 +127,7 @@ func (m *HavePatternsMatcher) FailureResult(actual interface{}) MatcherResult {
case string, []string:
a = actual
default:
a = fmt.Sprintf("object: %T", actual)
a = m.cachedActual
}
return MatcherResult{
Actual: a,
Expand All @@ -135,9 +139,12 @@ func (m *HavePatternsMatcher) FailureResult(actual interface{}) MatcherResult {
}

func (m *HavePatternsMatcher) NegatedFailureResult(actual interface{}) MatcherResult {
a, ok := actual.(string)
if !ok {
a = fmt.Sprintf("object: %T", actual)
var a string
switch v := actual.(type) {
case string:
a = v
default:
a = m.cachedActual
}
return MatcherResult{
Actual: a,
Expand Down
4 changes: 2 additions & 2 deletions testdata/out_matching_basic_failing.1.documentation
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ to satisfy at least one of these matchers
[{"have-len":2}]
Matching: basic_reader: matches:
Expected
"object: *strings.Reader"
"foo bar\nmoo cow"
to have patterns
["fox","/^t.*w$/","!foo","!/^foo/"]
the missing elements were
Expand Down Expand Up @@ -253,7 +253,7 @@ to satisfy at least one of these matchers

Matching: basic_reader: matches:
Expected
"object: *strings.Reader"
"foo bar\nmoo cow"
to have patterns
["fox","/^t.*w$/","!foo","!/^foo/"]
the missing elements were
Expand Down
2 changes: 1 addition & 1 deletion testdata/out_matching_basic_failing.1.rspecish
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ to satisfy at least one of these matchers

Matching: basic_reader: matches:
Expected
"object: *strings.Reader"
"foo bar\nmoo cow"
to have patterns
["fox","/^t.*w$/","!foo","!/^foo/"]
the missing elements were
Expand Down
2 changes: 1 addition & 1 deletion testdata/out_matching_basic_failing.1.tap
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ not ok 2 - Matching: basic_array_consists_of: matches: Expected ["foo","bar","mo
not ok 3 - Matching: basic_array_matchers: matches: Expected ["foo","bar","moo"] to satisfy at least one of these matchers [{"contain-elements":["fox","box"]},{"contain-elements":["fox","bax"]},["fox","bax","mox"],{"consist-of":["fox",{"have-prefix":"t"},"box"]},{"contain-element":{"have-prefix":"x"}},{"contain-element":{"have-suffix":"x"}}]
not ok 4 - Matching: basic_int: matches: Expected 42 to be numerically eq 43
not ok 5 - Matching: basic_len: matches: Expected "123" to satisfy at least one of these matchers [{"have-len":2}]
not ok 6 - Matching: basic_reader: matches: Expected "object: *strings.Reader" to have patterns ["fox","/^t.*w$/","!foo","!/^foo/"] the missing elements were ["fox","/^t.*w$/","!foo","!/^foo/"]
not ok 6 - Matching: basic_reader: matches: Expected "foo bar\nmoo cow" to have patterns ["fox","/^t.*w$/","!foo","!/^foo/"] the missing elements were ["fox","/^t.*w$/","!foo","!/^foo/"]
not ok 7 - Matching: basic_semver: matches: Expected "1.2.3" to satisfy at least one of these matchers [{"semver-constraint":">=9.9.0"}]
not ok 8 - Matching: basic_string: matches: Expected "this is a test" to equal "this is a failing test"
not ok 9 - Matching: basic_string_contain_substring: matches: Expected "foo" to contain substring "x"
Expand Down