aboutsummaryrefslogtreecommitdiffstats
path: root/spec/ruby/language/rescue_spec.rb
diff options
context:
space:
mode:
authoreregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-12-15 17:44:37 +0000
committereregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-12-15 17:44:37 +0000
commit30ed82e7724093bb3c2015a818c28c3592134bba (patch)
tree5445b9e26d9ee173fa3735007f3e2b42791d2ec5 /spec/ruby/language/rescue_spec.rb
parentf941bdf263dcc16ea4e6b4576341882b89567c0b (diff)
downloadruby-30ed82e7724093bb3c2015a818c28c3592134bba.tar.gz
Update to ruby/spec@595645f
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61285 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'spec/ruby/language/rescue_spec.rb')
-rw-r--r--spec/ruby/language/rescue_spec.rb154
1 files changed, 148 insertions, 6 deletions
diff --git a/spec/ruby/language/rescue_spec.rb b/spec/ruby/language/rescue_spec.rb
index d7042b7973..0dc8894740 100644
--- a/spec/ruby/language/rescue_spec.rb
+++ b/spec/ruby/language/rescue_spec.rb
@@ -31,6 +31,28 @@ describe "The rescue keyword" do
end
end
+ it "returns value from `rescue` if an exception was raised" do
+ begin
+ raise
+ rescue
+ :caught
+ end.should == :caught
+ end
+
+ it "returns value from `else` section if no exceptions were raised" do
+ result = begin
+ :begin
+ rescue
+ :rescue
+ else
+ :else
+ ensure
+ :ensure
+ end
+
+ result.should == :else
+ end
+
it "can rescue multiple raised exceptions with a single rescue block" do
[lambda{raise ArbitraryException}, lambda{raise SpecificExampleException}].map do |block|
begin
@@ -94,6 +116,32 @@ describe "The rescue keyword" do
end.should raise_error(OtherCustomException)
end
+ it "can rescue different types of exceptions in different ways" do
+ begin
+ raise Exception
+ rescue RuntimeError
+ rescue StandardError
+ rescue Exception
+ ScratchPad << :exception
+ end
+
+ ScratchPad.recorded.should == [:exception]
+ end
+
+ it "rescues exception within the first suitable section in order of declaration" do
+ begin
+ raise StandardError
+ rescue RuntimeError
+ ScratchPad << :runtime_error
+ rescue StandardError
+ ScratchPad << :standard_error
+ rescue Exception
+ ScratchPad << :exception
+ end
+
+ ScratchPad.recorded.should == [:standard_error]
+ end
+
it "will execute an else block only if no exceptions were raised" do
result = begin
ScratchPad << :one
@@ -147,6 +195,20 @@ describe "The rescue keyword" do
ScratchPad.recorded.should == [:one, :else_ran, :ensure_ran, :outside_begin]
end
+ it "will execute an else block even without rescue and ensure" do
+ lambda {
+ eval <<-ruby
+ begin
+ ScratchPad << :begin
+ else
+ ScratchPad << :else
+ end
+ ruby
+ }.should complain(/else without rescue is useless/)
+
+ ScratchPad.recorded.should == [:begin, :else]
+ end
+
it "will not execute an else block if an exception was raised" do
result = begin
ScratchPad << :one
@@ -223,14 +285,31 @@ describe "The rescue keyword" do
a.should == 'ac'
end
- it "without classes will not rescue Exception" do
- lambda do
+ context "without rescue expression" do
+ it "will rescue only StandardError and its subclasses" do
begin
- raise Exception
+ raise StandardError
rescue
- 'Exception wrongly rescued'
+ ScratchPad << :caught
+ end
+
+ ScratchPad.recorded.should == [:caught]
+ end
+
+ it "will not rescue exceptions except StandardError" do
+ [ Exception.new, NoMemoryError.new, ScriptError.new, SecurityError.new,
+ SignalException.new('INT'), SystemExit.new, SystemStackError.new
+ ].each do |exception|
+ lambda {
+ begin
+ raise exception
+ rescue
+ ScratchPad << :caught
+ end
+ }.should raise_error(exception.class)
end
- end.should raise_error(Exception)
+ ScratchPad.recorded.should == []
+ end
end
it "uses === to compare against rescued classes" do
@@ -279,7 +358,7 @@ describe "The rescue keyword" do
invalid_rescuer = Object.new
begin
:foo
- rescue rescuer
+ rescue invalid_rescuer
end.should == :foo
end
@@ -291,6 +370,44 @@ describe "The rescue keyword" do
end.should == :expected
end
+ it "allows rescue in class" do
+ eval <<-ruby
+ class RescueInClassExample
+ raise SpecificExampleException
+ rescue SpecificExampleException
+ ScratchPad << :caught
+ end
+ ruby
+
+ ScratchPad.recorded.should == [:caught]
+ end
+
+ it "does not allow rescue in {} block" do
+ lambda {
+ eval <<-ruby
+ lambda {
+ raise SpecificExampleException
+ rescue SpecificExampleException
+ :caught
+ }
+ ruby
+ }.should raise_error(SyntaxError)
+ end
+
+ ruby_version_is "2.5" do
+ it "allows rescue in 'do end' block" do
+ lambda = eval <<-ruby
+ lambda do
+ raise SpecificExampleException
+ rescue SpecificExampleException
+ ScratchPad << :caught
+ end.call
+ ruby
+
+ ScratchPad.recorded.should == [:caught]
+ end
+ end
+
ruby_version_is ""..."2.4" do
it "fails when using 'rescue' in method arguments" do
lambda { eval '1.+ (1 rescue 1)' }.should raise_error(SyntaxError)
@@ -305,6 +422,31 @@ describe "The rescue keyword" do
it "requires the 'rescue' in method arguments to be wrapped in parens" do
lambda { eval '1.+(1 rescue 1)' }.should raise_error(SyntaxError)
+ eval('1.+((1 rescue 1))').should == 2
+ end
+ end
+
+ describe "inline form" do
+ it "can be inlined" do
+ a = 1/0 rescue 1
+ a.should == 1
+ end
+
+ it "doesn't except rescue expression" do
+ lambda {
+ eval <<-ruby
+ a = 1 rescue RuntimeError 2
+ ruby
+ }.should raise_error(SyntaxError)
+ end
+
+ it "rescues only StandardError and its subclasses" do
+ a = raise(StandardError) rescue 1
+ a.should == 1
+
+ lambda {
+ a = raise(Exception) rescue 1
+ }.should raise_error(Exception)
end
end
end