Start testing the command line interface

This commit is contained in:
kojix2 2020-11-23 17:14:43 +09:00
parent 7a08d6bab9
commit 0ff8c6a9f0
3 changed files with 50 additions and 8 deletions

View File

@ -13,14 +13,15 @@ module YouPlot
attr_accessor :params attr_accessor :params
attr_reader :data, :fmt, :parser attr_reader :data, :fmt, :parser
def initialize def initialize(argv = ARGV)
@argv = argv
@params = Params.new @params = Params.new
@parser = Parser.new @parser = Parser.new
@backend = YouPlot::Backends::UnicodePlotBackend @backend = YouPlot::Backends::UnicodePlotBackend
end end
def run def run
parser.parse_options parser.parse_options(@argv)
command = parser.command command = parser.command
params = parser.params params = parser.params
delimiter = parser.delimiter delimiter = parser.delimiter
@ -62,7 +63,8 @@ module YouPlot
raise "unrecognized plot_type: #{command}" raise "unrecognized plot_type: #{command}"
end end
if output.is_a?(IO) case output
when IO
plot.render(output) plot.render(output)
else else
File.open(output, 'w') do |f| File.open(output, 'w') do |f|
@ -70,11 +72,14 @@ module YouPlot
end end
end end
if pass.is_a?(IO) case pass
print input when IO
elsif pass pass.print(input)
File.open(pass, 'w') do |f| else
f.print(input) if pass
File.open(pass, 'w') do |f|
f.print(input)
end
end end
end end
end end

20
test/fixtures/iris-scatter.txt vendored Normal file
View File

@ -0,0 +1,20 @@
IRIS
┌────────────────────────────────────────┐
6.9 │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠄⠃⠀⠀│ sepal_width
│⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡀⠀⠀⠀⠀⠀⠀⠀⠀⡄⠂⠄⠀⠀⠄⠀⠁│ petal_length
│⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⠀⠀⡀⡀⠂⠀⡆⠁⠄⠈⠀⠂⠀⠀⠀⠀⠀⠀⠀│ petal_width
│⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡀⡀⡀⠀⠂⡀⠃⡅⠀⠄⠁⡂⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ species
│⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠐⠀⠁⠀⠄⢕⠀⠄⡃⠀⠀⠀⡁⠄⠂⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀│
│⠀⠀⠀⠀⠀⠀⠀⠀⠀⠁⠀⠀⠀⠀⠁⣊⠀⡃⠀⡀⠉⠀⠅⠂⠅⠙⠀⠂⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│
│⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⠀⢅⠀⡄⡮⠀⠅⠇⠀⠒⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠄⠀⠄│
│⠀⠀⠀⠀⠀⠨⠀⠠⠀⡀⣷⠀⠆⠀⠄⠊⠀⠂⠀⠀⠄⠀⠄⡄⠀⠀⠀⡀⠀⠀⠀⠀⠁⠀⠀⠀⠀⠀⠀⠀│
│⠀⠀⠐⠀⠇⠘⠀⠑⠀⠂⠓⠀⠀⠀⠂⢰⠀⡆⡀⠃⢶⠀⡄⡄⡇⡳⠀⠂⡃⠃⠑⠀⠃⠄⡀⠀⠂⡂⠀⠀│
│⠀⠀⠀⠀⠀⠀⠀⠀⠀⡄⠠⠀⠁⠀⠀⡮⠀⠆⡃⠀⠑⠀⠀⡅⠁⠀⠀⡄⠀⠀⠀⠀⠄⠀⠀⠀⠀⠂⠀⠀│
│⠀⠀⠀⠀⠀⠁⠀⢀⠀⠀⢅⠀⠀⠀⠀⠡⠀⠄⡀⠀⠁⠀⠁⡁⡃⠅⠀⠃⠃⠃⠐⠀⠀⠀⡀⠀⠂⠅⠀⠄│
│⠀⠀⠀⠀⡀⢠⠀⢤⠀⡆⣴⠀⡤⠀⠆⡠⠀⠆⠀⠅⢍⠀⠅⠅⠅⢅⠀⡇⡀⠄⡀⠀⠅⠁⠀⠀⠀⠀⠀⠀│
│⠀⠀⠠⠀⠁⢁⠀⠁⠀⡀⡣⠀⠀⠀⠁⡯⠀⡃⡂⠀⡘⠀⠁⠁⠁⠈⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│
│⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│
0 │⠀⠀⢀⠀⡄⣲⠀⣴⠀⡄⣿⠀⣤⠀⡅⣄⠀⡃⡄⡀⣀⠀⡀⡀⡀⣀⠀⡀⡀⡀⣀⠀⡀⡀⡀⠀⡀⡀⠀⡀│
└────────────────────────────────────────┘
4 8
sepal_length

View File

@ -1,6 +1,23 @@
# frozen_string_literal: true # frozen_string_literal: true
require "tempfile"
require_relative '../test_helper' require_relative '../test_helper'
class YouPlotCommandTest < Test::Unit::TestCase 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 end