diff options
Diffstat (limited to 'spec/ruby/shared/kernel')
| -rw-r--r-- | spec/ruby/shared/kernel/at_exit.rb | 5 | ||||
| -rw-r--r-- | spec/ruby/shared/kernel/complex.rb | 2 | ||||
| -rw-r--r-- | spec/ruby/shared/kernel/equal.rb | 4 | ||||
| -rw-r--r-- | spec/ruby/shared/kernel/object_id.rb | 2 | ||||
| -rw-r--r-- | spec/ruby/shared/kernel/raise.rb | 171 |
5 files changed, 95 insertions, 89 deletions
diff --git a/spec/ruby/shared/kernel/at_exit.rb b/spec/ruby/shared/kernel/at_exit.rb index 29db79bb39..a868ed1e0e 100644 --- a/spec/ruby/shared/kernel/at_exit.rb +++ b/spec/ruby/shared/kernel/at_exit.rb @@ -60,10 +60,7 @@ describe :kernel_at_exit, shared: true do result = ruby_exe('{', options: "-r#{script}", args: "2>&1", exit_status: 1) $?.should_not.success? result.should.include?("handler ran\n") - - # it's tempting not to rely on error message and rely only on exception class name, - # but CRuby before 3.2 doesn't print class name for syntax error - result.should include_any_of("syntax error", "SyntaxError") + result.should.include?("SyntaxError") end it "calls the nested handler right after the outer one if a handler is nested into another handler" do diff --git a/spec/ruby/shared/kernel/complex.rb b/spec/ruby/shared/kernel/complex.rb index 98ee0b2b3f..6e0764dd3d 100644 --- a/spec/ruby/shared/kernel/complex.rb +++ b/spec/ruby/shared/kernel/complex.rb @@ -2,7 +2,7 @@ describe :kernel_complex, shared: true do it "returns a Complex object" do - @object.send(@method, '9').should be_an_instance_of(Complex) + @object.send(@method, '9').should.instance_of?(Complex) end it "understands integers" do diff --git a/spec/ruby/shared/kernel/equal.rb b/spec/ruby/shared/kernel/equal.rb index 0a70aec639..3b74232922 100644 --- a/spec/ruby/shared/kernel/equal.rb +++ b/spec/ruby/shared/kernel/equal.rb @@ -2,13 +2,13 @@ describe :object_equal, shared: true do it "returns true if other is identical to self" do obj = Object.new - obj.__send__(@method, obj).should be_true + obj.__send__(@method, obj).should == true end it "returns false if other is not identical to self" do a = Object.new b = Object.new - a.__send__(@method, b).should be_false + a.__send__(@method, b).should == false end it "returns true only if self and other are the same object" do diff --git a/spec/ruby/shared/kernel/object_id.rb b/spec/ruby/shared/kernel/object_id.rb index 099df8ff94..6469e49903 100644 --- a/spec/ruby/shared/kernel/object_id.rb +++ b/spec/ruby/shared/kernel/object_id.rb @@ -2,7 +2,7 @@ describe :object_id, shared: true do it "returns an integer" do o1 = @object.new - o1.__send__(@method).should be_kind_of(Integer) + o1.__send__(@method).should.is_a?(Integer) end it "returns the same value on all calls to id for a given object" do diff --git a/spec/ruby/shared/kernel/raise.rb b/spec/ruby/shared/kernel/raise.rb index d46c4b7b15..04eaa94e6a 100644 --- a/spec/ruby/shared/kernel/raise.rb +++ b/spec/ruby/shared/kernel/raise.rb @@ -7,9 +7,9 @@ describe :kernel_raise, shared: true do -> do @object.raise Exception, "abort" ScratchPad.record :no_abort - end.should raise_error(Exception, "abort") + end.should.raise(Exception, "abort") - ScratchPad.recorded.should be_nil + ScratchPad.recorded.should == nil end it "accepts an exception that implements to_hash" do @@ -19,7 +19,7 @@ describe :kernel_raise, shared: true do end end error = custom_error.new - -> { @object.raise(error) }.should raise_error(custom_error) + -> { @object.raise(error) }.should.raise(custom_error) end it "allows the message parameter to be a hash" do @@ -30,7 +30,7 @@ describe :kernel_raise, shared: true do end end - -> { @object.raise(data_error, {data: 42}) }.should raise_error(data_error) do |ex| + -> { @object.raise(data_error, {data: 42}) }.should.raise(data_error) do |ex| ex.data.should == {data: 42} end end @@ -44,22 +44,22 @@ describe :kernel_raise, shared: true do end end - -> { @object.raise(data_error, data: 42) }.should raise_error(data_error) do |ex| + -> { @object.raise(data_error, data: 42) }.should.raise(data_error) do |ex| ex.data.should == {data: 42} end end it "raises RuntimeError if no exception class is given" do - -> { @object.raise }.should raise_error(RuntimeError, "") + -> { @object.raise }.should.raise(RuntimeError, "") end it "raises a given Exception instance" do error = RuntimeError.new - -> { @object.raise(error) }.should raise_error(error) + -> { @object.raise(error) }.should.raise(error) end it "raises a RuntimeError if string given" do - -> { @object.raise("a bad thing") }.should raise_error(RuntimeError, "a bad thing") + -> { @object.raise("a bad thing") }.should.raise(RuntimeError, "a bad thing") end it "passes no arguments to the constructor when given only an exception class" do @@ -67,29 +67,44 @@ describe :kernel_raise, shared: true do def initialize end end - -> { @object.raise(klass) }.should raise_error(klass) { |e| e.message.should == klass.to_s } + -> { @object.raise(klass) }.should.raise(klass) { |e| e.message.should == klass.to_s } end it "raises a TypeError when passed a non-Exception object" do - -> { @object.raise(Object.new) }.should raise_error(TypeError, "exception class/object expected") - -> { @object.raise(Object.new, "message") }.should raise_error(TypeError, "exception class/object expected") - -> { @object.raise(Object.new, "message", []) }.should raise_error(TypeError, "exception class/object expected") + -> { @object.raise(Object.new) }.should.raise(TypeError, "exception class/object expected") + -> { @object.raise(Object.new, "message") }.should.raise(TypeError, "exception class/object expected") + -> { @object.raise(Object.new, "message", []) }.should.raise(TypeError, "exception class/object expected") end it "raises a TypeError when passed true" do - -> { @object.raise(true) }.should raise_error(TypeError, "exception class/object expected") + -> { @object.raise(true) }.should.raise(TypeError, "exception class/object expected") end it "raises a TypeError when passed false" do - -> { @object.raise(false) }.should raise_error(TypeError, "exception class/object expected") + -> { @object.raise(false) }.should.raise(TypeError, "exception class/object expected") end it "raises a TypeError when passed nil" do - -> { @object.raise(nil) }.should raise_error(TypeError, "exception class/object expected") + -> { @object.raise(nil) }.should.raise(TypeError, "exception class/object expected") end it "raises a TypeError when passed a message and an extra argument" do - -> { @object.raise("message", {cause: RuntimeError.new()}) }.should raise_error(TypeError, "exception class/object expected") + -> { @object.raise("message", {cause: RuntimeError.new()}) }.should.raise(TypeError, "exception class/object expected") + end + + it "raises result from #exception when passed a non-Exception object" do + e = Object.new + def e.exception = StandardError.new + + -> { @object.raise e }.should.raise(StandardError) + end + + it "raises result from #exception with given arguments when passed a non-Exception object" do + e = Object.new + def e.exception(msg) = StandardError.new(msg) + + -> { @object.raise e, "foo" }.should.raise(StandardError, "foo") + -> { @object.raise e }.should.raise(ArgumentError, "wrong number of arguments (given 0, expected 1)") end it "raises TypeError when passed a non-Exception object but it responds to #exception method that doesn't return an instance of Exception class" do @@ -100,7 +115,7 @@ describe :kernel_raise, shared: true do -> { @object.raise e - }.should raise_error(TypeError, "exception object expected") + }.should.raise(TypeError, "exception object expected") end it "re-raises a previously rescued exception without overwriting the backtrace" do @@ -127,7 +142,7 @@ describe :kernel_raise, shared: true do it "allows Exception, message, and backtrace parameters" do -> do @object.raise(ArgumentError, "message", caller) - end.should raise_error(ArgumentError, "message") + end.should.raise(ArgumentError, "message") end ruby_version_is "3.4" do @@ -135,68 +150,76 @@ describe :kernel_raise, shared: true do it "allows Exception, message, and backtrace_locations parameters" do -> do @object.raise(ArgumentError, "message", locations) - end.should raise_error(ArgumentError, "message") { |error| + end.should.raise(ArgumentError, "message") { |error| error.backtrace_locations.map(&:to_s).should == locations.map(&:to_s) } end end +end + +describe :kernel_raise_with_cause, shared: true do + context "without cause keyword argument" do + it "sets cause to nil when there is no previous exception" do + -> do + @object.raise("error without a cause") + end.should.raise(RuntimeError, "error without a cause", cause: nil) + end + + it "supports automatic cause chaining from a previous exception" do + begin + raise StandardError, "first error" + rescue => cause + -> { @object.raise("second error") }.should.raise(RuntimeError, "second error", cause:) + end + end + end - ruby_version_is "3.5" do - it "allows cause keyword argument" do + context "with cause keyword argument" do + it "allows setting exception's cause" do cause = StandardError.new("original error") - result = nil -> do - @object.raise("new error", cause: cause) - end.should raise_error(RuntimeError, "new error") do |error| - error.cause.should == cause + @object.raise("new error", cause:) + end.should.raise(RuntimeError, "new error", cause:) + end + + it "allows setting cause to nil" do + -> do + @object.raise("error without a cause", cause: nil) + end.should.raise(RuntimeError, "error without a cause") do |error| + error.cause.should == nil end end it "raises an ArgumentError when only cause is given" do cause = StandardError.new("cause") -> do - @object.raise(cause: cause) - end.should raise_error(ArgumentError, "only cause is given with no arguments") + @object.raise(cause:) + end.should.raise(ArgumentError, "only cause is given with no arguments") end it "raises an ArgumentError when only cause is given and is nil" do -> do @object.raise(cause: nil) - end.should raise_error(ArgumentError, "only cause is given with no arguments") + end.should.raise(ArgumentError, "only cause is given with no arguments") end - it "raises a TypeError when given cause is not an instance of Exception" do + it "raises a TypeError when given cause is not an instance of Exception or nil" do cause = Object.new -> do - @object.raise("message", cause: cause) - end.should raise_error(TypeError, "exception object expected") + @object.raise("message", cause:) + end.should.raise(TypeError, "exception object expected") end it "doesn't set given cause when it equals the raised exception" do cause = StandardError.new("cause") - result = nil - - -> do - @object.raise(cause, cause: cause) - end.should raise_error(StandardError, "cause") do |error| - error.should == cause - error.cause.should == nil - end - end - - it "accepts cause equal an exception" do - error = RuntimeError.new("message") - result = nil -> do - @object.raise(error, cause: error) - end.should raise_error(RuntimeError, "message") do |error| - error.cause.should == nil - end + @object.raise(cause, cause:) + end.should.raise(StandardError, "cause", cause: nil) end - it "rejects circular causes" do + it "raises ArgumentError when cause creates a circular reference" do -> { begin raise "Error 1" @@ -211,52 +234,40 @@ describe :kernel_raise, shared: true do end end end - }.should raise_error(ArgumentError, "circular causes") + }.should.raise(ArgumentError, "circular causes") end it "supports exception class with message and cause" do cause = StandardError.new("cause message") - result = nil -> do - @object.raise(ArgumentError, "argument error message", cause: cause) - end.should raise_error(ArgumentError, "argument error message") do |error| - error.should be_kind_of(ArgumentError) - error.message.should == "argument error message" - error.cause.should == cause - end + @object.raise(ArgumentError, "argument error message", cause:) + end.should.raise(ArgumentError, "argument error message", cause:) end it "supports exception class with message, backtrace and cause" do cause = StandardError.new("cause message") backtrace = ["line1", "line2"] - result = nil -> do - @object.raise(ArgumentError, "argument error message", backtrace, cause: cause) - end.should raise_error(ArgumentError, "argument error message") do |error| - error.should be_kind_of(ArgumentError) - error.message.should == "argument error message" - error.cause.should == cause + @object.raise(ArgumentError, "argument error message", backtrace, cause:) + end.should.raise(ArgumentError, "argument error message", cause:) do |error| error.backtrace.should == backtrace end end - it "supports automatic cause chaining" do + it "supports cause: exception, overriding previous exception" do + custom_error = StandardError.new("custom error") -> do begin raise "first error" rescue - # No explicit cause - should chain automatically: - @object.raise("second error") + @object.raise("second error", cause: custom_error) end - end.should raise_error(RuntimeError, "second error") do |error| - error.cause.should be_kind_of(RuntimeError) - error.cause.message.should == "first error" - end + end.should.raise(RuntimeError, "second error", cause: custom_error) end - it "supports cause: nil to prevent automatic cause chaining" do + it "supports cause: nil, discarding previous exception" do -> do begin raise "first error" @@ -264,15 +275,13 @@ describe :kernel_raise, shared: true do # Explicit nil prevents chaining: @object.raise("second error", cause: nil) end - end.should raise_error(RuntimeError, "second error") do |error| - error.cause.should == nil - end + end.should.raise(RuntimeError, "second error", cause: nil) end end end describe :kernel_raise_across_contexts, shared: true do - ruby_version_is "3.5" do + ruby_version_is "4.0" do describe "with cause keyword argument" do it "uses the cause from the calling context" do original_cause = nil @@ -292,7 +301,7 @@ describe :kernel_raise_across_contexts, shared: true do end end - result.should be_kind_of(RuntimeError) + result.should.is_a?(RuntimeError) result.message.should == "second error" result.cause.should == nil end @@ -320,7 +329,7 @@ describe :kernel_raise_across_contexts, shared: true do end end - result.should be_kind_of(RuntimeError) + result.should.is_a?(RuntimeError) result.message.should == "second error" result.cause.should == override_cause end @@ -340,7 +349,7 @@ describe :kernel_raise_across_contexts, shared: true do end end - result.should be_kind_of(RuntimeError) + result.should.is_a?(RuntimeError) result.message.should == "new error" # Calling context has no current exception: result.cause.should == nil @@ -364,7 +373,7 @@ describe :kernel_raise_across_contexts, shared: true do end end - result.should be_kind_of(RuntimeError) + result.should.is_a?(RuntimeError) result.message.should == "new error" result.cause.should == nil end @@ -379,7 +388,7 @@ describe :kernel_raise_across_contexts, shared: true do # Ignore - we expect the TypeError to be raised in the calling context end end - }.should raise_error(TypeError, "exception object expected") + }.should.raise(TypeError, "exception object expected") end it "raises ArgumentError when only cause is given with no arguments" do @@ -391,7 +400,7 @@ describe :kernel_raise_across_contexts, shared: true do # Ignore - we expect the ArgumentError to be raised in the calling context end end - }.should raise_error(ArgumentError, "only cause is given with no arguments") + }.should.raise(ArgumentError, "only cause is given with no arguments") end end end |
