diff options
Diffstat (limited to 'test/irb/test_color.rb')
-rw-r--r-- | test/irb/test_color.rb | 117 |
1 files changed, 57 insertions, 60 deletions
diff --git a/test/irb/test_color.rb b/test/irb/test_color.rb index ba99b1253f..9d78f5233e 100644 --- a/test/irb/test_color.rb +++ b/test/irb/test_color.rb @@ -1,11 +1,11 @@ # frozen_string_literal: false -require 'test/unit' require 'irb/color' -require 'rubygems' require 'stringio' +require_relative "helper" + module TestIRB - class TestColor < Test::Unit::TestCase + class ColorTest < TestCase CLEAR = "\e[0m" BOLD = "\e[1m" UNDERLINE = "\e[4m" @@ -17,6 +17,20 @@ module TestIRB MAGENTA = "\e[35m" CYAN = "\e[36m" + def setup + super + if IRB.respond_to?(:conf) + @colorize, IRB.conf[:USE_COLORIZE] = IRB.conf[:USE_COLORIZE], true + end + end + + def teardown + if instance_variable_defined?(:@colorize) + IRB.conf[:USE_COLORIZE] = @colorize + end + super + end + def test_colorize text = "text" { @@ -75,6 +89,7 @@ module TestIRB ":class" => "#{YELLOW}:#{CLEAR}#{YELLOW}class#{CLEAR}", "[:end, 2]" => "[#{YELLOW}:#{CLEAR}#{YELLOW}end#{CLEAR}, #{BLUE}#{BOLD}2#{CLEAR}]", "[:>, 3]" => "[#{YELLOW}:#{CLEAR}#{YELLOW}>#{CLEAR}, #{BLUE}#{BOLD}3#{CLEAR}]", + "[:`, 4]" => "[#{YELLOW}:#{CLEAR}#{YELLOW}`#{CLEAR}, #{BLUE}#{BOLD}4#{CLEAR}]", ":Hello ? world : nil" => "#{YELLOW}:#{CLEAR}#{YELLOW}Hello#{CLEAR} ? world : #{CYAN}#{BOLD}nil#{CLEAR}", 'raise "foo#{bar}baz"' => "raise #{RED}#{BOLD}\"#{CLEAR}#{RED}foo#{CLEAR}#{RED}\#{#{CLEAR}bar#{RED}}#{CLEAR}#{RED}baz#{CLEAR}#{RED}#{BOLD}\"#{CLEAR}", '["#{obj.inspect}"]' => "[#{RED}#{BOLD}\"#{CLEAR}#{RED}\#{#{CLEAR}obj.inspect#{RED}}#{CLEAR}#{RED}#{BOLD}\"#{CLEAR}]", @@ -84,20 +99,20 @@ module TestIRB "foo %i[bar]" => "foo #{YELLOW}%i[#{CLEAR}#{YELLOW}bar#{CLEAR}#{YELLOW}]#{CLEAR}", "foo :@bar, baz, :@@qux, :$quux" => "foo #{YELLOW}:#{CLEAR}#{YELLOW}@bar#{CLEAR}, baz, #{YELLOW}:#{CLEAR}#{YELLOW}@@qux#{CLEAR}, #{YELLOW}:#{CLEAR}#{YELLOW}$quux#{CLEAR}", "`echo`" => "#{RED}#{BOLD}`#{CLEAR}#{RED}echo#{CLEAR}#{RED}#{BOLD}`#{CLEAR}", - "\t" => "\t", # not ^I + "\t" => Reline::Unicode.escape_for_print("\t") == ' ' ? ' ' : "\t", # not ^I "foo(*%W(bar))" => "foo(*#{RED}#{BOLD}%W(#{CLEAR}#{RED}bar#{CLEAR}#{RED}#{BOLD})#{CLEAR})", "$stdout" => "#{GREEN}#{BOLD}$stdout#{CLEAR}", "__END__" => "#{GREEN}__END__#{CLEAR}", + "foo\n__END__\nbar" => "foo\n#{GREEN}__END__#{CLEAR}\nbar", + "foo\n<<A\0\0bar\nA\nbaz" => "foo\n#{RED}<<A#{CLEAR}^@^@bar\n#{RED}A#{CLEAR}\nbaz", + "<<A+1\nA" => "#{RED}<<A#{CLEAR}+#{BLUE}#{BOLD}1#{CLEAR}\n#{RED}A#{CLEAR}", } - # specific to Ruby 2.7+ - if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.7.0') - tests.merge!({ - "4.5.6" => "#{MAGENTA}#{BOLD}4.5#{CLEAR}#{RED}#{REVERSE}.6#{CLEAR}", - "\e[0m\n" => "#{RED}#{REVERSE}^[#{CLEAR}[#{BLUE}#{BOLD}0#{CLEAR}#{RED}#{REVERSE}m#{CLEAR}\n", - "<<EOS\nhere\nEOS" => "#{RED}<<EOS#{CLEAR}\n#{RED}here#{CLEAR}\n#{RED}EOS#{CLEAR}", - }) - end + tests.merge!({ + "4.5.6" => "#{MAGENTA}#{BOLD}4.5#{CLEAR}#{RED}#{REVERSE}.6#{CLEAR}", + "\e[0m\n" => "#{RED}#{REVERSE}^[#{CLEAR}[#{BLUE}#{BOLD}0#{CLEAR}#{RED}#{REVERSE}m#{CLEAR}\n", + "<<EOS\nhere\nEOS" => "#{RED}<<EOS#{CLEAR}\n#{RED}here#{CLEAR}\n#{RED}EOS#{CLEAR}", + }) # specific to Ruby 3.0+ if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('3.0.0') @@ -111,12 +126,15 @@ module TestIRB "class bad; end" => "#{GREEN}class#{CLEAR} #{RED}#{REVERSE}bad#{CLEAR}; #{GREEN}end#{CLEAR}", "def req(@a) end" => "#{GREEN}def#{CLEAR} #{BLUE}#{BOLD}req#{CLEAR}(#{RED}#{REVERSE}@a#{CLEAR}) #{GREEN}end#{CLEAR}", }) - else - tests.merge!({ - "[1]]]\u0013" => "[1]]]^S", + if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('3.2.0') + tests.merge!({ + "def req(true) end" => "#{GREEN}def#{CLEAR} #{BLUE}#{BOLD}req#{CLEAR}(#{RED}#{REVERSE}true#{CLEAR}#{RED}#{REVERSE})#{CLEAR} #{RED}#{REVERSE}end#{CLEAR}", }) + end + else tests.merge!({ - "def req(true) end" => "def req(true) end", + "[1]]]\u0013" => "[#{BLUE}#{BOLD}1#{CLEAR}]#{RED}#{REVERSE}]#{CLEAR}]^S", + "def req(true) end" => "#{GREEN}def#{CLEAR} #{BLUE}#{BOLD}req#{CLEAR}(#{RED}#{REVERSE}true#{CLEAR}) end", "nil = 1" => "#{CYAN}#{BOLD}nil#{CLEAR} = #{BLUE}#{BOLD}1#{CLEAR}", "alias $x $1" => "#{GREEN}alias#{CLEAR} #{GREEN}#{BOLD}$x#{CLEAR} $1", "class bad; end" => "#{GREEN}class#{CLEAR} bad; #{GREEN}end#{CLEAR}", @@ -125,31 +143,34 @@ module TestIRB end tests.each do |code, result| - if colorize_code_supported? - assert_equal_with_term(result, code, complete: true) - assert_equal_with_term(result, code, complete: false) + assert_equal_with_term(result, code, complete: true) + assert_equal_with_term(result, code, complete: false) - assert_equal_with_term(code, code, complete: true, tty: false) - assert_equal_with_term(code, code, complete: false, tty: false) + assert_equal_with_term(code, code, complete: true, tty: false) + assert_equal_with_term(code, code, complete: false, tty: false) - assert_equal_with_term(code, code, complete: true, colorable: false) + assert_equal_with_term(code, code, complete: true, colorable: false) - assert_equal_with_term(code, code, complete: false, colorable: false) + assert_equal_with_term(code, code, complete: false, colorable: false) - assert_equal_with_term(result, code, complete: true, tty: false, colorable: true) + assert_equal_with_term(result, code, complete: true, tty: false, colorable: true) - assert_equal_with_term(result, code, complete: false, tty: false, colorable: true) - else - assert_equal_with_term(code, code) - end + assert_equal_with_term(result, code, complete: false, tty: false, colorable: true) end end - def test_colorize_code_complete_true - unless complete_option_supported? - skip '`complete: true` is the same as `complete: false` in Ruby 2.6-' - end + def test_colorize_code_with_local_variables + code = "a /(b +1)/i" + result_without_lvars = "a #{RED}#{BOLD}/#{CLEAR}#{RED}(b +1)#{CLEAR}#{RED}#{BOLD}/i#{CLEAR}" + result_with_lvar = "a /(b #{BLUE}#{BOLD}+1#{CLEAR})/i" + result_with_lvars = "a /(b +#{BLUE}#{BOLD}1#{CLEAR})/i" + assert_equal_with_term(result_without_lvars, code) + assert_equal_with_term(result_with_lvar, code, local_variables: ['a']) + assert_equal_with_term(result_with_lvars, code, local_variables: ['a', 'b']) + end + + def test_colorize_code_complete_true # `complete: true` behaviors. Warn end-of-file. { "'foo' + 'bar" => "#{RED}#{BOLD}'#{CLEAR}#{RED}foo#{CLEAR}#{RED}#{BOLD}'#{CLEAR} + #{RED}#{BOLD}'#{CLEAR}#{RED}#{REVERSE}bar#{CLEAR}", @@ -171,27 +192,13 @@ module TestIRB "'foo' + 'bar" => "#{RED}#{BOLD}'#{CLEAR}#{RED}foo#{CLEAR}#{RED}#{BOLD}'#{CLEAR} + #{RED}#{BOLD}'#{CLEAR}#{RED}bar#{CLEAR}", "('foo" => "(#{RED}#{BOLD}'#{CLEAR}#{RED}foo#{CLEAR}", }.each do |code, result| - if colorize_code_supported? - assert_equal_with_term(result, code, complete: false) - - assert_equal_with_term(code, code, complete: false, tty: false) - - assert_equal_with_term(code, code, complete: false, colorable: false) + assert_equal_with_term(result, code, complete: false) - assert_equal_with_term(result, code, complete: false, tty: false, colorable: true) + assert_equal_with_term(code, code, complete: false, tty: false) - unless complete_option_supported? - assert_equal_with_term(result, code, complete: true) + assert_equal_with_term(code, code, complete: false, colorable: false) - assert_equal_with_term(code, code, complete: true, tty: false) - - assert_equal_with_term(code, code, complete: true, colorable: false) - - assert_equal_with_term(result, code, complete: true, tty: false, colorable: true) - end - else - assert_equal_with_term(code, code) - end + assert_equal_with_term(result, code, complete: false, tty: false, colorable: true) end end @@ -217,16 +224,6 @@ module TestIRB private - # `#colorize_code` is supported only for Ruby 2.5+. It just returns the original code in 2.4-. - def colorize_code_supported? - Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.5.0') - end - - # `complete: true` is the same as `complete: false` in Ruby 2.6- - def complete_option_supported? - Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.7.0') - end - def with_term(tty: true) stdout = $stdout io = StringIO.new |