-
-
Notifications
You must be signed in to change notification settings - Fork 396
Deduplicate and reorganize specs for Kernel#raise #1332
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,9 +4,19 @@ | |
|
|
||
| describe "Kernel#raise" do | ||
| it "is a private method" do | ||
| Kernel.should have_private_instance_method(:raise) | ||
| Kernel.private_instance_methods.should include(:raise) | ||
| end | ||
|
|
||
| # Shared specs expect a public #raise method. | ||
| public_raiser = Object.new | ||
| class << public_raiser | ||
| public :raise | ||
| end | ||
| it_behaves_like :kernel_raise, :raise, public_raiser | ||
| it_behaves_like :kernel_raise_with_cause, :raise, public_raiser | ||
| end | ||
|
|
||
| describe "Kernel#raise with previously rescued exception" do | ||
| it "re-raises the previously rescued exception if no exception is specified" do | ||
| ScratchPad.record nil | ||
|
|
||
|
|
@@ -28,87 +38,6 @@ | |
| ScratchPad.recorded.should be_nil | ||
| end | ||
|
|
||
| it "accepts a cause keyword argument that sets the cause" do | ||
| cause = StandardError.new | ||
| -> { raise("error", cause: cause) }.should raise_error(RuntimeError) { |e| e.cause.should == cause } | ||
| end | ||
|
|
||
| it "accepts a cause keyword argument that overrides the last exception" do | ||
| begin | ||
| raise "first raise" | ||
| rescue => ignored | ||
| cause = StandardError.new | ||
| -> { raise("error", cause: cause) }.should raise_error(RuntimeError) { |e| e.cause.should == cause } | ||
| end | ||
| end | ||
|
|
||
| it "raises an ArgumentError when only cause is given" do | ||
| cause = StandardError.new | ||
| -> { raise(cause: cause) }.should raise_error(ArgumentError, "only cause is given with no arguments") | ||
| end | ||
|
|
||
| it "raises an ArgumentError when only cause is given even if it has nil value" do | ||
| -> { raise(cause: nil) }.should raise_error(ArgumentError, "only cause is given with no arguments") | ||
| end | ||
|
|
||
| it "raises a TypeError when given cause is not an instance of Exception" do | ||
| -> { raise "message", cause: Object.new }.should raise_error(TypeError, "exception object expected") | ||
| end | ||
|
|
||
| it "doesn't raise a TypeError when given cause is nil" do | ||
| -> { raise "message", cause: nil }.should raise_error(RuntimeError, "message") | ||
| end | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. minor: similar issue
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See above comment |
||
|
|
||
| it "allows cause equal an exception" do | ||
| e = RuntimeError.new("message") | ||
| -> { raise e, cause: e }.should raise_error(e) | ||
| end | ||
|
|
||
| it "doesn't set given cause when it equals an exception" do | ||
| e = RuntimeError.new("message") | ||
|
|
||
| begin | ||
| raise e, cause: e | ||
| rescue | ||
| end | ||
|
|
||
| e.cause.should == nil | ||
| end | ||
|
|
||
| it "raises ArgumentError when exception is part of the cause chain" do | ||
| -> { | ||
| begin | ||
| raise "Error 1" | ||
| rescue => e1 | ||
| begin | ||
| raise "Error 2" | ||
| rescue => e2 | ||
| begin | ||
| raise "Error 3" | ||
| rescue => e3 | ||
| raise e1, cause: e3 | ||
| end | ||
| end | ||
| end | ||
| }.should raise_error(ArgumentError, "circular causes") | ||
| end | ||
|
|
||
| it "re-raises a rescued exception" do | ||
| -> do | ||
| begin | ||
| raise StandardError, "aaa" | ||
| rescue Exception | ||
| begin | ||
| raise ArgumentError | ||
| rescue ArgumentError | ||
| end | ||
|
|
||
| # should raise StandardError "aaa" | ||
| raise | ||
| end | ||
| end.should raise_error(StandardError, "aaa") | ||
| end | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. minor: the same issue
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one is covered by existing https://github.com/ruby/spec/pull/1332/changes#diff-866cdb19d113cd7e3798855d9e2bc74a30e2307efae414383ee88629580d80a5R20-R39 |
||
|
|
||
| it "re-raises a previously rescued exception without overwriting the cause" do | ||
| begin | ||
| begin | ||
|
|
@@ -283,10 +212,11 @@ | |
| end | ||
| end | ||
|
|
||
| describe "Kernel#raise" do | ||
| it_behaves_like :kernel_raise, :raise, Kernel | ||
| end | ||
|
|
||
| describe "Kernel.raise" do | ||
| it "needs to be reviewed for spec completeness" | ||
| it "is a public method" do | ||
| Kernel.singleton_class.should.public_method_defined?(:raise) | ||
| end | ||
|
|
||
| it_behaves_like :kernel_raise, :raise, Kernel | ||
| it_behaves_like :kernel_raise_with_cause, :raise, Kernel | ||
| end | ||
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.
minor: haven't found a similar test in
:kernel_raise_with_cause. Isn't it just being deleted?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.
Seems I had code blindness when reviewing changes. I've added tests for this and the next one. Also added a context "without cause keyword argument", which now houses tests for what happens when
causeis not specified.