Changes the default value to stdout if no argument is specified with the -o option

#9
This commit is contained in:
kojix2 2020-12-16 12:50:06 +09:00
parent b368dc44b4
commit 1b666038bb
2 changed files with 36 additions and 20 deletions

View File

@ -35,12 +35,13 @@ module YouPlot
opt.on_tail('') # Add a blank line at the end opt.on_tail('') # Add a blank line at the end
opt.separator('') opt.separator('')
opt.on('Common options:') opt.on('Common options:')
opt.on('-O', '--pass [VAL]', 'file to output standard input data to [stdout]', opt.on('-O', '--pass [FILE]', 'file to output input data to [stdout]',
'for inserting YouPlot in the middle of Unix pipes') do |v| 'for inserting YouPlot in the middle of Unix pipes') do |v|
@options[:pass] = v || $stdout @options[:pass] = v || $stdout
end end
opt.on('-o', '--output VAL', 'file to output results to [stderr]') do |v| opt.on('-o', '--output [FILE]', 'file to output plots to [stdout]',
@options[:output] = v 'If no option is specified, plot will print to stderr') do |v|
@options[:output] = v || $stdout
end end
opt.on('-d', '--delimiter VAL', String, 'use DELIM instead of TAB for field delimiter') do |v| opt.on('-d', '--delimiter VAL', String, 'use DELIM instead of TAB for field delimiter') do |v|
@options[:delimiter] = v @options[:delimiter] = v

View File

@ -7,23 +7,27 @@ class YouPlotCommandTest < Test::Unit::TestCase
class << self class << self
def startup def startup
@stdin = $stdin.dup @stdin = $stdin.dup
@stdout = $stdout.dup
@stderr = $stderr.dup @stderr = $stderr.dup
end end
def shutdown def shutdown
$stdin = @stdin $stdin = @stdin
$stdout = @stdout
$stderr = @stderr $stderr = @stderr
end end
end end
def setup def setup
$stdin = File.open(File.expand_path('../fixtures/iris.csv', __dir__), 'r') $stdin = File.open(File.expand_path('../fixtures/iris.csv', __dir__), 'r')
@tmp_file = Tempfile.new @stderr_file = Tempfile.new
$stderr = @tmp_file @stdout_file = Tempfile.new
$stderr = @stderr_file
$stdout = @stdout_file
end end
def teardown def teardown
@tmp_file.close @stderr_file.close
end end
def fixture(fname) def fixture(fname)
@ -32,71 +36,82 @@ class YouPlotCommandTest < Test::Unit::TestCase
test :bar do test :bar do
YouPlot::Command.new(['bar', '-H', '-d,', '-t', 'IRIS-BARPLOT']).run YouPlot::Command.new(['bar', '-H', '-d,', '-t', 'IRIS-BARPLOT']).run
assert_equal fixture('iris-barplot.txt'), @tmp_file.read assert_equal fixture('iris-barplot.txt'), @stderr_file.read
end end
test :barplot do test :barplot do
YouPlot::Command.new(['barplot', '-H', '-d,', '-t', 'IRIS-BARPLOT']).run YouPlot::Command.new(['barplot', '-H', '-d,', '-t', 'IRIS-BARPLOT']).run
assert_equal fixture('iris-barplot.txt'), @tmp_file.read assert_equal fixture('iris-barplot.txt'), @stderr_file.read
end end
test :hist do test :hist do
YouPlot::Command.new(['hist', '-H', '-d,', '-t', 'IRIS-HISTOGRAM']).run YouPlot::Command.new(['hist', '-H', '-d,', '-t', 'IRIS-HISTOGRAM']).run
assert_equal fixture('iris-histogram.txt'), @tmp_file.read assert_equal fixture('iris-histogram.txt'), @stderr_file.read
end end
test :histogram do test :histogram do
YouPlot::Command.new(['histogram', '-H', '-d,', '-t', 'IRIS-HISTOGRAM']).run YouPlot::Command.new(['histogram', '-H', '-d,', '-t', 'IRIS-HISTOGRAM']).run
assert_equal fixture('iris-histogram.txt'), @tmp_file.read assert_equal fixture('iris-histogram.txt'), @stderr_file.read
end end
test :line do test :line do
YouPlot::Command.new(['line', '-H', '-d,', '-t', 'IRIS-LINEPLOT']).run YouPlot::Command.new(['line', '-H', '-d,', '-t', 'IRIS-LINEPLOT']).run
assert_equal fixture('iris-lineplot.txt'), @tmp_file.read assert_equal fixture('iris-lineplot.txt'), @stderr_file.read
end end
test :lineplot do test :lineplot do
YouPlot::Command.new(['lineplot', '-H', '-d,', '-t', 'IRIS-LINEPLOT']).run YouPlot::Command.new(['lineplot', '-H', '-d,', '-t', 'IRIS-LINEPLOT']).run
assert_equal fixture('iris-lineplot.txt'), @tmp_file.read assert_equal fixture('iris-lineplot.txt'), @stderr_file.read
end end
test :lines do test :lines do
YouPlot::Command.new(['lines', '-H', '-d,', '-t', 'IRIS-LINEPLOTS']).run YouPlot::Command.new(['lines', '-H', '-d,', '-t', 'IRIS-LINEPLOTS']).run
assert_equal fixture('iris-lineplots.txt'), @tmp_file.read assert_equal fixture('iris-lineplots.txt'), @stderr_file.read
end end
test :lineplots do test :lineplots do
YouPlot::Command.new(['lineplots', '-H', '-d,', '-t', 'IRIS-LINEPLOTS']).run YouPlot::Command.new(['lineplots', '-H', '-d,', '-t', 'IRIS-LINEPLOTS']).run
assert_equal fixture('iris-lineplots.txt'), @tmp_file.read assert_equal fixture('iris-lineplots.txt'), @stderr_file.read
end end
test :s do test :s do
YouPlot::Command.new(['s', '-H', '-d,', '-t', 'IRIS-SCATTER']).run YouPlot::Command.new(['s', '-H', '-d,', '-t', 'IRIS-SCATTER']).run
assert_equal fixture('iris-scatter.txt'), @tmp_file.read assert_equal fixture('iris-scatter.txt'), @stderr_file.read
end end
test :scatter do test :scatter do
YouPlot::Command.new(['scatter', '-H', '-d,', '-t', 'IRIS-SCATTER']).run YouPlot::Command.new(['scatter', '-H', '-d,', '-t', 'IRIS-SCATTER']).run
assert_equal fixture('iris-scatter.txt'), @tmp_file.read assert_equal fixture('iris-scatter.txt'), @stderr_file.read
end end
test :d do test :d do
YouPlot::Command.new(['d', '-H', '-d,', '-t', 'IRIS-DENSITY']).run YouPlot::Command.new(['d', '-H', '-d,', '-t', 'IRIS-DENSITY']).run
assert_equal fixture('iris-density.txt'), @tmp_file.read assert_equal fixture('iris-density.txt'), @stderr_file.read
end end
test :density do test :density do
YouPlot::Command.new(['density', '-H', '-d,', '-t', 'IRIS-DENSITY']).run YouPlot::Command.new(['density', '-H', '-d,', '-t', 'IRIS-DENSITY']).run
assert_equal fixture('iris-density.txt'), @tmp_file.read assert_equal fixture('iris-density.txt'), @stderr_file.read
end end
test :box do test :box do
YouPlot::Command.new(['box', '-H', '-d,', '-t', 'IRIS-BOXPLOT']).run YouPlot::Command.new(['box', '-H', '-d,', '-t', 'IRIS-BOXPLOT']).run
assert_equal fixture('iris-boxplot.txt'), @tmp_file.read assert_equal fixture('iris-boxplot.txt'), @stderr_file.read
end end
test :boxplot do test :boxplot do
YouPlot::Command.new(['boxplot', '-H', '-d,', '-t', 'IRIS-BOXPLOT']).run YouPlot::Command.new(['boxplot', '-H', '-d,', '-t', 'IRIS-BOXPLOT']).run
assert_equal fixture('iris-boxplot.txt'), @tmp_file.read assert_equal fixture('iris-boxplot.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 end
end end