-
-
Notifications
You must be signed in to change notification settings - Fork 395
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow to define custom differ object in matchers #1096
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,18 +19,23 @@ def differ | |
|
||
# Raises an RSpec::Expectations::ExpectationNotMetError with message. | ||
# @param [String] message | ||
# @param [Object] expected | ||
# @param [Object] actual | ||
# @param [Object] matcher | ||
# | ||
# Adds a diff to the failure message when `expected` and `actual` are | ||
# Adds a diff to the failure message when `matcher.expected` and `matcher.actual` are | ||
# both present. | ||
def fail_with(message, expected=nil, actual=nil) | ||
def fail_with(message, matcher=nil) | ||
unless message | ||
raise ArgumentError, "Failure message is nil. Does your matcher define the " \ | ||
"appropriate failure_message[_when_negated] method to return a string?" | ||
end | ||
|
||
message = ::RSpec::Matchers::ExpectedsForMultipleDiffs.from(expected).message_with_diff(message, differ, actual) | ||
message = if !matcher.nil? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As you can see above we already inject the differ, it just can't come from a param here, or at least, it'd have to be an extra param. |
||
differ_in_use = matcher.respond_to?(:differ) ? matcher.differ : differ | ||
|
||
::RSpec::Matchers::ExpectedsForMultipleDiffs.from(matcher.expected).message_with_diff(message, differ_in_use, matcher.actual) | ||
else | ||
::RSpec::Matchers::ExpectedsForMultipleDiffs.from(nil).message_with_diff(message, differ, nil) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm yeah nahing this, expected and actual need to be passed in, differ could be an optional extra, this would eliminate the need for this if. |
||
end | ||
|
||
RSpec::Support.notify_failure(RSpec::Expectations::ExpectationNotMetError.new message) | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
RSpec.describe RSpec::Expectations, "#fail_with" do | ||
let(:differ) { double("differ") } | ||
let(:matcher_class) { Struct.new(:expected, :actual)} | ||
|
||
before(:example) do | ||
allow(RSpec::Matchers.configuration).to receive_messages(:color? => false) | ||
|
@@ -12,15 +13,15 @@ | |
expect(differ).to receive(:diff).and_return("diff text") | ||
|
||
expect { | ||
RSpec::Expectations.fail_with "message", "abc", "def" | ||
RSpec::Expectations.fail_with "message", matcher_class.new("abc", "def") | ||
}.to fail_with("message\nDiff:diff text") | ||
end | ||
|
||
it "does not include the diff if expected and actual are not diffable" do | ||
expect(differ).to receive(:diff).and_return("") | ||
|
||
expect { | ||
RSpec::Expectations.fail_with "message", "abc", "def" | ||
RSpec::Expectations.fail_with "message", matcher_class.new("abc", "def") | ||
}.to fail_with("message") | ||
end | ||
|
||
|
@@ -34,6 +35,8 @@ | |
end | ||
|
||
RSpec.describe RSpec::Expectations, "#fail_with with matchers" do | ||
let(:matcher_class) { Struct.new(:expected, :actual)} | ||
|
||
before do | ||
allow(RSpec::Matchers.configuration).to receive_messages(:color? => false) | ||
end | ||
|
@@ -45,29 +48,49 @@ | |
expected_diff = dedent(<<-EOS) | ||
| | ||
|@@ -1,2 +1,2 @@ | ||
|-["poo", "car"] | ||
|+[(a string matching /foo/), (a string matching /bar/)] | ||
|-[(a string matching /foo/), (a string matching /bar/)] | ||
|+["poo", "car"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To be backwards compatible, existing specs shouldn't change... |
||
| | ||
EOS | ||
|
||
expect { | ||
RSpec::Expectations.fail_with "message", actual, expected | ||
RSpec::Expectations.fail_with "message", matcher_class.new(expected, actual) | ||
}.to fail_with("message\nDiff:#{expected_diff}") | ||
end | ||
|
||
context "when matcher provides custom differ" do | ||
let(:custom_differ) { double("custom differ") } | ||
let(:custom_diff) { "custom diff" } | ||
let(:matcher_class) { Struct.new(:expected, :actual, :differ) } | ||
|
||
before(:example) do | ||
allow(custom_differ).to receive(:diff).and_return(custom_diff) | ||
end | ||
|
||
it "uses custom differ instead of default" do | ||
expect(RSpec::Expectations).not_to receive(:differ) | ||
|
||
expect { | ||
RSpec::Expectations.fail_with "message", matcher_class.new("abc", "def", custom_differ) | ||
}.to fail_with("message\nDiff:#{custom_diff}") | ||
end | ||
end | ||
end | ||
|
||
RSpec.describe RSpec::Expectations, "#fail_with with --color" do | ||
let(:matcher_class) { Struct.new(:expected, :actual)} | ||
|
||
before do | ||
allow(RSpec::Matchers.configuration).to receive_messages(:color? => true) | ||
end | ||
|
||
it "tells the differ to use color" do | ||
expected = "foo bar baz\n" | ||
actual = "foo bang baz\n" | ||
expected = "foo bang baz\n" | ||
actual = "foo bar baz\n" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To prove your new implementation works, these should have been left alone. |
||
expected_diff = "\e[0m\n\e[0m\e[34m@@ -1,2 +1,2 @@\n\e[0m\e[31m-foo bang baz\n\e[0m\e[32m+foo bar baz\n\e[0m" | ||
|
||
expect { | ||
RSpec::Expectations.fail_with "message", actual, expected | ||
RSpec::Expectations.fail_with "message", matcher_class.new(expected, actual) | ||
}.to fail_with("message\nDiff:#{expected_diff}") | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm afraid you can't do this, it changes a public api.