-
-
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
Added matchers for Hashes and JSON (which uses the Hash matcher) #79
Conversation
We definitely need a better diff for matching hashes, but this relies on a terminal with color, which happens a lot, but far from all the time (think CI servers, etc). So we'd need a diff that doesn't rely on color either instead of or in addition to the color diff. There are some more specific issues that I'll comment on directly in the diff. |
end | ||
|
||
failure_message_for_should do | ||
ENV['GET_FAILURE_MESSAGE'] ? @difference.to_s.inspect: @difference.to_s |
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.
Why do we need this ENV var? I'd prefer to get rid of it.
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.
It's not needed and we can get rid of it.
It was there to make it easier to create the failure messages for test cases.
I'll take it out and just toggle that on and off using a patch I could apply and un-apply when we need to make a new test.
…est failure messages.
As for, "need a diff that doesn't rely on color":
Where:
|
… without needing colours.
…ms and don't contain any other hashes that have missing items.
Hi again David, |
…atches, class matches and proc matches
…s that have additional values.
… added a 'show' which can show all items types or just :incorrect, :missing and or :additional item types.
ping |
@playupchris - I generally like this and want to include it, but I haven't had much spare time lately and this is the largest pull request I've encountered in a long while. Just perusing the diff again, I think there are too many options here and users would find them confusing. I think we should only have four: string.should be_json_matching(....) # partial match all
string.should be_json_matching_exactly(....) # exact match all
hash.should be_hash_matching(....) # partial match all
hash.should be_hash_matching_exactly(....) # exact match all I'm also unsure as to whether we should include the operator matcher. WDYT? @myronmarston, @justinko - you guys want to weigh in on this? |
@dchelimsky thanks for taking the time to look at this again. I initially thought the operator matcher was useful as one already exists for arrays (although our matcher also matches arrays), but in the array matcher:
Whereas in our differ that same test would fail. So I'm thinking now that maybe the operator is confusing, as well as not being backwardly compatible (for arrays). I think (we both agree that) the json_matchers should mirror the hash_matcher (or not exist at all; and we just expect users to convert json to hash before doing the match). As for the other matchers, I guess they are a little confusing.
I guess we could drop the |
Here are some examples of the 4 matchers... pat_datetime = /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z/
expected = {
"href"=>Regexp.compile("#{Regexp.quote '/api/goals/games/'}[0-9]+#{Regexp.quote '/matches/'}[0-9]+$"),
"scheduled_start"=>pat_datetime,
"networks"=>%w(abc nbc cnn),
"expected_key"=>"expected_value",
"end_date"=>pat_datetime,
"home_team"=>{
"name"=>String,
"short_name"=>lambda { |x| x.is_a?(String) and x.size == 3 },
"link"=>[{"href"=>"http://puge.example.org/api/goals/teams/FLA/players", "rel"=>"players"}],
"href"=>"http://puge.example.org/api/goals/teams/FLA"
},
"away_team"=>{
"name"=>"sharks",
"short_name"=>"SHA",
"link"=>[{"href"=>"http://puge.example.org/api/goals/teams/SHA/players", "rel"=>"players"}],
"href"=>"http://puge.example.org/api/goals/teams/SHA"
}
}
actual = {
"href"=>"http://puge.example.org/api/goals/games/635/matches/832",
"scheduled_start"=>"2010-01-01T00:00:00Z",
"networks"=>["abc", "cnn", "yyy", "zzz"],
"unexpected_key"=>"unexpected_value",
"end_date"=>"2010-01-01T01:00:00Z",
"home_team"=>{
"name"=>"flames",
"short_name"=>"FLA",
"link"=>[{"href"=>"http://puge.example.org/api/goals/teams/FLA/players", "rel"=>"players"}],
"href"=>"http://puge.example.org/api/goals/teams/FLA"
},
"away_team"=>{
"name"=>"sharks",
"short_name"=>"unexpected2",
"link"=>[{"href"=>"http://puge.example.org/api/goals/teams/SHA/players", "rel"=>"players"}],
"href"=>"http://puge.example.org/api/goals/teams/SHA"
}
}
(NB. "matched values" means eg. |
So whether "all" is part of the name effects the output, but not the success/failure of the matcher. I think the env var is one good option. Another would be to add an additional argument to the matcher: actual.should be_hash_including(expected, :show_all => true) Or perhaps go the other way, so the norm is the show all, but for larger structures you can constrain it: actual.should be_hash_including(expected, :condense_output => true) I don't like Thoughts? |
I like that. We'd end up setting the constraining argument with our own env variable, but doing it this way is more flexible. How bout |
Requests have been made to swap the green and yellow colours (because "green" currently means "additional" items, whereas people are used to "green" meaning "good"). |
I've split the Difference class off into it's own gem diff_matcher and changed the |
|
Hey @playupchris - thanks for all your hard work on this, and for extracting the diff out to a separate gem. Apologies if you did that due to this taking so long, but I think it's actually a good thing in the end. Once I saw all the differs you mention in the README, I thought a better direction for rspec-expectations would be to add better support for choosing your own differ. We had that in rspec-1, and I didn't include it in rspec-2 (which was a complete rewrite), but I think it would work well now. Rather than merging this pull, I'm going to add support for choosing your own differ to the next release (2.9 - 2.8 is already out as a release candidate and I don't want this to hold that up from going final). I've created a new issue (#97) for this, and welcome your input on that issue. Cheers, |
The image belows shows an example diff, where: