Skip to content

Commit

Permalink
Add missing specs for END and Kernel#at_exit
Browse files Browse the repository at this point in the history
  • Loading branch information
andrykonchin authored and eregon committed Feb 27, 2023
1 parent 757c4ca commit 4fd2644
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
4 changes: 4 additions & 0 deletions core/kernel/at_exit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
it "is a private method" do
Kernel.should have_private_instance_method(:at_exit)
end

it "raises ArgumentError if called without a block" do
-> { at_exit }.should raise_error(ArgumentError, "called without a block")
end
end

describe "Kernel#at_exit" do
Expand Down
12 changes: 10 additions & 2 deletions language/END_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,22 @@
ruby_exe("foo = 'foo'; END { puts foo }").should == "foo\n"
end

it "warns when END is used in a method" do
ruby_exe(<<~ruby, args: "2>&1").should =~ /warning: END in method; use at_exit/
def foo
END { }
end
ruby
end

context "END blocks and at_exit callbacks are mixed" do
it "runs them all in reverse order of registration" do
ruby_exe(<<~RUBY).should == "at_exit#2\nEND#2\nat_exit#1\nEND#1\n"
ruby_exe(<<~ruby).should == "at_exit#2\nEND#2\nat_exit#1\nEND#1\n"
END { puts 'END#1' }
at_exit { puts 'at_exit#1' }
END { puts 'END#2' }
at_exit { puts 'at_exit#2' }
RUBY
ruby
end
end
end
12 changes: 10 additions & 2 deletions shared/kernel/at_exit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
ruby_exe(code).should == "643"
end

it "gives access to the last raised exception" do
it "gives access to the last raised exception - global variables $! and $@" do
code = <<-EOC
#{@method} {
puts "The exception matches: \#{$! == $exception} (message=\#{$!.message})"
puts "The exception matches: \#{$! == $exception && $@ == $exception.backtrace} (message=\#{$!.message})"
}
begin
Expand Down Expand Up @@ -56,4 +56,12 @@
result.should.include?("handler ran\n")
result.should.include?("syntax error")
end

it "calls the nested handler right after the outer one if a handler is nested into another handler" do
ruby_exe(<<~ruby).should == "last\nbefore\nafter\nnested\nfirst\n"
#{@method} { puts :first }
#{@method} { puts :before; #{@method} { puts :nested }; puts :after };
#{@method} { puts :last }
ruby
end
end

0 comments on commit 4fd2644

Please sign in to comment.