Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

change matcher now accepts regexp in to/from

- Closes #62.
  • Loading branch information...
commit 5774d5c2161de66fe2bb34014ab2a84342cf6368 1 parent a832e96
@davisre davisre authored dchelimsky committed
Showing with 49 additions and 12 deletions.
  1. +17 −4 lib/rspec/matchers/change.rb
  2. +32 −8 spec/rspec/matchers/change_spec.rb
View
21 lib/rspec/matchers/change.rb
@@ -32,9 +32,9 @@ def evaluate_value_proc
end
def failure_message_for_should
- if @given_from && @before != @from
+ if @given_from && !given_matches_actual?(@from, @before)
"#{message} should have initially been #{@from.inspect}, but was #{@before.inspect}"
- elsif @given_to && @to != @after
+ elsif @given_to && !given_matches_actual?(@to, @after)
"#{message} should have been changed to #{@to.inspect}, but is now #{@after.inspect}"
elsif @amount
"#{message} should have been changed by #{@amount.inspect}, but was changed by #{actual_delta.inspect}"
@@ -101,11 +101,15 @@ def changed?
end
def matches_before?
- @given_from ? @from == @before : true
+ @given_from ? given_matches_actual?(@from, @before) : true
end
def matches_after?
- @given_to ? @to == @after : true
+ @given_to ? given_matches_actual?(@to, @after) : true
+ end
+
+ def given_matches_actual?(given, actual)
+ given === actual
end
def matches_amount?
@@ -174,6 +178,15 @@ def matches_max?
# employee.develop_great_new_social_networking_app
# }.should change(employee, :title).from("Mail Clerk").to("CEO")
#
+ # lambda {
+ # doctor.leave_office
+ # }.should change(doctor, :sign).from(/is in/).to(/is out/)
+ #
+ # user = User.new(:type => "admin")
+ # lambda {
+ # user.symbolize_type
+ # }.should change(user, :type).from(String).to(Symbol)
+ #
# == Notes
#
# Evaluates <tt>receiver.message</tt> or <tt>block</tt> before and after it
View
40 spec/rspec/matchers/change_spec.rb
@@ -273,11 +273,17 @@ class SomethingExpected
@instance.some_value = 'string'
end
- it "passes when attribute is == to expected value before executing block" do
+ it "passes when attribute is === to expected value before executing block" do
expect { @instance.some_value = "astring" }.to change(@instance, :some_value).from("string")
end
- it "fails when attribute is not == to expected value before executing block" do
+ it "compares the expected and actual values with ===" do
+ expected = "string"
+ expected.should_receive(:===).and_return true
+ expect { @instance.some_value = "astring" }.to change(@instance, :some_value).from(expected)
+ end
+
+ it "fails when attribute is not === to expected value before executing block" do
expect do
expect { @instance.some_value = "knot" }.to change(@instance, :some_value).from("cat")
end.to fail_with("some_value should have initially been \"cat\", but was \"string\"")
@@ -291,11 +297,17 @@ class SomethingExpected
@instance.some_value = 'string'
end
- it "passes when attribute is == to expected value before executing block" do
+ it "passes when attribute is === to expected value before executing block" do
expect { @instance.some_value = "astring" }.to change{@instance.some_value}.from("string")
end
- it "fails when attribute is not == to expected value before executing block" do
+ it "compares the expected and actual values with ===" do
+ expected = "string"
+ expected.should_receive(:===).and_return true
+ expect { @instance.some_value = "astring" }.to change{@instance.some_value}.from(expected)
+ end
+
+ it "fails when attribute is not === to expected value before executing block" do
expect do
expect { @instance.some_value = "knot" }.to change{@instance.some_value}.from("cat")
end.to fail_with("result should have initially been \"cat\", but was \"string\"")
@@ -325,11 +337,17 @@ class SomethingExpected
@instance.some_value = 'string'
end
- it "passes when attribute is == to expected value after executing block" do
+ it "passes when attribute is === to expected value after executing block" do
expect { @instance.some_value = "cat" }.to change(@instance, :some_value).to("cat")
end
- it "fails when attribute is not == to expected value after executing block" do
+ it "compares the expected and actual values with ===" do
+ expected = "cat"
+ expected.should_receive(:===).and_return true
+ expect { @instance.some_value = "cat" }.to change(@instance, :some_value).to(expected)
+ end
+
+ it "fails when attribute is not === to expected value after executing block" do
expect do
expect { @instance.some_value = "cat" }.to change(@instance, :some_value).from("string").to("dog")
end.to fail_with("some_value should have been changed to \"dog\", but is now \"cat\"")
@@ -343,11 +361,17 @@ class SomethingExpected
@instance.some_value = 'string'
end
- it "passes when attribute is == to expected value after executing block" do
+ it "passes when attribute is === to expected value after executing block" do
expect { @instance.some_value = "cat" }.to change{@instance.some_value}.to("cat")
end
- it "fails when attribute is not == to expected value after executing block" do
+ it "compares the expected and actual values with ===" do
+ expected = "cat"
+ expected.should_receive(:===).and_return true
+ expect { @instance.some_value = "cat" }.to change{@instance.some_value}.to(expected)
+ end
+
+ it "fails when attribute is not === to expected value after executing block" do
expect do
expect { @instance.some_value = "cat" }.to change{@instance.some_value}.from("string").to("dog")
end.to fail_with("result should have been changed to \"dog\", but is now \"cat\"")
Please sign in to comment.
Something went wrong with that request. Please try again.