Skip to content
Browse files

Define include matcher as diffable

Modify the specs so they match output rather than specify it exactly.

- Closes #49.
  • Loading branch information...
1 parent 4441c64 commit e4921ca1d984b9b9ab5acd5e10b58dd12a57cb52 @msassak msassak committed with dchelimsky Dec 10, 2010
Showing with 27 additions and 20 deletions.
  1. +3 −0 lib/rspec/matchers/include.rb
  2. +20 −20 spec/rspec/matchers/include_spec.rb
  3. +4 −0 spec/support/matchers.rb
View
3 lib/rspec/matchers/include.rb
@@ -18,6 +18,9 @@ module Matchers
# "spread".should_not include("red")
def include(*expected)
Matcher.new :include, *expected do |*_expected|
+
+ diffable
+
match_for_should do |actual|
perform_match(:all?, :all?, actual, _expected)
end
View
40 spec/rspec/matchers/include_spec.rb
@@ -33,7 +33,7 @@
it "fails if target does not include expected" do
lambda {
{:key => 'value'}.should include(:other)
- }.should fail_with(%Q|expected {:key=>"value"} to include :other|)
+ }.should fail_matching(%Q|expected {:key=>"value"} to include :other|)
end
end
end
@@ -71,7 +71,7 @@
it 'fails if target is missing any item as a key' do
lambda {
{:key => 'value'}.should include(:key, :other)
- }.should fail_with(%Q|expected {:key=>"value"} to include :key and :other|)
+ }.should fail_matching(%Q|expected {:key=>"value"} to include :key and :other|)
end
end
end
@@ -109,7 +109,7 @@
it "fails if target includes expected key" do
lambda {
{:key => 'value'}.should_not include(:key)
- }.should fail_with(%Q|expected {:key=>"value"} not to include :key|)
+ }.should fail_matching(%Q|expected {:key=>"value"} not to include :key|)
end
end
@@ -142,13 +142,13 @@
it "fails if the target includes all of the expected keys" do
expect {
{ :a => 1, :b => 2 }.should_not include(:a, :b)
- }.to fail_with(%q|expected {:a=>1, :b=>2} not to include :a and :b|)
+ }.to fail_matching(%Q|expected {:a=>1, :b=>2} not to include :a and :b|)
end
it "fails if the target includes some (but not all) of the expected keys" do
expect {
{ :a => 1, :b => 2 }.should_not include(:d, :b)
- }.to fail_with(%q|expected {:a=>1, :b=>2} not to include :d and :b|)
+ }.to fail_matching(%Q|expected {:a=>1, :b=>2} not to include :d and :b|)
end
end
@@ -184,21 +184,21 @@
it "fails if target has a different value for key" do
lambda {
{:key => 'different'}.should include(:key => 'value')
- }.should fail_with(%Q|expected {:key=>"different"} to include {:key=>"value"}|)
+ }.should fail_matching(%Q|expected {:key=>"different"} to include {:key=>"value"}|)
end
it "fails if target has a different key" do
lambda {
{:other => 'value'}.should include(:key => 'value')
- }.should fail_with(%Q|expected {:other=>"value"} to include {:key=>"value"}|)
+ }.should fail_matching(%Q|expected {:other=>"value"} to include {:key=>"value"}|)
end
end
context 'for a non-hash target' do
it "fails if the target does not contain the given hash" do
lambda {
['a', 'b'].should include(:key => 'value')
- }.should fail_with(%q|expected ["a", "b"] to include {:key=>"value"}|)
+ }.should fail_matching(%q|expected ["a", "b"] to include {:key=>"value"}|)
end
it "passes if the target contains the given hash" do
@@ -212,13 +212,13 @@
it "fails if target includes the key/value pair" do
lambda {
{:key => 'value'}.should_not include(:key => 'value')
- }.should fail_with(%Q|expected {:key=>"value"} not to include {:key=>"value"}|)
+ }.should fail_matching(%Q|expected {:key=>"value"} not to include {:key=>"value"}|)
end
it "fails if target includes the key/value pair among others" do
lambda {
{:key => 'value', :other => 'different'}.should_not include(:key => 'value')
- }.should fail_with(%Q|expected {:key=>"value", :other=>"different"} not to include {:key=>"value"}|)
+ }.should fail_matching(%Q|expected {:key=>"value", :other=>"different"} not to include {:key=>"value"}|)
end
it "passes if target has a different value for key" do
@@ -238,7 +238,7 @@
it "fails if the target contains the given hash" do
lambda {
['a', { :key => 'value' } ].should_not include(:key => 'value')
- }.should fail_with(%Q|expected ["a", {:key=>"value"}] not to include {:key=>"value"}|)
+ }.should fail_matching(%Q|expected ["a", {:key=>"value"}] not to include {:key=>"value"}|)
end
end
end
@@ -256,19 +256,19 @@
it "fails if target has a different value for one of the keys" do
lambda {
{:a => 1, :b => 2}.should include(:a => 2, :b => 2)
- }.should fail_with(%Q|expected {:a=>1, :b=>2} to include {:a=>2, :b=>2}|)
+ }.should fail_matching(%Q|expected {:a=>1, :b=>2} to include {:a=>2, :b=>2}|)
end
it "fails if target has a different value for both of the keys" do
lambda {
{:a => 1, :b => 1}.should include(:a => 2, :b => 2)
- }.should fail_with(%Q|expected {:a=>1, :b=>1} to include {:a=>2, :b=>2}|)
+ }.should fail_matching(%Q|expected {:a=>1, :b=>1} to include {:a=>2, :b=>2}|)
end
it "fails if target lacks one of the keys" do
lambda {
{:a => 1, :b => 1}.should include(:a => 1, :c => 1)
- }.should fail_with(%Q|expected {:a=>1, :b=>1} to include {:a=>1, :c=>1}|)
+ }.should fail_matching(%Q|expected {:a=>1, :b=>1} to include {:a=>1, :c=>1}|)
end
it "fails if target lacks both of the keys" do
@@ -288,7 +288,7 @@
it "fails if the target does not contain the given hash" do
lambda {
['a', 'b'].should include(:a => 1, :b => 1)
- }.should fail_with(%q|expected ["a", "b"] to include {:a=>1, :b=>1}|)
+ }.should fail_matching(%Q|expected ["a", "b"] to include {:a=>1, :b=>1}|)
end
it "passes if the target contains the given hash" do
@@ -302,20 +302,20 @@
it "fails if target includes the key/value pairs" do
lambda {
{:a => 1, :b => 2}.should_not include(:a => 1, :b => 2)
- }.should fail_with(%Q|expected {:a=>1, :b=>2} not to include {:a=>1, :b=>2}|)
+ }.should fail_matching(%Q|expected {:a=>1, :b=>2} not to include {:a=>1, :b=>2}|)
end
it "fails if target includes the key/value pairs among others" do
hash = {:a => 1, :b => 2, :c => 3}
lambda {
hash.should_not include(:a => 1, :b => 2)
- }.should fail_with(%Q|expected #{hash.inspect} not to include {:a=>1, :b=>2}|)
+ }.should fail_matching(%Q|expected #{hash.inspect} not to include {:a=>1, :b=>2}|)
end
it "fails if target has a different value for one of the keys" do
lambda {
{:a => 1, :b => 2}.should_not include(:a => 2, :b => 2)
- }.should fail_with(%Q|expected {:a=>1, :b=>2} not to include {:a=>2, :b=>2}|)
+ }.should fail_matching(%Q|expected {:a=>1, :b=>2} not to include {:a=>2, :b=>2}|)
end
it "passes if target has a different value for both of the keys" do
@@ -325,7 +325,7 @@
it "fails if target lacks one of the keys" do
lambda {
{:a => 1, :b => 1}.should_not include(:a => 1, :c => 1)
- }.should fail_with(%Q|expected {:a=>1, :b=>1} not to include {:a=>1, :c=>1}|)
+ }.should fail_matching(%Q|expected {:a=>1, :b=>1} not to include {:a=>1, :c=>1}|)
end
it "passes if target lacks both of the keys" do
@@ -341,7 +341,7 @@
it "fails if the target contains the given hash" do
lambda {
['a', { :a => 1, :b => 2 } ].should_not include(:a => 1, :b => 2)
- }.should fail_with(%Q|expected ["a", {:a=>1, :b=>2}] not to include {:a=>1, :b=>2}|)
+ }.should fail_matching(%Q|expected ["a", {:a=>1, :b=>2}] not to include {:a=>1, :b=>2}|)
end
end
end
View
4 spec/support/matchers.rb
@@ -13,6 +13,10 @@ def fail
def fail_with(message)
raise_error(RSpec::Expectations::ExpectationNotMetError, message)
end
+
+ def fail_matching(message)
+ raise_error(RSpec::Expectations::ExpectationNotMetError, /#{Regexp.escape(message)}/)
+ end
end
end

0 comments on commit e4921ca

Please sign in to comment.
Something went wrong with that request. Please try again.