diff --git a/lib/youplot/backends/unicode_plot_backend.rb b/lib/youplot/backends/unicode_plot_backend.rb index d9a5ff8..572a9f7 100644 --- a/lib/youplot/backends/unicode_plot_backend.rb +++ b/lib/youplot/backends/unicode_plot_backend.rb @@ -7,6 +7,8 @@ module YouPlot # plotting functions. module Backends module UnicodePlotBackend + class Error < StandardError; end + module_function def barplot(data, params, fmt = nil, count: false) @@ -193,14 +195,14 @@ module YouPlot warn " Headers: \e[35m#{data.headers.inspect}\e[0m" warn " The first item is: \e[35m\"#{series[0][0]}\"\e[0m" warn " The last item is : \e[35m\"#{series[0][-1]}\"\e[0m" - exit 1 if YouPlot.run_as_executable? + YouPlot.run_as_executable ? exit(1) : raise(Error) end if fmt == 'xyxy' && series.size.odd? warn 'YouPlot: In the xyxy format, the number of series must be even.' warn '' warn " Number of series: \e[35m#{series.size}\e[0m" warn " Headers: \e[35m#{data.headers.inspect}\e[0m" - exit 1 if YouPlot.run_as_executable? + YouPlot.run_as_executable ? exit(1) : raise(Error) end end end diff --git a/test/fixtures/simple-boxplot.txt b/test/fixtures/simple-boxplot.txt new file mode 100644 index 0000000..58e8546 --- /dev/null +++ b/test/fixtures/simple-boxplot.txt @@ -0,0 +1,6 @@ + ┌ ┐ + ╷ ┌──────────┬──────────┐ ╷ + 1 ├───────┤ │ ├────────┤ + ╵ └──────────┴──────────┘ ╵ + └ ┘ + -50 0 50 diff --git a/test/fixtures/simple-histogram.txt b/test/fixtures/simple-histogram.txt new file mode 100644 index 0000000..63864e3 --- /dev/null +++ b/test/fixtures/simple-histogram.txt @@ -0,0 +1,9 @@ + ┌ ┐ + [-60.0, -40.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1 + [-40.0, -20.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 2 + [-20.0, 0.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 2 + [ 0.0, 20.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1 + [ 20.0, 40.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 2 + [ 40.0, 60.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 2 + └ ┘ + Frequency diff --git a/test/fixtures/simple-lineplot.txt b/test/fixtures/simple-lineplot.txt new file mode 100644 index 0000000..f89a570 --- /dev/null +++ b/test/fixtures/simple-lineplot.txt @@ -0,0 +1,18 @@ + ┌────────────────────────────────────────┐ + 50 │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸│ + │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡄⠀⠀⠀⠀⠀⠀⠀⡸│ + │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⢇⠀⠀⠀⠀⠀⠀⠀⡇│ + │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⢇⠀⠀⠀⠀⠀⠀⠀⡎⢸⠀⠀⠀⠀⠀⠀⢠⠃│ + │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡄⠀⠀⠀⠀⠀⠀⠀⡜⢸⠀⠀⠀⠀⠀⠀⢠⠃⠈⡆⠀⠀⠀⠀⠀⢸⠀│ + │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡸⠸⡀⠀⠀⠀⠀⠀⢠⠃⠀⡇⠀⠀⠀⠀⠀⢸⠀⠀⢇⠀⠀⠀⠀⠀⡎⠀│ + │⠀⠀⠀⡠⠳⡀⠀⠀⠀⠀⠀⢠⠃⠀⢣⠀⠀⠀⠀⠀⡜⠀⠀⢱⠀⠀⠀⠀⠀⡇⠀⠀⢸⠀⠀⠀⠀⠀⡇⠀│ + │⠤⢤⠼⠤⠤⠵⡤⠤⠤⠤⢤⠧⠤⠤⠼⡤⠤⠤⠤⢤⠧⠤⠤⠬⡦⠤⠤⠤⢴⠥⠤⠤⠬⡦⠤⠤⠤⢼⠤⠤│ + │⡠⠃⠀⠀⠀⠀⠱⡀⠀⠀⡜⠀⠀⠀⠀⢱⠀⠀⠀⡸⠀⠀⠀⠀⢣⠀⠀⠀⡸⠀⠀⠀⠀⡇⠀⠀⠀⡜⠀⠀│ + │⠁⠀⠀⠀⠀⠀⠀⠱⡀⡰⠁⠀⠀⠀⠀⠀⡇⠀⢀⠇⠀⠀⠀⠀⠸⡀⠀⠀⡇⠀⠀⠀⠀⢸⠀⠀⠀⡇⠀⠀│ + │⠀⠀⠀⠀⠀⠀⠀⠀⠱⠃⠀⠀⠀⠀⠀⠀⠸⡀⡸⠀⠀⠀⠀⠀⠀⢇⠀⢸⠀⠀⠀⠀⠀⠘⡄⠀⢰⠁⠀⠀│ + │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢇⠇⠀⠀⠀⠀⠀⠀⢸⠀⡎⠀⠀⠀⠀⠀⠀⡇⠀⢸⠀⠀⠀│ + │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠀⠀⠀⠀⠀⠀⠀⠀⣇⠇⠀⠀⠀⠀⠀⠀⢸⠀⡇⠀⠀⠀│ + │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠹⠀⠀⠀⠀⠀⠀⠀⠘⣄⠇⠀⠀⠀│ + -50 │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⠀⠀⠀⠀│ + └────────────────────────────────────────┘ + 1 10 diff --git a/test/youplot/iris_test.rb b/test/youplot/iris_test.rb index 340f506..2246c09 100644 --- a/test/youplot/iris_test.rb +++ b/test/youplot/iris_test.rb @@ -104,27 +104,26 @@ class YouPlotIRISTest < Test::Unit::TestCase assert_equal fixture('iris-boxplot.txt'), @stderr_file.read end - test :c do - omit - YouPlot::Command.new(['count', '-H', '-d,']).run - assert_equal fixture('iris-count.txt'), @stderr_file.read - end + # test :c do + # YouPlot::Command.new(['count', '-H', '-d,']).run + # assert_equal fixture('iris-count.txt'), @stderr_file.read + # end - test :count do - omit - YouPlot::Command.new(['c', '-H', '-d,']).run - assert_equal fixture('iris-count.txt'), @stderr_file.read - end + # test :count do + # YouPlot::Command.new(['c', '-H', '-d,']).run + # assert_equal fixture('iris-count.txt'), @stderr_file.read + # end test :plot_output_stdout do YouPlot::Command.new(['bar', '-o', '-H', '-d,', '-t', 'IRIS-BARPLOT']).run assert_equal '', @stderr_file.read + assert_equal fixture('iris-barplot.txt'), @stdout_file.read end test :data_output_stdout do YouPlot::Command.new(['bar', '-O', '-H', '-d,', '-t', 'IRIS-BARPLOT']).run assert_equal fixture('iris-barplot.txt'), @stderr_file.read - assert_equal File.read(File.expand_path('../fixtures/iris.csv', __dir__)), @stdout_file.read + assert_equal fixture('iris.csv'), @stdout_file.read end %i[colors color colours colour].each do |cmd_name| diff --git a/test/youplot/simple_test.rb b/test/youplot/simple_test.rb index 008d5ef..5ccd787 100644 --- a/test/youplot/simple_test.rb +++ b/test/youplot/simple_test.rb @@ -40,70 +40,77 @@ class YouPlotSimpleTest < Test::Unit::TestCase end end - # test :barplot do - # YouPlot::Command.new(['barplot', '-H', '-d,', '-t', 'IRIS-BARPLOT']).run - # assert_equal fixture('iris-barplot.txt'), @stderr_file.read - # end + test :barplot do + assert_raise(ArgumentError) do + YouPlot::Command.new(['barplot']).run + end + end - # test :hist do - # YouPlot::Command.new(['hist', '-H', '-d,', '-t', 'IRIS-HISTOGRAM']).run - # assert_equal fixture('iris-histogram.txt'), @stderr_file.read - # end + test :hist do + YouPlot::Command.new(['hist']).run + assert_equal fixture('simple-histogram.txt'), @stderr_file.read + end - # test :histogram do - # YouPlot::Command.new(['histogram', '-H', '-d,', '-t', 'IRIS-HISTOGRAM']).run - # assert_equal fixture('iris-histogram.txt'), @stderr_file.read - # end + test :histogram do + YouPlot::Command.new(['histogram']).run + assert_equal fixture('simple-histogram.txt'), @stderr_file.read + end - # test :line do - # YouPlot::Command.new(['line', '-H', '-d,', '-t', 'IRIS-LINEPLOT']).run - # assert_equal fixture('iris-lineplot.txt'), @stderr_file.read - # end + test :line do + YouPlot::Command.new(['line']).run + assert_equal fixture('simple-lineplot.txt'), @stderr_file.read + end - # test :lineplot do - # YouPlot::Command.new(['lineplot', '-H', '-d,', '-t', 'IRIS-LINEPLOT']).run - # assert_equal fixture('iris-lineplot.txt'), @stderr_file.read - # end + test :lineplot do + YouPlot::Command.new(['lineplot']).run + assert_equal fixture('simple-lineplot.txt'), @stderr_file.read + end - # test :lines do - # YouPlot::Command.new(['lines', '-H', '-d,', '-t', 'IRIS-LINEPLOTS']).run - # assert_equal fixture('iris-lineplots.txt'), @stderr_file.read - # end + test :lines do + assert_raise(YouPlot::Backends::UnicodePlotBackend::Error) do + YouPlot::Command.new(['lines']).run + end + end - # test :lineplots do - # YouPlot::Command.new(['lineplots', '-H', '-d,', '-t', 'IRIS-LINEPLOTS']).run - # assert_equal fixture('iris-lineplots.txt'), @stderr_file.read - # end + test :lineplots do + assert_raise(YouPlot::Backends::UnicodePlotBackend::Error) do + YouPlot::Command.new(['lineplots']).run + end + end - # test :s do - # YouPlot::Command.new(['s', '-H', '-d,', '-t', 'IRIS-SCATTER']).run - # assert_equal fixture('iris-scatter.txt'), @stderr_file.read - # end + test :s do + assert_raise(YouPlot::Backends::UnicodePlotBackend::Error) do + YouPlot::Command.new(['s']).run + end + end - # test :scatter do - # YouPlot::Command.new(['scatter', '-H', '-d,', '-t', 'IRIS-SCATTER']).run - # assert_equal fixture('iris-scatter.txt'), @stderr_file.read - # end + test :scatter do + assert_raise(YouPlot::Backends::UnicodePlotBackend::Error) do + YouPlot::Command.new(['scatter']).run + end + end - # test :d do - # YouPlot::Command.new(['d', '-H', '-d,', '-t', 'IRIS-DENSITY']).run - # assert_equal fixture('iris-density.txt'), @stderr_file.read - # end + test :d do + assert_raise(YouPlot::Backends::UnicodePlotBackend::Error) do + YouPlot::Command.new(['d']).run + end + end - # test :density do - # YouPlot::Command.new(['density', '-H', '-d,', '-t', 'IRIS-DENSITY']).run - # assert_equal fixture('iris-density.txt'), @stderr_file.read - # end + test :density do + assert_raise(YouPlot::Backends::UnicodePlotBackend::Error) do + YouPlot::Command.new(['density']).run + end + end - # test :box do - # YouPlot::Command.new(['box', '-H', '-d,', '-t', 'IRIS-BOXPLOT']).run - # assert_equal fixture('iris-boxplot.txt'), @stderr_file.read - # end + test :box do + YouPlot::Command.new(['box']).run + assert_equal fixture('simple-boxplot.txt'), @stderr_file.read + end - # test :boxplot do - # YouPlot::Command.new(['boxplot', '-H', '-d,', '-t', 'IRIS-BOXPLOT']).run - # assert_equal fixture('iris-boxplot.txt'), @stderr_file.read - # end + test :boxplot do + YouPlot::Command.new(['boxplot']).run + assert_equal fixture('simple-boxplot.txt'), @stderr_file.read + end # test :c do # omit @@ -117,36 +124,15 @@ class YouPlotSimpleTest < Test::Unit::TestCase # assert_equal fixture('iris-count.txt'), @stderr_file.read # end - # test :plot_output_stdout do - # YouPlot::Command.new(['bar', '-o', '-H', '-d,', '-t', 'IRIS-BARPLOT']).run - # assert_equal '', @stderr_file.read - # end + test :plot_output_stdout do + YouPlot::Command.new(['line', '-o']).run + assert_equal '', @stderr_file.read + assert_equal fixture('simple-lineplot.txt'), @stdout_file.read + end - # test :data_output_stdout do - # YouPlot::Command.new(['bar', '-O', '-H', '-d,', '-t', 'IRIS-BARPLOT']).run - # assert_equal fixture('iris-barplot.txt'), @stderr_file.read - # assert_equal File.read(File.expand_path('../fixtures/iris.csv', __dir__)), @stdout_file.read - # end - - # %i[colors color colours colour].each do |cmd_name| - # test cmd_name do - # YouPlot::Command.new([cmd_name.to_s]).run - # assert_equal fixture('colors.txt'), @stderr_file.read - # assert_equal '', @stdout_file.read - # end - # end - - # test :colors_output_stdout do - # YouPlot::Command.new(['colors', '-o']).run - # assert_equal '', @stderr_file.read - # assert_equal fixture('colors.txt'), @stdout_file.read - # end - - # test :unrecognized_command do - # assert_raise(YouPlot::Command::Parser::Error) do - # YouPlot::Command.new(['abracadabra', '--hadley', '--wickham']).run - # end - # assert_equal '', @stderr_file.read - # assert_equal '', @stdout_file.read - # end + test :data_output_stdout do + YouPlot::Command.new(['box', '-O']).run + assert_equal fixture('simple-boxplot.txt'), @stderr_file.read + assert_equal fixture('simple.tsv'), @stdout_file.read + end end