Skip to content

Commit 3cdcf87

Browse files
committed
Add edge case tests of barplot
1 parent 6bfb316 commit 3cdcf87

4 files changed

Lines changed: 44 additions & 0 deletions

File tree

lib/unicode_plot/barplot.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,13 @@ def print_row(out, row_index)
9797
raise ArgumentError, "invalid arguments"
9898
end
9999

100+
unless keys.length == heights.length
101+
raise ArgumentError, "The given vectors must be of the same length"
102+
end
103+
unless heights.min >= 0
104+
raise ArgumentError, "All values have to be positive. Negative bars are not supported."
105+
end
106+
100107
xlabel ||= ValueTransformer.transform_name(xscale)
101108
plot = Barplot.new(heights, width, color, symbol, xscale,
102109
border: border, xlabel: xlabel,
@@ -130,6 +137,7 @@ def print_row(out, row_index)
130137
if keys.empty?
131138
raise ArgumentError, "Can't append empty array to barplot"
132139
end
140+
133141
cur_idx = plot.n_rows
134142
plot.add_row!(heights)
135143
keys.each_with_index do |key, i|
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
 ┌ ┐
2+
a ┤ 1  
3+
b ┤ 1  
4+
c ┤ 1  
5+
d ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 1000000  
6+
 └ ┘
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
 ┌ ┐
2+
5 ┤ 0  
3+
4 ┤ 0  
4+
3 ┤ 0  
5+
2 ┤ 0  
6+
1 ┤ 0  
7+
 └ ┘

test/test-barplot.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@ class BarplotTest < Test::Unit::TestCase
33
include Helper::WithTerm
44

55
sub_test_case("UnicodePlot.barplot") do
6+
test("errors") do
7+
assert_raise(ArgumentError) do
8+
UnicodePlot.barplot([:a], [-1, 2])
9+
end
10+
assert_raise(ArgumentError) do
11+
UnicodePlot.barplot([:a, :b], [-1, 2])
12+
end
13+
end
14+
615
test("colored") do
716
data = { bar: 23, foo: 37 }
817
plot = UnicodePlot.barplot(data: data)
@@ -69,6 +78,20 @@ class BarplotTest < Test::Unit::TestCase
6978
assert_equal(fixture_path("barplot/ranges.txt").read,
7079
output)
7180
end
81+
82+
test("all zeros") do
83+
plot = UnicodePlot.barplot([5, 4, 3, 2, 1], [0, 0, 0, 0, 0])
84+
_, output = with_term { plot.render($stdout) }
85+
assert_equal(fixture_path("barplot/edgecase_zeros.txt").read,
86+
output)
87+
end
88+
89+
test("one large") do
90+
plot = UnicodePlot.barplot([:a, :b, :c, :d], [1, 1, 1, 1000000])
91+
_, output = with_term { plot.render($stdout) }
92+
assert_equal(fixture_path("barplot/edgecase_onelarge.txt").read,
93+
output)
94+
end
7295
end
7396

7497
sub_test_case("UnicodePlot.barplot!") do

0 commit comments

Comments
 (0)