Improved error message in barplot

This commit is contained in:
kojix2 2021-01-20 21:41:54 +09:00
parent 3b34ca0e27
commit 3bbcea165f
4 changed files with 176 additions and 2 deletions

View File

@ -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)

10
test/fixtures/simple.tsv vendored Normal file
View File

@ -0,0 +1,10 @@
-10
10
-20
20
-30
30
-40
40
-50
50
1 -10
2 10
3 -20
4 20
5 -30
6 30
7 -40
8 40
9 -50
10 50

View File

@ -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

152
test/youplot/simple_test.rb Normal file
View File

@ -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