From 1b666038bbfa64f65c2f86cd4d6e0008bb3749b6 Mon Sep 17 00:00:00 2001 From: kojix2 <2xijok@gmail.com> Date: Wed, 16 Dec 2020 12:50:06 +0900 Subject: [PATCH] Changes the default value to stdout if no argument is specified with the `-o` option #9 --- lib/youplot/command/parser.rb | 7 ++--- test/youplot/command_test.rb | 49 +++++++++++++++++++++++------------ 2 files changed, 36 insertions(+), 20 deletions(-) diff --git a/lib/youplot/command/parser.rb b/lib/youplot/command/parser.rb index 3ca9a51..3458a5e 100644 --- a/lib/youplot/command/parser.rb +++ b/lib/youplot/command/parser.rb @@ -35,12 +35,13 @@ module YouPlot opt.on_tail('') # Add a blank line at the end opt.separator('') 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| @options[:pass] = v || $stdout end - opt.on('-o', '--output VAL', 'file to output results to [stderr]') do |v| - @options[:output] = v + opt.on('-o', '--output [FILE]', 'file to output plots to [stdout]', + 'If no option is specified, plot will print to stderr') do |v| + @options[:output] = v || $stdout end opt.on('-d', '--delimiter VAL', String, 'use DELIM instead of TAB for field delimiter') do |v| @options[:delimiter] = v diff --git a/test/youplot/command_test.rb b/test/youplot/command_test.rb index 91a71c5..1bddbfb 100644 --- a/test/youplot/command_test.rb +++ b/test/youplot/command_test.rb @@ -7,23 +7,27 @@ class YouPlotCommandTest < 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/iris.csv', __dir__), 'r') - @tmp_file = Tempfile.new - $stderr = @tmp_file + @stderr_file = Tempfile.new + @stdout_file = Tempfile.new + $stderr = @stderr_file + $stdout = @stdout_file end def teardown - @tmp_file.close + @stderr_file.close end def fixture(fname) @@ -32,71 +36,82 @@ class YouPlotCommandTest < Test::Unit::TestCase test :bar do 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 test :barplot do 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 test :hist do 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 test :histogram do 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 test :line do 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 test :lineplot do 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 test :lines do 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 test :lineplots do 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 test :s do 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 test :scatter do 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 test :d do 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 test :density do 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 test :box do 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 test :boxplot do 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