Skip to content

Commit 8c9a90b

Browse files
authored
Merge pull request #43 from red-data-tools/check_border_type
Check border type
2 parents a4bbdcc + 3555883 commit 8c9a90b

File tree

9 files changed

+66
-3
lines changed

9 files changed

+66
-3
lines changed

lib/unicode_plot/plot.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def initialize(title: nil,
1717
@title = title
1818
@xlabel = xlabel
1919
@ylabel = ylabel
20-
@border = border
20+
@border = check_border(border)
2121
@margin = check_margin(margin)
2222
@padding = padding
2323
@labels_left = {}
@@ -142,5 +142,10 @@ def to_s
142142
raise ArgumentError, "row_index out of bounds"
143143
end
144144
end
145+
146+
private def check_border(border)
147+
return border if BORDER_MAP.key?(border)
148+
raise ArgumentError, "unknown border type: #{border}"
149+
end
145150
end
146151
end

lib/unicode_plot/renderer.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ module BorderMaps
4040
barplot: BorderMaps::BORDER_BARPLOT,
4141
}.freeze
4242

43+
def self.border_types
44+
BORDER_MAP.keys
45+
end
46+
4347
module BorderPrinter
4448
include StyledPrinter
4549

test/test-barplot.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ class BarplotTest < Test::Unit::TestCase
1212
end
1313
end
1414

15+
sub_test_case("with invalid arguments") do
16+
test("unknown border type") do
17+
assert_raise(ArgumentError.new("unknown border type: invalid_border_name")) do
18+
UnicodePlot.barplot(data: {bar: 23, foo: 37}, border: :invalid_border_name)
19+
end
20+
end
21+
end
22+
1523
test("colored") do
1624
data = { bar: 23, foo: 37 }
1725
plot = UnicodePlot.barplot(data: data)

test/test-boxplot.rb

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

55
sub_test_case("UnicodePlot.boxplot") do
6+
sub_test_case("with invalid arguments") do
7+
test("unknown border type") do
8+
assert_raise(ArgumentError.new("unknown border type: invalid_border_name")) do
9+
UnicodePlot.boxplot([1, 2, 3, 4, 5], border: :invalid_border_name)
10+
end
11+
end
12+
end
13+
614
sub_test_case("print to tty") do
715
test("without name") do
816
plot = UnicodePlot.boxplot([1, 2, 3, 4, 5])

test/test-densityplot.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ def setup
1010
assert_equal(1000, @dy.length)
1111
end
1212

13+
sub_test_case("with invalid arguments") do
14+
test("unknown border type") do
15+
assert_raise(ArgumentError.new("unknown border type: invalid_border_name")) do
16+
UnicodePlot.densityplot(@dx, @dy, border: :invalid_border_name)
17+
end
18+
end
19+
end
20+
1321
test("default") do
1422
plot = UnicodePlot.densityplot(@dx, @dy)
1523
dx2 = @dx.map {|x| x + 2 }

test/test-histogram.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ def setup
77
@x = fixture_path("randn.txt").read.lines.map(&:to_f)
88
end
99

10+
sub_test_case("with invalid arguments") do
11+
test("unknown border type") do
12+
assert_raise(ArgumentError.new("unknown border type: invalid_border_name")) do
13+
UnicodePlot.histogram(@x, border: :invalid_border_name)
14+
end
15+
end
16+
end
17+
1018
test("default") do
1119
plot = UnicodePlot.histogram(@x)
1220
_, output = with_term { plot.render($stdout) }

test/test-lineplot.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ def setup
2323
assert_raise(ArgumentError) { UnicodePlot.lineplot(1..3, 1..2) }
2424
end
2525

26+
sub_test_case("with invalid arguments") do
27+
test("unknown border type") do
28+
assert_raise(ArgumentError.new("unknown border type: invalid_border_name")) do
29+
UnicodePlot.lineplot(@x, @y, border: :invalid_border_name)
30+
end
31+
end
32+
end
33+
2634
sub_test_case("with numeric array") do
2735
test("default") do
2836
plot = UnicodePlot.lineplot(@x, @y)

test/test-scatterplot.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ def setup
2222
end
2323
end
2424

25+
sub_test_case("with invalid arguments") do
26+
test("unknown border type") do
27+
assert_raise(ArgumentError.new("unknown border type: invalid_border_name")) do
28+
UnicodePlot.scatterplot(@x, @y, border: :invalid_border_name)
29+
end
30+
end
31+
end
32+
2533
test("default") do
2634
plot = UnicodePlot.scatterplot(@x, @y)
2735
_, output = with_term { plot.render($stdout, newline: false) }

test/test-utils.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
class UnicodePlotTest < Test::Unit::TestCase
22
test("UnicodePlot.canvas_types") do
33
available_canvas_types = [:ascii, :block, :braille, :density, :dot]
4-
assert_equal(available_canvas_types,
5-
UnicodePlot.canvas_types)
4+
assert_equal(available_canvas_types.sort,
5+
UnicodePlot.canvas_types.sort)
6+
end
7+
8+
test("UnicodePlot.border_types") do
9+
available_border_types = [:solid, :corners, :barplot]
10+
assert_equal(available_border_types.sort,
11+
UnicodePlot.border_types.sort)
612
end
713
end

0 commit comments

Comments
 (0)