Skip to content

Commit

Permalink
[ruby/irb] Assertions on non-tty
Browse files Browse the repository at this point in the history
  • Loading branch information
nobu authored and matzbot committed Apr 26, 2021
1 parent 75f1ad8 commit 687ab5d
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions test/irb/test_color.rb
Expand Up @@ -31,6 +31,8 @@ def test_colorize
[:CYAN] => "#{CYAN}#{text}#{CLEAR}",
}.each do |seq, result|
assert_equal_with_term(result, text, seq: seq)

assert_equal_with_term(text, text, seq: seq, tty: false)
end
end

Expand Down Expand Up @@ -124,6 +126,9 @@ def test_colorize_code
if colorize_code_supported?
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)
else
assert_equal_with_term(code, code)
end
Expand All @@ -141,6 +146,8 @@ def test_colorize_code_complete_true
"('foo" => "(#{RED}#{BOLD}'#{CLEAR}#{RED}#{REVERSE}foo#{CLEAR}",
}.each do |code, result|
assert_equal_with_term(result, code, complete: true)

assert_equal_with_term(code, code, complete: true, tty: false)
end
end

Expand All @@ -153,8 +160,12 @@ def test_colorize_code_complete_false
if colorize_code_supported?
assert_equal_with_term(result, code, complete: false)

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: true, tty: false)
end
else
assert_equal_with_term(code, code)
Expand Down Expand Up @@ -194,10 +205,10 @@ def complete_option_supported?
Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.7.0')
end

def with_term
def with_term(tty: true)
stdout = $stdout
io = StringIO.new
def io.tty?; true; end
def io.tty?; true; end if tty
$stdout = io

env = ENV.to_h.dup
Expand All @@ -209,8 +220,8 @@ def io.tty?; true; end
ENV.replace(env) if env
end

def assert_equal_with_term(result, code, seq: nil, **opts)
actual = with_term do
def assert_equal_with_term(result, code, seq: nil, tty: true, **opts)
actual = with_term(tty: tty) do
if seq
IRB::Color.colorize(code, seq, **opts)
else
Expand Down

0 comments on commit 687ab5d

Please sign in to comment.