2019-04-25 15:16:21 +03:00
|
|
|
# frozen_string_literal: false
|
|
|
|
require 'test/unit'
|
|
|
|
require 'irb/color'
|
2019-05-25 18:01:51 +03:00
|
|
|
require 'rubygems'
|
2019-04-25 17:53:57 +03:00
|
|
|
require 'stringio'
|
2019-04-25 15:16:21 +03:00
|
|
|
|
|
|
|
module TestIRB
|
|
|
|
class TestColor < Test::Unit::TestCase
|
|
|
|
CLEAR = "\e[0m"
|
|
|
|
BOLD = "\e[1m"
|
|
|
|
UNDERLINE = "\e[4m"
|
2019-05-27 13:59:17 +03:00
|
|
|
REVERSE = "\e[7m"
|
2019-04-25 15:16:21 +03:00
|
|
|
RED = "\e[31m"
|
|
|
|
GREEN = "\e[32m"
|
2019-05-26 06:47:29 +03:00
|
|
|
YELLOW = "\e[33m"
|
2019-04-25 15:16:21 +03:00
|
|
|
BLUE = "\e[34m"
|
|
|
|
MAGENTA = "\e[35m"
|
|
|
|
CYAN = "\e[36m"
|
|
|
|
|
|
|
|
def test_colorize_code
|
2019-04-26 12:28:54 +03: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-05-31 00:03:18 +03:00
|
|
|
# Common behaviors. Warn parser error, but do not warn compile error.
|
2019-04-25 15:16:21 +03:00
|
|
|
{
|
|
|
|
"1" => "#{BLUE}#{BOLD}1#{CLEAR}",
|
|
|
|
"2.3" => "#{MAGENTA}#{BOLD}2.3#{CLEAR}",
|
2019-05-26 17:44:57 +03:00
|
|
|
"7r" => "#{BLUE}#{BOLD}7r#{CLEAR}",
|
2019-05-26 20:22:57 +03:00
|
|
|
"8i" => "#{BLUE}#{BOLD}8i#{CLEAR}",
|
2019-05-26 06:47:29 +03:00
|
|
|
"['foo', :bar]" => "[#{RED}'#{CLEAR}#{RED}foo#{CLEAR}#{RED}'#{CLEAR}, #{YELLOW}:#{CLEAR}#{YELLOW}bar#{CLEAR}]",
|
2019-04-25 15:16:21 +03:00
|
|
|
"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}",
|
2019-05-26 20:29:20 +03:00
|
|
|
'erb = ERB.new("a#{nil}b", trim_mode: "-")' => "erb = #{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})",
|
2019-04-25 15:16:21 +03:00
|
|
|
"# comment" => "#{BLUE}#{BOLD}# comment#{CLEAR}",
|
2019-04-25 19:43:11 +03:00
|
|
|
"yield(hello)" => "#{GREEN}yield#{CLEAR}(hello)",
|
2019-04-28 14:18:44 +03: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 14:33:12 +03:00
|
|
|
'/r#{e}g/' => "#{RED}#{BOLD}/#{CLEAR}#{RED}r#{CLEAR}#{RED}\#{#{CLEAR}e#{RED}}#{CLEAR}#{RED}g#{CLEAR}#{RED}#{BOLD}/#{CLEAR}",
|
2019-05-25 17:48:58 +03:00
|
|
|
"'a\nb'" => "#{RED}'#{CLEAR}#{RED}a#{CLEAR}\n#{RED}b#{CLEAR}#{RED}'#{CLEAR}",
|
2019-05-27 13:59:17 +03:00
|
|
|
"4.5.6" => "#{MAGENTA}#{BOLD}4.5#{CLEAR}#{RED}#{REVERSE}.6#{CLEAR}",
|
2019-06-03 18:29:53 +03:00
|
|
|
"[1]]]\u0013" => "[1]]]^S",
|
2019-06-03 18:14:55 +03:00
|
|
|
"\e[0m\n" => "#{RED}#{REVERSE}^[#{CLEAR}[#{BLUE}#{BOLD}0#{CLEAR}m\n",
|
2019-05-25 18:01:51 +03:00
|
|
|
"%w[a b]" => "#{RED}%w[#{CLEAR}#{RED}a#{CLEAR} #{RED}b#{CLEAR}#{RED}]#{CLEAR}",
|
|
|
|
"%i[c d]" => "#{RED}%i[#{CLEAR}#{RED}c#{CLEAR} #{RED}d#{CLEAR}#{RED}]#{CLEAR}",
|
|
|
|
"{'a': 1}" => "{#{RED}'#{CLEAR}#{RED}a#{CLEAR}#{RED}':#{CLEAR} #{BLUE}#{BOLD}1#{CLEAR}}",
|
2019-05-26 06:47:29 +03:00
|
|
|
":Struct" => "#{YELLOW}:#{CLEAR}#{YELLOW}Struct#{CLEAR}",
|
2019-05-25 18:01:51 +03:00
|
|
|
"<<EOS\nhere\nEOS" => "#{RED}<<EOS#{CLEAR}\n#{RED}here#{CLEAR}\n#{RED}EOS#{CLEAR}",
|
2019-05-25 23:54:03 +03:00
|
|
|
'"#{}"' => "#{RED}\"#{CLEAR}#{RED}\#{#{CLEAR}#{RED}}#{CLEAR}#{RED}\"#{CLEAR}",
|
2019-05-26 06:47:29 +03:00
|
|
|
':"a#{}b"' => "#{YELLOW}:\"#{CLEAR}#{YELLOW}a#{CLEAR}#{YELLOW}\#{#{CLEAR}#{YELLOW}}#{CLEAR}#{YELLOW}b#{CLEAR}#{YELLOW}\"#{CLEAR}",
|
|
|
|
':"a#{ def b; end; \'c\' + "#{ :d }" }e"' => "#{YELLOW}:\"#{CLEAR}#{YELLOW}a#{CLEAR}#{YELLOW}\#{#{CLEAR} #{GREEN}def#{CLEAR} #{BLUE}#{BOLD}b#{CLEAR}; #{GREEN}end#{CLEAR}; #{RED}'#{CLEAR}#{RED}c#{CLEAR}#{RED}'#{CLEAR} + #{RED}\"#{CLEAR}#{RED}\#{#{CLEAR} #{YELLOW}:#{CLEAR}#{YELLOW}d#{CLEAR} #{RED}}#{CLEAR}#{RED}\"#{CLEAR} #{YELLOW}}#{CLEAR}#{YELLOW}e#{CLEAR}#{YELLOW}\"#{CLEAR}",
|
2019-05-26 08:29:16 +03:00
|
|
|
"[__FILE__, __LINE__]" => "[#{CYAN}#{BOLD}__FILE__#{CLEAR}, #{CYAN}#{BOLD}__LINE__#{CLEAR}]",
|
2019-05-26 07:39:11 +03:00
|
|
|
":self" => "#{YELLOW}:#{CLEAR}#{YELLOW}self#{CLEAR}",
|
|
|
|
":class" => "#{YELLOW}:#{CLEAR}#{YELLOW}class#{CLEAR}",
|
2019-06-03 18:14:55 +03:00
|
|
|
":@1" => "#{YELLOW}:#{CLEAR}#{RED}#{REVERSE}@1#{CLEAR}",
|
|
|
|
"@@1" => "#{RED}#{REVERSE}@@1#{CLEAR}",
|
2019-05-26 08:32:28 +03:00
|
|
|
"[:end, 2]" => "[#{YELLOW}:#{CLEAR}#{YELLOW}end#{CLEAR}, #{BLUE}#{BOLD}2#{CLEAR}]",
|
2019-05-26 07:39:11 +03:00
|
|
|
"[:>, 3]" => "[#{YELLOW}:#{CLEAR}#{YELLOW}>#{CLEAR}, #{BLUE}#{BOLD}3#{CLEAR}]",
|
|
|
|
":Hello ? world : nil" => "#{YELLOW}:#{CLEAR}#{YELLOW}Hello#{CLEAR} ? world : #{CYAN}#{BOLD}nil#{CLEAR}",
|
|
|
|
'raise "foo#{bar}baz"' => "raise #{RED}\"#{CLEAR}#{RED}foo#{CLEAR}#{RED}\#{#{CLEAR}bar#{RED}}#{CLEAR}#{RED}baz#{CLEAR}#{RED}\"#{CLEAR}",
|
|
|
|
'["#{obj.inspect}"]' => "[#{RED}\"#{CLEAR}#{RED}\#{#{CLEAR}obj.inspect#{RED}}#{CLEAR}#{RED}\"#{CLEAR}]",
|
|
|
|
'URI.parse "#{}"' => "#{BLUE}#{BOLD}#{UNDERLINE}URI#{CLEAR}.parse #{RED}\"#{CLEAR}#{RED}\#{#{CLEAR}#{RED}}#{CLEAR}#{RED}\"#{CLEAR}",
|
|
|
|
"begin\nrescue\nend" => "#{GREEN}begin#{CLEAR}\n#{GREEN}rescue#{CLEAR}\n#{GREEN}end#{CLEAR}",
|
|
|
|
"foo %w[bar]" => "foo #{RED}%w[#{CLEAR}#{RED}bar#{CLEAR}#{RED}]#{CLEAR}",
|
|
|
|
"foo %i[bar]" => "foo #{RED}%i[#{CLEAR}#{RED}bar#{CLEAR}#{RED}]#{CLEAR}",
|
2019-05-26 20:55:37 +03:00
|
|
|
"foo :@bar, baz, :@@qux, :$quux" => "foo #{YELLOW}:#{CLEAR}#{YELLOW}@bar#{CLEAR}, baz, #{YELLOW}:#{CLEAR}#{YELLOW}@@qux#{CLEAR}, #{YELLOW}:#{CLEAR}#{YELLOW}$quux#{CLEAR}",
|
2019-05-26 08:29:16 +03:00
|
|
|
"`echo`" => "#{RED}`#{CLEAR}#{RED}echo#{CLEAR}#{RED}`#{CLEAR}",
|
|
|
|
"\t" => "\t", # not ^I
|
|
|
|
"foo(*%W(bar))" => "foo(*#{RED}%W(#{CLEAR}#{RED}bar#{CLEAR}#{RED})#{CLEAR})",
|
2019-05-26 21:32:23 +03:00
|
|
|
"$stdout" => "#{GREEN}#{BOLD}$stdout#{CLEAR}",
|
2019-05-31 00:03:18 +03:00
|
|
|
}.each do |code, result|
|
|
|
|
actual = with_term { IRB::Color.colorize_code(code, complete: true) }
|
|
|
|
assert_equal(result, actual, "Case: colorize_code(#{code.dump}, complete: true)\nResult: #{humanized_literal(actual)}")
|
|
|
|
|
|
|
|
actual = with_term { IRB::Color.colorize_code(code, complete: false) }
|
|
|
|
assert_equal(result, actual, "Case: colorize_code(#{code.dump}, complete: false)\nResult: #{humanized_literal(actual)}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_colorize_code_complete_true
|
2019-06-03 18:22:22 +03:00
|
|
|
# `complete: true` behaviors. Warn compile_error.
|
2019-05-31 00:03:18 +03:00
|
|
|
{
|
2019-05-29 16:03:47 +03:00
|
|
|
"'foo' + 'bar" => "#{RED}'#{CLEAR}#{RED}foo#{CLEAR}#{RED}'#{CLEAR} + #{RED}'#{CLEAR}#{RED}#{REVERSE}bar#{CLEAR}",
|
2019-05-31 00:03:18 +03:00
|
|
|
}.each do |code, result|
|
|
|
|
actual = with_term { IRB::Color.colorize_code(code, complete: true) }
|
|
|
|
assert_equal(result, actual, "Case: colorize_code(#{code.dump}, complete: true)\nResult: #{humanized_literal(actual)}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_colorize_code_complete_false
|
2019-06-03 18:22:22 +03:00
|
|
|
# `complete: false` behaviors. Do not warn compile_error.
|
2019-05-31 00:03:18 +03:00
|
|
|
{
|
|
|
|
"'foo' + 'bar" => "#{RED}'#{CLEAR}#{RED}foo#{CLEAR}#{RED}'#{CLEAR} + #{RED}'#{CLEAR}#{RED}bar#{CLEAR}",
|
2019-04-28 14:33:12 +03:00
|
|
|
}.each do |code, result|
|
2019-05-31 00:03:18 +03:00
|
|
|
actual = with_term { IRB::Color.colorize_code(code, complete: false) }
|
|
|
|
assert_equal(result, actual, "Case: colorize_code(#{code.dump}, complete: false)\nResult: #{humanized_literal(actual)}")
|
2019-04-25 15:16:21 +03:00
|
|
|
end
|
|
|
|
end
|
2019-04-25 15:36:48 +03:00
|
|
|
|
|
|
|
def test_inspect_colorable
|
|
|
|
{
|
|
|
|
1 => true,
|
|
|
|
2.3 => true,
|
|
|
|
['foo', :bar] => true,
|
|
|
|
{ a: 4 } => true,
|
2019-04-25 19:15:30 +03:00
|
|
|
/reg/ => true,
|
2019-04-28 15:51:35 +03:00
|
|
|
(1..3) => true,
|
2019-04-25 15:36:48 +03:00
|
|
|
Object.new => false,
|
2019-04-27 16:01:10 +03:00
|
|
|
Struct => true,
|
2019-04-27 20:02:30 +03:00
|
|
|
Test => true,
|
2019-04-25 15:36:48 +03:00
|
|
|
Struct.new(:a) => false,
|
|
|
|
Struct.new(:a).new(1) => false,
|
|
|
|
}.each do |object, result|
|
2019-04-27 16:01:10 +03:00
|
|
|
assert_equal(result, IRB::Color.inspect_colorable?(object), "Case: inspect_colorable?(#{object.inspect})")
|
2019-04-25 15:36:48 +03:00
|
|
|
end
|
|
|
|
end
|
2019-04-25 17:53:57 +03: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-05-25 17:48:58 +03:00
|
|
|
|
|
|
|
def humanized_literal(str)
|
|
|
|
str
|
|
|
|
.gsub(CLEAR, '@@@{CLEAR}')
|
|
|
|
.gsub(BOLD, '@@@{BOLD}')
|
|
|
|
.gsub(UNDERLINE, '@@@{UNDERLINE}')
|
2019-05-27 13:59:17 +03:00
|
|
|
.gsub(REVERSE, '@@@{REVERSE}')
|
2019-05-25 17:48:58 +03:00
|
|
|
.gsub(RED, '@@@{RED}')
|
|
|
|
.gsub(GREEN, '@@@{GREEN}')
|
2019-05-26 06:47:29 +03:00
|
|
|
.gsub(YELLOW, '@@@{YELLOW}')
|
2019-05-25 17:48:58 +03:00
|
|
|
.gsub(BLUE, '@@@{BLUE}')
|
|
|
|
.gsub(MAGENTA, '@@@{MAGENTA}')
|
|
|
|
.gsub(CYAN, '@@@{CYAN}')
|
|
|
|
.dump.gsub(/@@@/, '#')
|
|
|
|
end
|
2019-04-25 15:16:21 +03:00
|
|
|
end
|
|
|
|
end
|