mirror of
https://github.com/red-data-tools/YouPlot.git
synced 2025-05-07 23:51:12 +08:00
Add test for unrecognized command
This commit is contained in:
parent
8db1306e07
commit
991cf90267
@ -8,8 +8,10 @@ require 'youplot/command'
|
|||||||
module YouPlot
|
module YouPlot
|
||||||
class << self
|
class << self
|
||||||
attr_accessor :run_as_executable
|
attr_accessor :run_as_executable
|
||||||
|
|
||||||
def run_as_executable?
|
def run_as_executable?
|
||||||
@run_as_executable
|
@run_as_executable
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@run_as_executable = false
|
||||||
end
|
end
|
||||||
|
@ -7,6 +7,8 @@ require_relative 'plot_params'
|
|||||||
module YouPlot
|
module YouPlot
|
||||||
class Command
|
class Command
|
||||||
class Parser
|
class Parser
|
||||||
|
class Error < StandardError; end
|
||||||
|
|
||||||
attr_reader :command, :options, :params,
|
attr_reader :command, :options, :params,
|
||||||
:main_parser, :sub_parser
|
:main_parser, :sub_parser
|
||||||
|
|
||||||
@ -193,7 +195,7 @@ module YouPlot
|
|||||||
when nil
|
when nil
|
||||||
warn main_parser.banner
|
warn main_parser.banner
|
||||||
warn "\n"
|
warn "\n"
|
||||||
exit 1
|
exit 1 if YouPlot.run_as_executable?
|
||||||
|
|
||||||
when :barplot, :bar
|
when :barplot, :bar
|
||||||
sub_parser_add_symbol
|
sub_parser_add_symbol
|
||||||
@ -263,8 +265,13 @@ module YouPlot
|
|||||||
end
|
end
|
||||||
|
|
||||||
else
|
else
|
||||||
warn "uplot: unrecognized command '#{command}'"
|
error_message = "uplot: unrecognized command '#{command}'"
|
||||||
exit 1
|
if YouPlot.run_as_executable?
|
||||||
|
warn error_message
|
||||||
|
exit 1
|
||||||
|
else
|
||||||
|
raise Error, error_message
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -273,16 +280,16 @@ module YouPlot
|
|||||||
create_main_parser.order!(argv)
|
create_main_parser.order!(argv)
|
||||||
rescue OptionParser::ParseError => e
|
rescue OptionParser::ParseError => e
|
||||||
warn "uplot: #{e.message}"
|
warn "uplot: #{e.message}"
|
||||||
exit 1
|
exit 1 if YouPlot.run_as_executable?
|
||||||
end
|
end
|
||||||
|
|
||||||
@command = argv.shift&.to_sym
|
@command = argv.shift&.to_sym
|
||||||
|
|
||||||
begin
|
begin
|
||||||
create_sub_parser.parse!(argv)
|
create_sub_parser&.parse!(argv)
|
||||||
rescue OptionParser::ParseError => e
|
rescue OptionParser::ParseError => e
|
||||||
warn "uplot: #{e.message}"
|
warn "uplot: #{e.message}"
|
||||||
exit 1
|
exit 1 if YouPlot.run_as_executable?
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -138,4 +138,12 @@ class YouPlotCommandTest < Test::Unit::TestCase
|
|||||||
assert_equal '', @stderr_file.read
|
assert_equal '', @stderr_file.read
|
||||||
assert_equal fixture('colors.txt'), @stdout_file.read
|
assert_equal fixture('colors.txt'), @stdout_file.read
|
||||||
end
|
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
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user