diff options
Diffstat (limited to 'spec/ruby/language/return_spec.rb')
| -rw-r--r-- | spec/ruby/language/return_spec.rb | 395 |
1 files changed, 187 insertions, 208 deletions
diff --git a/spec/ruby/language/return_spec.rb b/spec/ruby/language/return_spec.rb index 7eef6d06ca..36a3cba4d7 100644 --- a/spec/ruby/language/return_spec.rb +++ b/spec/ruby/language/return_spec.rb @@ -19,7 +19,7 @@ describe "The return keyword" do it "returns nil by default" do def r; return; end - r().should be_nil + r().should == nil end describe "in a Thread" do @@ -31,7 +31,7 @@ describe "The return keyword" do e end } - t.value.should be_an_instance_of(LocalJumpError) + t.value.should.instance_of?(LocalJumpError) end end @@ -176,11 +176,11 @@ describe "The return keyword" do end it "causes lambda to return nil if invoked without any arguments" do - -> { return; 456 }.call.should be_nil + -> { return; 456 }.call.should == nil end it "causes lambda to return nil if invoked with an empty expression" do - -> { return (); 456 }.call.should be_nil + -> { return (); 456 }.call.should == nil end it "causes lambda to return the value passed to return" do @@ -229,7 +229,7 @@ describe "The return keyword" do def f 1.times { 1.times {return true}; false}; false end - f.should be_true + f.should == true end end @@ -250,261 +250,240 @@ describe "The return keyword" do end end - ruby_version_is '2.4.2' do - describe "at top level" do - before :each do - @filename = tmp("top_return.rb") - ScratchPad.record [] - end + describe "at top level" do + before :each do + @filename = tmp("top_return.rb") + ScratchPad.record [] + end - after do - rm_r @filename - end + after do + rm_r @filename + end - it "stops file execution" do - ruby_exe(<<-END_OF_CODE).should == "before return\n" - puts "before return" - return + it "stops file execution" do + ruby_exe(<<-END_OF_CODE).should == "before return\n" + puts "before return" + return - puts "after return" + puts "after return" + END_OF_CODE + + $?.exitstatus.should == 0 + end + + describe "within if" do + it "is allowed" do + File.write(@filename, <<-END_OF_CODE) + ScratchPad << "before if" + if true + return + end + + ScratchPad << "after if" END_OF_CODE - $?.exitstatus.should == 0 + load @filename + ScratchPad.recorded.should == ["before if"] end + end - describe "within if" do - it "is allowed" do - File.write(@filename, <<-END_OF_CODE) - ScratchPad << "before if" - if true - return - end + describe "within while loop" do + it "is allowed" do + File.write(@filename, <<-END_OF_CODE) + ScratchPad << "before while" + while true + return + end - ScratchPad << "after if" - END_OF_CODE + ScratchPad << "after while" + END_OF_CODE - load @filename - ScratchPad.recorded.should == ["before if"] - end + load @filename + ScratchPad.recorded.should == ["before while"] end + end - describe "within while loop" do - it "is allowed" do - File.write(@filename, <<-END_OF_CODE) - ScratchPad << "before while" - while true - return - end + describe "within a begin" do + it "is allowed in begin block" do + File.write(@filename, <<-END_OF_CODE) + ScratchPad << "before begin" + begin + return + end - ScratchPad << "after while" - END_OF_CODE + ScratchPad << "after begin" + END_OF_CODE - load @filename - ScratchPad.recorded.should == ["before while"] - end + load @filename + ScratchPad.recorded.should == ["before begin"] end - describe "within a begin" do - it "is allowed in begin block" do - File.write(@filename, <<-END_OF_CODE) - ScratchPad << "before begin" - begin - return - end + it "is allowed in ensure block" do + File.write(@filename, <<-END_OF_CODE) + ScratchPad << "before begin" + begin + ensure + return + end - ScratchPad << "after begin" - END_OF_CODE + ScratchPad << "after begin" + END_OF_CODE - load @filename - ScratchPad.recorded.should == ["before begin"] - end + load @filename + ScratchPad.recorded.should == ["before begin"] + end - it "is allowed in ensure block" do - File.write(@filename, <<-END_OF_CODE) - ScratchPad << "before begin" - begin - ensure - return - end + it "is allowed in rescue block" do + File.write(@filename, <<-END_OF_CODE) + ScratchPad << "before begin" + begin + raise + rescue RuntimeError + return + end - ScratchPad << "after begin" - END_OF_CODE + ScratchPad << "after begin" + END_OF_CODE - load @filename - ScratchPad.recorded.should == ["before begin"] - end + load @filename + ScratchPad.recorded.should == ["before begin"] + end - it "is allowed in rescue block" do - File.write(@filename, <<-END_OF_CODE) - ScratchPad << "before begin" - begin - raise - rescue RuntimeError - return - end + it "fires ensure block before returning" do + ruby_exe(<<-END_OF_CODE).should == "within ensure\n" + begin + return + ensure + puts "within ensure" + end - ScratchPad << "after begin" - END_OF_CODE + puts "after begin" + END_OF_CODE + end - load @filename - ScratchPad.recorded.should == ["before begin"] - end + it "fires ensure block before returning while loads file" do + File.write(@filename, <<-END_OF_CODE) + ScratchPad << "before begin" + begin + return + ensure + ScratchPad << "within ensure" + end - it "fires ensure block before returning" do - ruby_exe(<<-END_OF_CODE).should == "within ensure\n" - begin - return - ensure - puts "within ensure" - end + ScratchPad << "after begin" + END_OF_CODE - puts "after begin" - END_OF_CODE - end + load @filename + ScratchPad.recorded.should == ["before begin", "within ensure"] + end - ruby_bug "#14061", "2.4"..."2.5" do - it "fires ensure block before returning while loads file" do - File.write(@filename, <<-END_OF_CODE) - ScratchPad << "before begin" - begin - return - ensure - ScratchPad << "within ensure" - end - - ScratchPad << "after begin" - END_OF_CODE - - load @filename - ScratchPad.recorded.should == ["before begin", "within ensure"] + it "swallows exception if returns in ensure block" do + File.write(@filename, <<-END_OF_CODE) + begin + raise + ensure + ScratchPad << "before return" + return end - end + END_OF_CODE - it "swallows exception if returns in ensure block" do - File.write(@filename, <<-END_OF_CODE) - begin - raise - ensure - ScratchPad << "before return" - return - end - END_OF_CODE - - load @filename - ScratchPad.recorded.should == ["before return"] - end + load @filename + ScratchPad.recorded.should == ["before return"] end + end - describe "within a block" do - it "is allowed" do - File.write(@filename, <<-END_OF_CODE) - ScratchPad << "before call" - proc { return }.call + describe "within a block" do + it "is allowed" do + File.write(@filename, <<-END_OF_CODE) + ScratchPad << "before call" + proc { return }.call - ScratchPad << "after call" - END_OF_CODE + ScratchPad << "after call" + END_OF_CODE - load @filename - ScratchPad.recorded.should == ["before call"] - end + load @filename + ScratchPad.recorded.should == ["before call"] end + end - describe "within a class" do - ruby_version_is ""..."2.5" do - it "is allowed" do - File.write(@filename, <<-END_OF_CODE) - class ReturnSpecs::A - ScratchPad << "before return" - return - - ScratchPad << "after return" - end - END_OF_CODE + describe "within a class" do + it "raises a SyntaxError" do + File.write(@filename, <<-END_OF_CODE) + class ReturnSpecs::A + ScratchPad << "before return" + return - load @filename - ScratchPad.recorded.should == ["before return"] + ScratchPad << "after return" end - end - - ruby_version_is "2.5" do - it "raises a SyntaxError" do - File.write(@filename, <<-END_OF_CODE) - class ReturnSpecs::A - ScratchPad << "before return" - return - - ScratchPad << "after return" - end - END_OF_CODE + END_OF_CODE - -> { load @filename }.should raise_error(SyntaxError) - end - end + -> { load @filename }.should.raise(SyntaxError) end + end - describe "within a block within a class" do - ruby_version_is "2.7" do - it "is not allowed" do - File.write(@filename, <<-END_OF_CODE) - class ReturnSpecs::A - ScratchPad << "before return" - 1.times { return } - ScratchPad << "after return" - end - END_OF_CODE - - -> { load @filename }.should raise_error(LocalJumpError) + describe "within a block within a class" do + it "is not allowed" do + File.write(@filename, <<-END_OF_CODE) + class ReturnSpecs::A + ScratchPad << "before return" + 1.times { return } + ScratchPad << "after return" end - end + END_OF_CODE + + -> { load @filename }.should.raise(LocalJumpError) end + end - describe "file loading" do - it "stops file loading and execution" do - File.write(@filename, <<-END_OF_CODE) - ScratchPad << "before return" + describe "within BEGIN" do + it "is allowed" do + File.write(@filename, <<-END_OF_CODE) + BEGIN { + ScratchPad << "before call" return - ScratchPad << "after return" - END_OF_CODE + ScratchPad << "after call" + } + END_OF_CODE - load @filename - ScratchPad.recorded.should == ["before return"] - end + load @filename + ScratchPad.recorded.should == ["before call"] end + end - describe "file requiring" do - it "stops file loading and execution" do - File.write(@filename, <<-END_OF_CODE) - ScratchPad << "before return" - return - ScratchPad << "after return" - END_OF_CODE + describe "file loading" do + it "stops file loading and execution" do + File.write(@filename, <<-END_OF_CODE) + ScratchPad << "before return" + return + ScratchPad << "after return" + END_OF_CODE - require @filename - ScratchPad.recorded.should == ["before return"] - end + load @filename + ScratchPad.recorded.should == ["before return"] end + end - describe "return with argument" do - ruby_version_is ""..."2.7" do - it "does not affect exit status" do - ruby_exe(<<-END_OF_CODE).should == "" - return 10 - END_OF_CODE + describe "file requiring" do + it "stops file loading and execution" do + File.write(@filename, <<-END_OF_CODE) + ScratchPad << "before return" + return + ScratchPad << "after return" + END_OF_CODE - $?.exitstatus.should == 0 - end - end + require @filename + ScratchPad.recorded.should == ["before return"] + end + end - ruby_version_is "2.7" do - it "warns but does not affect exit status" do - err = ruby_exe(<<-END_OF_CODE, args: "2>&1") - return 10 - END_OF_CODE - $?.exitstatus.should == 0 + describe "return with argument" do + it "warns but does not affect exit status" do + err = ruby_exe(<<-END_OF_CODE, args: "2>&1") + return 10 + END_OF_CODE + $?.exitstatus.should == 0 - err.should =~ /warning: argument of top-level return is ignored/ - end - end + err.should =~ /warning: argument of top-level return is ignored/ end end end |
