From 3bbcea165f0223782470573b710e6da408d5570e Mon Sep 17 00:00:00 2001 From: kojix2 <2xijok@gmail.com> Date: Wed, 20 Jan 2021 21:41:54 +0900 Subject: [PATCH] Improved error message in barplot --- lib/youplot/backends/unicode_plot_backend.rb | 14 +- test/fixtures/simple.tsv | 10 ++ .../youplot/{command_test.rb => iris_test.rb} | 2 +- test/youplot/simple_test.rb | 152 ++++++++++++++++++ 4 files changed, 176 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/simple.tsv rename test/youplot/{command_test.rb => iris_test.rb} (98%) create mode 100644 test/youplot/simple_test.rb diff --git a/lib/youplot/backends/unicode_plot_backend.rb b/lib/youplot/backends/unicode_plot_backend.rb index 7a21f29..d9a5ff8 100644 --- a/lib/youplot/backends/unicode_plot_backend.rb +++ b/lib/youplot/backends/unicode_plot_backend.rb @@ -37,7 +37,19 @@ module YouPlot labels = series[x_col] values = series[y_col].map(&:to_f) end - UnicodePlot.barplot(labels, values, **params.to_hc) + begin + UnicodePlot.barplot(labels, values, **params.to_hc) + # UnicodePlot error: + # All values have to be positive. Negative bars are not supported. + rescue ArgumentError => e + if YouPlot.run_as_executable? + warn e.backtrace[0] + warn "\e[35m#{e}\e[0m" + exit 1 + else + raise e + end + end end def histogram(data, params) diff --git a/test/fixtures/simple.tsv b/test/fixtures/simple.tsv new file mode 100644 index 0000000..770e789 --- /dev/null +++ b/test/fixtures/simple.tsv @@ -0,0 +1,10 @@ +-10 +10 +-20 +20 +-30 +30 +-40 +40 +-50 +50 diff --git a/test/youplot/command_test.rb b/test/youplot/iris_test.rb similarity index 98% rename from test/youplot/command_test.rb rename to test/youplot/iris_test.rb index e3ba15a..340f506 100644 --- a/test/youplot/command_test.rb +++ b/test/youplot/iris_test.rb @@ -3,7 +3,7 @@ require 'tempfile' require_relative '../test_helper' -class YouPlotCommandTest < Test::Unit::TestCase +class YouPlotIRISTest < Test::Unit::TestCase class << self def startup @stdin = $stdin.dup diff --git a/test/youplot/simple_test.rb b/test/youplot/simple_test.rb new file mode 100644 index 0000000..008d5ef --- /dev/null +++ b/test/youplot/simple_test.rb @@ -0,0 +1,152 @@ +# frozen_string_literal: true + +require 'tempfile' +require_relative '../test_helper' + +class YouPlotSimpleTest < Test::Unit::TestCase + class << self + def startup + @stdin = $stdin.dup + @stdout = $stdout.dup + @stderr = $stderr.dup + end + + def shutdown + $stdin = @stdin + $stdout = @stdout + $stderr = @stderr + end + end + + def setup + $stdin = File.open(File.expand_path('../fixtures/simple.tsv', __dir__), 'r') + @stderr_file = Tempfile.new + @stdout_file = Tempfile.new + $stderr = @stderr_file + $stdout = @stdout_file + end + + def teardown + @stderr_file.close + end + + def fixture(fname) + File.read(File.expand_path("../fixtures/#{fname}", __dir__)) + end + + test :bar do + assert_raise(ArgumentError) do + YouPlot::Command.new(['bar']).run + 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 :hist do + # YouPlot::Command.new(['hist', '-H', '-d,', '-t', 'IRIS-HISTOGRAM']).run + # assert_equal fixture('iris-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 :line do + # YouPlot::Command.new(['line', '-H', '-d,', '-t', 'IRIS-LINEPLOT']).run + # assert_equal fixture('iris-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 :lines do + # YouPlot::Command.new(['lines', '-H', '-d,', '-t', 'IRIS-LINEPLOTS']).run + # assert_equal fixture('iris-lineplots.txt'), @stderr_file.read + # 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 :s do + # YouPlot::Command.new(['s', '-H', '-d,', '-t', 'IRIS-SCATTER']).run + # assert_equal fixture('iris-scatter.txt'), @stderr_file.read + # 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 :d do + # YouPlot::Command.new(['d', '-H', '-d,', '-t', 'IRIS-DENSITY']).run + # assert_equal fixture('iris-density.txt'), @stderr_file.read + # 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 :box do + # YouPlot::Command.new(['box', '-H', '-d,', '-t', 'IRIS-BOXPLOT']).run + # assert_equal fixture('iris-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 :c do + # omit + # 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 :plot_output_stdout do + # YouPlot::Command.new(['bar', '-o', '-H', '-d,', '-t', 'IRIS-BARPLOT']).run + # assert_equal '', @stderr_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 +end