Skip to content
This repository has been archived by the owner on May 1, 2021. It is now read-only.

Commit

Permalink
complete command line tests
Browse files Browse the repository at this point in the history
  • Loading branch information
subosito committed Apr 30, 2013
1 parent c78ad1a commit b6940f1
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 24 deletions.
44 changes: 23 additions & 21 deletions lib/gingerice/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,26 @@
module Gingerice
class Command

attr_reader :args, :oparser
attr_reader :args, :args_parser, :options

def initialize(args)
@args = args
end
@args << '-h' if @args.empty?

def processor
options = Gingerice::Parser.default_options
options[:verbose] = false
@options = Gingerice::Parser.default_options.merge({ :verbose => false })
@args_parser = args_parser
end

@oparser = OptionParser.new do |opt|
def args_parser
OptionParser.new do |opt|
opt.banner = 'Usage: gingerice [options] "some texts"'

opt.on("--api-endpoint API_ENDPOINT", "Set API endpoint") do |endpoint|
options[:api_endpoint] = endpoint
end

opt.on("--api-version API_VERSION", "Set API version") do |version|
options[:api_endpoint] = version
options[:api_version] = version
end

opt.on("--api-key API_KEY", "Set API key") do |api_key|
Expand All @@ -40,30 +41,31 @@ def processor
end

opt.on("--version", "Show version") do
puts Gingerice::VERSION
exit
options[:show] = :version
end

opt.on_tail("-h", "--help", "Show this message") do
puts opt
exit
options[:show] = :help
end
end

@oparser.parse!(args)
options
opt.parse!(args)
end
end

def execute
options = processor
if options.has_key?(:show)

if args.empty?
puts oparser
else
parser_options = options.reject { |key, value| key == :verbose }
case options[:show]
when :help
puts args_parser
when :version
puts "Gingerice: #{Gingerice::VERSION}"
end

parser = Parser.new(parser_options)
response = parser.parse(args.last)
else
parser_opts = options.select { |k, _| Parser.default_options.keys.include?(k) }
parser = Parser.new(parser_opts)
response = parser.parse(args.last)

if options[:verbose]
ap response
Expand Down
49 changes: 46 additions & 3 deletions test/test_gingerice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ def capture_stdout
end

require 'test/unit'
require 'gingerice/parser'
require 'gingerice/command'

class TestGingerice < Test::Unit::TestCase
Expand Down Expand Up @@ -66,20 +65,64 @@ def test_exceptions
assert_equal 'getaddrinfo: Name or service not known', exception.message
end

def test_command_usage
def test_command_simple_output
output = capture_stdout do
command = Gingerice::Command.new(["Edwards will be sck yesterday"])
command.execute
end
assert_equal "Edwards was sick yesterday\n", output.string
end

def test_command_help
def test_command_verbose_output
output = capture_stdout do
command = Gingerice::Command.new(["-v", "He flyed to Jakarta"])
command.execute
end
assert_match 'corrections', output.string
end

def test_command_help_usage
output = capture_stdout do
command = Gingerice::Command.new([])
command.execute
end
assert_match "Usage:", output.string
end

def test_command_show_version
output = capture_stdout do
command = Gingerice::Command.new(['--version'])
command.execute
end
assert_equal "Gingerice: #{Gingerice::VERSION}\n", output.string
end

def test_command_arg_api_endpoint
command = Gingerice::Command.new(['--api-endpoint', 'http://example.id/'])
options = command.options

assert_equal "http://example.id/", options[:api_endpoint]
end

def test_command_arg_api_version
command = Gingerice::Command.new(['--api-version', '1.0'])
options = command.options

assert_equal "1.0", options[:api_version]
end

def test_command_arg_api_key
command = Gingerice::Command.new(['--api-key', '123456'])
options = command.options

assert_equal "123456", options[:api_key]
end

def test_command_arg_lang
command = Gingerice::Command.new(['--lang', 'ID'])
options = command.options

assert_equal "ID", options[:lang]
end
end

0 comments on commit b6940f1

Please sign in to comment.