1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00

Do something sensible about uninspectable things

This commit is contained in:
Conrad Irwin 2011-08-24 01:07:52 -07:00
parent d007eff35c
commit f6758bfb4e
2 changed files with 32 additions and 4 deletions

View file

@ -19,11 +19,21 @@ class Pry
# The default prints
DEFAULT_PRINT = proc do |output, value|
begin
Helpers::BaseHelpers.stagger_output("=> #{Helpers::BaseHelpers.colorize_code(value.pretty_inspect)}", output)
rescue NoMethodError
output.puts "=> unknown"
stringified = begin
value.pretty_inspect
rescue Exception => ex
nil
end
unless String === stringified
# Read the class name off of the singleton class to provide a default inspect.
klass = (class << value; self; end).ancestors.first
stringified = "#<#{klass}:0x#{value.__id__.to_s(16)}>"
warning = "output error: #{ex ? ex.inspect : ".pretty_inspect didn't return a String"}"
Helpers::BaseHelpers.stagger_output(warning, output)
end
Helpers::BaseHelpers.stagger_output("=> #{Helpers::BaseHelpers.colorize_code(stringified)}", output)
end
# Will only show the first line of the backtrace

View file

@ -27,4 +27,22 @@ describe Pry do
mock_pry("1").should =~ /output error: failed to show result/
end
end
describe "DEFAULT_PRINT" do
it "should output the right thing" do
mock_pry("{:a => 1}").should =~ /\{:a=>1\}/
end
it "should not be phased by un-inspectable things" do
mock_pry("class NastyClass; undef pretty_inspect; end", "NastyClass.new").should =~ /#<NastyClass:0x[0-9a-f]+>/
end
it "should warn you about un-inspectable things" do
mock_pry("class NastyClass; undef pretty_inspect; end", "NastyClass.new").should =~ /output error: #<(NoMethodError|NameError): undefined method `pretty_inspect'/
end
it "should warn you when you have badly behaved objects" do
mock_pry("class UnCouth; def pretty_inspect(*a); :cussing_symbol; end; end", "UnCouth.new").should =~ /output error: .pretty_inspect didn't return a String/
end
end
end