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
Make all matchers composable for 3.0 #280
Comments
I'm in favour of this, I've often run into situations where I want to use matchers in a more composable partial fashion.
In my experience
One thing I'm keen to try, is to use constants rather than aliased over methods, altho we could provide those in a configurable fashion on top. I'm not sure this style would work here but would avoid over "squatting".
I agree this is very important. |
👍 ❤️ I'm very much in favor for this. I agree with all of @myronmarston suggestions above.
I think this is the proper way to go. Per # Just showing semantic meaning not that we would use matchers like this
case obj
when a_string_matching(/foo/)
when a_value_within(1.second).of(Time.now)
# ... This is actually a trick commonly used with procs to provide more semantic case statements.
I actually ran into this yesterday with
I believe we briefly talked about that on IRC. 👍 It is definitely something that makes it hard to debug these types of chains. One other thing that I think should be given consideration is how this fits in to the matcher DSL and custom matcher classes. |
This is because it's meant to be used in case statements, and |
I've always considered |
You must have a different understanding of "strict" than I do. Consider that there is exactly one object (
That expression is not equivalent to
Note that
I wonder if you're confusing the semantics of Javascript's |
Who knows, I haven't used it much obviously :) I'll defer to you here as I'm obviously mistaken |
I think @myronmarston is right. In languages like PHP and JavaScript, @avdi once termed it "magic fuzzy match" :) |
I would really like something like: # Composed matcher
expect({ foo: 'bar'}).to be_kind_of(Hash).and(have(1).item).
and(include(:foo)).and_not(include(:bang))
# much better than repeated code.
expect({ foo: 'bar'}).to be_kind_of(Hash)
expect({ foo: 'bar'}).to have(1).item
expect({ foo: 'bar'}).to include(:foo)
expect({ foo: 'bar'}).to_not include(:bang) This would be awesome for me, as I found myself much times doing something like: hash_that_interest = { foo: 'bar'} # set up first so I don't repeat it on every expectation.
expect(hash_that_interest).to be_kind_of(Hash)
expect(hash_that_interest).to have(1).item
expect(hash_that_interest).to include(:foo)
expect(hash_that_interest).to_not include(:bang) |
Closing in favor of #393 since we'll be merging that soon. |
To me, the real value of using matcher objects rather than simple
assert_xyz
methods is the extra power matcher objects give you that simple methods don't. In particularly, hamcrest, discussed in GOOS, allows matchers to be fully composed so that you can express detailed intent through a combination of matchers. In rspec-expectations, we have some places where composition currently works:...but it's not yet supported everywhere. For example, this expression doesn't work yet:
...which means "I expect
foo
to yield with a collection that includes an element matching the regex/foo/
and an element matching the regex/bar/
". In this case, we're composing 3 different matchers.I think it would be a great new feature for 3.0 to make all matchers fully composable. Things to consider:
===
rather than==
for this, as===
means "matches" in ruby, where as==
means "equals", and===
makes much more sense (IMO).expect().to
expression, but poor when composed as an argument to another matcher. For example, for the examples above, I think this reads better:RSpec::Matchers::ComposableAliases
that defines aliases for each of the built-ins in this kind of voice, and provide a config API that will include the module. (I think I'd want it opt-in since the aliases will squat on a lot more name real estate that users might otherwise be able to use).and
matcher and anor
matcher. We'd have to figure out the right phrasing for all these.inspect
output of the composed matchers.Thoughts?
The text was updated successfully, but these errors were encountered: