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

Adds strict option for string equality comparison #3709

Conversation

samueleaton
Copy link
Contributor

@samueleaton samueleaton commented Dec 15, 2016

Solves #3707

Doing:

expect_raises MyError, "my message", strict: true do...

...will now do a strict comparison of messages instead of ex_to_s.includes?(msg)

Does strict string equality comparison for expect_raises spec messages.

@samueleaton
Copy link
Contributor Author

samueleaton commented Dec 15, 2016

@asterite This should fulfill both of our wants. It wont break any existing specs, and if you do want strict comparison, it is available.

@asterite
Copy link
Member

I think I would just change the default behaviour of String to strict comparison. One can always use Regex for inclusion. This simplifies the API a bit and shouldn't impact performance a lot (and if it does, well, it's used in the context of specs so super fast performance isn't needed there). The change will probably break all compiler specs which rely on the old behaviour, though, but maybe for now we can change the definition of assert_error "code", "message" to do Regex.new(Regex.escape(message)) inside it.

@asterite
Copy link
Member

(That is, I now agree with you about doing an equality check for strings)

@Sija
Copy link
Contributor

Sija commented Dec 16, 2016

Or maybe add assert_error_matches macro?

@samueleaton
Copy link
Contributor Author

samueleaton commented Dec 16, 2016

@asterite I added the strict equality checks and lots of test are failing now. So either:

  1. Change all of the string to be absolute matches
  2. Replace all string messages with regex
  3. Do what @Sija recommended and implement a new macro for stricter string comparison
  4. Implement a macro for fuzzy string comparison

I have no problem doing any of the 4 options. Let me know what you prefer.

@RX14
Copy link
Contributor

RX14 commented Dec 16, 2016

I like the default of fuzzily matching, I think just having the strict option is best.

@kostya
Copy link
Contributor

kostya commented Jan 15, 2017

in master you can do

expect_raises(MyError) {...}.message.should eq "my message"

@spalladino
Copy link
Contributor

Closing in favour of the solution mentioned by @kostya above

@spalladino spalladino closed this Jan 16, 2017
@samueleaton
Copy link
Contributor Author

@spalladino Should we add that type of example to a doc somewhere so people know how to do a non-fuzzy check?

@spalladino
Copy link
Contributor

@samueleaton good catch. I've opened #3909 since Spec::Expectations lacks documentation in most methods.

@veelenga
Copy link
Contributor

veelenga commented Apr 24, 2017

Just want to mention:

expect_raises(MyError, "") {...}

will pass when block raises MyError with any String message, because of:

"my_string".includes? "" #=> true
"".includes? ""          #=> true

Which is not really expected IMO. Maybe if we had decided to leave check by inclusion, we should have to handle this case differently :

expect_raises(MyError, "") { raise MyError.new("O_o") } # => Expected MyError with "", got #<MyError: O_o>

WDYT ?

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.

None yet

7 participants