2019-04-25 08:16:21 -04:00
|
|
|
# frozen_string_literal: false
|
|
|
|
require 'test/unit'
|
|
|
|
require 'irb/color'
|
2019-05-25 11:01:51 -04:00
|
|
|
require 'rubygems'
|
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"
|
2019-05-27 06:59:17 -04:00
|
|
|
REVERSE = "\e[7m"
|
2019-04-25 08:16:21 -04:00
|
|
|
RED = "\e[31m"
|
|
|
|
GREEN = "\e[32m"
|
2019-05-25 23:47:29 -04:00
|
|
|
YELLOW = "\e[33m"
|
2019-04-25 08:16:21 -04:00
|
|
|
BLUE = "\e[34m"
|
|
|
|
MAGENTA = "\e[35m"
|
|
|
|
CYAN = "\e[36m"
|
|
|
|
|
2021-04-07 12:00:41 -04:00
|
|
|
def test_colorize
|
|
|
|
text = "text"
|
|
|
|
{
|
|
|
|
[:BOLD] => "#{BOLD}#{text}#{CLEAR}",
|
|
|
|
[:UNDERLINE] => "#{UNDERLINE}#{text}#{CLEAR}",
|
|
|
|
[:REVERSE] => "#{REVERSE}#{text}#{CLEAR}",
|
|
|
|
[:RED] => "#{RED}#{text}#{CLEAR}",
|
|
|
|
[:GREEN] => "#{GREEN}#{text}#{CLEAR}",
|
|
|
|
[:YELLOW] => "#{YELLOW}#{text}#{CLEAR}",
|
|
|
|
[:BLUE] => "#{BLUE}#{text}#{CLEAR}",
|
|
|
|
[:MAGENTA] => "#{MAGENTA}#{text}#{CLEAR}",
|
|
|
|
[:CYAN] => "#{CYAN}#{text}#{CLEAR}",
|
|
|
|
}.each do |seq, result|
|
|
|
|
assert_equal_with_term(result, text, seq: seq)
|
2021-04-07 11:20:10 -04:00
|
|
|
|
|
|
|
assert_equal_with_term(text, text, seq: seq, tty: false)
|
2021-04-07 12:01:16 -04:00
|
|
|
assert_equal_with_term(text, text, seq: seq, colorable: false)
|
|
|
|
assert_equal_with_term(result, text, seq: seq, tty: false, colorable: true)
|
2021-04-07 12:00:41 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-25 08:16:21 -04:00
|
|
|
def test_colorize_code
|
2019-05-30 17:03:18 -04:00
|
|
|
# Common behaviors. Warn parser error, but do not warn compile error.
|
2019-06-12 11:29:45 -04:00
|
|
|
tests = {
|
2019-04-25 08:16:21 -04:00
|
|
|
"1" => "#{BLUE}#{BOLD}1#{CLEAR}",
|
|
|
|
"2.3" => "#{MAGENTA}#{BOLD}2.3#{CLEAR}",
|
2019-05-26 10:44:57 -04:00
|
|
|
"7r" => "#{BLUE}#{BOLD}7r#{CLEAR}",
|
2019-05-26 13:22:57 -04:00
|
|
|
"8i" => "#{BLUE}#{BOLD}8i#{CLEAR}",
|
2019-11-10 16:33:23 -05:00
|
|
|
"['foo', :bar]" => "[#{RED}#{BOLD}'#{CLEAR}#{RED}foo#{CLEAR}#{RED}#{BOLD}'#{CLEAR}, #{YELLOW}:#{CLEAR}#{YELLOW}bar#{CLEAR}]",
|
2019-04-25 08:16:21 -04: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-11-10 16:33:23 -05:00
|
|
|
'erb = ERB.new("a#{nil}b", trim_mode: "-")' => "erb = #{BLUE}#{BOLD}#{UNDERLINE}ERB#{CLEAR}.new(#{RED}#{BOLD}\"#{CLEAR}#{RED}a#{CLEAR}#{RED}\#{#{CLEAR}#{CYAN}#{BOLD}nil#{CLEAR}#{RED}}#{CLEAR}#{RED}b#{CLEAR}#{RED}#{BOLD}\"#{CLEAR}, #{MAGENTA}trim_mode:#{CLEAR} #{RED}#{BOLD}\"#{CLEAR}#{RED}-#{CLEAR}#{RED}#{BOLD}\"#{CLEAR})",
|
2019-04-25 08:16:21 -04:00
|
|
|
"# comment" => "#{BLUE}#{BOLD}# comment#{CLEAR}",
|
2020-02-19 05:27:53 -05:00
|
|
|
"def f;yield(hello);end" => "#{GREEN}def#{CLEAR} #{BLUE}#{BOLD}f#{CLEAR};#{GREEN}yield#{CLEAR}(hello);#{GREEN}end#{CLEAR}",
|
2019-11-10 16:33:23 -05:00
|
|
|
'"##@var]"' => "#{RED}#{BOLD}\"#{CLEAR}#{RED}\##{CLEAR}#{RED}\##{CLEAR}@var#{RED}]#{CLEAR}#{RED}#{BOLD}\"#{CLEAR}",
|
|
|
|
'"foo#{a} #{b}"' => "#{RED}#{BOLD}\"#{CLEAR}#{RED}foo#{CLEAR}#{RED}\#{#{CLEAR}a#{RED}}#{CLEAR}#{RED} #{CLEAR}#{RED}\#{#{CLEAR}b#{RED}}#{CLEAR}#{RED}#{BOLD}\"#{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-11-10 16:33:23 -05:00
|
|
|
"'a\nb'" => "#{RED}#{BOLD}'#{CLEAR}#{RED}a#{CLEAR}\n#{RED}b#{CLEAR}#{RED}#{BOLD}'#{CLEAR}",
|
2019-11-10 16:41:34 -05:00
|
|
|
"%[str]" => "#{RED}#{BOLD}%[#{CLEAR}#{RED}str#{CLEAR}#{RED}#{BOLD}]#{CLEAR}",
|
|
|
|
"%Q[str]" => "#{RED}#{BOLD}%Q[#{CLEAR}#{RED}str#{CLEAR}#{RED}#{BOLD}]#{CLEAR}",
|
|
|
|
"%q[str]" => "#{RED}#{BOLD}%q[#{CLEAR}#{RED}str#{CLEAR}#{RED}#{BOLD}]#{CLEAR}",
|
|
|
|
"%x[cmd]" => "#{RED}#{BOLD}%x[#{CLEAR}#{RED}cmd#{CLEAR}#{RED}#{BOLD}]#{CLEAR}",
|
|
|
|
"%r[reg]" => "#{RED}#{BOLD}%r[#{CLEAR}#{RED}reg#{CLEAR}#{RED}#{BOLD}]#{CLEAR}",
|
2019-11-10 16:33:23 -05:00
|
|
|
"%w[a b]" => "#{RED}#{BOLD}%w[#{CLEAR}#{RED}a#{CLEAR} #{RED}b#{CLEAR}#{RED}#{BOLD}]#{CLEAR}",
|
2019-11-10 16:41:34 -05:00
|
|
|
"%W[a b]" => "#{RED}#{BOLD}%W[#{CLEAR}#{RED}a#{CLEAR} #{RED}b#{CLEAR}#{RED}#{BOLD}]#{CLEAR}",
|
|
|
|
"%s[a b]" => "#{YELLOW}%s[#{CLEAR}#{YELLOW}a b#{CLEAR}#{YELLOW}]#{CLEAR}",
|
2019-11-10 16:54:44 -05:00
|
|
|
"%i[c d]" => "#{YELLOW}%i[#{CLEAR}#{YELLOW}c#{CLEAR}#{YELLOW} #{CLEAR}#{YELLOW}d#{CLEAR}#{YELLOW}]#{CLEAR}",
|
|
|
|
"%I[c d]" => "#{YELLOW}%I[#{CLEAR}#{YELLOW}c#{CLEAR}#{YELLOW} #{CLEAR}#{YELLOW}d#{CLEAR}#{YELLOW}]#{CLEAR}",
|
2019-11-10 16:33:23 -05:00
|
|
|
"{'a': 1}" => "{#{RED}#{BOLD}'#{CLEAR}#{RED}a#{CLEAR}#{RED}#{BOLD}':#{CLEAR} #{BLUE}#{BOLD}1#{CLEAR}}",
|
2019-05-25 23:47:29 -04:00
|
|
|
":Struct" => "#{YELLOW}:#{CLEAR}#{YELLOW}Struct#{CLEAR}",
|
2019-11-10 16:33:23 -05:00
|
|
|
'"#{}"' => "#{RED}#{BOLD}\"#{CLEAR}#{RED}\#{#{CLEAR}#{RED}}#{CLEAR}#{RED}#{BOLD}\"#{CLEAR}",
|
2019-05-25 23:47:29 -04:00
|
|
|
':"a#{}b"' => "#{YELLOW}:\"#{CLEAR}#{YELLOW}a#{CLEAR}#{YELLOW}\#{#{CLEAR}#{YELLOW}}#{CLEAR}#{YELLOW}b#{CLEAR}#{YELLOW}\"#{CLEAR}",
|
2019-11-10 16:33:23 -05:00
|
|
|
':"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}#{BOLD}'#{CLEAR}#{RED}c#{CLEAR}#{RED}#{BOLD}'#{CLEAR} + #{RED}#{BOLD}\"#{CLEAR}#{RED}\#{#{CLEAR} #{YELLOW}:#{CLEAR}#{YELLOW}d#{CLEAR} #{RED}}#{CLEAR}#{RED}#{BOLD}\"#{CLEAR} #{YELLOW}}#{CLEAR}#{YELLOW}e#{CLEAR}#{YELLOW}\"#{CLEAR}",
|
2021-01-13 09:36:10 -05:00
|
|
|
"[__FILE__, __LINE__, __ENCODING__]" => "[#{CYAN}#{BOLD}__FILE__#{CLEAR}, #{CYAN}#{BOLD}__LINE__#{CLEAR}, #{CYAN}#{BOLD}__ENCODING__#{CLEAR}]",
|
2019-05-26 00:39:11 -04:00
|
|
|
":self" => "#{YELLOW}:#{CLEAR}#{YELLOW}self#{CLEAR}",
|
|
|
|
":class" => "#{YELLOW}:#{CLEAR}#{YELLOW}class#{CLEAR}",
|
2019-05-26 01:32:28 -04:00
|
|
|
"[:end, 2]" => "[#{YELLOW}:#{CLEAR}#{YELLOW}end#{CLEAR}, #{BLUE}#{BOLD}2#{CLEAR}]",
|
2019-05-26 00:39:11 -04:00
|
|
|
"[:>, 3]" => "[#{YELLOW}:#{CLEAR}#{YELLOW}>#{CLEAR}, #{BLUE}#{BOLD}3#{CLEAR}]",
|
|
|
|
":Hello ? world : nil" => "#{YELLOW}:#{CLEAR}#{YELLOW}Hello#{CLEAR} ? world : #{CYAN}#{BOLD}nil#{CLEAR}",
|
2019-11-10 16:33:23 -05:00
|
|
|
'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}]",
|
|
|
|
'URI.parse "#{}"' => "#{BLUE}#{BOLD}#{UNDERLINE}URI#{CLEAR}.parse #{RED}#{BOLD}\"#{CLEAR}#{RED}\#{#{CLEAR}#{RED}}#{CLEAR}#{RED}#{BOLD}\"#{CLEAR}",
|
2019-05-26 00:39:11 -04:00
|
|
|
"begin\nrescue\nend" => "#{GREEN}begin#{CLEAR}\n#{GREEN}rescue#{CLEAR}\n#{GREEN}end#{CLEAR}",
|
2019-11-10 16:33:23 -05:00
|
|
|
"foo %w[bar]" => "foo #{RED}#{BOLD}%w[#{CLEAR}#{RED}bar#{CLEAR}#{RED}#{BOLD}]#{CLEAR}",
|
2019-11-10 16:54:44 -05:00
|
|
|
"foo %i[bar]" => "foo #{YELLOW}%i[#{CLEAR}#{YELLOW}bar#{CLEAR}#{YELLOW}]#{CLEAR}",
|
2019-05-26 13:55:37 -04:00
|
|
|
"foo :@bar, baz, :@@qux, :$quux" => "foo #{YELLOW}:#{CLEAR}#{YELLOW}@bar#{CLEAR}, baz, #{YELLOW}:#{CLEAR}#{YELLOW}@@qux#{CLEAR}, #{YELLOW}:#{CLEAR}#{YELLOW}$quux#{CLEAR}",
|
2019-11-10 16:33:23 -05:00
|
|
|
"`echo`" => "#{RED}#{BOLD}`#{CLEAR}#{RED}echo#{CLEAR}#{RED}#{BOLD}`#{CLEAR}",
|
2019-05-26 01:29:16 -04:00
|
|
|
"\t" => "\t", # not ^I
|
2019-11-10 16:33:23 -05:00
|
|
|
"foo(*%W(bar))" => "foo(*#{RED}#{BOLD}%W(#{CLEAR}#{RED}bar#{CLEAR}#{RED}#{BOLD})#{CLEAR})",
|
2019-05-26 14:32:23 -04:00
|
|
|
"$stdout" => "#{GREEN}#{BOLD}$stdout#{CLEAR}",
|
2021-04-01 08:09:32 -04:00
|
|
|
"__END__" => "#{GREEN}__END__#{CLEAR}",
|
2022-08-07 07:58:17 -04:00
|
|
|
"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",
|
2019-06-12 11:29:45 -04:00
|
|
|
}
|
2019-05-30 17:03:18 -04:00
|
|
|
|
2019-06-12 11:40:27 -04:00
|
|
|
# specific to Ruby 2.7+
|
2019-06-12 11:29:45 -04:00
|
|
|
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
|
|
|
|
|
2020-11-20 04:30:05 -05:00
|
|
|
# specific to Ruby 3.0+
|
|
|
|
if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('3.0.0')
|
|
|
|
tests.merge!({
|
|
|
|
"[1]]]\u0013" => "[#{BLUE}#{BOLD}1#{CLEAR}]#{RED}#{REVERSE}]#{CLEAR}#{RED}#{REVERSE}]#{CLEAR}#{RED}#{REVERSE}^S#{CLEAR}",
|
|
|
|
})
|
2020-12-29 02:07:20 -05:00
|
|
|
tests.merge!({
|
|
|
|
"def req(true) end" => "#{GREEN}def#{CLEAR} #{BLUE}#{BOLD}req#{CLEAR}(#{RED}#{REVERSE}true#{CLEAR}) #{RED}#{REVERSE}end#{CLEAR}",
|
|
|
|
"nil = 1" => "#{RED}#{REVERSE}nil#{CLEAR} = #{BLUE}#{BOLD}1#{CLEAR}",
|
|
|
|
"alias $x $1" => "#{GREEN}alias#{CLEAR} #{GREEN}#{BOLD}$x#{CLEAR} #{RED}#{REVERSE}$1#{CLEAR}",
|
|
|
|
"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}",
|
|
|
|
})
|
2020-11-20 04:30:05 -05:00
|
|
|
else
|
|
|
|
tests.merge!({
|
|
|
|
"[1]]]\u0013" => "[1]]]^S",
|
2020-12-29 02:07:20 -05:00
|
|
|
})
|
|
|
|
tests.merge!({
|
|
|
|
"def req(true) end" => "def req(true) 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}",
|
|
|
|
"def req(@a) end" => "#{GREEN}def#{CLEAR} #{BLUE}#{BOLD}req#{CLEAR}(@a) #{GREEN}end#{CLEAR}",
|
2020-11-20 04:30:05 -05:00
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2019-06-12 11:29:45 -04:00
|
|
|
tests.each do |code, result|
|
2019-06-12 11:40:27 -04:00
|
|
|
if colorize_code_supported?
|
2021-04-07 11:09:30 -04:00
|
|
|
assert_equal_with_term(result, code, complete: true)
|
|
|
|
assert_equal_with_term(result, code, complete: false)
|
2021-04-07 11:20:10 -04:00
|
|
|
|
|
|
|
assert_equal_with_term(code, code, complete: true, tty: false)
|
|
|
|
assert_equal_with_term(code, code, complete: false, tty: false)
|
2021-04-07 12:01:16 -04:00
|
|
|
|
|
|
|
assert_equal_with_term(code, code, complete: true, 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: false, tty: false, colorable: true)
|
2019-06-12 11:40:27 -04:00
|
|
|
else
|
2021-04-07 11:09:30 -04:00
|
|
|
assert_equal_with_term(code, code)
|
2019-06-12 11:29:45 -04:00
|
|
|
end
|
2019-05-30 17:03:18 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_colorize_code_complete_true
|
2019-06-12 11:40:27 -04:00
|
|
|
unless complete_option_supported?
|
2021-05-31 01:56:50 -04:00
|
|
|
pend '`complete: true` is the same as `complete: false` in Ruby 2.6-'
|
2019-06-12 11:29:45 -04:00
|
|
|
end
|
2019-06-12 11:40:27 -04:00
|
|
|
|
2019-06-03 11:32:16 -04:00
|
|
|
# `complete: true` behaviors. Warn end-of-file.
|
2019-05-30 17:03:18 -04:00
|
|
|
{
|
2019-11-10 16:33:23 -05:00
|
|
|
"'foo' + 'bar" => "#{RED}#{BOLD}'#{CLEAR}#{RED}foo#{CLEAR}#{RED}#{BOLD}'#{CLEAR} + #{RED}#{BOLD}'#{CLEAR}#{RED}#{REVERSE}bar#{CLEAR}",
|
|
|
|
"('foo" => "(#{RED}#{BOLD}'#{CLEAR}#{RED}#{REVERSE}foo#{CLEAR}",
|
2019-05-30 17:03:18 -04:00
|
|
|
}.each do |code, result|
|
2021-04-07 11:09:30 -04:00
|
|
|
assert_equal_with_term(result, code, complete: true)
|
2021-04-07 11:20:10 -04:00
|
|
|
|
|
|
|
assert_equal_with_term(code, code, complete: true, tty: false)
|
2021-04-07 12:01:16 -04:00
|
|
|
|
|
|
|
assert_equal_with_term(code, code, complete: true, colorable: false)
|
|
|
|
|
|
|
|
assert_equal_with_term(result, code, complete: true, tty: false, colorable: true)
|
2019-05-30 17:03:18 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_colorize_code_complete_false
|
2019-06-03 11:32:16 -04:00
|
|
|
# `complete: false` behaviors. Do not warn end-of-file.
|
2019-05-30 17:03:18 -04:00
|
|
|
{
|
2019-11-10 16:33:23 -05:00
|
|
|
"'foo' + 'bar" => "#{RED}#{BOLD}'#{CLEAR}#{RED}foo#{CLEAR}#{RED}#{BOLD}'#{CLEAR} + #{RED}#{BOLD}'#{CLEAR}#{RED}bar#{CLEAR}",
|
|
|
|
"('foo" => "(#{RED}#{BOLD}'#{CLEAR}#{RED}foo#{CLEAR}",
|
2019-04-28 07:33:12 -04:00
|
|
|
}.each do |code, result|
|
2019-06-12 11:40:27 -04:00
|
|
|
if colorize_code_supported?
|
2021-04-07 11:09:30 -04:00
|
|
|
assert_equal_with_term(result, code, complete: false)
|
2019-06-12 11:29:45 -04:00
|
|
|
|
2021-04-07 11:20:10 -04:00
|
|
|
assert_equal_with_term(code, code, complete: false, tty: false)
|
|
|
|
|
2021-04-07 12:01:16 -04:00
|
|
|
assert_equal_with_term(code, code, complete: false, colorable: false)
|
|
|
|
|
|
|
|
assert_equal_with_term(result, code, complete: false, tty: false, colorable: true)
|
|
|
|
|
2019-06-12 11:40:27 -04:00
|
|
|
unless complete_option_supported?
|
2021-04-07 11:09:30 -04:00
|
|
|
assert_equal_with_term(result, code, complete: true)
|
2021-04-07 11:20:10 -04:00
|
|
|
|
|
|
|
assert_equal_with_term(code, code, complete: true, tty: false)
|
2021-04-07 12:01:16 -04:00
|
|
|
|
|
|
|
assert_equal_with_term(code, code, complete: true, colorable: false)
|
|
|
|
|
|
|
|
assert_equal_with_term(result, code, complete: true, tty: false, colorable: true)
|
2019-06-12 11:29:45 -04:00
|
|
|
end
|
2019-06-12 11:40:27 -04:00
|
|
|
else
|
2021-04-07 11:09:30 -04:00
|
|
|
assert_equal_with_term(code, code)
|
2019-06-12 11:29:45 -04:00
|
|
|
end
|
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,
|
2019-10-15 00:25:05 -04:00
|
|
|
(a = []; a << a; a) => false,
|
|
|
|
(h = {}; h[h] = h; h) => false,
|
2019-04-25 08:36:48 -04:00
|
|
|
{ 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
|
|
|
|
|
2019-06-12 11:40:27 -04:00
|
|
|
# `#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?
|
2019-06-12 11:29:45 -04:00
|
|
|
Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.7.0')
|
|
|
|
end
|
|
|
|
|
2021-04-07 11:20:10 -04:00
|
|
|
def with_term(tty: true)
|
2019-04-25 10:53:57 -04:00
|
|
|
stdout = $stdout
|
|
|
|
io = StringIO.new
|
2021-04-07 11:20:10 -04:00
|
|
|
def io.tty?; true; end if tty
|
2019-04-25 10:53:57 -04:00
|
|
|
$stdout = io
|
|
|
|
|
|
|
|
env = ENV.to_h.dup
|
|
|
|
ENV['TERM'] = 'xterm-256color'
|
|
|
|
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
$stdout = stdout
|
|
|
|
ENV.replace(env) if env
|
|
|
|
end
|
2019-05-25 10:48:58 -04:00
|
|
|
|
2021-04-07 11:20:10 -04:00
|
|
|
def assert_equal_with_term(result, code, seq: nil, tty: true, **opts)
|
|
|
|
actual = with_term(tty: tty) do
|
2021-04-07 12:00:41 -04:00
|
|
|
if seq
|
|
|
|
IRB::Color.colorize(code, seq, **opts)
|
|
|
|
else
|
|
|
|
IRB::Color.colorize_code(code, **opts)
|
|
|
|
end
|
|
|
|
end
|
2021-04-07 11:09:30 -04:00
|
|
|
message = -> {
|
|
|
|
args = [code.dump]
|
2021-04-07 12:00:41 -04:00
|
|
|
args << seq.inspect if seq
|
2021-04-07 11:09:30 -04:00
|
|
|
opts.each {|kwd, val| args << "#{kwd}: #{val}"}
|
2021-04-07 12:00:41 -04:00
|
|
|
"Case: colorize#{seq ? "" : "_code"}(#{args.join(', ')})\nResult: #{humanized_literal(actual)}"
|
2021-04-07 11:09:30 -04:00
|
|
|
}
|
|
|
|
assert_equal(result, actual, message)
|
|
|
|
end
|
|
|
|
|
2019-05-25 10:48:58 -04:00
|
|
|
def humanized_literal(str)
|
|
|
|
str
|
|
|
|
.gsub(CLEAR, '@@@{CLEAR}')
|
|
|
|
.gsub(BOLD, '@@@{BOLD}')
|
|
|
|
.gsub(UNDERLINE, '@@@{UNDERLINE}')
|
2019-05-27 06:59:17 -04:00
|
|
|
.gsub(REVERSE, '@@@{REVERSE}')
|
2019-05-25 10:48:58 -04:00
|
|
|
.gsub(RED, '@@@{RED}')
|
|
|
|
.gsub(GREEN, '@@@{GREEN}')
|
2019-05-25 23:47:29 -04:00
|
|
|
.gsub(YELLOW, '@@@{YELLOW}')
|
2019-05-25 10:48:58 -04:00
|
|
|
.gsub(BLUE, '@@@{BLUE}')
|
|
|
|
.gsub(MAGENTA, '@@@{MAGENTA}')
|
|
|
|
.gsub(CYAN, '@@@{CYAN}')
|
|
|
|
.dump.gsub(/@@@/, '#')
|
|
|
|
end
|
2019-04-25 08:16:21 -04:00
|
|
|
end
|
|
|
|
end
|