Skip to content
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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

sponomarev
Copy link

The questions about custom differ already appeared in the past in #627, rspec/rspec#76. I think it could be valuable to allow custom differ objects in matchers.

The proposed solution should be completely backward compatible. And, I hope, will provide a way to extend custom RSpec matchers libraries.

@sponomarev sponomarev force-pushed the chore/custom-differs-from-matchers branch from 547fa8a to cc98519 Compare January 16, 2019 21:20
Copy link
Member

@JonRowe JonRowe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this stands this changes a public API so cannot be merged at this point. I'm also not sure having a matcher level differ makes any sense, we're interested in building a new built in differ for RSpec 4, and I'm open to providing configurable differs globally, but per matcher has problems.

# both present.
def fail_with(message, expected=nil, actual=nil)
def fail_with(message, matcher=nil)
Copy link
Member

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.

expected = "foo bar baz\n"
actual = "foo bang baz\n"
expected = "foo bang baz\n"
actual = "foo bar baz\n"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To prove your new implementation works, these should have been left alone.

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?
Copy link
Member

Choose a reason for hiding this comment

The 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.


::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)
Copy link
Member

Choose a reason for hiding this comment

The 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.

|-["poo", "car"]
|+[(a string matching /foo/), (a string matching /bar/)]
|-[(a string matching /foo/), (a string matching /bar/)]
|+["poo", "car"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be backwards compatible, existing specs shouldn't change...

@pirj
Copy link
Member

pirj commented Jan 11, 2020

Would you be interested in taking another approach to the problem @sponomarev ?

@benoittgt
Copy link
Member

benoittgt commented Jan 14, 2020

I am interested to work on this if @sponomarev can not, after I finish the task on documentation and yard. rspec/rspec.github.io#131

@sponomarev
Copy link
Author

@pirj Yeah, for sure. I'll take care of it this week. @benoittgt thanks for the offer, I think I'll deal with it on my own =)

@pirj
Copy link
Member

pirj commented Jun 11, 2020

@sponomarev A friendly reminder

@JonRowe JonRowe changed the base branch from master to main August 2, 2020 02:06
@benoittgt
Copy link
Member

@sponomarev Little bump on this one 😊

@benoittgt benoittgt mentioned this pull request Sep 26, 2020
53 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants