Skip to content

Commit

Permalink
Update some ruby specs for the new message format of NoMethodError
Browse files Browse the repository at this point in the history
  • Loading branch information
mame committed Feb 20, 2023
1 parent 8f868a1 commit 0e830e6
Showing 1 changed file with 40 additions and 17 deletions.
57 changes: 40 additions & 17 deletions spec/ruby/core/exception/no_method_error_spec.rb
Expand Up @@ -62,26 +62,49 @@
NoMethodErrorSpecs::NoMethodErrorC.new.a_private_method
rescue Exception => e
e.should be_kind_of(NoMethodError)
e.message.lines[0].should =~ /private method `a_private_method' called for #<NoMethodErrorSpecs::NoMethodErrorC:0x[\h]+>/
e.message.lines[0].should =~ /private method `a_private_method' called for /
end
end

it "calls receiver.inspect only when calling Exception#message" do
ScratchPad.record []
test_class = Class.new do
def inspect
ScratchPad << :inspect_called
"<inspect>"
ruby_version_is ""..."3.3" do
it "calls receiver.inspect only 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 Exception => e
e.name.should == :bar
ScratchPad.recorded.should == []
e.message.should =~ /undefined method.+\bbar\b/
ScratchPad.recorded.should == [:inspect_called]
end
end
instance = test_class.new
begin
instance.bar
rescue Exception => e
e.name.should == :bar
ScratchPad.recorded.should == []
e.message.should =~ /undefined method.+\bbar\b/
ScratchPad.recorded.should == [:inspect_called]
end

ruby_version_is "3.3" do
it "does not call receiver.inspect even 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 Exception => e
e.name.should == :bar
ScratchPad.recorded.should == []
e.message.should =~ /undefined method.+\bbar\b/
ScratchPad.recorded.should == []
end
end
end

Expand All @@ -108,14 +131,14 @@ def inspect
begin
klass.foo
rescue NoMethodError => error
error.message.lines.first.chomp.should == "undefined method `foo' for MyClass:Class"
error.message.lines.first.chomp.should =~ /^undefined method `foo' for /
end

mod = Module.new { def self.name; "MyModule"; end }
begin
mod.foo
rescue NoMethodError => error
error.message.lines.first.chomp.should == "undefined method `foo' for MyModule:Module"
error.message.lines.first.chomp.should =~ /^undefined method `foo' for /
end
end
end
Expand Down

0 comments on commit 0e830e6

Please sign in to comment.