summaryrefslogtreecommitdiff
path: root/spec/ruby/shared/kernel
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/shared/kernel')
-rw-r--r--spec/ruby/shared/kernel/at_exit.rb67
-rw-r--r--spec/ruby/shared/kernel/complex.rb133
-rw-r--r--spec/ruby/shared/kernel/fixtures/END.rb3
-rw-r--r--spec/ruby/shared/kernel/fixtures/at_exit.rb3
-rw-r--r--spec/ruby/shared/kernel/raise.rb123
5 files changed, 21 insertions, 308 deletions
diff --git a/spec/ruby/shared/kernel/at_exit.rb b/spec/ruby/shared/kernel/at_exit.rb
deleted file mode 100644
index 26ad361a5b..0000000000
--- a/spec/ruby/shared/kernel/at_exit.rb
+++ /dev/null
@@ -1,67 +0,0 @@
-describe :kernel_at_exit, shared: true do
- it "runs after all other code" do
- ruby_exe("#{@method} { print 5 }; print 6").should == "65"
- end
-
- it "runs in reverse order of registration" do
- code = "#{@method} { print 4 }; #{@method} { print 5 }; print 6; #{@method} { print 7 }"
- ruby_exe(code).should == "6754"
- end
-
- it "allows calling exit inside a handler" do
- code = "#{@method} { print 3 }; #{@method} { print 4; exit; print 5 }; #{@method} { print 6 }"
- ruby_exe(code).should == "643"
- end
-
- it "gives access to the last raised exception - global variables $! and $@" do
- code = <<-EOC
- #{@method} {
- puts "The exception matches: \#{$! == $exception && $@ == $exception.backtrace} (message=\#{$!.message})"
- }
-
- begin
- raise "foo"
- rescue => $exception
- raise
- end
- EOC
-
- result = ruby_exe(code, args: "2>&1", exit_status: 1)
- result.lines.should.include?("The exception matches: true (message=foo)\n")
- end
-
- it "both exceptions in a handler and in the main script are printed" do
- code = "#{@method} { raise 'at_exit_error' }; raise 'main_script_error'"
- result = ruby_exe(code, args: "2>&1", exit_status: 1)
- result.should.include?('at_exit_error (RuntimeError)')
- result.should.include?('main_script_error (RuntimeError)')
- end
-
- it "decides the exit status if both at_exit and the main script raise SystemExit" do
- ruby_exe("#{@method} { exit 43 }; exit 42", args: "2>&1", exit_status: 43)
- $?.exitstatus.should == 43
- end
-
- it "runs all handlers even if some raise exceptions" do
- code = "#{@method} { STDERR.puts 'last' }; #{@method} { exit 43 }; #{@method} { STDERR.puts 'first' }; exit 42"
- result = ruby_exe(code, args: "2>&1", exit_status: 43)
- result.should == "first\nlast\n"
- $?.exitstatus.should == 43
- end
-
- it "runs handlers even if the main script fails to parse" do
- script = fixture(__FILE__, "#{@method}.rb")
- result = ruby_exe('{', options: "-r#{script}", args: "2>&1", exit_status: 1)
- $?.should_not.success?
- 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
diff --git a/spec/ruby/shared/kernel/complex.rb b/spec/ruby/shared/kernel/complex.rb
deleted file mode 100644
index 98ee0b2b3f..0000000000
--- a/spec/ruby/shared/kernel/complex.rb
+++ /dev/null
@@ -1,133 +0,0 @@
-# Specs shared by Kernel#Complex() and String#to_c()
-describe :kernel_complex, shared: true do
-
- it "returns a Complex object" do
- @object.send(@method, '9').should be_an_instance_of(Complex)
- end
-
- it "understands integers" do
- @object.send(@method, '20').should == Complex(20)
- end
-
- it "understands negative integers" do
- @object.send(@method, '-3').should == Complex(-3)
- end
-
- it "understands fractions (numerator/denominator) for the real part" do
- @object.send(@method, '2/3').should == Complex(Rational(2, 3))
- end
-
- it "understands fractions (numerator/denominator) for the imaginary part" do
- @object.send(@method, '4+2/3i').should == Complex(4, Rational(2, 3))
- end
-
- it "understands negative fractions (-numerator/denominator) for the real part" do
- @object.send(@method, '-2/3').should == Complex(Rational(-2, 3))
- end
-
- it "understands negative fractions (-numerator/denominator) for the imaginary part" do
- @object.send(@method, '7-2/3i').should == Complex(7, Rational(-2, 3))
- end
-
- it "understands floats (a.b) for the real part" do
- @object.send(@method, '2.3').should == Complex(2.3)
- end
-
- it "understands floats (a.b) for the imaginary part" do
- @object.send(@method, '4+2.3i').should == Complex(4, 2.3)
- end
-
- it "understands negative floats (-a.b) for the real part" do
- @object.send(@method, '-2.33').should == Complex(-2.33)
- end
-
- it "understands negative floats (-a.b) for the imaginary part" do
- @object.send(@method, '7-28.771i').should == Complex(7, -28.771)
- end
-
- it "understands an integer followed by 'i' to mean that integer is the imaginary part" do
- @object.send(@method, '35i').should == Complex(0,35)
- end
-
- it "understands a negative integer followed by 'i' to mean that negative integer is the imaginary part" do
- @object.send(@method, '-29i').should == Complex(0,-29)
- end
-
- it "understands an 'i' by itself as denoting a complex number with an imaginary part of 1" do
- @object.send(@method, 'i').should == Complex(0,1)
- end
-
- it "understands a '-i' by itself as denoting a complex number with an imaginary part of -1" do
- @object.send(@method, '-i').should == Complex(0,-1)
- end
-
- it "understands 'a+bi' to mean a complex number with 'a' as the real part, 'b' as the imaginary" do
- @object.send(@method, '79+4i').should == Complex(79,4)
- end
-
- it "understands 'a-bi' to mean a complex number with 'a' as the real part, '-b' as the imaginary" do
- @object.send(@method, '79-4i').should == Complex(79,-4)
- end
-
- it "understands 'a+i' to mean a complex number with 'a' as the real part, 1i as the imaginary" do
- @object.send(@method, '79+i').should == Complex(79, 1)
- end
-
- it "understands 'a-i' to mean a complex number with 'a' as the real part, -1i as the imaginary" do
- @object.send(@method, '79-i').should == Complex(79, -1)
- end
-
- it "understands i, I, j, and J imaginary units" do
- @object.send(@method, '79+4i').should == Complex(79, 4)
- @object.send(@method, '79+4I').should == Complex(79, 4)
- @object.send(@method, '79+4j').should == Complex(79, 4)
- @object.send(@method, '79+4J').should == Complex(79, 4)
- end
-
- it "understands scientific notation for the real part" do
- @object.send(@method, '2e3+4i').should == Complex(2e3,4)
- end
-
- it "understands negative scientific notation for the real part" do
- @object.send(@method, '-2e3+4i').should == Complex(-2e3,4)
- end
-
- it "understands scientific notation for the imaginary part" do
- @object.send(@method, '4+2e3i').should == Complex(4, 2e3)
- end
-
- it "understands negative scientific notation for the imaginary part" do
- @object.send(@method, '4-2e3i').should == Complex(4, -2e3)
- end
-
- it "understands scientific notation for the real and imaginary part in the same String" do
- @object.send(@method, '2e3+2e4i').should == Complex(2e3,2e4)
- end
-
- it "understands negative scientific notation for the real and imaginary part in the same String" do
- @object.send(@method, '-2e3-2e4i').should == Complex(-2e3,-2e4)
- end
-
- it "understands scientific notation with e and E" do
- @object.send(@method, '2e3+2e4i').should == Complex(2e3, 2e4)
- @object.send(@method, '2E3+2E4i').should == Complex(2e3, 2e4)
- end
-
- it "understands 'm@a' to mean a complex number in polar form with 'm' as the modulus, 'a' as the argument" do
- @object.send(@method, '79@4').should == Complex.polar(79, 4)
- @object.send(@method, '-79@4').should == Complex.polar(-79, 4)
- @object.send(@method, '79@-4').should == Complex.polar(79, -4)
- end
-
- it "ignores leading whitespaces" do
- @object.send(@method, ' 79+4i').should == Complex(79, 4)
- end
-
- it "ignores trailing whitespaces" do
- @object.send(@method, '79+4i ').should == Complex(79, 4)
- end
-
- it "understands _" do
- @object.send(@method, '7_9+4_0i').should == Complex(79, 40)
- end
-end
diff --git a/spec/ruby/shared/kernel/fixtures/END.rb b/spec/ruby/shared/kernel/fixtures/END.rb
deleted file mode 100644
index cc8ac17c36..0000000000
--- a/spec/ruby/shared/kernel/fixtures/END.rb
+++ /dev/null
@@ -1,3 +0,0 @@
-END {
- STDERR.puts "handler ran"
-}
diff --git a/spec/ruby/shared/kernel/fixtures/at_exit.rb b/spec/ruby/shared/kernel/fixtures/at_exit.rb
deleted file mode 100644
index e7bc8baf52..0000000000
--- a/spec/ruby/shared/kernel/fixtures/at_exit.rb
+++ /dev/null
@@ -1,3 +0,0 @@
-at_exit do
- STDERR.puts "handler ran"
-end
diff --git a/spec/ruby/shared/kernel/raise.rb b/spec/ruby/shared/kernel/raise.rb
index 82fb0333c8..70d638fff9 100644
--- a/spec/ruby/shared/kernel/raise.rb
+++ b/spec/ruby/shared/kernel/raise.rb
@@ -4,7 +4,7 @@ describe :kernel_raise, shared: true do
end
it "aborts execution" do
- -> do
+ lambda do
@object.raise Exception, "abort"
ScratchPad.record :no_abort
end.should raise_error(Exception, "abort")
@@ -12,137 +12,56 @@ describe :kernel_raise, shared: true do
ScratchPad.recorded.should be_nil
end
- it "accepts an exception that implements to_hash" do
- custom_error = Class.new(StandardError) do
- def to_hash
- {}
- end
- end
- error = custom_error.new
- -> { @object.raise(error) }.should raise_error(custom_error)
- end
-
- it "allows the message parameter to be a hash" do
- data_error = Class.new(StandardError) do
- attr_reader :data
- def initialize(data)
- @data = data
- end
- end
-
- -> { @object.raise(data_error, {data: 42}) }.should raise_error(data_error) do |ex|
- ex.data.should == {data: 42}
- end
- end
-
- # https://bugs.ruby-lang.org/issues/8257#note-36
- it "allows extra keyword arguments for compatibility" do
- data_error = Class.new(StandardError) do
- attr_reader :data
- def initialize(data)
- @data = data
- end
- end
-
- -> { @object.raise(data_error, data: 42) }.should raise_error(data_error) do |ex|
- ex.data.should == {data: 42}
- end
- end
-
- it "does not allow message and extra keyword arguments" do
- data_error = Class.new(StandardError) do
- attr_reader :data
- def initialize(data)
- @data = data
- end
- end
-
- -> { @object.raise(data_error, {a: 1}, b: 2) }.should raise_error(StandardError) do |e|
- [TypeError, ArgumentError].should.include?(e.class)
- end
-
- -> { @object.raise(data_error, {a: 1}, [], b: 2) }.should raise_error(ArgumentError)
- end
-
it "raises RuntimeError if no exception class is given" do
- -> { @object.raise }.should raise_error(RuntimeError, "")
+ lambda { @object.raise }.should raise_error(RuntimeError)
end
it "raises a given Exception instance" do
error = RuntimeError.new
- -> { @object.raise(error) }.should raise_error(error)
+ lambda { @object.raise(error) }.should raise_error(error)
end
it "raises a RuntimeError if string given" do
- -> { @object.raise("a bad thing") }.should raise_error(RuntimeError)
- end
-
- it "passes no arguments to the constructor when given only an exception class" do
- klass = Class.new(Exception) do
- def initialize
- end
- end
- -> { @object.raise(klass) }.should raise_error(klass) { |e| e.message.should == klass.to_s }
+ lambda { @object.raise("a bad thing") }.should raise_error(RuntimeError)
end
it "raises a TypeError when passed a non-Exception object" do
- -> { @object.raise(Object.new) }.should raise_error(TypeError)
+ lambda { @object.raise(Object.new) }.should raise_error(TypeError)
end
it "raises a TypeError when passed true" do
- -> { @object.raise(true) }.should raise_error(TypeError)
+ lambda { @object.raise(true) }.should raise_error(TypeError)
end
it "raises a TypeError when passed false" do
- -> { @object.raise(false) }.should raise_error(TypeError)
+ lambda { @object.raise(false) }.should raise_error(TypeError)
end
it "raises a TypeError when passed nil" do
- -> { @object.raise(nil) }.should raise_error(TypeError)
+ lambda { @object.raise(nil) }.should raise_error(TypeError)
end
- it "re-raises a previously rescued exception without overwriting the backtrace" do
- # This spec is written using #backtrace and matching the line number
- # from the string, as backtrace_locations is a more advanced
- # method that is not always supported by implementations.
- #
- initial_raise_line = nil
- raise_again_line = nil
- raised_again = nil
-
- if defined?(FiberSpecs::NewFiberToRaise) and @object == FiberSpecs::NewFiberToRaise
- fiber = Fiber.new do
- begin
- initial_raise_line = __LINE__; Fiber.yield
- rescue => raised
- begin
- raise_again_line = __LINE__; Fiber.yield raised
- rescue => raised_again
- raised_again
- end
- end
- end
- fiber.resume
- raised = fiber.raise 'raised'
- raised_again = fiber.raise raised
- else
+ it "re-raises the rescued exception" do
+ lambda do
begin
- initial_raise_line = __LINE__; @object.raise 'raised'
- rescue => raised
+ raise Exception, "outer"
+ ScratchPad.record :no_abort
+ rescue
begin
- raise_again_line = __LINE__; @object.raise raised
- rescue => raised_again
- raised_again
+ raise StandardError, "inner"
+ rescue
end
+
+ @object.raise
+ ScratchPad.record :no_reraise
end
- end
+ end.should raise_error(Exception, "outer")
- raised_again.backtrace.first.should include("#{__FILE__}:#{initial_raise_line}:")
- raised_again.backtrace.first.should_not include("#{__FILE__}:#{raise_again_line}:")
+ ScratchPad.recorded.should be_nil
end
it "allows Exception, message, and backtrace parameters" do
- -> do
+ lambda do
@object.raise(ArgumentError, "message", caller)
end.should raise_error(ArgumentError, "message")
end