Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add specs for Exception#detailed_message #1029

Merged
merged 1 commit into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions core/exception/detailed_message_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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 "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 "allows and ignores other keyword arguments" do
RuntimeError.new("new error").detailed_message(foo: true).should == "new error (RuntimeError)"
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 just class name for an instance of RuntimeError sublass with empty message" do
DetailedMessageSpec::C.new("").detailed_message.should == "DetailedMessageSpec::C"
end
andrykonchin marked this conversation as resolved.
Show resolved Hide resolved

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
end
end
4 changes: 4 additions & 0 deletions core/exception/fixtures/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,7 @@ def call_undefined_class_variable
end
end
end

module DetailedMessageSpec
C = Class.new(RuntimeError)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is C enough, or should it be more meaningful?

Copy link
Member

@andrykonchin andrykonchin May 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeap, it's OK. There are a lot of helper classes like A, B, M etc with short names for readability (I suppose)

end
21 changes: 21 additions & 0 deletions core/exception/full_message_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,25 @@
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) { |**opt| "DETAILED MESSAGE" }

e.full_message.lines.first.should =~ /DETAILED MESSAGE/
end

it "passes all its own keyword arguments to #detailed_message" do
e = RuntimeError.new("new error")
opt_ = nil
e.define_singleton_method(:detailed_message) do |**opt|
opt_ = opt
"DETAILED MESSAGE"
end

e.full_message(foo: "bar")
opt_.should == { foo: "bar", highlight: Exception.to_tty? }
end
andrykonchin marked this conversation as resolved.
Show resolved Hide resolved
end
end