2019-04-25 08:16:21 -04:00
|
|
|
# frozen_string_literal: false
|
|
|
|
require 'test/unit'
|
|
|
|
require 'irb/color'
|
2019-04-25 10:53:57 -04:00
|
|
|
require 'stringio'
|
2019-04-25 08:16:21 -04:00
|
|
|
|
|
|
|
module TestIRB
|
|
|
|
class TestColor < Test::Unit::TestCase
|
|
|
|
CLEAR = "\e[0m"
|
|
|
|
BOLD = "\e[1m"
|
|
|
|
UNDERLINE = "\e[4m"
|
|
|
|
RED = "\e[31m"
|
|
|
|
GREEN = "\e[32m"
|
|
|
|
BLUE = "\e[34m"
|
|
|
|
MAGENTA = "\e[35m"
|
|
|
|
CYAN = "\e[36m"
|
|
|
|
|
|
|
|
def test_colorize_code
|
2019-04-26 05:28:54 -04:00
|
|
|
if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.5.0')
|
|
|
|
assert_equal({}, IRB::Color::TOKEN_SEQ_EXPRS)
|
|
|
|
skip "this Ripper version is not supported"
|
|
|
|
end
|
|
|
|
|
2019-04-25 08:16:21 -04:00
|
|
|
{
|
|
|
|
"1" => "#{BLUE}#{BOLD}1#{CLEAR}",
|
|
|
|
"2.3" => "#{MAGENTA}#{BOLD}2.3#{CLEAR}",
|
|
|
|
"['foo', :bar]" => "[#{RED}'#{CLEAR}#{RED}foo#{CLEAR}#{RED}'#{CLEAR}, #{BLUE}#{BOLD}:#{CLEAR}#{BLUE}#{BOLD}bar#{CLEAR}]",
|
|
|
|
"class A; end" => "#{GREEN}class#{CLEAR} #{BLUE}#{BOLD}#{UNDERLINE}A#{CLEAR}; #{GREEN}end#{CLEAR}",
|
|
|
|
"def self.foo; bar; end" => "#{GREEN}def#{CLEAR} #{CYAN}#{BOLD}self#{CLEAR}.#{BLUE}#{BOLD}foo#{CLEAR}; bar; #{GREEN}end#{CLEAR}",
|
|
|
|
'ERB.new("a#{nil}b", trim_mode: "-")' => "#{BLUE}#{BOLD}#{UNDERLINE}ERB#{CLEAR}.new(#{RED}\"#{CLEAR}#{RED}a#{CLEAR}#{RED}\#{#{CLEAR}#{CYAN}#{BOLD}nil#{CLEAR}#{RED}}#{CLEAR}#{RED}b#{CLEAR}#{RED}\"#{CLEAR}, #{MAGENTA}trim_mode:#{CLEAR} #{RED}\"#{CLEAR}#{RED}-#{CLEAR}#{RED}\"#{CLEAR})",
|
|
|
|
"# comment" => "#{BLUE}#{BOLD}# comment#{CLEAR}",
|
2019-04-25 12:43:11 -04:00
|
|
|
"yield(hello)" => "#{GREEN}yield#{CLEAR}(hello)",
|
2019-04-28 07:18:44 -04:00
|
|
|
'"##@var]"' => "#{RED}\"#{CLEAR}#{RED}##{CLEAR}#{RED}##{CLEAR}@var#{RED}]#{CLEAR}#{RED}\"#{CLEAR}",
|
|
|
|
'"foo#{a} #{b}"' => "#{RED}\"#{CLEAR}#{RED}foo#{CLEAR}#{RED}\#{#{CLEAR}a#{RED}}#{CLEAR}#{RED} #{CLEAR}#{RED}\#{#{CLEAR}b#{RED}}#{CLEAR}#{RED}\"#{CLEAR}",
|
2019-04-28 07:33:12 -04:00
|
|
|
'/r#{e}g/' => "#{RED}#{BOLD}/#{CLEAR}#{RED}r#{CLEAR}#{RED}\#{#{CLEAR}e#{RED}}#{CLEAR}#{RED}g#{CLEAR}#{RED}#{BOLD}/#{CLEAR}",
|
2019-05-25 00:21:22 -04:00
|
|
|
"'a\nb'" => "#{RED}'#{CLEAR}#{RED}a\n#{CLEAR}#{RED}b#{CLEAR}#{RED}'#{CLEAR}",
|
|
|
|
"4.5.6" => "4.5.6",
|
|
|
|
"[1]]]" => "[1]]]",
|
2019-05-25 09:52:10 -04:00
|
|
|
"\e[0m\n" => "^[[#{BLUE}#{BOLD}0#{CLEAR}m\n",
|
2019-04-28 07:33:12 -04:00
|
|
|
}.each do |code, result|
|
2019-04-27 09:01:10 -04:00
|
|
|
assert_equal(result, with_term { IRB::Color.colorize_code(code) }, "Case: colorize_code(#{code.dump})")
|
2019-04-25 08:16:21 -04:00
|
|
|
end
|
|
|
|
end
|
2019-04-25 08:36:48 -04:00
|
|
|
|
|
|
|
def test_inspect_colorable
|
|
|
|
{
|
|
|
|
1 => true,
|
|
|
|
2.3 => true,
|
|
|
|
['foo', :bar] => true,
|
|
|
|
{ a: 4 } => true,
|
2019-04-25 12:15:30 -04:00
|
|
|
/reg/ => true,
|
2019-04-28 08:51:35 -04:00
|
|
|
(1..3) => true,
|
2019-04-25 08:36:48 -04:00
|
|
|
Object.new => false,
|
2019-04-27 09:01:10 -04:00
|
|
|
Struct => true,
|
2019-04-27 13:02:30 -04:00
|
|
|
Test => true,
|
2019-04-25 08:36:48 -04:00
|
|
|
Struct.new(:a) => false,
|
|
|
|
Struct.new(:a).new(1) => false,
|
|
|
|
}.each do |object, result|
|
2019-04-27 09:01:10 -04:00
|
|
|
assert_equal(result, IRB::Color.inspect_colorable?(object), "Case: inspect_colorable?(#{object.inspect})")
|
2019-04-25 08:36:48 -04:00
|
|
|
end
|
|
|
|
end
|
2019-04-25 10:53:57 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def with_term
|
|
|
|
stdout = $stdout
|
|
|
|
io = StringIO.new
|
|
|
|
def io.tty?; true; end
|
|
|
|
$stdout = io
|
|
|
|
|
|
|
|
env = ENV.to_h.dup
|
|
|
|
ENV['TERM'] = 'xterm-256color'
|
|
|
|
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
$stdout = stdout
|
|
|
|
ENV.replace(env) if env
|
|
|
|
end
|
2019-04-25 08:16:21 -04:00
|
|
|
end
|
|
|
|
end
|