diff options
Diffstat (limited to 'spec/ruby/core/exception')
28 files changed, 402 insertions, 466 deletions
diff --git a/spec/ruby/core/exception/backtrace_locations_spec.rb b/spec/ruby/core/exception/backtrace_locations_spec.rb index 86eb9d3413..62eab8a0f3 100644 --- a/spec/ruby/core/exception/backtrace_locations_spec.rb +++ b/spec/ruby/core/exception/backtrace_locations_spec.rb @@ -7,15 +7,15 @@ describe "Exception#backtrace_locations" do end it "returns nil if no backtrace was set" do - Exception.new.backtrace_locations.should be_nil + Exception.new.backtrace_locations.should == nil end it "returns an Array" do - @backtrace.should be_an_instance_of(Array) + @backtrace.should.instance_of?(Array) end it "sets each element to a Thread::Backtrace::Location" do - @backtrace.each {|l| l.should be_an_instance_of(Thread::Backtrace::Location)} + @backtrace.each {|l| l.should.instance_of?(Thread::Backtrace::Location)} end it "produces a backtrace for an exception captured using $!" do diff --git a/spec/ruby/core/exception/backtrace_spec.rb b/spec/ruby/core/exception/backtrace_spec.rb index 9a65ea3820..a4a8272030 100644 --- a/spec/ruby/core/exception/backtrace_spec.rb +++ b/spec/ruby/core/exception/backtrace_spec.rb @@ -7,15 +7,15 @@ describe "Exception#backtrace" do end it "returns nil if no backtrace was set" do - Exception.new.backtrace.should be_nil + Exception.new.backtrace.should == nil end it "returns an Array" do - @backtrace.should be_an_instance_of(Array) + @backtrace.should.instance_of?(Array) end it "sets each element to a String" do - @backtrace.each {|l| l.should be_an_instance_of(String)} + @backtrace.each {|l| l.should.instance_of?(String)} end it "includes the filename of the location where self raised in the first element" do @@ -94,13 +94,13 @@ describe "Exception#backtrace" do raise rescue RuntimeError => err bt = err.backtrace - err.dup.backtrace.should equal(bt) + err.dup.backtrace.should.equal?(bt) new_bt = ['hi'] err.set_backtrace new_bt err.backtrace.should == new_bt - err.dup.backtrace.should equal(new_bt) + err.dup.backtrace.should.equal?(new_bt) end end end diff --git a/spec/ruby/core/exception/cause_spec.rb b/spec/ruby/core/exception/cause_spec.rb index cf4aaeb188..cfc15bdda3 100644 --- a/spec/ruby/core/exception/cause_spec.rb +++ b/spec/ruby/core/exception/cause_spec.rb @@ -4,53 +4,35 @@ describe "Exception#cause" do it "returns the active exception when an exception is raised" do begin raise Exception, "the cause" - rescue Exception - begin + rescue Exception => cause + -> { raise RuntimeError, "the consequence" - rescue RuntimeError => e - e.should be_an_instance_of(RuntimeError) - e.message.should == "the consequence" - - e.cause.should be_an_instance_of(Exception) - e.cause.message.should == "the cause" - end + }.should.raise(RuntimeError, "the consequence", cause:) end end it "is set for user errors caused by internal errors" do - -> { - begin - 1 / 0 - rescue - raise "foo" - end - }.should raise_error(RuntimeError) { |e| - e.cause.should be_kind_of(ZeroDivisionError) - } + begin + 1 / 0 + rescue => cause + -> { raise "foo" }.should.raise(RuntimeError, cause:) + end end it "is set for internal errors caused by user errors" do cause = RuntimeError.new "cause" - -> { - begin - raise cause - rescue - 1 / 0 - end - }.should raise_error(ZeroDivisionError) { |e| - e.cause.should equal(cause) - } + begin + raise cause + rescue + -> { 1 / 0 }.should.raise(ZeroDivisionError, cause:) + end end it "is not set to the exception itself when it is re-raised" do - -> { - begin - raise RuntimeError - rescue RuntimeError => e - raise e - end - }.should raise_error(RuntimeError) { |e| - e.cause.should == nil - } + begin + raise RuntimeError + rescue RuntimeError => e + -> { raise e }.should.raise(RuntimeError, cause: nil) + end end end diff --git a/spec/ruby/core/exception/detailed_message_spec.rb b/spec/ruby/core/exception/detailed_message_spec.rb index 8178278b2b..9df164a1cf 100644 --- a/spec/ruby/core/exception/detailed_message_spec.rb +++ b/spec/ruby/core/exception/detailed_message_spec.rb @@ -2,51 +2,49 @@ require_relative '../../spec_helper' require_relative 'fixtures/common' describe "Exception#detailed_message" do - ruby_version_is "3.2" do - it "returns decorated message" do - RuntimeError.new("new error").detailed_message.should == "new error (RuntimeError)" - end + it "returns decorated message" do + RuntimeError.new("new error").detailed_message.should == "new error (RuntimeError)" + end - it "is called by #full_message to allow message customization" do - exception = Exception.new("new error") - def exception.detailed_message(**) - "<prefix>#{message}<suffix>" - end - exception.full_message(highlight: false).should.include? "<prefix>new error<suffix>" + it "is called by #full_message to allow message customization" do + exception = Exception.new("new error") + def exception.detailed_message(**) + "<prefix>#{message}<suffix>" end + exception.full_message(highlight: false).should.include? "<prefix>new error<suffix>" + end - it "returns just a message if exception class is anonymous" do - Class.new(RuntimeError).new("message").detailed_message.should == "message" - end + it "returns just a message if exception class is anonymous" do + Class.new(RuntimeError).new("message").detailed_message.should == "message" + end - it "returns 'unhandled exception' for an instance of RuntimeError with empty message" do - RuntimeError.new("").detailed_message.should == "unhandled exception" - end + it "returns 'unhandled exception' for an instance of RuntimeError with empty message" do + RuntimeError.new("").detailed_message.should == "unhandled exception" + end - it "returns just class name for an instance other than RuntimeError with empty message" do - DetailedMessageSpec::C.new("").detailed_message.should == "DetailedMessageSpec::C" - StandardError.new("").detailed_message.should == "StandardError" - end + it "returns just class name for an instance other than RuntimeError with empty message" do + DetailedMessageSpec::C.new("").detailed_message.should == "DetailedMessageSpec::C" + StandardError.new("").detailed_message.should == "StandardError" + end - it "returns a generated class name for an instance of RuntimeError anonymous subclass with empty message" do - klass = Class.new(RuntimeError) - klass.new("").detailed_message.should =~ /\A#<Class:0x\h+>\z/ - end + it "returns a generated class name for an instance of RuntimeError anonymous subclass with empty message" do + klass = Class.new(RuntimeError) + klass.new("").detailed_message.should =~ /\A#<Class:0x\h+>\z/ + end - it "accepts highlight keyword argument and adds escape control sequences" do - RuntimeError.new("new error").detailed_message(highlight: true).should == "\e[1mnew error (\e[1;4mRuntimeError\e[m\e[1m)\e[m" - end + it "accepts highlight keyword argument and adds escape control sequences" do + RuntimeError.new("new error").detailed_message(highlight: true).should == "\e[1mnew error (\e[1;4mRuntimeError\e[m\e[1m)\e[m" + end - it "accepts highlight keyword argument and adds escape control sequences for an instance of RuntimeError with empty message" do - RuntimeError.new("").detailed_message(highlight: true).should == "\e[1;4munhandled exception\e[m" - end + it "accepts highlight keyword argument and adds escape control sequences for an instance of RuntimeError with empty message" do + RuntimeError.new("").detailed_message(highlight: true).should == "\e[1;4munhandled exception\e[m" + end - it "accepts highlight keyword argument and adds escape control sequences for an instance other than RuntimeError with empty message" do - StandardError.new("").detailed_message(highlight: true).should == "\e[1;4mStandardError\e[m" - end + it "accepts highlight keyword argument and adds escape control sequences for an instance other than RuntimeError with empty message" do + StandardError.new("").detailed_message(highlight: true).should == "\e[1;4mStandardError\e[m" + end - it "allows and ignores other keyword arguments" do - RuntimeError.new("new error").detailed_message(foo: true).should == "new error (RuntimeError)" - end + it "allows and ignores other keyword arguments" do + RuntimeError.new("new error").detailed_message(foo: true).should == "new error (RuntimeError)" end end diff --git a/spec/ruby/core/exception/dup_spec.rb b/spec/ruby/core/exception/dup_spec.rb index edd54bfb37..b53ad79bf3 100644 --- a/spec/ruby/core/exception/dup_spec.rb +++ b/spec/ruby/core/exception/dup_spec.rb @@ -20,7 +20,7 @@ describe "Exception#dup" do it "does not copy singleton methods" do def @obj.special() :the_one end dup = @obj.dup - -> { dup.special }.should raise_error(NameError) + -> { dup.special }.should.raise(NameError) end it "does not copy modules included in the singleton class" do @@ -29,7 +29,7 @@ describe "Exception#dup" do end dup = @obj.dup - -> { dup.repr }.should raise_error(NameError) + -> { dup.repr }.should.raise(NameError) end it "does not copy constants defined in the singleton class" do @@ -38,7 +38,7 @@ describe "Exception#dup" do end dup = @obj.dup - -> { class << dup; CLONE; end }.should raise_error(NameError) + -> { class << dup; CLONE; end }.should.raise(NameError) end it "does copy the message" do @@ -61,13 +61,13 @@ describe "Exception#dup" do it "does copy the cause" do begin - raise StandardError, "the cause" + raise StandardError rescue StandardError => cause begin - raise RuntimeError, "the consequence" + raise RuntimeError rescue RuntimeError => e - e.cause.should equal(cause) - e.dup.cause.should equal(cause) + e.cause.should.equal?(cause) + e.dup.cause.should.equal?(cause) end end end diff --git a/spec/ruby/core/exception/equal_value_spec.rb b/spec/ruby/core/exception/equal_value_spec.rb index e8f3ce0f8d..b76b3bcd4a 100644 --- a/spec/ruby/core/exception/equal_value_spec.rb +++ b/spec/ruby/core/exception/equal_value_spec.rb @@ -31,10 +31,10 @@ describe "Exception#==" do it "returns false if the two exceptions inherit from Exception but have different classes" do one = RuntimeError.new("message") one.set_backtrace [__dir__] - one.should be_kind_of(Exception) + one.should.is_a?(Exception) two = TypeError.new("message") two.set_backtrace [__dir__] - two.should be_kind_of(Exception) + two.should.is_a?(Exception) one.should_not == two end diff --git a/spec/ruby/core/exception/errno_spec.rb b/spec/ruby/core/exception/errno_spec.rb index a063e522ea..36beae9976 100644 --- a/spec/ruby/core/exception/errno_spec.rb +++ b/spec/ruby/core/exception/errno_spec.rb @@ -4,21 +4,21 @@ require_relative 'fixtures/common' describe "Errno::EINVAL.new" do it "can be called with no arguments" do exc = Errno::EINVAL.new - exc.should be_an_instance_of(Errno::EINVAL) + exc.should.instance_of?(Errno::EINVAL) exc.errno.should == Errno::EINVAL::Errno exc.message.should == "Invalid argument" end it "accepts an optional custom message" do exc = Errno::EINVAL.new('custom message') - exc.should be_an_instance_of(Errno::EINVAL) + exc.should.instance_of?(Errno::EINVAL) exc.errno.should == Errno::EINVAL::Errno exc.message.should == "Invalid argument - custom message" end it "accepts an optional custom message and location" do exc = Errno::EINVAL.new('custom message', 'location') - exc.should be_an_instance_of(Errno::EINVAL) + exc.should.instance_of?(Errno::EINVAL) exc.errno.should == Errno::EINVAL::Errno exc.message.should == "Invalid argument @ location - custom message" end @@ -28,7 +28,9 @@ describe "Errno::EMFILE" do it "can be subclassed" do ExceptionSpecs::EMFILESub = Class.new(Errno::EMFILE) exc = ExceptionSpecs::EMFILESub.new - exc.should be_an_instance_of(ExceptionSpecs::EMFILESub) + exc.should.instance_of?(ExceptionSpecs::EMFILESub) + ensure + ExceptionSpecs.send(:remove_const, :EMFILESub) end end @@ -45,7 +47,7 @@ end describe "Errno::ENOTSUP" do it "is defined" do - Errno.should have_constant(:ENOTSUP) + Errno.should.const_defined?(:ENOTSUP, false) end it "is the same class as Errno::EOPNOTSUPP if they represent the same errno value" do diff --git a/spec/ruby/core/exception/exception_spec.rb b/spec/ruby/core/exception/exception_spec.rb index d6f5283bd9..f5424cdabd 100644 --- a/spec/ruby/core/exception/exception_spec.rb +++ b/spec/ruby/core/exception/exception_spec.rb @@ -20,7 +20,7 @@ describe "Exception#exception" do it "returns an exception of the same class as self with the message given as argument" do e = RuntimeError.new e2 = e.exception("message") - e2.should be_an_instance_of(RuntimeError) + e2.should.instance_of?(RuntimeError) e2.message.should == "message" end @@ -62,7 +62,7 @@ describe "Exception#exception" do it "returns an exception of the same class as self with the message given as argument, but without reinitializing" do e = CustomArgumentError.new(:boom) e2 = e.exception("message") - e2.should be_an_instance_of(CustomArgumentError) + e2.should.instance_of?(CustomArgumentError) e2.val.should == :boom e2.message.should == "message" end diff --git a/spec/ruby/core/exception/exit_value_spec.rb b/spec/ruby/core/exception/exit_value_spec.rb index 99987dd1bc..bb6cff1831 100644 --- a/spec/ruby/core/exception/exit_value_spec.rb +++ b/spec/ruby/core/exception/exit_value_spec.rb @@ -6,7 +6,7 @@ describe "LocalJumpError#exit_value" do end it "returns the value given to return" do - -> { get_me_a_return.call }.should raise_error(LocalJumpError) { |e| + -> { get_me_a_return.call }.should.raise(LocalJumpError) { |e| e.exit_value.should == 42 } end diff --git a/spec/ruby/core/exception/frozen_error_spec.rb b/spec/ruby/core/exception/frozen_error_spec.rb index 979ec2ff98..a28f524b54 100644 --- a/spec/ruby/core/exception/frozen_error_spec.rb +++ b/spec/ruby/core/exception/frozen_error_spec.rb @@ -3,7 +3,7 @@ require_relative '../../spec_helper' describe "FrozenError.new" do it "should take optional receiver argument" do o = Object.new - FrozenError.new("msg", receiver: o).receiver.should equal(o) + FrozenError.new("msg", receiver: o).receiver.should.equal?(o) end end @@ -13,14 +13,30 @@ describe "FrozenError#receiver" do begin def o.x; end rescue => e - e.should be_kind_of(FrozenError) - e.receiver.should equal(o) + e.should.is_a?(FrozenError) + e.receiver.should.equal?(o) else raise end end end +describe "FrozenError#message" do + it "includes a receiver" do + object = Object.new + object.freeze + + msg_class = ruby_version_is("4.0") ? "Object" : "object" + + -> { + def object.x; end + }.should.raise(FrozenError, "can't modify frozen #{msg_class}: #{object}") + + object = [].freeze + -> { object << nil }.should.raise(FrozenError, "can't modify frozen Array: []") + end +end + describe "Modifying a frozen object" do context "#inspect is redefined and modifies the object" do it "returns ... instead of String representation of object" do @@ -32,7 +48,7 @@ describe "Modifying a frozen object" do # CRuby's message contains multiple whitespaces before '...'. # So handle both multiple and single whitespace. - -> { object.modify }.should raise_error(FrozenError, /can't modify frozen .*?: \s*.../) + -> { object.modify }.should.raise(FrozenError, /can't modify frozen .*?: \s*.../) end end end diff --git a/spec/ruby/core/exception/full_message_spec.rb b/spec/ruby/core/exception/full_message_spec.rb index d752083db2..5a5e0a2b3a 100644 --- a/spec/ruby/core/exception/full_message_spec.rb +++ b/spec/ruby/core/exception/full_message_spec.rb @@ -6,10 +6,10 @@ describe "Exception#full_message" do e.set_backtrace(["a.rb:1", "b.rb:2"]) full_message = e.full_message - full_message.should include "RuntimeError" - full_message.should include "Some runtime error" - full_message.should include "a.rb:1" - full_message.should include "b.rb:2" + full_message.should.include? "RuntimeError" + full_message.should.include? "Some runtime error" + full_message.should.include? "a.rb:1" + full_message.should.include? "b.rb:2" end it "supports :highlight option and adds escape sequences to highlight some strings" do @@ -141,8 +141,8 @@ describe "Exception#full_message" do exception = e end - exception.full_message.should include "main exception" - exception.full_message.should include "the cause" + exception.full_message.should.include? "main exception" + exception.full_message.should.include? "the cause" end it 'contains all the chain of exceptions' do @@ -160,57 +160,67 @@ describe "Exception#full_message" do exception = e end - exception.full_message.should include "last exception" - exception.full_message.should include "intermediate exception" - exception.full_message.should include "origin exception" + exception.full_message.should.include? "last exception" + exception.full_message.should.include? "intermediate exception" + exception.full_message.should.include? "origin exception" end - ruby_version_is "3.2" do - it "relies on #detailed_message" do - e = RuntimeError.new("new error") - e.define_singleton_method(:detailed_message) { |**| "DETAILED MESSAGE" } + it "relies on #detailed_message" do + e = RuntimeError.new("new error") + e.define_singleton_method(:detailed_message) { |**| "DETAILED MESSAGE" } - e.full_message.lines.first.should =~ /DETAILED MESSAGE/ + e.full_message.lines.first.should =~ /DETAILED MESSAGE/ + end + + it "passes all its own keyword arguments (with :highlight default value and without :order default value) to #detailed_message" do + e = RuntimeError.new("new error") + options_passed = nil + e.define_singleton_method(:detailed_message) do |**options| + options_passed = options + "DETAILED MESSAGE" end - it "passes all its own keyword arguments (with :highlight default value and without :order default value) to #detailed_message" do - e = RuntimeError.new("new error") - options_passed = nil - e.define_singleton_method(:detailed_message) do |**options| - options_passed = options - "DETAILED MESSAGE" - end + e.full_message(foo: "bar") + options_passed.should == { foo: "bar", highlight: Exception.to_tty? } + end - e.full_message(foo: "bar") - options_passed.should == { foo: "bar", highlight: Exception.to_tty? } - end + it "converts #detailed_message returned value to String if it isn't a String" do + message = Object.new + def message.to_str; "DETAILED MESSAGE"; end - it "converts #detailed_message returned value to String if it isn't a String" do - message = Object.new - def message.to_str; "DETAILED MESSAGE"; end + e = RuntimeError.new("new error") + e.define_singleton_method(:detailed_message) { |**| message } - e = RuntimeError.new("new error") - e.define_singleton_method(:detailed_message) { |**| message } + e.full_message.lines.first.should =~ /DETAILED MESSAGE/ + end - e.full_message.lines.first.should =~ /DETAILED MESSAGE/ - end + it "uses class name if #detailed_message returns nil" do + e = RuntimeError.new("new error") + e.define_singleton_method(:detailed_message) { |**| nil } - it "uses class name if #detailed_message returns nil" do - e = RuntimeError.new("new error") - e.define_singleton_method(:detailed_message) { |**| nil } + e.full_message(highlight: false).lines.first.should =~ /RuntimeError/ + e.full_message(highlight: true).lines.first.should =~ /#{Regexp.escape("\e[1;4mRuntimeError\e[m")}/ + end - e.full_message(highlight: false).lines.first.should =~ /RuntimeError/ - e.full_message(highlight: true).lines.first.should =~ /#{Regexp.escape("\e[1;4mRuntimeError\e[m")}/ + it "uses class name if exception object doesn't respond to #detailed_message" do + e = RuntimeError.new("new error") + class << e + undef :detailed_message end - it "uses class name if exception object doesn't respond to #detailed_message" do - e = RuntimeError.new("new error") - class << e - undef :detailed_message - end + e.full_message(highlight: false).lines.first.should =~ /RuntimeError/ + e.full_message(highlight: true).lines.first.should =~ /#{Regexp.escape("\e[1;4mRuntimeError\e[m")}/ + end - e.full_message(highlight: false).lines.first.should =~ /RuntimeError/ - e.full_message(highlight: true).lines.first.should =~ /#{Regexp.escape("\e[1;4mRuntimeError\e[m")}/ + it "allows cause with empty backtrace" do + begin + raise RuntimeError.new("Some runtime error"), cause: RuntimeError.new("Some other runtime error") + rescue => e end + + full_message = e.full_message + full_message.should.include? "RuntimeError" + full_message.should.include? "Some runtime error" + full_message.should.include? "Some other runtime error" end end diff --git a/spec/ruby/core/exception/io_error_spec.rb b/spec/ruby/core/exception/io_error_spec.rb index ab8a72518f..940d5be876 100644 --- a/spec/ruby/core/exception/io_error_spec.rb +++ b/spec/ruby/core/exception/io_error_spec.rb @@ -3,14 +3,14 @@ require_relative '../../spec_helper' describe "IO::EAGAINWaitReadable" do it "combines Errno::EAGAIN and IO::WaitReadable" do IO::EAGAINWaitReadable.superclass.should == Errno::EAGAIN - IO::EAGAINWaitReadable.ancestors.should include IO::WaitReadable + IO::EAGAINWaitReadable.ancestors.should.include? IO::WaitReadable end it "is the same as IO::EWOULDBLOCKWaitReadable if Errno::EAGAIN is the same as Errno::EWOULDBLOCK" do if Errno::EAGAIN.equal? Errno::EWOULDBLOCK - IO::EAGAINWaitReadable.should equal IO::EWOULDBLOCKWaitReadable + IO::EAGAINWaitReadable.should.equal? IO::EWOULDBLOCKWaitReadable else - IO::EAGAINWaitReadable.should_not equal IO::EWOULDBLOCKWaitReadable + IO::EAGAINWaitReadable.should_not.equal? IO::EWOULDBLOCKWaitReadable end end end @@ -18,21 +18,21 @@ end describe "IO::EWOULDBLOCKWaitReadable" do it "combines Errno::EWOULDBLOCK and IO::WaitReadable" do IO::EWOULDBLOCKWaitReadable.superclass.should == Errno::EWOULDBLOCK - IO::EAGAINWaitReadable.ancestors.should include IO::WaitReadable + IO::EAGAINWaitReadable.ancestors.should.include? IO::WaitReadable end end describe "IO::EAGAINWaitWritable" do it "combines Errno::EAGAIN and IO::WaitWritable" do IO::EAGAINWaitWritable.superclass.should == Errno::EAGAIN - IO::EAGAINWaitWritable.ancestors.should include IO::WaitWritable + IO::EAGAINWaitWritable.ancestors.should.include? IO::WaitWritable end it "is the same as IO::EWOULDBLOCKWaitWritable if Errno::EAGAIN is the same as Errno::EWOULDBLOCK" do if Errno::EAGAIN.equal? Errno::EWOULDBLOCK - IO::EAGAINWaitWritable.should equal IO::EWOULDBLOCKWaitWritable + IO::EAGAINWaitWritable.should.equal? IO::EWOULDBLOCKWaitWritable else - IO::EAGAINWaitWritable.should_not equal IO::EWOULDBLOCKWaitWritable + IO::EAGAINWaitWritable.should_not.equal? IO::EWOULDBLOCKWaitWritable end end end @@ -40,6 +40,6 @@ end describe "IO::EWOULDBLOCKWaitWritable" do it "combines Errno::EWOULDBLOCK and IO::WaitWritable" do IO::EWOULDBLOCKWaitWritable.superclass.should == Errno::EWOULDBLOCK - IO::EAGAINWaitWritable.ancestors.should include IO::WaitWritable + IO::EAGAINWaitWritable.ancestors.should.include? IO::WaitWritable end end diff --git a/spec/ruby/core/exception/name_spec.rb b/spec/ruby/core/exception/name_spec.rb index c8a49b40e2..6e0e99d194 100644 --- a/spec/ruby/core/exception/name_spec.rb +++ b/spec/ruby/core/exception/name_spec.rb @@ -4,25 +4,25 @@ describe "NameError#name" do it "returns a method name as a symbol" do -> { doesnt_exist - }.should raise_error(NameError) {|e| e.name.should == :doesnt_exist } + }.should.raise(NameError) {|e| e.name.should == :doesnt_exist } end it "returns a constant name as a symbol" do -> { DoesntExist - }.should raise_error(NameError) {|e| e.name.should == :DoesntExist } + }.should.raise(NameError) {|e| e.name.should == :DoesntExist } end it "returns a constant name without namespace as a symbol" do -> { Object::DoesntExist - }.should raise_error(NameError) {|e| e.name.should == :DoesntExist } + }.should.raise(NameError) {|e| e.name.should == :DoesntExist } end it "returns a class variable name as a symbol" do -> { eval("class singleton_class::A; @@doesnt_exist end", binding, __FILE__, __LINE__) - }.should raise_error(NameError) { |e| e.name.should == :@@doesnt_exist } + }.should.raise(NameError) { |e| e.name.should == :@@doesnt_exist } end it "returns the first argument passed to the method when a NameError is raised from #instance_variable_get" do @@ -30,7 +30,7 @@ describe "NameError#name" do -> { Object.new.instance_variable_get(invalid_ivar_name) - }.should raise_error(NameError) {|e| e.name.should equal(invalid_ivar_name) } + }.should.raise(NameError) {|e| e.name.should.equal?(invalid_ivar_name) } end it "returns the first argument passed to the method when a NameError is raised from #class_variable_get" do @@ -38,6 +38,6 @@ describe "NameError#name" do -> { Object.class_variable_get(invalid_cvar_name) - }.should raise_error(NameError) {|e| e.name.should equal(invalid_cvar_name) } + }.should.raise(NameError) {|e| e.name.should.equal?(invalid_cvar_name) } end end diff --git a/spec/ruby/core/exception/no_method_error_spec.rb b/spec/ruby/core/exception/no_method_error_spec.rb index 772c569f67..9f92104082 100644 --- a/spec/ruby/core/exception/no_method_error_spec.rb +++ b/spec/ruby/core/exception/no_method_error_spec.rb @@ -35,7 +35,7 @@ describe "NoMethodError#args" do NoMethodErrorSpecs::NoMethodErrorB.new.foo(1,a) rescue Exception => e e.args.should == [1,a] - e.args[1].should equal a + e.args[1].should.equal? a end end end @@ -45,7 +45,7 @@ describe "NoMethodError#message" do begin NoMethodErrorSpecs::NoMethodErrorD.new.foo rescue Exception => e - e.should be_kind_of(NoMethodError) + e.should.is_a?(NoMethodError) end end @@ -53,7 +53,7 @@ describe "NoMethodError#message" do begin NoMethodErrorSpecs::NoMethodErrorC.new.a_protected_method rescue Exception => e - e.should be_kind_of(NoMethodError) + e.should.is_a?(NoMethodError) end end @@ -61,209 +61,150 @@ describe "NoMethodError#message" do begin NoMethodErrorSpecs::NoMethodErrorC.new.a_private_method rescue Exception => e - e.should be_kind_of(NoMethodError) + e.should.is_a?(NoMethodError) e.message.lines[0].should =~ /private method [`']a_private_method' called for / end end - ruby_version_is ""..."3.3" do - it "calls #inspect when calling Exception#message" do - ScratchPad.record [] - test_class = Class.new do - def inspect - ScratchPad << :inspect_called - "<inspect>" - end - end - instance = test_class.new - - begin - instance.bar - rescue NoMethodError => error - error.message.should =~ /\Aundefined method [`']bar' for <inspect>:#<Class:0x\h+>$/ - ScratchPad.recorded.should == [:inspect_called] - end - end - - it "fallbacks to a simpler representation of the receiver when receiver.inspect raises an exception" do - test_class = Class.new do - def inspect - raise NoMethodErrorSpecs::InstanceException - end - end - instance = test_class.new - - begin - instance.bar - rescue NoMethodError => error - message = error.message - message.should =~ /undefined method.+\bbar\b/ - message.should include test_class.inspect - end - end - - it "uses #name to display the receiver if it is a class" do - klass = Class.new { def self.name; "MyClass"; end } - - begin - klass.foo - rescue NoMethodError => error - error.message.should =~ /\Aundefined method [`']foo' for MyClass:Class$/ - end + it "uses a literal name when receiver is nil" do + begin + nil.foo + rescue NoMethodError => error + error.message.should =~ /\Aundefined method [`']foo' for nil\Z/ end + end - it "uses #name to display the receiver if it is a module" do - mod = Module.new { def self.name; "MyModule"; end } - - begin - mod.foo - rescue NoMethodError => error - error.message.should =~ /\Aundefined method [`']foo' for MyModule:Module$/ - end + it "uses a literal name when receiver is true" do + begin + true.foo + rescue NoMethodError => error + error.message.should =~ /\Aundefined method [`']foo' for true\Z/ end end - ruby_version_is "3.3" do - it "uses a literal name when receiver is nil" do - begin - nil.foo - rescue NoMethodError => error - error.message.should =~ /\Aundefined method [`']foo' for nil\Z/ - end + it "uses a literal name when receiver is false" do + begin + false.foo + rescue NoMethodError => error + error.message.should =~ /\Aundefined method [`']foo' for false\Z/ end + end - it "uses a literal name when receiver is true" do - begin - true.foo - rescue NoMethodError => error - error.message.should =~ /\Aundefined method [`']foo' for true\Z/ - end - end + it "uses #name when receiver is a class" do + klass = Class.new { def self.name; "MyClass"; end } - it "uses a literal name when receiver is false" do - begin - false.foo - rescue NoMethodError => error - error.message.should =~ /\Aundefined method [`']foo' for false\Z/ - end + begin + klass.foo + rescue NoMethodError => error + error.message.should =~ /\Aundefined method [`']foo' for class MyClass\Z/ end + end - it "uses #name when receiver is a class" do - klass = Class.new { def self.name; "MyClass"; end } + it "uses class' string representation when receiver is an anonymous class" do + klass = Class.new - begin - klass.foo - rescue NoMethodError => error - error.message.should =~ /\Aundefined method [`']foo' for class MyClass\Z/ - end + begin + klass.foo + rescue NoMethodError => error + error.message.should =~ /\Aundefined method [`']foo' for class #<Class:0x\h+>\Z/ end + end - it "uses class' string representation when receiver is an anonymous class" do - klass = Class.new + it "uses class' string representation when receiver is a singleton class" do + obj = Object.new + singleton_class = obj.singleton_class - begin - klass.foo - rescue NoMethodError => error - error.message.should =~ /\Aundefined method [`']foo' for class #<Class:0x\h+>\Z/ - end + begin + singleton_class.foo + rescue NoMethodError => error + error.message.should =~ /\Aundefined method [`']foo' for class #<Class:#<Object:0x\h+>>\Z/ end + end - it "uses class' string representation when receiver is a singleton class" do - obj = Object.new - singleton_class = obj.singleton_class + it "uses #name when receiver is a module" do + mod = Module.new { def self.name; "MyModule"; end } - begin - singleton_class.foo - rescue NoMethodError => error - error.message.should =~ /\Aundefined method [`']foo' for class #<Class:#<Object:0x\h+>>\Z/ - end + begin + mod.foo + rescue NoMethodError => error + error.message.should =~ /\Aundefined method [`']foo' for module MyModule\Z/ end + end - it "uses #name when receiver is a module" do - mod = Module.new { def self.name; "MyModule"; end } + it "uses module's string representation when receiver is an anonymous module" do + m = Module.new - begin - mod.foo - rescue NoMethodError => error - error.message.should =~ /\Aundefined method [`']foo' for module MyModule\Z/ - end + begin + m.foo + rescue NoMethodError => error + error.message.should =~ /\Aundefined method [`']foo' for module #<Module:0x\h+>\Z/ end + end - it "uses module's string representation when receiver is an anonymous module" do - m = Module.new + it "uses class #name when receiver is an ordinary object" do + klass = Class.new { def self.name; "MyClass"; end } + instance = klass.new - begin - m.foo - rescue NoMethodError => error - error.message.should =~ /\Aundefined method [`']foo' for module #<Module:0x\h+>\Z/ - end + begin + instance.bar + rescue NoMethodError => error + error.message.should =~ /\Aundefined method [`']bar' for an instance of MyClass\Z/ end + end - it "uses class #name when receiver is an ordinary object" do - klass = Class.new { def self.name; "MyClass"; end } - instance = klass.new + it "uses class string representation when receiver is an instance of anonymous class" do + klass = Class.new + instance = klass.new - begin - instance.bar - rescue NoMethodError => error - error.message.should =~ /\Aundefined method [`']bar' for an instance of MyClass\Z/ - end + begin + instance.bar + rescue NoMethodError => error + error.message.should =~ /\Aundefined method [`']bar' for an instance of #<Class:0x\h+>\Z/ end + end - it "uses class string representation when receiver is an instance of anonymous class" do - klass = Class.new - instance = klass.new + it "uses class name when receiver has a singleton class" do + instance = NoMethodErrorSpecs::NoMethodErrorA.new + def instance.foo; end - begin - instance.bar - rescue NoMethodError => error - error.message.should =~ /\Aundefined method [`']bar' for an instance of #<Class:0x\h+>\Z/ - end + begin + instance.bar + rescue NoMethodError => error + error.message.should =~ /\Aundefined method [`']bar' for #<NoMethodErrorSpecs::NoMethodErrorA:0x\h+>\Z/ end + end - it "uses class name when receiver has a singleton class" do - instance = NoMethodErrorSpecs::NoMethodErrorA.new - def instance.foo; end - - begin - instance.bar - rescue NoMethodError => error - error.message.should =~ /\Aundefined method [`']bar' for #<NoMethodErrorSpecs::NoMethodErrorA:0x\h+>\Z/ + it "does not call #inspect when calling Exception#message" do + ScratchPad.record [] + test_class = Class.new do + def inspect + ScratchPad << :inspect_called + "<inspect>" end end + instance = test_class.new - it "does not call #inspect when calling Exception#message" do - ScratchPad.record [] - test_class = Class.new do - def inspect - ScratchPad << :inspect_called - "<inspect>" - end - end - instance = test_class.new - - begin - instance.bar - rescue NoMethodError => error - error.message.should =~ /\Aundefined method [`']bar' for an instance of #<Class:0x\h+>\Z/ - ScratchPad.recorded.should == [] - end + begin + instance.bar + rescue NoMethodError => error + error.message.should =~ /\Aundefined method [`']bar' for an instance of #<Class:0x\h+>\Z/ + ScratchPad.recorded.should == [] end + end - it "does not truncate long class names" do - class_name = 'ExceptionSpecs::A' + 'a'*100 + it "does not truncate long class names" do + class_name = 'ExceptionSpecs::A' + 'a'*100 - begin - eval <<~RUBY - class #{class_name} - end + begin + eval <<~RUBY + class #{class_name} + end - obj = #{class_name}.new - obj.foo - RUBY - rescue NoMethodError => error - error.message.should =~ /\Aundefined method [`']foo' for an instance of #{class_name}\Z/ - end + obj = #{class_name}.new + obj.foo + RUBY + rescue NoMethodError => error + error.message.should =~ /\Aundefined method [`']foo' for an instance of #{class_name}\Z/ end end end diff --git a/spec/ruby/core/exception/reason_spec.rb b/spec/ruby/core/exception/reason_spec.rb index 210bbc9725..d7022768b6 100644 --- a/spec/ruby/core/exception/reason_spec.rb +++ b/spec/ruby/core/exception/reason_spec.rb @@ -6,7 +6,7 @@ describe "LocalJumpError#reason" do end it "returns 'return' for a return" do - -> { get_me_a_return.call }.should raise_error(LocalJumpError) { |e| + -> { get_me_a_return.call }.should.raise(LocalJumpError) { |e| e.reason.should == :return } end diff --git a/spec/ruby/core/exception/receiver_spec.rb b/spec/ruby/core/exception/receiver_spec.rb index d1c23b67be..6ecf640ad8 100644 --- a/spec/ruby/core/exception/receiver_spec.rb +++ b/spec/ruby/core/exception/receiver_spec.rb @@ -11,31 +11,31 @@ describe "NameError#receiver" do -> { receiver.doesnt_exist - }.should raise_error(NameError) {|e| e.receiver.should equal(receiver) } + }.should.raise(NameError) {|e| e.receiver.should.equal?(receiver) } end it "returns the Object class when an undefined constant is called without namespace" do -> { DoesntExist - }.should raise_error(NameError) {|e| e.receiver.should equal(Object) } + }.should.raise(NameError) {|e| e.receiver.should.equal?(Object) } end it "returns a class when an undefined constant is called" do -> { NameErrorSpecs::ReceiverClass::DoesntExist - }.should raise_error(NameError) {|e| e.receiver.should equal(NameErrorSpecs::ReceiverClass) } + }.should.raise(NameError) {|e| e.receiver.should.equal?(NameErrorSpecs::ReceiverClass) } end it "returns the Object class when an undefined class variable is called" do -> { eval("class singleton_class::A; @@doesnt_exist end", binding, __FILE__, __LINE__) - }.should raise_error(NameError) {|e| e.receiver.should equal(singleton_class::A) } + }.should.raise(NameError) {|e| e.receiver.should.equal?(singleton_class::A) } end it "returns a class when an undefined class variable is called in a subclass' namespace" do -> { NameErrorSpecs::ReceiverClass.new.call_undefined_class_variable - }.should raise_error(NameError) {|e| e.receiver.should equal(NameErrorSpecs::ReceiverClass) } + }.should.raise(NameError) {|e| e.receiver.should.equal?(NameErrorSpecs::ReceiverClass) } end it "returns the receiver when raised from #instance_variable_get" do @@ -43,16 +43,16 @@ describe "NameError#receiver" do -> { receiver.instance_variable_get("invalid_ivar_name") - }.should raise_error(NameError) {|e| e.receiver.should equal(receiver) } + }.should.raise(NameError) {|e| e.receiver.should.equal?(receiver) } end it "returns the receiver when raised from #class_variable_get" do -> { Object.class_variable_get("invalid_cvar_name") - }.should raise_error(NameError) {|e| e.receiver.should equal(Object) } + }.should.raise(NameError) {|e| e.receiver.should.equal?(Object) } end it "raises an ArgumentError when the receiver is none" do - -> { NameError.new.receiver }.should raise_error(ArgumentError) + -> { NameError.new.receiver }.should.raise(ArgumentError) end end diff --git a/spec/ruby/core/exception/result_spec.rb b/spec/ruby/core/exception/result_spec.rb index d42fcdffcb..451ff43af5 100644 --- a/spec/ruby/core/exception/result_spec.rb +++ b/spec/ruby/core/exception/result_spec.rb @@ -14,8 +14,8 @@ describe "StopIteration#result" do it "returns the method-returned-object from an Enumerator" do @enum.next @enum.next - -> { @enum.next }.should raise_error(StopIteration) { |error| - error.result.should equal(:method_returned) + -> { @enum.next }.should.raise(StopIteration) { |error| + error.result.should.equal?(:method_returned) } end end diff --git a/spec/ruby/core/exception/set_backtrace_spec.rb b/spec/ruby/core/exception/set_backtrace_spec.rb index 12c1da919c..2cd93326ec 100644 --- a/spec/ruby/core/exception/set_backtrace_spec.rb +++ b/spec/ruby/core/exception/set_backtrace_spec.rb @@ -1,13 +1,8 @@ require_relative '../../spec_helper' require_relative 'fixtures/common' +require_relative 'shared/set_backtrace' describe "Exception#set_backtrace" do - it "accepts an Array of Strings" do - err = RuntimeError.new - err.set_backtrace ["unhappy"] - err.backtrace.should == ["unhappy"] - end - it "allows the user to set the backtrace from a rescued exception" do bt = ExceptionSpecs::Backtrace.backtrace err = RuntimeError.new @@ -20,65 +15,9 @@ describe "Exception#set_backtrace" do err.backtrace_locations.should == nil end - ruby_version_is "3.4" do - it "allows the user to set backtrace locations from a rescued exception" do - bt_locations = ExceptionSpecs::Backtrace.backtrace_locations - err = RuntimeError.new - err.backtrace.should == nil - err.backtrace_locations.should == nil - - err.set_backtrace bt_locations - - err.backtrace_locations.size.should == bt_locations.size - err.backtrace_locations.each_with_index do |loc, index| - other_loc = bt_locations[index] - - loc.path.should == other_loc.path - loc.label.should == other_loc.label - loc.base_label.should == other_loc.base_label - loc.lineno.should == other_loc.lineno - loc.absolute_path.should == other_loc.absolute_path - loc.to_s.should == other_loc.to_s - end - err.backtrace.size.should == err.backtrace_locations.size - end - end - - it "accepts an empty Array" do - err = RuntimeError.new - err.set_backtrace [] - err.backtrace.should == [] - end - - it "accepts a String" do + it_behaves_like :exception_set_backtrace, -> backtrace { err = RuntimeError.new - err.set_backtrace "unhappy" - err.backtrace.should == ["unhappy"] - end - - it "accepts nil" do - err = RuntimeError.new - err.set_backtrace nil - err.backtrace.should be_nil - end - - it "raises a TypeError when passed a Symbol" do - err = RuntimeError.new - -> { err.set_backtrace :unhappy }.should raise_error(TypeError) - end - - it "raises a TypeError when the Array contains a Symbol" do - err = RuntimeError.new - -> { err.set_backtrace ["String", :unhappy] }.should raise_error(TypeError) - end - - it "raises a TypeError when the array contains nil" do - err = Exception.new - -> { err.set_backtrace ["String", nil] }.should raise_error(TypeError) - end - - it "raises a TypeError when the argument is a nested array" do - err = Exception.new - -> { err.set_backtrace ["String", ["String"]] }.should raise_error(TypeError) - end + err.set_backtrace(backtrace) + err + } end diff --git a/spec/ruby/core/exception/shared/new.rb b/spec/ruby/core/exception/shared/new.rb index bcde8ee4b2..048fd14dd2 100644 --- a/spec/ruby/core/exception/shared/new.rb +++ b/spec/ruby/core/exception/shared/new.rb @@ -1,6 +1,6 @@ describe :exception_new, shared: true do it "creates a new instance of Exception" do - Exception.should be_ancestor_of(Exception.send(@method).class) + Exception.send(@method).class.ancestors.should.include?(Exception) end it "sets the message of the Exception when passes a message" do @@ -12,7 +12,7 @@ describe :exception_new, shared: true do end it "returns the exception when it has a custom constructor" do - ExceptionSpecs::ConstructorException.send(@method).should be_kind_of(ExceptionSpecs::ConstructorException) + ExceptionSpecs::ConstructorException.send(@method).should.is_a?(ExceptionSpecs::ConstructorException) end end diff --git a/spec/ruby/core/exception/shared/set_backtrace.rb b/spec/ruby/core/exception/shared/set_backtrace.rb new file mode 100644 index 0000000000..934bf3dc5f --- /dev/null +++ b/spec/ruby/core/exception/shared/set_backtrace.rb @@ -0,0 +1,64 @@ +require_relative '../fixtures/common' + +describe :exception_set_backtrace, shared: true do + it "accepts an Array of Strings" do + err = @method.call(["unhappy"]) + err.backtrace.should == ["unhappy"] + end + + it "allows the user to set the backtrace from a rescued exception" do + bt = ExceptionSpecs::Backtrace.backtrace + err = @method.call(bt) + err.backtrace.should == bt + end + + ruby_version_is "3.4" do + it "allows the user to set backtrace locations from a rescued exception" do + bt_locations = ExceptionSpecs::Backtrace.backtrace_locations + err = @method.call(bt_locations) + err.backtrace_locations.size.should == bt_locations.size + err.backtrace_locations.each_with_index do |loc, index| + other_loc = bt_locations[index] + + loc.path.should == other_loc.path + loc.label.should == other_loc.label + loc.base_label.should == other_loc.base_label + loc.lineno.should == other_loc.lineno + loc.absolute_path.should == other_loc.absolute_path + loc.to_s.should == other_loc.to_s + end + err.backtrace.size.should == err.backtrace_locations.size + end + end + + it "accepts an empty Array" do + err = @method.call([]) + err.backtrace.should == [] + end + + it "accepts a String" do + err = @method.call("unhappy") + err.backtrace.should == ["unhappy"] + end + + it "accepts nil" do + err = @method.call(nil) + err.backtrace.should == nil + end + + it "raises a TypeError when passed a Symbol" do + -> { @method.call(:unhappy) }.should.raise(TypeError) + end + + it "raises a TypeError when the Array contains a Symbol" do + -> { @method.call(["String", :unhappy]) }.should.raise(TypeError) + end + + it "raises a TypeError when the array contains nil" do + -> { @method.call(["String", nil]) }.should.raise(TypeError) + end + + it "raises a TypeError when the argument is a nested array" do + -> { @method.call(["String", ["String"]]) }.should.raise(TypeError) + end +end diff --git a/spec/ruby/core/exception/signal_exception_spec.rb b/spec/ruby/core/exception/signal_exception_spec.rb index 1a0940743f..010181bc55 100644 --- a/spec/ruby/core/exception/signal_exception_spec.rb +++ b/spec/ruby/core/exception/signal_exception_spec.rb @@ -9,7 +9,7 @@ describe "SignalException.new" do end it "raises an exception with an invalid signal number" do - -> { SignalException.new(100000) }.should raise_error(ArgumentError) + -> { SignalException.new(100000) }.should.raise(ArgumentError) end it "takes a signal name without SIG prefix as the first argument" do @@ -27,11 +27,11 @@ describe "SignalException.new" do end it "raises an exception with an invalid signal name" do - -> { SignalException.new("NONEXISTENT") }.should raise_error(ArgumentError) + -> { SignalException.new("NONEXISTENT") }.should.raise(ArgumentError) end it "raises an exception with an invalid first argument type" do - -> { SignalException.new(Object.new) }.should raise_error(ArgumentError) + -> { SignalException.new(Object.new) }.should.raise(ArgumentError) end it "takes a signal symbol without SIG prefix as the first argument" do @@ -49,7 +49,7 @@ describe "SignalException.new" do end it "raises an exception with an invalid signal name" do - -> { SignalException.new(:NONEXISTENT) }.should raise_error(ArgumentError) + -> { SignalException.new(:NONEXISTENT) }.should.raise(ArgumentError) end it "takes an optional message argument with a signal number" do @@ -60,7 +60,7 @@ describe "SignalException.new" do end it "raises an exception for an optional argument with a signal name" do - -> { SignalException.new("INT","name") }.should raise_error(ArgumentError) + -> { SignalException.new("INT","name") }.should.raise(ArgumentError) end end diff --git a/spec/ruby/core/exception/signm_spec.rb b/spec/ruby/core/exception/signm_spec.rb index 4adff3b5ee..cabcc7ad58 100644 --- a/spec/ruby/core/exception/signm_spec.rb +++ b/spec/ruby/core/exception/signm_spec.rb @@ -2,7 +2,7 @@ require_relative '../../spec_helper' describe "SignalException#signm" do it "returns the signal name" do - -> { Process.kill(:TERM, Process.pid) }.should raise_error(SignalException) { |e| + -> { Process.kill(:TERM, Process.pid) }.should.raise(SignalException) { |e| e.signm.should == 'SIGTERM' } end diff --git a/spec/ruby/core/exception/signo_spec.rb b/spec/ruby/core/exception/signo_spec.rb index 62fc321516..46e79a8daf 100644 --- a/spec/ruby/core/exception/signo_spec.rb +++ b/spec/ruby/core/exception/signo_spec.rb @@ -2,7 +2,7 @@ require_relative '../../spec_helper' describe "SignalException#signo" do it "returns the signal number" do - -> { Process.kill(:TERM, Process.pid) }.should raise_error(SignalException) { |e| + -> { Process.kill(:TERM, Process.pid) }.should.raise(SignalException) { |e| e.signo.should == Signal.list['TERM'] } end diff --git a/spec/ruby/core/exception/standard_error_spec.rb b/spec/ruby/core/exception/standard_error_spec.rb index 17e98ce7f0..b05d247f67 100644 --- a/spec/ruby/core/exception/standard_error_spec.rb +++ b/spec/ruby/core/exception/standard_error_spec.rb @@ -18,6 +18,6 @@ describe "StandardError" do end it "does not rescue superclass of StandardError" do - -> { begin; raise Exception; rescue; end }.should raise_error(Exception) + -> { begin; raise Exception; rescue; end }.should.raise(Exception) end end diff --git a/spec/ruby/core/exception/status_spec.rb b/spec/ruby/core/exception/status_spec.rb index 8ace00fe10..7369b0815d 100644 --- a/spec/ruby/core/exception/status_spec.rb +++ b/spec/ruby/core/exception/status_spec.rb @@ -2,7 +2,7 @@ require_relative '../../spec_helper' describe "SystemExit#status" do it "returns the exit status" do - -> { exit 42 }.should raise_error(SystemExit) { |e| + -> { exit 42 }.should.raise(SystemExit) { |e| e.status.should == 42 } end diff --git a/spec/ruby/core/exception/success_spec.rb b/spec/ruby/core/exception/success_spec.rb index 6f21743340..5ab8f94454 100644 --- a/spec/ruby/core/exception/success_spec.rb +++ b/spec/ruby/core/exception/success_spec.rb @@ -2,13 +2,13 @@ require_relative '../../spec_helper' describe "SystemExit#success?" do it "returns true if the process exited successfully" do - -> { exit 0 }.should raise_error(SystemExit) { |e| + -> { exit 0 }.should.raise(SystemExit) { |e| e.should.success? } end it "returns false if the process exited unsuccessfully" do - -> { exit(-1) }.should raise_error(SystemExit) { |e| + -> { exit(-1) }.should.raise(SystemExit) { |e| e.should_not.success? } end diff --git a/spec/ruby/core/exception/syntax_error_spec.rb b/spec/ruby/core/exception/syntax_error_spec.rb index 6cc8522de3..66eb5649aa 100644 --- a/spec/ruby/core/exception/syntax_error_spec.rb +++ b/spec/ruby/core/exception/syntax_error_spec.rb @@ -1,27 +1,25 @@ require_relative '../../spec_helper' -ruby_version_is "3.2" do - describe "SyntaxError#path" do - it "returns the file path provided to eval" do - filename = "speccing.rb" +describe "SyntaxError#path" do + it "returns the file path provided to eval" do + filename = "speccing.rb" - -> { - eval("if true", TOPLEVEL_BINDING, filename) - }.should raise_error(SyntaxError) { |e| - e.path.should == filename - } - end + -> { + eval("if true", TOPLEVEL_BINDING, filename) + }.should.raise(SyntaxError) { |e| + e.path.should == filename + } + end - it "returns the file path that raised an exception" do - expected_path = fixture(__FILE__, "syntax_error.rb") + it "returns the file path that raised an exception" do + expected_path = fixture(__FILE__, "syntax_error.rb") - -> { - require_relative "fixtures/syntax_error" - }.should raise_error(SyntaxError) { |e| e.path.should == expected_path } - end + -> { + require_relative "fixtures/syntax_error" + }.should.raise(SyntaxError) { |e| e.path.should == expected_path } + end - it "returns nil when constructed directly" do - SyntaxError.new.path.should == nil - end + it "returns nil when constructed directly" do + SyntaxError.new.path.should == nil end end diff --git a/spec/ruby/core/exception/system_call_error_spec.rb b/spec/ruby/core/exception/system_call_error_spec.rb index f6995d3c5e..da01c5b6b0 100644 --- a/spec/ruby/core/exception/system_call_error_spec.rb +++ b/spec/ruby/core/exception/system_call_error_spec.rb @@ -14,8 +14,10 @@ describe "SystemCallError" do end exc = ExceptionSpecs::SCESub.new - ScratchPad.recorded.should equal(:initialize) - exc.should be_an_instance_of(ExceptionSpecs::SCESub) + ScratchPad.recorded.should.equal?(:initialize) + exc.should.instance_of?(ExceptionSpecs::SCESub) + ensure + ExceptionSpecs.send(:remove_const, :SCESub) end end @@ -25,10 +27,11 @@ describe "SystemCallError.new" do @example_errno_class = Errno::EINVAL @last_known_errno = Errno.constants.size - 1 @unknown_errno = Errno.constants.size + @some_human_readable = /[[:graph:]]+/ end it "requires at least one argument" do - -> { SystemCallError.new }.should raise_error(ArgumentError) + -> { SystemCallError.new }.should.raise(ArgumentError) end it "accepts single Integer argument as errno" do @@ -41,16 +44,16 @@ describe "SystemCallError.new" do end it "constructs a SystemCallError for an unknown error number" do - SystemCallError.new(-2**24).should be_an_instance_of(SystemCallError) - SystemCallError.new(-1).should be_an_instance_of(SystemCallError) - SystemCallError.new(@unknown_errno).should be_an_instance_of(SystemCallError) - SystemCallError.new(2**24).should be_an_instance_of(SystemCallError) + SystemCallError.new(-2**24).should.instance_of?(SystemCallError) + SystemCallError.new(-1).should.instance_of?(SystemCallError) + SystemCallError.new(@unknown_errno).should.instance_of?(SystemCallError) + SystemCallError.new(2**24).should.instance_of?(SystemCallError) end it "constructs the appropriate Errno class" do e = SystemCallError.new(@example_errno) - e.should be_kind_of(SystemCallError) - e.should be_an_instance_of(@example_errno_class) + e.should.is_a?(SystemCallError) + e.should.instance_of?(@example_errno_class) end it "sets an error message corresponding to an appropriate Errno class" do @@ -60,14 +63,14 @@ describe "SystemCallError.new" do it "accepts an optional custom message preceding the errno" do exc = SystemCallError.new("custom message", @example_errno) - exc.should be_an_instance_of(@example_errno_class) + exc.should.instance_of?(@example_errno_class) exc.errno.should == @example_errno exc.message.should == 'Invalid argument - custom message' end it "accepts an optional third argument specifying the location" do exc = SystemCallError.new("custom message", @example_errno, "location") - exc.should be_an_instance_of(@example_errno_class) + exc.should.instance_of?(@example_errno_class) exc.errno.should == @example_errno exc.message.should == 'Invalid argument @ location - custom message' end @@ -87,7 +90,7 @@ describe "SystemCallError.new" do end it "treats nil errno as unknown error value" do - SystemCallError.new(nil).should be_an_instance_of(SystemCallError) + SystemCallError.new(nil).should.instance_of?(SystemCallError) end it "treats nil custom message as if it is not passed at all" do @@ -96,23 +99,11 @@ describe "SystemCallError.new" do end it "sets an 'unknown error' message when an unknown error number" do - platform_is_not :windows do - SystemCallError.new(-1).message.should =~ /Unknown error(:)? -1/ - end - - platform_is :windows do - SystemCallError.new(-1).message.should == "The operation completed successfully." - end + SystemCallError.new(-1).message.should =~ @some_human_readable end it "adds a custom error message to an 'unknown error' message when an unknown error number and a custom message specified" do - platform_is_not :windows do - SystemCallError.new("custom message", -1).message.should =~ /Unknown error(:)? -1 - custom message/ - end - - platform_is :windows do - SystemCallError.new("custom message", -1).message.should == "The operation completed successfully. - custom message" - end + SystemCallError.new("custom message", -1).message.should =~ /#{@some_human_readable}.* - custom message/ end it "converts to Integer if errno is a Complex convertible to Integer" do @@ -120,15 +111,15 @@ describe "SystemCallError.new" do end it "raises TypeError if message is not a String" do - -> { SystemCallError.new(:foo, 1) }.should raise_error(TypeError, /no implicit conversion of Symbol into String/) + -> { SystemCallError.new(:foo, 1) }.should.raise(TypeError, /no implicit conversion of Symbol into String/) end it "raises TypeError if errno is not an Integer" do - -> { SystemCallError.new('foo', 'bar') }.should raise_error(TypeError, /no implicit conversion of String into Integer/) + -> { SystemCallError.new('foo', 'bar') }.should.raise(TypeError, /no implicit conversion of String into Integer/) end it "raises RangeError if errno is a Complex not convertible to Integer" do - -> { SystemCallError.new('foo', Complex(2.9, 1)) }.should raise_error(RangeError, /can't convert/) + -> { SystemCallError.new('foo', Complex(2.9, 1)) }.should.raise(RangeError, /can't convert/) end end @@ -149,12 +140,7 @@ end describe "SystemCallError#message" do it "returns the default message when no message is given" do - platform_is :aix do - SystemCallError.new(2**28).message.should =~ /Error .*occurred/i - end - platform_is_not :aix do - SystemCallError.new(2**28).message.should =~ /Unknown error/i - end + SystemCallError.new(2**28).message.should =~ @some_human_readable end it "returns the message given as an argument to new" do |
