Skip to content

Commit

Permalink
Define include matcher as diffable
Browse files Browse the repository at this point in the history
Modify the specs so they match output rather than specify it exactly.

- Closes #49.
  • Loading branch information
msassak authored and dchelimsky committed Dec 11, 2010
1 parent 4441c64 commit e4921ca
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 20 deletions.
3 changes: 3 additions & 0 deletions lib/rspec/matchers/include.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 20 additions & 20 deletions spec/rspec/matchers/include_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
4 changes: 4 additions & 0 deletions spec/support/matchers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.