Skip to content

Commit

Permalink
Use test utils
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Nov 25, 2012
1 parent fd88368 commit 8d93363
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 27 deletions.
10 changes: 5 additions & 5 deletions test/command/test-table-create.rb
Expand Up @@ -18,15 +18,16 @@

class TableCreateCommandTest < Test::Unit::TestCase
class CommandLineTest < self
include GroongaCommandTestUtils::CommandLineCommandParser

def test_ordered_arguments
name = "Users"
flags = "TABLE_PAT_KEY"
key_type = "ShortText"
value_type = "UInt32"
default_tokenizer = "TokenBigram"

command = parse("table_create",
name, flags, key_type, value_type,
command = parse(name, flags, key_type, value_type,
default_tokenizer)
assert_instance_of(Groonga::Command::TableCreate, command)
assert_equal({
Expand All @@ -40,9 +41,8 @@ def test_ordered_arguments
end

private
def parse(command, *parameters)
command_line = "#{command} " + parameters.join(" ")
Groonga::Command::Parser.parse(command_line)
def parse(*arguments)
super("table_create", *arguments, :output_type => false)
end
end
end
9 changes: 5 additions & 4 deletions test/command/test-table-remove.rb
Expand Up @@ -18,10 +18,12 @@

class TableRemoveCommandTest < Test::Unit::TestCase
class CommandLineTest < self
include GroongaCommandTestUtils::CommandLineCommandParser

def test_ordered_arguments
name = "Users"

command = parse("table_remove", name)
command = parse(name)
assert_instance_of(Groonga::Command::TableRemove, command)
assert_equal({
:name => name,
Expand All @@ -30,9 +32,8 @@ def test_ordered_arguments
end

private
def parse(command, *parameters)
command_line = "#{command} " + parameters.join(" ")
Groonga::Command::Parser.parse(command_line)
def parse(*arguments)
super("table_remove", *arguments, :output_type => false)
end
end
end
9 changes: 5 additions & 4 deletions test/command/test-table-rename.rb
Expand Up @@ -18,11 +18,13 @@

class TableRenameCommandTest < Test::Unit::TestCase
class CommandLineTest < self
include GroongaCommandTestUtils::CommandLineCommandParser

def test_ordered_arguments
name = "Users"
new_name = "People"

command = parse("table_rename", name, new_name)
command = parse(name, new_name)
assert_instance_of(Groonga::Command::TableRename, command)
assert_equal({
:name => name,
Expand All @@ -32,9 +34,8 @@ def test_ordered_arguments
end

private
def parse(command, *arguments)
command_line = "#{command} " + arguments.join(" ")
Groonga::Command::Parser.parse(command_line)
def parse(*arguments)
super("table_rename", arguments, :output_type => false)
end
end
end
56 changes: 42 additions & 14 deletions test/groonga-command-test-utils.rb
Expand Up @@ -28,47 +28,75 @@ def command(name, arguments)
Groonga::Command.find(name).new(name, arguments)
end

def parse_http_path(command, arguments)
path = "/d/#{command}.json"
def parse_http_path(command, arguments, options={})
path = "/d/#{command}"
case options[:output_type]
when false
when nil
path << ".json"
else
path << ".#{options[:output_type]}"
end

unless arguments.empty?
uri_arguments = arguments.collect do |key, value|
[CGI.escape(key.to_s), CGI.escape(value.to_s)].join("=")
end
path << "?"
path << uri_arguments.join("&")
end

Groonga::Command::Parser.parse(path)
end

def parse_command_line(command, arguments)
command_line = "#{command} --output_type json"
arguments.each do |key, value|
if /"| / =~ value
escaped_value = '"' + value.gsub(/"/, '\"') + '"'
else
escaped_value = value
def parse_command_line(command, arguments, options={})
command_line = "#{command}"
case options[:output_type]
when false
when nil
command_line << " --output_type json"
else
command_line << " --output_type #{options[:output_type]}"
end

if arguments.is_a?(Hash)
arguments.each do |key, value|
escaped_value = escape_command_line_value(value)
command_line << " --#{key} #{escaped_value}"
end
else
arguments.each do |argument|
command_line << " #{escape_command_line_value(argument)}"
end
command_line << " --#{key} #{escaped_value}"
end

Groonga::Command::Parser.parse(command_line)
end

def escape_command_line_value(value)
if /"| / =~ value
'"' + value.gsub(/"/, '\"') + '"'
else
value
end
end
end

module HTTPCommandParser
include CommandParser

private
def parse(command, arguments={})
parse_http_path(command, arguments)
def parse(command, arguments={}, options={})
parse_http_path(command, arguments, options)
end
end

module CommandLineCommandParser
include CommandParser

private
def parse(command, arguments={})
parse_command_line(command, arguments)
def parse(command, arguments={}, options={})
parse_command_line(command, arguments, options)
end
end
end

0 comments on commit 8d93363

Please sign in to comment.