Skip to content

Commit

Permalink
refactoring an error handling in IRB::Inspector
Browse files Browse the repository at this point in the history
* 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`
  • Loading branch information
no6v committed Jan 7, 2021
1 parent 29cd767 commit 9d112fa
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
16 changes: 7 additions & 9 deletions lib/irb/inspector.rb
Expand Up @@ -100,21 +100,19 @@ def init
# Proc to call when the input is evaluated and output in irb.
def inspect_value(v)
@inspect.call(v)
rescue
puts "(Object doesn't support #inspect)"
''
end
end

Inspector.def_inspector([false, :to_s, :raw]){|v| v.to_s}
Inspector.def_inspector([:p, :inspect]){|v|
begin
result = v.inspect
if IRB.conf[:MAIN_CONTEXT]&.use_colorize? && Color.inspect_colorable?(v)
result = Color.colorize_code(result)
end
result
rescue NoMethodError
puts "(Object doesn't support #inspect)"
''
result = v.inspect
if IRB.conf[:MAIN_CONTEXT]&.use_colorize? && Color.inspect_colorable?(v)
result = Color.colorize_code(result)
end
result
}
Inspector.def_inspector([true, :pp, :pretty_inspect], proc{require "irb/color_printer"}){|v|
if IRB.conf[:MAIN_CONTEXT]&.use_colorize?
Expand Down
23 changes: 15 additions & 8 deletions test/irb/test_context.rb
Expand Up @@ -106,15 +106,22 @@ def test_eval_input

def test_eval_object_without_inspect_method
verbose, $VERBOSE = $VERBOSE, nil
input = TestInputMethod.new([
"BasicObject.new\n",
])
irb = IRB::Irb.new(IRB::WorkSpace.new(Object.new), input)
out, err = capture_output do
irb.eval_input
all_assertions do |all|
IRB::Inspector::INSPECTORS.invert.each_value do |mode|
all.for(mode) do
input = TestInputMethod.new([
"[BasicObject.new, Class.new]\n",
])
irb = IRB::Irb.new(IRB::WorkSpace.new(Object.new), input)
irb.context.inspect_mode = mode
out, err = capture_output do
irb.eval_input
end
assert_empty err
assert_match(/\(Object doesn't support #inspect\)\n(=> )?\n/, out)
end
end
end
assert_empty err
assert(/\(Object doesn't support #inspect\)\n(=> )?\n/, out)
ensure
$VERBOSE = verbose
end
Expand Down

0 comments on commit 9d112fa

Please sign in to comment.