summaryrefslogtreecommitdiff
path: root/spec/ruby/language/ensure_spec.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/language/ensure_spec.rb')
-rw-r--r--spec/ruby/language/ensure_spec.rb139
1 files changed, 76 insertions, 63 deletions
diff --git a/spec/ruby/language/ensure_spec.rb b/spec/ruby/language/ensure_spec.rb
index a930bda36b..04ff0305ab 100644
--- a/spec/ruby/language/ensure_spec.rb
+++ b/spec/ruby/language/ensure_spec.rb
@@ -6,7 +6,7 @@ describe "An ensure block inside a begin block" do
ScratchPad.record []
end
- it "is executed when an exception is raised in it's corresponding begin block" do
+ it "is executed when an exception is raised in its corresponding begin block" do
-> {
begin
ScratchPad << :begin
@@ -14,12 +14,12 @@ describe "An ensure block inside a begin block" do
ensure
ScratchPad << :ensure
end
- }.should raise_error(EnsureSpec::Error)
+ }.should.raise(EnsureSpec::Error)
ScratchPad.recorded.should == [:begin, :ensure]
end
- it "is executed when an exception is raised and rescued in it's corresponding begin block" do
+ it "is executed when an exception is raised and rescued in its corresponding begin block" do
begin
ScratchPad << :begin
raise "An exception occurred!"
@@ -32,7 +32,7 @@ describe "An ensure block inside a begin block" do
ScratchPad.recorded.should == [:begin, :rescue, :ensure]
end
- it "is executed even when a symbol is thrown in it's corresponding begin block" do
+ it "is executed even when a symbol is thrown in its corresponding begin block" do
catch(:symbol) do
begin
ScratchPad << :begin
@@ -47,7 +47,7 @@ describe "An ensure block inside a begin block" do
ScratchPad.recorded.should == [:begin, :ensure]
end
- it "is executed when nothing is raised or thrown in it's corresponding begin block" do
+ it "is executed when nothing is raised or thrown in its corresponding begin block" do
begin
ScratchPad << :begin
rescue
@@ -74,9 +74,9 @@ describe "An ensure block inside a begin block" do
ensure
raise "from ensure"
end
- }.should raise_error(RuntimeError, "from ensure") do |e|
+ }.should.raise(RuntimeError, "from ensure") { |e|
e.cause.message.should == "from block"
- end
+ }
end
end
@@ -108,7 +108,7 @@ describe "An ensure block inside a method" do
end
it "is executed when an exception is raised in the method" do
- -> { @obj.raise_in_method_with_ensure }.should raise_error(EnsureSpec::Error)
+ -> { @obj.raise_in_method_with_ensure }.should.raise(EnsureSpec::Error)
@obj.executed.should == [:method, :ensure]
end
@@ -149,13 +149,13 @@ describe "An ensure block inside a method" do
it "overrides exception raised in rescue if raises exception itself" do
-> {
@obj.raise_in_rescue_and_raise_in_ensure
- }.should raise_error(RuntimeError, "raised in ensure")
+ }.should.raise(RuntimeError, "raised in ensure")
end
it "suppresses exception raised in method if raises exception itself" do
-> {
@obj.raise_in_method_and_raise_in_ensure
- }.should raise_error(RuntimeError, "raised in ensure")
+ }.should.raise(RuntimeError, "raised in ensure")
end
end
@@ -174,7 +174,7 @@ describe "An ensure block inside a class" do
ScratchPad << :ensure
end
ruby
- }.should raise_error(EnsureSpec::Error)
+ }.should.raise(EnsureSpec::Error)
ScratchPad.recorded.should == [:class, :ensure]
end
@@ -247,87 +247,100 @@ describe "An ensure block inside {} block" do
ensure
}
ruby
- }.should raise_error(SyntaxError)
+ }.should.raise(SyntaxError)
end
end
-ruby_version_is "2.5" do
- describe "An ensure block inside 'do end' block" do
- before :each do
- ScratchPad.record []
- end
-
- it "is executed when an exception is raised in it's corresponding begin block" do
- -> {
- eval(<<-ruby).call
- lambda do
- ScratchPad << :begin
- raise EnsureSpec::Error
- ensure
- ScratchPad << :ensure
- end
- ruby
- }.should raise_error(EnsureSpec::Error)
-
- ScratchPad.recorded.should == [:begin, :ensure]
- end
+describe "An ensure block inside 'do end' block" do
+ before :each do
+ ScratchPad.record []
+ end
- it "is executed when an exception is raised and rescued in it's corresponding begin block" do
+ it "is executed when an exception is raised in its corresponding begin block" do
+ -> {
eval(<<-ruby).call
lambda do
ScratchPad << :begin
- raise "An exception occurred!"
- rescue
- ScratchPad << :rescue
+ raise EnsureSpec::Error
ensure
ScratchPad << :ensure
end
ruby
+ }.should.raise(EnsureSpec::Error)
- ScratchPad.recorded.should == [:begin, :rescue, :ensure]
- end
+ ScratchPad.recorded.should == [:begin, :ensure]
+ end
- it "is executed even when a symbol is thrown in it's corresponding begin block" do
- catch(:symbol) do
- eval(<<-ruby).call
- lambda do
- ScratchPad << :begin
- throw(:symbol)
- rescue
- ScratchPad << :rescue
- ensure
- ScratchPad << :ensure
- end
- ruby
+ it "is executed when an exception is raised and rescued in its corresponding begin block" do
+ eval(<<-ruby).call
+ lambda do
+ ScratchPad << :begin
+ raise "An exception occurred!"
+ rescue
+ ScratchPad << :rescue
+ ensure
+ ScratchPad << :ensure
end
+ ruby
- ScratchPad.recorded.should == [:begin, :ensure]
- end
+ ScratchPad.recorded.should == [:begin, :rescue, :ensure]
+ end
- it "is executed when nothing is raised or thrown in it's corresponding begin block" do
+ it "is executed even when a symbol is thrown in its corresponding begin block" do
+ catch(:symbol) do
eval(<<-ruby).call
lambda do
ScratchPad << :begin
+ throw(:symbol)
rescue
ScratchPad << :rescue
ensure
ScratchPad << :ensure
end
ruby
-
- ScratchPad.recorded.should == [:begin, :ensure]
end
- it "has no return value" do
- result = eval(<<-ruby).call
- lambda do
- :begin
+ ScratchPad.recorded.should == [:begin, :ensure]
+ end
+
+ it "is executed when nothing is raised or thrown in its corresponding begin block" do
+ eval(<<-ruby).call
+ lambda do
+ ScratchPad << :begin
+ rescue
+ ScratchPad << :rescue
+ ensure
+ ScratchPad << :ensure
+ end
+ ruby
+
+ ScratchPad.recorded.should == [:begin, :ensure]
+ end
+
+ it "has no return value" do
+ result = eval(<<-ruby).call
+ lambda do
+ :begin
+ ensure
+ :ensure
+ end
+ ruby
+
+ result.should == :begin
+ end
+
+ ruby_version_is "3.4" do
+ it "does not introduce extra backtrace entries" do
+ def foo
+ begin
+ raise "oops"
ensure
- :ensure
+ return caller(0, 2) # rubocop:disable Lint/EnsureReturn
end
- ruby
-
- result.should == :begin
+ end
+ line = __LINE__
+ foo[0].should =~ /#{__FILE__}:#{line-3}:in 'foo'/
+ foo[1].should =~ /#{__FILE__}:#{line+2}:in 'block/
end
end
end