Skip to content

Commit 80a5a30

Browse files
committed
adding tests for staircase plot, borrowed from unicodeplots.jl
1 parent 20e5db9 commit 80a5a30

File tree

1 file changed

+70
-1
lines changed

1 file changed

+70
-1
lines changed

test/test-lineplot.rb

+70-1
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,75 @@ def setup
194194

195195
# TODO: functions
196196

197-
# TODO: stairs
197+
sub_test_case("stairs") do
198+
def setup
199+
@sx = [1, 2, 4, 7, 8]
200+
@sy = [1, 3, 4, 2, 7]
201+
end
202+
203+
test("pre") do
204+
plot = UnicodePlot.stairs(@sx, @sy, style: :pre)
205+
_, output = with_term { plot.render($stdout, newline: false) }
206+
assert_equal(fixture_path("lineplot/stairs_pre.txt").read, output)
207+
end
208+
209+
test("post") do
210+
# inferred post
211+
plot = UnicodePlot.stairs(@sx, @sy)
212+
_, output = with_term { plot.render($stdout, newline: false) }
213+
assert_equal(fixture_path("lineplot/stairs_post.txt").read, output)
214+
# explicit post
215+
plot = UnicodePlot.stairs(@sx, @sy, style: :post)
216+
_, output = with_term { plot.render($stdout, newline: false) }
217+
assert_equal(fixture_path("lineplot/stairs_post.txt").read, output)
218+
end
219+
220+
test("with parameters") do
221+
plot = UnicodePlot.stairs(@sx, @sy, title: "Foo", color: :red, xlabel: "x", name: "1")
222+
sx2 = @sx.map { |val| val - 0.2 }
223+
sy2 = @sy.map { |val| val + 1.5 }
224+
plot2 = UnicodePlot.stairs!(plot, sx2, sy2, name: "2")
225+
assert_equal(plot.class, plot2.class)
226+
_, output = with_term { plot.render($stdout, newline: false) }
227+
assert_equal(fixture_path("lineplot/stairs_parameters.txt").read, output)
228+
229+
# add a 3rd staircase and check again
230+
plot3 = UnicodePlot.stairs!(plot, @sx, @sy, name: "3", style: :pre)
231+
assert_equal(plot.class, plot3.class)
232+
_, output = with_term { plot.render($stdout, newline: false) }
233+
assert_equal(fixture_path("lineplot/stairs_parameters2.txt").read, output)
234+
235+
# check with color disabled
236+
output = StringIO.open do |sio|
237+
plot.render(sio)
238+
sio.close
239+
sio.string
240+
end
241+
assert_equal("\n", output[-1])
242+
assert_equal(fixture_path("lineplot/stairs_parameters2_nocolor.txt").read,
243+
output.chomp)
244+
end
245+
246+
test("special weird case") do
247+
plot = UnicodePlot.stairs(@sx, [1, 3, 4, 2, 7000])
248+
_, output = with_term { plot.render($stdout, newline: false) }
249+
assert_equal(fixture_path("lineplot/stairs_edgecase.txt").read, output)
250+
end
251+
252+
test("annotations") do
253+
plot = UnicodePlot.stairs(@sx, @sy, width: 10, padding: 3)
254+
plot.annotate!(:tl, "Hello")
255+
plot.annotate!(:t, "how are")
256+
plot.annotate!(:tr, "you?")
257+
plot.annotate!(:bl, "Hello")
258+
plot.annotate!(:b, "how are")
259+
plot.annotate!(:br, "you?")
260+
UnicodePlot.lineplot!(plot, 1, 0.5)
261+
_, output = with_term { plot.render($stdout, newline: false) }
262+
assert_equal(fixture_path("lineplot/squeeze_annotations.txt").read, output)
263+
end
264+
265+
end
266+
198267
end
199268
end

0 commit comments

Comments
 (0)