Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add match_array matcher method for the old =~ array matcher.

This is needed because we've decided not to support operator matchers off of `expect(value).to`, and `match_array` is the best name we've come up with for it.
  • Loading branch information...
commit f00de5729299f93b1d86d7f078876b92bc220024 1 parent f1b8aa2
@myronmarston myronmarston authored
Showing with 24 additions and 4 deletions.
  1. +12 −4 lib/rspec/matchers.rb
  2. +12 −0 spec/rspec/matchers/match_array_spec.rb
View
16 lib/rspec/matchers.rb
@@ -663,19 +663,27 @@ def yield_successive_args(*args)
BuiltIn::YieldSuccessiveArgs.new(*args)
end
- # Passes if actual contains all of the expected regardless of order.
- # This works for collections. Pass in multiple args and it will only
+ # Passes if actual contains all of the expected regardless of order.
+ # This works for collections. Pass in multiple args and it will only
# pass if all args are found in collection.
#
- # NOTE: there is no should_not version of array.should =~ other_array
- #
+ # @note This is also available using the `=~` operator with `should`,
+ # but `=~` is not supported with `expect`.
+ # @note There is no should_not version of array.should =~ other_array
+ #
# @example
#
+ # expect([1,2,3]).to match_array([1,2,3])
+ # expect([1,2,3]).to match_array([1,3,2])
# [1,2,3].should =~ [1,2,3] # => would pass
# [1,2,3].should =~ [2,3,1] # => would pass
# [1,2,3,4].should =~ [1,2,3] # => would fail
# [1,2,2,3].should =~ [1,2,3] # => would fail
# [1,2,3].should =~ [1,2,3,4] # => would fail
+ def match_array(array)
+ BuiltIn::MatchArray.new(array)
+ end
+
OperatorMatcher.register(Array, '=~', BuiltIn::MatchArray)
end
end
View
12 spec/rspec/matchers/match_array_spec.rb
@@ -14,6 +14,18 @@ def ==(other)
end
end
+describe "using match_array with expect" do
+ it "passes a valid positive expectation" do
+ expect([1, 2]).to match_array [2, 1]
+ end
+
+ it "fails an invalid positive expectation" do
+ expect {
+ expect([1, 2, 3]).to match_array [2, 1]
+ }.to fail_with(/expected collection contained/)
+ end
+end
+
describe "array.should =~ other_array" do
it "passes if target contains all items" do
[1,2,3].should =~ [1,2,3]
Please sign in to comment.
Something went wrong with that request. Please try again.