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

[ruby/irb] 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`

9d112fab8e
This commit is contained in:
Nobuhiro IMAI 2021-01-07 19:21:06 +09:00 committed by aycabta
parent f594775230
commit ed3264d37a
2 changed files with 22 additions and 17 deletions

View file

@ -100,21 +100,19 @@ module IRB # :nodoc:
# Proc to call when the input is evaluated and output in irb. # Proc to call when the input is evaluated and output in irb.
def inspect_value(v) def inspect_value(v)
@inspect.call(v) @inspect.call(v)
rescue
puts "(Object doesn't support #inspect)"
''
end end
end end
Inspector.def_inspector([false, :to_s, :raw]){|v| v.to_s} Inspector.def_inspector([false, :to_s, :raw]){|v| v.to_s}
Inspector.def_inspector([:p, :inspect]){|v| Inspector.def_inspector([:p, :inspect]){|v|
begin result = v.inspect
result = v.inspect if IRB.conf[:MAIN_CONTEXT]&.use_colorize? && Color.inspect_colorable?(v)
if IRB.conf[:MAIN_CONTEXT]&.use_colorize? && Color.inspect_colorable?(v) result = Color.colorize_code(result)
result = Color.colorize_code(result)
end
result
rescue NoMethodError
puts "(Object doesn't support #inspect)"
''
end end
result
} }
Inspector.def_inspector([true, :pp, :pretty_inspect], proc{require "irb/color_printer"}){|v| Inspector.def_inspector([true, :pp, :pretty_inspect], proc{require "irb/color_printer"}){|v|
if IRB.conf[:MAIN_CONTEXT]&.use_colorize? if IRB.conf[:MAIN_CONTEXT]&.use_colorize?

View file

@ -106,15 +106,22 @@ module TestIRB
def test_eval_object_without_inspect_method def test_eval_object_without_inspect_method
verbose, $VERBOSE = $VERBOSE, nil verbose, $VERBOSE = $VERBOSE, nil
input = TestInputMethod.new([ all_assertions do |all|
"BasicObject.new\n", IRB::Inspector::INSPECTORS.invert.each_value do |mode|
]) all.for(mode) do
irb = IRB::Irb.new(IRB::WorkSpace.new(Object.new), input) input = TestInputMethod.new([
out, err = capture_output do "[BasicObject.new, Class.new]\n",
irb.eval_input ])
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 end
assert_empty err
assert(/\(Object doesn't support #inspect\)\n(=> )?\n/, out)
ensure ensure
$VERBOSE = verbose $VERBOSE = verbose
end end