From 0ff8c6a9f0dad45d7e920c3da7c8f56d04f5743e Mon Sep 17 00:00:00 2001 From: kojix2 <2xijok@gmail.com> Date: Mon, 23 Nov 2020 17:14:43 +0900 Subject: [PATCH] Start testing the command line interface --- lib/youplot/command.rb | 21 +++++++++++++-------- test/fixtures/iris-scatter.txt | 20 ++++++++++++++++++++ test/uplot/command_test.rb | 17 +++++++++++++++++ 3 files changed, 50 insertions(+), 8 deletions(-) create mode 100644 test/fixtures/iris-scatter.txt diff --git a/lib/youplot/command.rb b/lib/youplot/command.rb index 8d392ac..fa0e7b6 100644 --- a/lib/youplot/command.rb +++ b/lib/youplot/command.rb @@ -13,14 +13,15 @@ module YouPlot attr_accessor :params attr_reader :data, :fmt, :parser - def initialize + def initialize(argv = ARGV) + @argv = argv @params = Params.new @parser = Parser.new @backend = YouPlot::Backends::UnicodePlotBackend end def run - parser.parse_options + parser.parse_options(@argv) command = parser.command params = parser.params delimiter = parser.delimiter @@ -62,7 +63,8 @@ module YouPlot raise "unrecognized plot_type: #{command}" end - if output.is_a?(IO) + case output + when IO plot.render(output) else File.open(output, 'w') do |f| @@ -70,11 +72,14 @@ module YouPlot end end - if pass.is_a?(IO) - print input - elsif pass - File.open(pass, 'w') do |f| - f.print(input) + case pass + when IO + pass.print(input) + else + if pass + File.open(pass, 'w') do |f| + f.print(input) + end end end end diff --git a/test/fixtures/iris-scatter.txt b/test/fixtures/iris-scatter.txt new file mode 100644 index 0000000..8020adb --- /dev/null +++ b/test/fixtures/iris-scatter.txt @@ -0,0 +1,20 @@ + IRIS + ┌────────────────────────────────────────┐ + 6.9 │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠄⠃⠀⠀│ sepal_width + │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡀⠀⠀⠀⠀⠀⠀⠀⠀⡄⠂⠄⠀⠀⠄⠀⠁│ petal_length + │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⠀⠀⡀⡀⠂⠀⡆⠁⠄⠈⠀⠂⠀⠀⠀⠀⠀⠀⠀│ petal_width + │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡀⡀⡀⠀⠂⡀⠃⡅⠀⠄⠁⡂⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ species + │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠐⠀⠁⠀⠄⢕⠀⠄⡃⠀⠀⠀⡁⠄⠂⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ + │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠁⠀⠀⠀⠀⠁⣊⠀⡃⠀⡀⠉⠀⠅⠂⠅⠙⠀⠂⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ + │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⠀⢅⠀⡄⡮⠀⠅⠇⠀⠒⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠄⠀⠄│ + │⠀⠀⠀⠀⠀⠨⠀⠠⠀⡀⣷⠀⠆⠀⠄⠊⠀⠂⠀⠀⠄⠀⠄⡄⠀⠀⠀⡀⠀⠀⠀⠀⠁⠀⠀⠀⠀⠀⠀⠀│ + │⠀⠀⠐⠀⠇⠘⠀⠑⠀⠂⠓⠀⠀⠀⠂⢰⠀⡆⡀⠃⢶⠀⡄⡄⡇⡳⠀⠂⡃⠃⠑⠀⠃⠄⡀⠀⠂⡂⠀⠀│ + │⠀⠀⠀⠀⠀⠀⠀⠀⠀⡄⠠⠀⠁⠀⠀⡮⠀⠆⡃⠀⠑⠀⠀⡅⠁⠀⠀⡄⠀⠀⠀⠀⠄⠀⠀⠀⠀⠂⠀⠀│ + │⠀⠀⠀⠀⠀⠁⠀⢀⠀⠀⢅⠀⠀⠀⠀⠡⠀⠄⡀⠀⠁⠀⠁⡁⡃⠅⠀⠃⠃⠃⠐⠀⠀⠀⡀⠀⠂⠅⠀⠄│ + │⠀⠀⠀⠀⡀⢠⠀⢤⠀⡆⣴⠀⡤⠀⠆⡠⠀⠆⠀⠅⢍⠀⠅⠅⠅⢅⠀⡇⡀⠄⡀⠀⠅⠁⠀⠀⠀⠀⠀⠀│ + │⠀⠀⠠⠀⠁⢁⠀⠁⠀⡀⡣⠀⠀⠀⠁⡯⠀⡃⡂⠀⡘⠀⠁⠁⠁⠈⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ + │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ + 0 │⠀⠀⢀⠀⡄⣲⠀⣴⠀⡄⣿⠀⣤⠀⡅⣄⠀⡃⡄⡀⣀⠀⡀⡀⡀⣀⠀⡀⡀⡀⣀⠀⡀⡀⡀⠀⡀⡀⠀⡀│ + └────────────────────────────────────────┘ + 4 8 + sepal_length diff --git a/test/uplot/command_test.rb b/test/uplot/command_test.rb index 491504b..b50776a 100644 --- a/test/uplot/command_test.rb +++ b/test/uplot/command_test.rb @@ -1,6 +1,23 @@ # frozen_string_literal: true +require "tempfile" require_relative '../test_helper' class YouPlotCommandTest < Test::Unit::TestCase + def setup + @ta = "ta" + @stdin = $stdin.dup + @stderr = $stderr.dup + end + + test :scatter do + $stdin = File.open(File.expand_path("../fixtures/iris.csv", __dir__), "r") + Tempfile.new("iris-scatter") do |tmp_file| + $stderr = tmp_file + YouPlot::Command.new(["scatter", "-H", "-d,", "-t", "IRIS"]).run + assert_equal File.read(File.expand_path("../fixtures/iris-scatter.txt", __dir__)), tmp_file.read + end + $stdin = @stdin + $stderr = @stderr + end end