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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Spec: let should be_nil
and should be_a(...)
cast the value
#8240
Conversation
Isn't the require "spec"
context "foo" do
it "be_nil" do
x = nil
x.not_nil!.should eq "x"
end
it "be_a" do
x = rand > 1 ? 1 : nil
x.as(Int32).should eq 1
end
end
|
+1 for naming them Matchers. Calling them expectations is confusing because those already have a meaning in RSpec. In RSpec, "expectations" are the result of I actually prefer the |
Compare the output: with not_nil!
with should_not be_nil
I think |
No, we already had this discussion in the past and we kind of agreed that |
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.
looks good!
I pushed another commit to make it backwards-compatible with a deprecation message, so now this should be fine to merge. We can change |
I also want to rename |
I might actually prefer implementing the special cases for I don't think we need to care for extensibility here, because I honestly can't think of any other valid use case for a generic solution. And if there was, it could always be changed later without breaking anything. But we can reduce complexity by going the simple way. Now, adding |
I agree, I'll send a simpler PR later. |
@asteite Is this still on your radar? Otherwise we should make an issue to track this. |
Yes, please make an issue. I'm in the middle of a move and I don't have time for Crystal other than just replying on commenting on issues. Thank you! |
I'm fine with merging this PR without simplifying. It's not a big deal one way or the other, and can be changed in the future. |
Thanks for reminding me about this, I'll send a PR with the simplified version tomorrow. |
A way to solve this forum request
Before this PR we had to do this:
The
not_nil!
is needed because the compiler has no way to know that theshould_not be_nil
means thatindex
will not benil
.With this PR we can now do:
Basically, the result of
obj.should_not be_nil
will returnobj
as a non-nil object.The same applies for
obj.should be_a(T)
andobj.should_not be_a(T)
.This still doesn't automatically restrict the var but is significantly better than the old option.
This PR is a breaking change because I introduced the
Expectation
module that all expectations must include. An alternative to implement this PR is to overloadshould
andshould_not
withBeNil
andBeA
but it felt like a worse option and it's less extensible.This is just a draft because together with this PR we can actually change many
should_not be_nil
combined withnot_nil!
in the stdlib specs.Finally, this PR also finally documents the way to extend
spec
with custom expectations.馃挱 In RSpec they are called "matchers", maybe we could rename them to be consistent with the Ruby world? It's also shorter.