summaryrefslogtreecommitdiff
path: root/test/irb
diff options
context:
space:
mode:
authorStan Lo <stan001212@gmail.com>2022-06-28 14:30:36 +0100
committergit <svn-admin@ruby-lang.org>2022-06-28 22:30:42 +0900
commit44c1316293f80abaa0e76b3818322544b9372a97 (patch)
treec560d27798f751ab059b4db59de749dbdd09b1de /test/irb
parent5ccdcd81685cfedd31344690fdb0fd9fc001e3ca (diff)
[ruby/irb] Centralize coloring control (https://github.com/ruby/irb/pull/374)
* Use colorable: argument as the only coloring control * Centalize color controling logic at Color.colorable? There are 2 requirements for coloring output: 1. It's supported on the platform 2. The user wants it: `IRB.conf[:USE_COLORIZE] == true` Right now we check 1 and 2 separately whenever we colorize things. But it's error-prone because while 1 is the default of `colorable` parameter, 2 always need to manually checked. When 2 is overlooked, it causes issues like https://github.com/ruby/irb/pull/362 And there's 0 case where we may want to colorize even when the user disables it. So I think we should merge 2 into `Color.colorable?` so it can be automatically picked up. * Add tests for all inspect modes * Simplify inspectors' coloring logic * Replace use_colorize? with Color.colorable? * Remove Context#use_colorize cause it's redundant https://github.com/ruby/irb/commit/1c53023ac4
Diffstat (limited to 'test/irb')
-rw-r--r--test/irb/test_context.rb46
1 files changed, 28 insertions, 18 deletions
diff --git a/test/irb/test_context.rb b/test/irb/test_context.rb
index 20f80e4af0..64296d7352 100644
--- a/test/irb/test_context.rb
+++ b/test/irb/test_context.rb
@@ -145,30 +145,40 @@ module TestIRB
assert_equal "=> 1\n", out
end
- def test_eval_object_without_inspect_method
- verbose, $VERBOSE = $VERBOSE, nil
- 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)
+ {
+ successful: [
+ [false, "class Foo < Struct.new(:bar); end; Foo.new(123)\n", /#<struct bar=123>/],
+ [:p, "class Foo < Struct.new(:bar); end; Foo.new(123)\n", /#<struct bar=123>/],
+ [true, "class Foo < Struct.new(:bar); end; Foo.new(123)\n", /#<struct #<Class:.*>::Foo bar=123>/],
+ [:yaml, "123", /--- 123\n/],
+ [:marshal, "123", Marshal.dump(123)],
+ ],
+ failed: [
+ [false, "BasicObject.new", /\(Object doesn't support #inspect\)\n(=> )?\n/],
+ [:p, "class Foo; undef inspect ;end; Foo.new", /\(Object doesn't support #inspect\)\n(=> )?\n/],
+ [true, "BasicObject.new", /\(Object doesn't support #inspect\)\n(=> )?\n/],
+ [:yaml, "BasicObject.new", /\(Object doesn't support #inspect\)\n(=> )?\n/],
+ [:marshal, "[Object.new, Class.new]", /\(Object doesn't support #inspect\)\n(=> )?\n/]
+ ]
+ }.each do |scenario, cases|
+ cases.each do |inspect_mode, input, expected|
+ define_method "test_#{inspect_mode}_inspect_mode_#{scenario}" do
+ pend if RUBY_ENGINE == 'truffleruby'
+ verbose, $VERBOSE = $VERBOSE, nil
+ irb = IRB::Irb.new(IRB::WorkSpace.new(Object.new), TestInputMethod.new([input]))
+ irb.context.inspect_mode = inspect_mode
+ out, err = capture_output do
+ irb.eval_input
end
+ assert_empty err
+ assert_match(expected, out)
+ ensure
+ $VERBOSE = verbose
end
end
- ensure
- $VERBOSE = verbose
end
def test_default_config
- assert_equal(true, @context.use_colorize?)
assert_equal(true, @context.use_autocomplete?)
end