Add test for unrecognized command

This commit is contained in:
kojix2 2021-01-20 00:57:56 +09:00
parent 8db1306e07
commit 991cf90267
3 changed files with 23 additions and 6 deletions

View File

@ -8,8 +8,10 @@ require 'youplot/command'
module YouPlot
class << self
attr_accessor :run_as_executable
def run_as_executable?
@run_as_executable
end
end
@run_as_executable = false
end

View File

@ -7,6 +7,8 @@ require_relative 'plot_params'
module YouPlot
class Command
class Parser
class Error < StandardError; end
attr_reader :command, :options, :params,
:main_parser, :sub_parser
@ -193,7 +195,7 @@ module YouPlot
when nil
warn main_parser.banner
warn "\n"
exit 1
exit 1 if YouPlot.run_as_executable?
when :barplot, :bar
sub_parser_add_symbol
@ -263,8 +265,13 @@ module YouPlot
end
else
warn "uplot: unrecognized command '#{command}'"
exit 1
error_message = "uplot: unrecognized command '#{command}'"
if YouPlot.run_as_executable?
warn error_message
exit 1
else
raise Error, error_message
end
end
end
@ -273,16 +280,16 @@ module YouPlot
create_main_parser.order!(argv)
rescue OptionParser::ParseError => e
warn "uplot: #{e.message}"
exit 1
exit 1 if YouPlot.run_as_executable?
end
@command = argv.shift&.to_sym
begin
create_sub_parser.parse!(argv)
create_sub_parser&.parse!(argv)
rescue OptionParser::ParseError => e
warn "uplot: #{e.message}"
exit 1
exit 1 if YouPlot.run_as_executable?
end
end
end

View File

@ -138,4 +138,12 @@ class YouPlotCommandTest < Test::Unit::TestCase
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