Skip to content

Commit 34204bc

Browse files
committed
Add test case for render(color: false)
1 parent 6691885 commit 34204bc

2 files changed

Lines changed: 26 additions & 17 deletions

File tree

test/helper/with_term.rb

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,26 @@
22

33
module Helper
44
module WithTerm
5-
def with_term(tty=true)
5+
def with_sio(tty: true)
66
sio = StringIO.new
77
def sio.tty?; true; end if tty
88

9-
orig_stdout, $stdout = $stdout, sio
10-
orig_env = ENV.to_h.dup
11-
ENV['TERM'] = 'xterm-256color'
12-
13-
result = yield
9+
result = yield(sio)
1410
sio.close
1511

1612
[result, sio.string]
17-
ensure
18-
$stdout.close
19-
$stdout = orig_stdout
20-
ENV.replace(orig_env) if orig_env
13+
end
14+
15+
def with_term(tty=true)
16+
with_sio(tty: tty) do |sio|
17+
orig_stdout, $stdout = $stdout, sio
18+
orig_env = ENV.to_h.dup
19+
ENV['TERM'] = 'xterm-256color'
20+
yield
21+
ensure
22+
$stdout = orig_stdout
23+
ENV.replace(orig_env) if orig_env
24+
end
2125
end
2226
end
2327
end

test/test-plot.rb

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
require 'stringio'
22

33
class TestPlot < Test::Unit::TestCase
4+
include Helper::WithTerm
5+
46
sub_test_case("#render") do
57
test("render to $stdout when no arguments") do
68
sio = StringIO.new
@@ -18,14 +20,17 @@ class TestPlot < Test::Unit::TestCase
1820
end
1921

2022
test("color: true") do
21-
sio_notty = StringIO.new
22-
UnicodePlot.barplot(data: {a: 23, b: 37}).render(sio_notty, color: true)
23+
_, tty_output = with_sio(tty: true) {|sio| UnicodePlot.barplot(data: {a: 23, b: 37}).render(sio) }
24+
_, notty_output = with_sio(tty: false) {|sio| UnicodePlot.barplot(data: {a: 23, b: 37}).render(sio, color: true) }
2325

24-
class << (sio_tty = StringIO.new)
25-
def tty?; true; end
26-
end
27-
UnicodePlot.barplot(data: {a: 23, b: 37}).render(sio_tty)
28-
assert_equal(sio_tty.string, sio_notty.string)
26+
assert_equal(tty_output, notty_output)
27+
end
28+
29+
test("color: false") do
30+
_, tty_output = with_sio(tty: true) {|sio| UnicodePlot.barplot(data: {a: 23, b: 37}).render(sio, color: false) }
31+
_, notty_output = with_sio(tty: false) {|sio| UnicodePlot.barplot(data: {a: 23, b: 37}).render(sio) }
32+
33+
assert_equal(tty_output, notty_output)
2934
end
3035
end
3136
end

0 commit comments

Comments
 (0)