Skip to content

Commit 9d112fa

Browse files
committed
refactoring an error handling in IRB::Inspector
* moved rescue clause to `#inspect_value` to catch all failures in inspectors * test with all (currently five kind of) inspect modes - tweaked the input due to only `Marshal` can inspect(dump) a `BasicObject`
1 parent 29cd767 commit 9d112fa

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

lib/irb/inspector.rb

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,21 +100,19 @@ def init
100100
# Proc to call when the input is evaluated and output in irb.
101101
def inspect_value(v)
102102
@inspect.call(v)
103+
rescue
104+
puts "(Object doesn't support #inspect)"
105+
''
103106
end
104107
end
105108

106109
Inspector.def_inspector([false, :to_s, :raw]){|v| v.to_s}
107110
Inspector.def_inspector([:p, :inspect]){|v|
108-
begin
109-
result = v.inspect
110-
if IRB.conf[:MAIN_CONTEXT]&.use_colorize? && Color.inspect_colorable?(v)
111-
result = Color.colorize_code(result)
112-
end
113-
result
114-
rescue NoMethodError
115-
puts "(Object doesn't support #inspect)"
116-
''
111+
result = v.inspect
112+
if IRB.conf[:MAIN_CONTEXT]&.use_colorize? && Color.inspect_colorable?(v)
113+
result = Color.colorize_code(result)
117114
end
115+
result
118116
}
119117
Inspector.def_inspector([true, :pp, :pretty_inspect], proc{require "irb/color_printer"}){|v|
120118
if IRB.conf[:MAIN_CONTEXT]&.use_colorize?

test/irb/test_context.rb

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,22 @@ def test_eval_input
106106

107107
def test_eval_object_without_inspect_method
108108
verbose, $VERBOSE = $VERBOSE, nil
109-
input = TestInputMethod.new([
110-
"BasicObject.new\n",
111-
])
112-
irb = IRB::Irb.new(IRB::WorkSpace.new(Object.new), input)
113-
out, err = capture_output do
114-
irb.eval_input
109+
all_assertions do |all|
110+
IRB::Inspector::INSPECTORS.invert.each_value do |mode|
111+
all.for(mode) do
112+
input = TestInputMethod.new([
113+
"[BasicObject.new, Class.new]\n",
114+
])
115+
irb = IRB::Irb.new(IRB::WorkSpace.new(Object.new), input)
116+
irb.context.inspect_mode = mode
117+
out, err = capture_output do
118+
irb.eval_input
119+
end
120+
assert_empty err
121+
assert_match(/\(Object doesn't support #inspect\)\n(=> )?\n/, out)
122+
end
123+
end
115124
end
116-
assert_empty err
117-
assert(/\(Object doesn't support #inspect\)\n(=> )?\n/, out)
118125
ensure
119126
$VERBOSE = verbose
120127
end

0 commit comments

Comments
 (0)