Kouhei Sutou
null+****@clear*****
Wed Jan 13 17:29:47 JST 2016
Kouhei Sutou 2013-09-29 18:08:24 +0900 (Sun, 29 Sep 2013) New Revision: 15254347df0f9545534be7faa44620be7a7779b7 https://github.com/groonga/groonga-command/commit/15254347df0f9545534be7faa44620be7a7779b7 Message: Extract parser as groonga-command-parser gem Because it requires YAJL. It is extension library. Now, groonga-command doesn't depend on any exntesion libraries. It is easy to install. Removed files: lib/groonga/command/parser.rb test/test-parser.rb Modified files: README.md groonga-command.gemspec lib/groonga/command.rb test/command/test-column-create.rb test/command/test-column-remove.rb test/command/test-column-rename.rb test/command/test-delete.rb test/command/test-dump.rb test/command/test-get.rb test/command/test-load.rb test/command/test-register.rb test/command/test-select.rb test/command/test-suggest.rb test/command/test-table-create.rb test/command/test-table-remove.rb test/command/test-table-rename.rb test/command/test-truncate.rb test/groonga-command-test-utils.rb Modified: README.md (+3 -3) =================================================================== --- README.md 2013-09-18 17:25:21 +0900 (c691c72) +++ README.md 2013-09-29 18:08:24 +0900 (f743183) @@ -6,9 +6,9 @@ groonga-command ## Description -Groonga-command is a library to process -[groonga](http://groonga.org/)'s command syntax. You can write a -program to process groonga's command by using groonga-command. +Groonga-command is a library that represents +[groonga](http://groonga.org/)'s command. You can write a program that +handle groonga's command by using groonga-command. ## Install Modified: groonga-command.gemspec (+1 -3) =================================================================== --- groonga-command.gemspec 2013-09-18 17:25:21 +0900 (9d3e079) +++ groonga-command.gemspec 2013-09-29 18:08:24 +0900 (83fbe82) @@ -1,6 +1,6 @@ # -*- mode: ruby; coding: utf-8 -*- # -# Copyright (C) 2012 Kouhei Sutou <kou �� clear-code.com> +# Copyright (C) 2012-2013 Kouhei Sutou <kou �� clear-code.com> # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public @@ -52,8 +52,6 @@ Gem::Specification.new do |spec| spec.licenses = ["LGPLv2.1+"] spec.require_paths = ["lib"] - spec.add_runtime_dependency("yajl-ruby") - spec.add_development_dependency("test-unit") spec.add_development_dependency("test-unit-notify") spec.add_development_dependency("rake") Modified: lib/groonga/command.rb (+18 -2) =================================================================== --- lib/groonga/command.rb 2013-09-18 17:25:21 +0900 (b0066a7) +++ lib/groonga/command.rb 2013-09-29 18:08:24 +0900 (acdd9c6) @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- # -# Copyright (C) 2012 Kouhei Sutou <kou �� clear-code.com> +# Copyright (C) 2012-2013 Kouhei Sutou <kou �� clear-code.com> # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public @@ -17,4 +17,20 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA require "groonga/command/version" -require "groonga/command/parser" + +require "groonga/command/error" + +require "groonga/command/column-create" +require "groonga/command/column-remove" +require "groonga/command/column-rename" +require "groonga/command/delete" +require "groonga/command/dump" +require "groonga/command/get" +require "groonga/command/load" +require "groonga/command/register" +require "groonga/command/select" +require "groonga/command/suggest" +require "groonga/command/table-create" +require "groonga/command/table-remove" +require "groonga/command/table-rename" +require "groonga/command/truncate" Deleted: lib/groonga/command/parser.rb (+0 -449) 100644 =================================================================== --- lib/groonga/command/parser.rb 2013-09-18 17:25:21 +0900 (656cf97) +++ /dev/null @@ -1,449 +0,0 @@ -# -*- coding: utf-8 -*- -# -# Copyright (C) 2011-2013 Kouhei Sutou <kou �� clear-code.com> -# -# This library is free software; you can redistribute it and/or -# modify it under the terms of the GNU Lesser General Public -# License as published by the Free Software Foundation; either -# version 2.1 of the License, or (at your option) any later version. -# -# This library is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public -# License along with this library; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - -require "shellwords" -require "cgi" - -require "yajl" - -require "groonga/command/error" - -require "groonga/command/base" - -require "groonga/command/dump" -require "groonga/command/get" -require "groonga/command/select" -require "groonga/command/suggest" -require "groonga/command/load" -require "groonga/command/table-create" -require "groonga/command/table-remove" -require "groonga/command/table-rename" -require "groonga/command/column-create" -require "groonga/command/column-remove" -require "groonga/command/column-rename" -require "groonga/command/delete" -require "groonga/command/truncate" -require "groonga/command/register" - -module Groonga - module Command - class ParseError < Error - attr_reader :reason, :location - def initialize(reason, before, after) - @reason = reason - @location = compute_location(before, after) - super("#{@reason}:\n#{@location}") - end - - private - def compute_location(before, after) - location = "" - if before[-1] == ?\n - location << before - location << after - location << "^" - elsif after[0] == ?\n - location << before - location << "\n" - location << " " * before.bytesize + "^" - location << after - else - location << before - location << after - location << " " * before.bytesize + "^" - end - location - end - end - - class Parser - class << self - - # parses groonga command or HTTP (starts with "/d/") command. - # @overload parse(data) - # @!macro [new] parser.parse.argument - # @param [String] data parsed command. - # @return [Groonga::Command] Returns - # {Groonga::Command} including parsed data. - # @!macro parser.parse.argument - # @overload parse(data, &block) - # @!macro parser.parse.argument - def parse(data, &block) - if block_given? - event_parse(data, &block) - else - stand_alone_parse(data) - end - end - - private - def event_parse(data) - parser = new - - parser.on_command do |command| - yield(:on_command, command) - end - parser.on_load_start do |command| - yield(:on_load_start, command) - end - parser.on_load_columns do |command, header| - yield(:on_load_columns, command, header) - end - parser.on_load_value do |command, value| - yield(:on_load_value, command, value) - end - parser.on_load_complete do |command| - yield(:on_load_complete, command) - end - parser.on_comment do |comment| - yield(:on_comment, comment) - end - - consume_data(parser, data) - end - - def stand_alone_parse(data) - parsed_command = nil - - parser = new - parser.on_command do |command| - parsed_command = command - end - parser.on_load_columns do |command, columns| - command[:columns] ||= columns.join(",") - end - values = [] - parser.on_load_value do |_, value| - values << value - end - parser.on_load_complete do |command| - parsed_command = command - parsed_command[:values] ||= Yajl::Encoder.encode(values) - end - - consume_data(parser, data) - if parsed_command.nil? - raise ParseError.new("not completed", data.lines.to_a.last, "") - end - - parsed_command - end - - def consume_data(parser, data) - if data.respond_to?(:each) - data.each do |chunk| - parser << chunk - end - else - parser << data - end - parser.finish - end - end - - def initialize - reset - initialize_hooks - end - - # Streaming parsing command. - # @param [String] chunk parsed chunk of command. - def <<(chunk) - @buffer << chunk - consume_buffer - end - - # Finishes parsing. If Parser is loading values specified "load" - # command, this method raises {ParseError}. - def finish - if @loading - raise ParseError.new("not completed", - @command.original_source.lines.to_a.last, - "") - else - catch do |tag| - parse_line(@buffer) - end - end - end - - # @overload on_command(command) - # @overload on_command {|command| } - def on_command(*arguments, &block) - if block_given? - @on_command_hook = block - else - @on_command_hook.call(*arguments) if @on_command_hook - end - end - - # @overload on_load_start(command) - # @overload on_load_start {|command| } - def on_load_start(*arguments, &block) - if block_given? - @on_load_start_hook = block - else - @on_load_start_hook.call(*arguments) if @on_load_start_hook - end - end - - # @overload on_load_columns(command) - # @overload on_load_columns {|command| } - def on_load_columns(*arguments, &block) - if block_given? - @on_load_columns_hook = block - else - @on_load_columns_hook.call(*arguments) if @on_load_columns_hook - end - end - - # @overload on_load_value(command) - # @overload on_load_value {|command| } - def on_load_value(*arguments, &block) - if block_given? - @on_load_value_hook = block - else - @on_load_value_hook.call(*arguments) if @on_load_value_hook - end - end - - # @overload on_load_complete(command) - # @overload on_load_complete(command) { } - def on_load_complete(*arguments, &block) - if block_given? - @on_load_complete_hook = block - else - @on_load_complete_hook.call(*arguments) if @on_load_complete_hook - end - end - - # @overload on_comment(comment) - # @overload on_comment {|comment| } - def on_comment(*arguments, &block) - if block_given? - @on_comment_hook = block - else - @on_comment_hook.call(*arguments) if @on_comment_hook - end - end - - private - def consume_buffer - catch do |tag| - loop do - if @loading - consume_load_values(tag) - else - parse_line(consume_line(tag)) - end - end - end - end - - def consume_load_values(tag) - if @in_load_values - json, separator, rest =****@buffe*****(/[\]},]/) - if @load_value_completed - throw(tag) if separator.empty? - if separator == "," - if /\A\s*\z/ =~ json - @command.original_source << json << separator - @buffer = rest - @load_value_completed = false - return - else - raise ParseError.new("record separate comma is missing", - @command.original_source.lines.to_a.last, - json) - end - elsif separator == "]" - if /\A\s*\z/ =~ json - @command.original_source << json << separator - @buffer = rest - on_load_complete(@command) - reset - return - end - end - end - @buffer = rest - parse_json(json) - if separator.empty? - throw(tag) - else - @load_value_completed = false - parse_json(separator) - end - else - spaces, start_json, rest =****@buffe*****("[") - unless /\A\s*\z/ =~ spaces - raise ParseError.new("there are garbages before JSON", - @command.original_source.lines.to_a.last, - spaces) - end - if start_json.empty? - @command.original_source << @buffer - @buffer.clear - throw(tag) - else - @command.original_source << spaces << start_json - @buffer = rest - @json_parser = Yajl::Parser.new - @json_parser.on_parse_complete = lambda do |object| - if object.is_a?(::Array) and****@comma*****? - @command.columns = object - on_load_columns(@command, object) - else - on_load_value(@command, object) - end - @load_value_completed = true - end - @in_load_values = true - end - end - end - - def parse_json(json) - @command.original_source << json - begin - @json_parser << json - rescue Yajl::ParseError - before_json =****@comma*****_source[0..(-json.bytesize)] - message = "invalid JSON: #{$!.message}: <#{json}>:\n" - message << before_json - raise ParseError.new(message, before_json, json) - end - end - - def consume_line(tag) - current_line, separator, rest =****@buffe*****(/\r?\n/) - throw(tag) if separator.empty? - - if current_line.end_with?("\\") - @buffer.sub!(/\\\r?\n/, "") - consume_line(tag) - else - @buffer = rest - current_line - end - end - - def parse_line(line) - case line - when /\A\s*\z/ - # ignore empty line - when /\A\#/ - on_comment($POSTMATCH) - else - @command = parse_command(line) - @command.original_source = line - process_command - end - end - - def process_command - if****@comma***** == "load" - on_load_start(@command) - if****@comma***** - on_load_columns(@command, @command.columns) - end - if @command[:values] - values = Yajl::Parser.parse(@command[:values]) - if****@comma*****? and values.first.is_a?(::Array) - header = values.shift - @command.columns = header - on_load_columns(@command, header) - end - values.each do |value| - on_load_value(@command, value) - end - on_load_complete(@command) - reset - else - @command.original_source << "\n" - @loading = true - end - else - on_command(@command) - @command = nil - end - end - - def parse_command(input) - if input.start_with?("/d/") - parse_uri_path(input) - else - parse_command_line(input) - end - end - - def parse_uri_path(path) - name, arguments_string = path.split(/\?/, 2) - arguments = {} - if arguments_string - arguments_string.split(/&/).each do |argument_string| - key, value = argument_string.split(/\=/, 2) - next if value.nil? - arguments[key] = CGI.unescape(value) - end - end - name = name.gsub(/\A\/d\//, '') - name, output_type = name.split(/\./, 2) - arguments["output_type"] = output_type if output_type - command_class = Command.find(name) - command = command_class.new(name, arguments) - command.original_format = :uri - command - end - - def parse_command_line(command_line) - name, *arguments = Shellwords.shellwords(command_line) - pair_arguments = {} - ordered_arguments = [] - until arguments.empty? - argument = arguments.shift - if argument.start_with?("--") - pair_arguments[argument.sub(/\A--/, "")] = arguments.shift - else - ordered_arguments << argument - end - end - command_class = Command.find(name) - command = command_class.new(name, pair_arguments, ordered_arguments) - command.original_format = :command - command - end - - def reset - @command = nil - @loading = false - @in_load_values = false - @load_value_completed = false - @buffer = "".force_encoding("ASCII-8BIT") - end - - def initialize_hooks - @on_command_hook = nil - @on_load_start_hook = nil - @on_load_columns_hook = nil - @on_load_value_hook = nil - @on_load_complete_hook = nil - end - end - end -end Modified: test/command/test-column-create.rb (+107 -108) =================================================================== --- test/command/test-column-create.rb 2013-09-18 17:25:21 +0900 (8cabca8) +++ test/command/test-column-create.rb 2013-09-29 18:08:24 +0900 (3f25ad7) @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- # -# Copyright (C) 2012 Kouhei Sutou <kou �� clear-code.com> +# Copyright (C) 2012-213 Kouhei Sutou <kou �� clear-code.com> # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public @@ -17,9 +17,14 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA class ColumnCreateCommandTest < Test::Unit::TestCase - class CommandLineTest < self - include GroongaCommandTestUtils::CommandLineCommandParser + private + def column_create_command(pair_arguments={}, ordered_arguments=[]) + Groonga::Command::ColumnCreate.new("column_create", + pair_arguments, + ordered_arguments) + end + class ConstructorTest < self def test_ordered_arguments table = "Lexicon" name = "content_index" @@ -27,8 +32,7 @@ class ColumnCreateCommandTest < Test::Unit::TestCase type = "Posts" source = "content" - command = parse([table, name, flags, type, source]) - assert_instance_of(Groonga::Command::ColumnCreate, command) + command = column_create_command({}, [table, name, flags, type, source]) assert_equal({ :table => table, :name => name, @@ -38,120 +42,115 @@ class ColumnCreateCommandTest < Test::Unit::TestCase }, command.arguments) end + end - private - def parse(arguments) - super("column_create", arguments, :output_type => false) + class FlagsTest < self + def test_multiple + command = column_create_command({"flags" => "COLUMN_INDEX|WITH_POSITION"}) + assert_equal(["COLUMN_INDEX", "WITH_POSITION"], + command.flags) end - class FlagsTest < self - def test_multiple - command = parse({"flags" => "COLUMN_INDEX|WITH_POSITION"}) - assert_equal(["COLUMN_INDEX", "WITH_POSITION"], - command.flags) - end + def test_one + command = column_create_command({"flags" => "COLUMN_VECTOR"}) + assert_equal(["COLUMN_VECTOR"], + command.flags) + end - def test_one - command = parse({"flags" => "COLUMN_VECTOR"}) - assert_equal(["COLUMN_VECTOR"], - command.flags) - end + def test_no_flags + command = column_create_command({}) + assert_equal([], command.flags) + end - def test_no_flags - command = parse({}) - assert_equal([], command.flags) + class PredicateTest < self + data({ + "COLUMN_SCALAR" => { + :expected => true, + :flags => "COLUMN_SCALAR", + }, + "other flag" => { + :expected => false, + :flags => "COLUMN_VECTOR", + } + }) + def test_column_scalar?(data) + command = column_create_command({"flags" => data[:flags]}) + assert_equal(data[:expected], command.column_scalar?) end - class PredicateTest < self - data({ - "COLUMN_SCALAR" => { - :expected => true, - :flags => "COLUMN_SCALAR", - }, - "other flag" => { - :expected => false, - :flags => "COLUMN_VECTOR", - } - }) - def test_column_scalar?(data) - command = parse({"flags" => data[:flags]}) - assert_equal(data[:expected], command.column_scalar?) - end - - data({ - "COLUMN_VECTOR" => { - :expected => true, - :flags => "COLUMN_VECTOR", - }, - "other flag" => { - :expected => false, - :flags => "COLUMN_INDEX", - } - }) - def test_column_vector?(data) - command = parse({"flags" => data[:flags]}) - assert_equal(data[:expected], command.column_vector?) - end + data({ + "COLUMN_VECTOR" => { + :expected => true, + :flags => "COLUMN_VECTOR", + }, + "other flag" => { + :expected => false, + :flags => "COLUMN_INDEX", + } + }) + def test_column_vector?(data) + command = column_create_command({"flags" => data[:flags]}) + assert_equal(data[:expected], command.column_vector?) + end - data({ - "COLUMN_INDEX" => { - :expected => true, - :flags => "COLUMN_INDEX", - }, - "other flag" => { - :expected => false, - :flags => "COLUMN_SCALAR", - } - }) - def test_column_index?(data) - command = parse({"flags" => data[:flags]}) - assert_equal(data[:expected], command.column_index?) - end + data({ + "COLUMN_INDEX" => { + :expected => true, + :flags => "COLUMN_INDEX", + }, + "other flag" => { + :expected => false, + :flags => "COLUMN_SCALAR", + } + }) + def test_column_index?(data) + command = column_create_command({"flags" => data[:flags]}) + assert_equal(data[:expected], command.column_index?) + end - data({ - "WITH_SECTION" => { - :expected => true, - :flags => "COLUMN_INDEX|WITH_SECTION", - }, - "other flag" => { - :expected => false, - :flags => "COLUMN_INDEX|WITH_WEIGHT", - } - }) - def test_with_section?(data) - command = parse({"flags" => data[:flags]}) - assert_equal(data[:expected], command.with_section?) - end + data({ + "WITH_SECTION" => { + :expected => true, + :flags => "COLUMN_INDEX|WITH_SECTION", + }, + "other flag" => { + :expected => false, + :flags => "COLUMN_INDEX|WITH_WEIGHT", + } + }) + def test_with_section?(data) + command = column_create_command({"flags" => data[:flags]}) + assert_equal(data[:expected], command.with_section?) + end - data({ - "WITH_WEIGHT" => { - :expected => true, - :flags => "COLUMN_INDEX|WITH_WEIGHT", - }, - "other flag" => { - :expected => false, - :flags => "COLUMN_INDEX|WITH_POSITION", - } - }) - def test_with_weight?(data) - command = parse({"flags" => data[:flags]}) - assert_equal(data[:expected], command.with_weight?) - end + data({ + "WITH_WEIGHT" => { + :expected => true, + :flags => "COLUMN_INDEX|WITH_WEIGHT", + }, + "other flag" => { + :expected => false, + :flags => "COLUMN_INDEX|WITH_POSITION", + } + }) + def test_with_weight?(data) + command = column_create_command({"flags" => data[:flags]}) + assert_equal(data[:expected], command.with_weight?) + end - data({ - "WITH_POSITION" => { - :expected => true, - :flags => "COLUMN_INDEX|WITH_POSITION", - }, - "other flag" => { - :expected => false, - :flags => "COLUMN_INDEX|WITH_SECTION", - } - }) - def test_with_position?(data) - command = parse({"flags" => data[:flags]}) - assert_equal(data[:expected], command.with_position?) - end + data({ + "WITH_POSITION" => { + :expected => true, + :flags => "COLUMN_INDEX|WITH_POSITION", + }, + "other flag" => { + :expected => false, + :flags => "COLUMN_INDEX|WITH_SECTION", + } + }) + def test_with_position?(data) + command = column_create_command({"flags" => data[:flags]}) + assert_equal(data[:expected], command.with_position?) end end end Modified: test/command/test-column-remove.rb (+9 -10) =================================================================== --- test/command/test-column-remove.rb 2013-09-18 17:25:21 +0900 (90a4b9a) +++ test/command/test-column-remove.rb 2013-09-29 18:08:24 +0900 (aeedc2e) @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- # -# Copyright (C) 2012 Kouhei Sutou <kou �� clear-code.com> +# Copyright (C) 2012-2013 Kouhei Sutou <kou �� clear-code.com> # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public @@ -17,25 +17,24 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA class ColumnRemoveCommandTest < Test::Unit::TestCase - class CommandLineTest < self - include GroongaCommandTestUtils::CommandLineCommandParser + private + def column_remove_command(pair_arguments={}, ordered_arguments=[]) + Groonga::Command::ColumnRemove.new("column_remove", + pair_arguments, + ordered_arguments) + end + class ConstructorTest < self def test_ordered_arguments table = "Users" name = "age" - command = parse(table, name) - assert_instance_of(Groonga::Command::ColumnRemove, command) + command = column_remove_command({}, [table, name]) assert_equal({ :table => table, :name => name, }, command.arguments) end - - private - def parse(*arguments) - super("column_remove", arguments, :output_type => false) - end end end Modified: test/command/test-column-rename.rb (+9 -10) =================================================================== --- test/command/test-column-rename.rb 2013-09-18 17:25:21 +0900 (3ed8754) +++ test/command/test-column-rename.rb 2013-09-29 18:08:24 +0900 (ba08c1a) @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- # -# Copyright (C) 2012 Kouhei Sutou <kou �� clear-code.com> +# Copyright (C) 2012-2013 Kouhei Sutou <kou �� clear-code.com> # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public @@ -17,16 +17,20 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA class ColumnRenameCommandTest < Test::Unit::TestCase - class CommandLineTest < self - include GroongaCommandTestUtils::CommandLineCommandParser + private + def column_rename_command(pair_arguments={}, ordered_arguments=[]) + Groonga::Command::ColumnRename.new("column_rename", + pair_arguments, + ordered_arguments) + end + class ConstructorTest < self def test_ordered_arguments table = "Users" name = "name" new_name = "nick" - command = parse(table, name, new_name) - assert_instance_of(Groonga::Command::ColumnRename, command) + command = column_rename_command({}, [table, name, new_name]) assert_equal({ :table => table, :name => name, @@ -34,10 +38,5 @@ class ColumnRenameCommandTest < Test::Unit::TestCase }, command.arguments) end - - private - def parse(*arguments) - super("column_rename", arguments, :output_type => false) - end end end Modified: test/command/test-delete.rb (+9 -10) =================================================================== --- test/command/test-delete.rb 2013-09-18 17:25:21 +0900 (7941912) +++ test/command/test-delete.rb 2013-09-29 18:08:24 +0900 (2ff9b64) @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- # -# Copyright (C) 2012 Kouhei Sutou <kou �� clear-code.com> +# Copyright (C) 2012-2013 Kouhei Sutou <kou �� clear-code.com> # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public @@ -17,17 +17,21 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA class DeleteCommandTest < Test::Unit::TestCase - class CommandLineTest < self - include GroongaCommandTestUtils::CommandLineCommandParser + private + def delete_command(pair_arguments={}, ordered_arguments=[]) + Groonga::Command::Delete.new("delete", + pair_arguments, + ordered_arguments) + end + class ConstructorTest < self def test_ordered_arguments table = "Users" key = "Alice" id = "29" filter = "age == 20" - command = parse(table, key, id, filter) - assert_instance_of(Groonga::Command::Delete, command) + command = delete_command({}, [table, key, id, filter]) assert_equal({ :table => table, :key => key, @@ -36,10 +40,5 @@ class DeleteCommandTest < Test::Unit::TestCase }, command.arguments) end - - private - def parse(*arguments) - super("delete", arguments, :output_type => false) - end end end Modified: test/command/test-dump.rb (+7 -7) =================================================================== --- test/command/test-dump.rb 2013-09-18 17:25:21 +0900 (90e98dc) +++ test/command/test-dump.rb 2013-09-29 18:08:24 +0900 (2b01118) @@ -17,14 +17,14 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA class DumpCommandTest < Test::Unit::TestCase - include GroongaCommandTestUtils::CommandLineCommandParser - - def test_output_type - assert_equal(:none, command.output_type) + private + def dump_command(pair_arguments={}, ordered_arguments=[]) + Groonga::Command::Dump.new("dump", pair_arguments, ordered_arguments) end - private - def command(arguments={}) - Groonga::Command::Dump.new("dump", arguments) + class OutputTypeTest < self + def test_none + assert_equal(:none, dump_command.output_type) + end end end Modified: test/command/test-get.rb (+13 -16) =================================================================== --- test/command/test-get.rb 2013-09-18 17:25:21 +0900 (1609e2a) +++ test/command/test-get.rb 2013-09-29 18:08:24 +0900 (5e08dab) @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- # +# Copyright (C) 2013 Kouhei Sutou <kou �� clear-code.com> # Copyright (C) 2012 Haruka Yoshihara <yoshihara �� clear-code.com> # # This library is free software; you can redistribute it and/or @@ -17,28 +18,24 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA class GetCommandTest < Test::Unit::TestCase - class CommandLineTest < self - include GroongaCommandTestUtils::CommandLineCommandParser + private + def get_command(pair_arguments={}, ordered_arguments=[]) + Groonga::Command::Get.new("get", pair_arguments, ordered_arguments) + end + class ConstructorTest < self def test_ordered_arguments table = "Users" key = "Alice" output_columns = "name, address" - command = parse(table, key, output_columns) - assert_instance_of(Groonga::Command::Get, command) - - expected_arguments = { - :table => table, - :key => key, - :output_columns => output_columns, - } - assert_equal(expected_arguments, command.arguments) - end - - private - def parse(*arguments) - super("get", arguments, :output_type => false) + command = get_command({}, [table, key, output_columns]) + assert_equal({ + :table => table, + :key => key, + :output_columns => output_columns, + }, + command.arguments) end end end Modified: test/command/test-load.rb (+17 -10) =================================================================== --- test/command/test-load.rb 2013-09-18 17:25:21 +0900 (2ba8908) +++ test/command/test-load.rb 2013-09-29 18:08:24 +0900 (8094ca8) @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- # -# Copyright (C) 2012 Kouhei Sutou <kou �� clear-code.com> +# Copyright (C) 2012-2013 Kouhei Sutou <kou �� clear-code.com> # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public @@ -17,9 +17,14 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA class LoadCommandTest < Test::Unit::TestCase - class CommandLineTest < self - include GroongaCommandTestUtils::CommandLineCommandParser + private + def load_command(pair_arguments={}, ordered_arguments=[]) + Groonga::Command::Load.new("load", + pair_arguments, + ordered_arguments) + end + class ConstructorTest < self def test_ordered_arguments values = "[[\"Alice\"], [\"Bob\"]]" table = "Users" @@ -28,8 +33,15 @@ class LoadCommandTest < Test::Unit::TestCase input_type = "json" each = "strlen(_key)" - command = parse(values, table, columns, ifexists, input_type, each) - assert_instance_of(Groonga::Command::Load, command) + ordered_arguments = [ + values, + table, + columns, + ifexists, + input_type, + each, + ] + command = load_command({}, ordered_arguments) assert_equal({ :values => values, :table => table, @@ -40,10 +52,5 @@ class LoadCommandTest < Test::Unit::TestCase }, command.arguments) end - - private - def parse(*arguments) - super("load", arguments, :output_type => false) - end end end Modified: test/command/test-register.rb (+9 -10) =================================================================== --- test/command/test-register.rb 2013-09-18 17:25:21 +0900 (ada2aca) +++ test/command/test-register.rb 2013-09-29 18:08:24 +0900 (bdd35a0) @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- # -# Copyright (C) 2012 Kouhei Sutou <kou �� clear-code.com> +# Copyright (C) 2012-2013 Kouhei Sutou <kou �� clear-code.com> # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public @@ -17,23 +17,22 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA class RegisterCommandTest < Test::Unit::TestCase - class CommandLineTest < self - include GroongaCommandTestUtils::CommandLineCommandParser + private + def register_command(pair_arguments={}, ordered_arguments=[]) + Groonga::Command::Register.new("register", + pair_arguments, + ordered_arguments) + end + class ConstructorTest < self def test_ordered_arguments path = "tokenizers/mecab" - command = parse(path) - assert_instance_of(Groonga::Command::Register, command) + command = register_command({}, [path]) assert_equal({ :path => path, }, command.arguments) end - - private - def parse(*arguments) - super("register", arguments, :output_type => false) - end end end Modified: test/command/test-select.rb (+83 -82) =================================================================== --- test/command/test-select.rb 2013-09-18 17:25:21 +0900 (fd6fd58) +++ test/command/test-select.rb 2013-09-29 18:08:24 +0900 (f49c20d) @@ -17,87 +17,88 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA class SelectCommandTest < Test::Unit::TestCase - include GroongaCommandTestUtils::CommandLineCommandParser + private + def select_command(pair_arguments={}, ordered_arguments=[]) + Groonga::Command::Select.new("select", pair_arguments, ordered_arguments) + end - def test_ordered_argument - table = "Users" - match_columns = "name * 10 || description" - query = "groonga" - filter = "age >= 18" - scorer = "_score = age" - sortby = "_score" - output_columns = "_key, name" - offset = "10" - limit = "20" - drilldown = "name" - drilldown_sortby = "_nsubrecs" - drilldown_output_columns = "name, _nsubrecs" - drilldown_offset = "5" - drilldown_limit = "10" - cache = "no" - match_escalation_threshold = "-1" - query_expansion = "deprecated" - query_flags = "ALLOW_LEADING_NOT" - query_expander = "Terms.synonym" - command = parse("select", - [ - table, - match_columns, - query, - filter, - scorer, - sortby, - output_columns, - offset, - limit, - drilldown, - drilldown_sortby, - drilldown_output_columns, - drilldown_offset, - drilldown_limit, - cache, - match_escalation_threshold, - query_expansion, - query_flags, - query_expander, - ], - :output_type => false) + class ConstructorTest < self + def test_ordered_argument + table = "Users" + match_columns = "name * 10 || description" + query = "groonga" + filter = "age >= 18" + scorer = "_score = age" + sortby = "_score" + output_columns = "_key, name" + offset = "10" + limit = "20" + drilldown = "name" + drilldown_sortby = "_nsubrecs" + drilldown_output_columns = "name, _nsubrecs" + drilldown_offset = "5" + drilldown_limit = "10" + cache = "no" + match_escalation_threshold = "-1" + query_expansion = "deprecated" + query_flags = "ALLOW_LEADING_NOT" + query_expander = "Terms.synonym" - assert_instance_of(Groonga::Command::Select, command) - assert_equal({ - :table => table, - :match_columns => match_columns, - :query => query, - :filter => filter, - :scorer => scorer, - :sortby => sortby, - :output_columns => output_columns, - :offset => offset, - :limit => limit, - :drilldown => drilldown, - :drilldown_sortby => drilldown_sortby, - :drilldown_output_columns => drilldown_output_columns, - :drilldown_offset => drilldown_offset, - :drilldown_limit => drilldown_limit, - :cache => cache, - :match_escalation_threshold => match_escalation_threshold, - :query_expansion => query_expansion, - :query_flags => query_flags, - :query_expander => query_expander, - }, - command.arguments) - end + ordered_arguments = [ + table, + match_columns, + query, + filter, + scorer, + sortby, + output_columns, + offset, + limit, + drilldown, + drilldown_sortby, + drilldown_output_columns, + drilldown_offset, + drilldown_limit, + cache, + match_escalation_threshold, + query_expansion, + query_flags, + query_expander, + ] + command = select_command({}, ordered_arguments) - def test_scorer - select = command(:table => "Users", - :filter => "age<=30", - :scorer => "_score = random()") - assert_equal("_score = random()", select.scorer) + assert_equal({ + :table => table, + :match_columns => match_columns, + :query => query, + :filter => filter, + :scorer => scorer, + :sortby => sortby, + :output_columns => output_columns, + :offset => offset, + :limit => limit, + :drilldown => drilldown, + :drilldown_sortby => drilldown_sortby, + :drilldown_output_columns => drilldown_output_columns, + :drilldown_offset => drilldown_offset, + :drilldown_limit => drilldown_limit, + :cache => cache, + :match_escalation_threshold => match_escalation_threshold, + :query_expansion => query_expansion, + :query_flags => query_flags, + :query_expander => query_expander, + }, + command.arguments) + end end - private - def command(arguments) - Groonga::Command::Select.new("select", arguments) + class ScorerTest < self + def test_reader + command = select_command(:table => "Users", + :filter => "age<=30", + :scorer => "_score = random()") + assert_equal("_score = random()", command.scorer) + end end class FilterTest < self @@ -106,8 +107,8 @@ class SelectCommandTest < Test::Unit::TestCase '"35.73360x139.7394","62614x139.7714") && ' + '((type == "たいやき" || type == "和菓子")) && ' + 'keyword @ "たいやき" &! keyword @ "白" &! keyword @ "養殖"' - select = command(:table => "Users", - :filter => filter) + command = select_command(:table => "Users", + :filter => filter) assert_equal(['geo_in_rectangle(location,' + '"35.73360x139.7394","62614x139.7714")', 'type == "たいやき"', @@ -115,14 +116,14 @@ class SelectCommandTest < Test::Unit::TestCase 'keyword @ "たいやき"', 'keyword @ "白"', 'keyword @ "養殖"'], - select.conditions) + command.conditions) end def test_omitted - select = command(:table => "Users", - :filter => nil) + command = select_command(:table => "Users", + :filter => nil) assert_equal([], - select.conditions) + command.conditions) end end end Modified: test/command/test-suggest.rb (+43 -35) =================================================================== --- test/command/test-suggest.rb 2013-09-18 17:25:21 +0900 (4491671) +++ test/command/test-suggest.rb 2013-09-29 18:08:24 +0900 (4025ba4) @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- # +# Copyright (C) 2013 Kouhei Sutou <kou �� clear-code.com> # Copyright (C) 2012 Haruka Yoshihara <yoshihara �� clear-code.com> # # This library is free software; you can redistribute it and/or @@ -17,52 +18,59 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA class SuggestCommandTest < Test::Unit::TestCase - class CommandLineTest < self - include GroongaCommandTestUtils::CommandLineCommandParser + private + def suggest_command(pair_arguments={}, ordered_arguments=[]) + Groonga::Command::Suggest.new("suggest", + pair_arguments, + ordered_arguments) + end + class ConstructorTest < self def test_ordered_arguments - types = "complete" - table = "Users" - column = "name" - query = "name:@ Ali" - sortby = "_score" - output_columns = "name" - offset = "0" - limit = "5" - frequency_threshold = "120" + types = "complete" + table = "Users" + column = "name" + query = "name:@ Ali" + sortby = "_score" + output_columns = "name" + offset = "0" + limit = "5" + frequency_threshold = "120" conditional_probability_threshold = "0.3" - prefix_search = "yes" - similar_search = "yes" + prefix_search = "yes" + similar_search = "yes" - arguments = [ - types, table, column, query, sortby, output_columns, - offset, limit, frequency_threshold, + ordered_arguments = [ + types, + table, + column, + query, + sortby, + output_columns, + offset, + limit, + frequency_threshold, conditional_probability_threshold, - prefix_search, similar_search + prefix_search, + similar_search ] - command = parse(*arguments) - assert_instance_of(Groonga::Command::Suggest, command) + command = suggest_command({}, ordered_arguments) expected_arguments = { - :types => types, - :table => table, - :column => column, - :query => query, - :sortby => sortby, - :output_columns => output_columns, - :offset => offset, - :limit => limit, - :frequency_threshold => frequency_threshold, + :types => types, + :table => table, + :column => column, + :query => query, + :sortby => sortby, + :output_columns => output_columns, + :offset => offset, + :limit => limit, + :frequency_threshold => frequency_threshold, :conditional_probability_threshold => conditional_probability_threshold, - :prefix_search => prefix_search, - :similar_search => similar_search + :prefix_search => prefix_search, + :similar_search => similar_search } assert_equal(expected_arguments, command.arguments) end - - private - def parse(*arguments) - super("suggest", arguments, :output_type => false) - end end end Modified: test/command/test-table-create.rb (+101 -94) =================================================================== --- test/command/test-table-create.rb 2013-09-18 17:25:21 +0900 (70df59d) +++ test/command/test-table-create.rb 2013-09-29 18:08:24 +0900 (c3ee6cb) @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- # -# Copyright (C) 2012 Kouhei Sutou <kou �� clear-code.com> +# Copyright (C) 2012-2013 Kouhei Sutou <kou �� clear-code.com> # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public @@ -17,9 +17,14 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA class TableCreateCommandTest < Test::Unit::TestCase - class CommandLineTest < self - include GroongaCommandTestUtils::CommandLineCommandParser + private + def table_create_command(pair_arguments={}, ordered_arguments=[]) + Groonga::Command::TableCreate.new("table_create", + pair_arguments, + ordered_arguments) + end + class ConstructorTest < self def test_ordered_arguments name = "Users" flags = "TABLE_PAT_KEY" @@ -28,8 +33,15 @@ class TableCreateCommandTest < Test::Unit::TestCase default_tokenizer = "TokenBigram" normalizer = "NormalizerAuto" - command = parse([name, flags, key_type, value_type, default_tokenizer, normalizer]) - assert_instance_of(Groonga::Command::TableCreate, command) + ordered_arguments = [ + name, + flags, + key_type, + value_type, + default_tokenizer, + normalizer, + ] + command = table_create_command({}, ordered_arguments) assert_equal({ :name => name, :flags => flags, @@ -40,105 +52,100 @@ class TableCreateCommandTest < Test::Unit::TestCase }, command.arguments) end + end - private - def parse(arguments) - super("table_create", arguments, :output_type => false) + class FlagsTest < self + def test_multiple + command = table_create_command({"flags" => "TABLE_PAT_KEY|KEY_WITH_SIS"}) + assert_equal(["TABLE_PAT_KEY", "KEY_WITH_SIS"], + command.flags) end - class FlagsTest < self - def test_multiple - command = parse({"flags" => "TABLE_PAT_KEY|KEY_WITH_SIS"}) - assert_equal(["TABLE_PAT_KEY", "KEY_WITH_SIS"], - command.flags) - end + def test_one + command = table_create_command({"flags" => "TABLE_NO_KEY"}) + assert_equal(["TABLE_NO_KEY"], + command.flags) + end - def test_one - command = parse({"flags" => "TABLE_NO_KEY"}) - assert_equal(["TABLE_NO_KEY"], - command.flags) - end + def test_no_flags + command = table_create_command + assert_equal([], command.flags) + end - def test_no_flags - command = parse({}) - assert_equal([], command.flags) + class PredicateTest < self + data({ + "TABLE_NO_KEY" => { + :expected => true, + :flags => "TABLE_NO_KEY", + }, + "other flag" => { + :expected => false, + :flags => "TABLE_HASH_KEY", + } + }) + def test_table_no_key?(data) + command = table_create_command({"flags" => data[:flags]}) + assert_equal(data[:expected], command.table_no_key?) end - class PredicateTest < self - data({ - "TABLE_NO_KEY" => { - :expected => true, - :flags => "TABLE_NO_KEY", - }, - "other flag" => { - :expected => false, - :flags => "TABLE_HASH_KEY", - } - }) - def test_table_no_key?(data) - command = parse({"flags" => data[:flags]}) - assert_equal(data[:expected], command.table_no_key?) - end - - data({ - "TABLE_HASH_KEY" => { - :expected => true, - :flags => "TABLE_HASH_KEY", - }, - "other flag" => { - :expected => false, - :flags => "TABLE_PAT_KEY", - } - }) - def test_table_hash_key?(data) - command = parse({"flags" => data[:flags]}) - assert_equal(data[:expected], command.table_hash_key?) - end + data({ + "TABLE_HASH_KEY" => { + :expected => true, + :flags => "TABLE_HASH_KEY", + }, + "other flag" => { + :expected => false, + :flags => "TABLE_PAT_KEY", + } + }) + def test_table_hash_key?(data) + command = table_create_command({"flags" => data[:flags]}) + assert_equal(data[:expected], command.table_hash_key?) + end - data({ - "TABLE_PAT_KEY" => { - :expected => true, - :flags => "TABLE_PAT_KEY", - }, - "other flag" => { - :expected => false, - :flags => "TABLE_DAT_KEY", - } - }) - def test_table_pat_key?(data) - command = parse({"flags" => data[:flags]}) - assert_equal(data[:expected], command.table_pat_key?) - end + data({ + "TABLE_PAT_KEY" => { + :expected => true, + :flags => "TABLE_PAT_KEY", + }, + "other flag" => { + :expected => false, + :flags => "TABLE_DAT_KEY", + } + }) + def test_table_pat_key?(data) + command = table_create_command({"flags" => data[:flags]}) + assert_equal(data[:expected], command.table_pat_key?) + end - data({ - "TABLE_DAT_KEY" => { - :expected => true, - :flags => "TABLE_DAT_KEY", - }, - "other flag" => { - :expected => false, - :flags => "TABLE_NO_KEY", - } - }) - def test_table_dat_key?(data) - command = parse({"flags" => data[:flags]}) - assert_equal(data[:expected], command.table_dat_key?) - end + data({ + "TABLE_DAT_KEY" => { + :expected => true, + :flags => "TABLE_DAT_KEY", + }, + "other flag" => { + :expected => false, + :flags => "TABLE_NO_KEY", + } + }) + def test_table_dat_key?(data) + command = table_create_command({"flags" => data[:flags]}) + assert_equal(data[:expected], command.table_dat_key?) + end - data({ - "KEY_WITH_SIS" => { - :expected => true, - :flags => "KEY_WITH_SIS|TABLE_PAT_KEY", - }, - "other flag" => { - :expected => false, - :flags => "TABLE_NO_KEY", - } - }) - def test_key_with_sis?(data) - command = parse({"flags" => data[:flags]}) - assert_equal(data[:expected], command.key_with_sis?) - end + data({ + "KEY_WITH_SIS" => { + :expected => true, + :flags => "KEY_WITH_SIS|TABLE_PAT_KEY", + }, + "other flag" => { + :expected => false, + :flags => "TABLE_NO_KEY", + } + }) + def test_key_with_sis?(data) + command = table_create_command({"flags" => data[:flags]}) + assert_equal(data[:expected], command.key_with_sis?) end end end Modified: test/command/test-table-remove.rb (+9 -10) =================================================================== --- test/command/test-table-remove.rb 2013-09-18 17:25:21 +0900 (d0c546c) +++ test/command/test-table-remove.rb 2013-09-29 18:08:24 +0900 (8c8c35c) @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- # -# Copyright (C) 2012 Kouhei Sutou <kou �� clear-code.com> +# Copyright (C) 2012-2013 Kouhei Sutou <kou �� clear-code.com> # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public @@ -17,23 +17,22 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA class TableRemoveCommandTest < Test::Unit::TestCase - class CommandLineTest < self - include GroongaCommandTestUtils::CommandLineCommandParser + private + def table_remove_command(pair_arguments={}, ordered_arguments=[]) + Groonga::Command::TableRemove.new("table_remove", + pair_arguments, + ordered_arguments) + end + class ConstructorTest < self def test_ordered_arguments name = "Users" - command = parse(name) - assert_instance_of(Groonga::Command::TableRemove, command) + command = table_remove_command({}, [name]) assert_equal({ :name => name, }, command.arguments) end - - private - def parse(*arguments) - super("table_remove", arguments, :output_type => false) - end end end Modified: test/command/test-table-rename.rb (+9 -10) =================================================================== --- test/command/test-table-rename.rb 2013-09-18 17:25:21 +0900 (403f316) +++ test/command/test-table-rename.rb 2013-09-29 18:08:24 +0900 (79b2c1c) @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- # -# Copyright (C) 2012 Kouhei Sutou <kou �� clear-code.com> +# Copyright (C) 2012-2013 Kouhei Sutou <kou �� clear-code.com> # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public @@ -17,25 +17,24 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA class TableRenameCommandTest < Test::Unit::TestCase - class CommandLineTest < self - include GroongaCommandTestUtils::CommandLineCommandParser + private + def table_rename_command(pair_arguments={}, ordered_arguments=[]) + Groonga::Command::TableRename.new("table_rename", + pair_arguments, + ordered_arguments) + end + class ConstructorTest < self def test_ordered_arguments name = "Users" new_name = "People" - command = parse(name, new_name) - assert_instance_of(Groonga::Command::TableRename, command) + command = table_rename_command({}, [name, new_name]) assert_equal({ :name => name, :new_name => new_name, }, command.arguments) end - - private - def parse(*arguments) - super("table_rename", arguments, :output_type => false) - end end end Modified: test/command/test-truncate.rb (+9 -10) =================================================================== --- test/command/test-truncate.rb 2013-09-18 17:25:21 +0900 (02069ff) +++ test/command/test-truncate.rb 2013-09-29 18:08:24 +0900 (18a2b16) @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- # -# Copyright (C) 2012 Kouhei Sutou <kou �� clear-code.com> +# Copyright (C) 2012-2013 Kouhei Sutou <kou �� clear-code.com> # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public @@ -17,23 +17,22 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA class TruncateCommandTest < Test::Unit::TestCase - class CommandLineTest < self - include GroongaCommandTestUtils::CommandLineCommandParser + private + def truncate_command(pair_arguments={}, ordered_arguments=[]) + Groonga::Command::Truncate.new("truncate", + pair_arguments, + ordered_arguments) + end + class ConstructorTest < self def test_ordered_arguments table = "Users" - command = parse(table) - assert_instance_of(Groonga::Command::Truncate, command) + command = truncate_command([], [table]) assert_equal({ :table => table, }, command.arguments) end - - private - def parse(*arguments) - super("truncate", arguments, :output_type => false) - end end end Modified: test/groonga-command-test-utils.rb (+1 -81) =================================================================== --- test/groonga-command-test-utils.rb 2013-09-18 17:25:21 +0900 (0412f03) +++ test/groonga-command-test-utils.rb 2013-09-29 18:08:24 +0900 (1d7c3b2) @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- # -# Copyright (C) 2011-2012 Kouhei Sutou <kou �� clear-code.com> +# Copyright (C) 2011-2013 Kouhei Sutou <kou �� clear-code.com> # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public @@ -16,87 +16,7 @@ # License along with this library; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -require "cgi" -require "stringio" - require "groonga/command" module GroongaCommandTestUtils - module CommandParser - private - def command(name, arguments) - Groonga::Command.find(name).new(name, arguments) - end - - 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, 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 - 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={}, options={}) - parse_http_path(command, arguments, options) - end - end - - module CommandLineCommandParser - include CommandParser - - private - def parse(command, arguments={}, options={}) - parse_command_line(command, arguments, options) - end - end end Deleted: test/test-parser.rb (+0 -377) 100644 =================================================================== --- test/test-parser.rb 2013-09-18 17:25:21 +0900 (015d5dd) +++ /dev/null @@ -1,377 +0,0 @@ -# -*- coding: utf-8 -*- -# -# Copyright (C) 2011-2012 Kouhei Sutou <kou �� clear-code.com> -# -# This library is free software; you can redistribute it and/or -# modify it under the terms of the GNU Lesser General Public -# License as published by the Free Software Foundation; either -# version 2.1 of the License, or (at your option) any later version. -# -# This library is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public -# License along with this library; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - -class ParserTest < Test::Unit::TestCase - module ParseTests - def test_parameters - select = parse("select", - :table => "Users", - :filter => "age<=30") - assert_equal(command("select", - "table" => "Users", - "filter" => "age<=30", - "output_type" => "json"), - select) - end - end - - class HTTPTest < self - include GroongaCommandTestUtils::HTTPCommandParser - - def test_uri_format? - command = parse("status") - assert_predicate(command, :uri_format?) - end - - def test_command_format? - command = parse("status") - assert_not_predicate(command, :command_format?) - end - - def test_no_value - path = "/d/select?table=Users&key_only" - command = Groonga::Command::Parser.parse(path) - assert_equal({:table => "Users"}, command.arguments) - end - - class ParseTest < self - include ParseTests - end - end - - class CommandLineTest < self - include GroongaCommandTestUtils::CommandLineCommandParser - - def test_uri_format? - command = parse("status") - assert_not_predicate(command, :uri_format?) - end - - def test_command_format? - command = parse("status") - assert_predicate(command, :command_format?) - end - - class ParseTest < self - include ParseTests - end - - class EventTest < self - def setup - @parser = Groonga::Command::Parser.new - end - - class CommandTest < self - def test_newline - parsed_command = nil - @parser.on_command do |command| - parsed_command = command - end - - @parser << "status" - assert_nil(parsed_command) - @parser << "\n" - assert_equal("status", parsed_command.name) - end - - def test_finish - parsed_command = nil - @parser.on_command do |command| - parsed_command = command - end - - @parser << "status" - assert_nil(parsed_command) - @parser.finish - assert_equal("status", parsed_command.name) - end - - def test_empty_line - parsed_command = nil - @parser.on_command do |command| - parsed_command = command - end - - @parser << "\n" - assert_nil(parsed_command) - - @parser << "status\n" - assert_equal("status", parsed_command.name) - end - - def test_multi_lines - parsed_commands = [] - @parser.on_command do |command| - parsed_commands << command - end - - @parser << <<-COMMAND_LIST.chomp -table_list -status - COMMAND_LIST - assert_equal(["table_list"], - parsed_commands.collect(&:name)) - - @parser.finish - assert_equal(["table_list", "status"], - parsed_commands.collect(&:name)) - end - end - - class LoadTest < self - def setup - super - @events = [] - @parser.on_load_start do |command| - @events << [:load_start, command.original_source.dup] - end - @parser.on_load_columns do |command, header| - @events << [:load_columns, command.original_source.dup, header] - end - @parser.on_load_value do |command, value| - @events << [:load_value, command.original_source.dup, value] - end - @parser.on_load_complete do |command| - @events << [:load_complete, command.original_source.dup] - end - end - - class InlineTest < self - class BracketTest < self - def test_have_columns - command_line = - "load " + - "--columns '_key, name' " + - "--values '[[\"alice\", \"Alice\"]]' " + - "--table Users" - @parser << command_line - assert_equal([], @events) - @parser << "\n" - assert_equal([ - [:load_start, command_line], - [:load_columns, command_line, ["_key", "name"]], - [:load_value, command_line, ["alice", "Alice"]], - [:load_complete, command_line], - ], - @events) - end - - def test_no_columns - command_line = "load --values '[[\"_key\"], [1]]' --table IDs" - @parser << command_line - assert_equal([], @events) - @parser << "\n" - assert_equal([ - [:load_start, command_line], - [:load_columns, command_line, ["_key"]], - [:load_value, command_line, [1]], - [:load_complete, command_line], - ], - @events) - end - end - - def test_brace - command_line = "load --values '[{\"_key\": 1}]' --table IDs" - @parser << command_line - assert_equal([], @events) - @parser << "\n" - assert_equal([ - [:load_start, command_line], - [:load_value, command_line, {"_key" => 1}], - [:load_complete, command_line], - ], - @events) - end - end - - class MultiLineTest < self - class BracketTest < self - def test_have_columns - @parser << <<-EOC -load --table Users --columns "_key, name" -[ -["alice", "Alice"] -] -EOC - expected_events = [] - expected_events << [:load_start, <<-EOC.chomp] -load --table Users --columns "_key, name" -EOC - expected_events << [:load_columns, <<-EOC.chomp, ["_key", "name"]] -load --table Users --columns "_key, name" -EOC - expected_events << [:load_value, <<-EOC.chomp, ["alice", "Alice"]] -load --table Users --columns "_key, name" -[ -["alice", "Alice"] -EOC - expected_events << [:load_complete, <<-EOC.chomp] -load --table Users --columns "_key, name" -[ -["alice", "Alice"] -] -EOC - assert_equal(expected_events, @events) - end - - def test_no_columns - @parser << <<-EOC -load --table Users -[ -["_key", "name"], -["alice", "Alice"] -] -EOC - expected_events = [] - expected_events << [:load_start, <<-EOC.chomp] -load --table Users -EOC - expected_events << [:load_columns, <<-EOC.chomp, ["_key", "name"]] -load --table Users -[ -["_key", "name"] -EOC - expected_events << [:load_value, <<-EOC.chomp, ["alice", "Alice"]] -load --table Users -[ -["_key", "name"], -["alice", "Alice"] -EOC - expected_events << [:load_complete, <<-EOC.chomp] -load --table Users -[ -["_key", "name"], -["alice", "Alice"] -] -EOC - assert_equal(expected_events, @events) - end - end - - def test_brace - @parser << <<-EOC -load --table Users -[ -{"_key": "alice", "name": "Alice"}, -{"_key": "bob", "name": "Bob"} -] -EOC - expected_events = [] - expected_events << [:load_start, <<-EOC.chomp] -load --table Users -EOC - value = {"_key" => "alice", "name" => "Alice"} - expected_events << [:load_value, <<-EOC.chomp, value] -load --table Users -[ -{"_key": "alice", "name": "Alice"} -EOC - value = {"_key" => "bob", "name" => "Bob"} - expected_events << [:load_value, <<-EOC.chomp, value] -load --table Users -[ -{"_key": "alice", "name": "Alice"}, -{"_key": "bob", "name": "Bob"} -EOC - expected_events << [:load_complete, <<-EOC.chomp] -load --table Users -[ -{"_key": "alice", "name": "Alice"}, -{"_key": "bob", "name": "Bob"} -] -EOC - assert_equal(expected_events, @events) - end - end - - class ErrorTest < self - def test_location - message = "record separate comma is missing" - before = "{\"_key\": \"alice\", \"name\": \"Alice\"}" - after = "\n{\"_key\": \"bob\"" - error = Groonga::Command::ParseError.new(message, before, after) - assert_equal(<<-EOS.chomp, error.message) -record separate comma is missing: -{"_key": "alice", "name": "Alice"} - ^ -{"_key": "bob" -EOS - end - - def test_no_record_separate_comma - message = "record separate comma is missing" - before = "{\"_key\": \"alice\", \"name\": \"Alice\"}" - after = "\n{\"_key\": \"bob\"" - error = Groonga::Command::ParseError.new(message, before, after) - assert_raise(error) do - @parser << <<-EOC -load --table Users -[ -{"_key": "alice", "name": "Alice"} -{"_key": "bob", "name": "Bob"} -EOC - end - end - - def test_garbage_before_json - message = "there are garbages before JSON" - before = "load --table Users\n" - after = "XXX\n" - error = Groonga::Command::ParseError.new(message, before, after) - assert_raise(error) do - @parser << <<-EOC -load --table Users -XXX -[ -{"_key": "alice", "name": "Alice"} -] -EOC - end - end - end - end - - class CommentTest < self - def test_newline - parsed_comment = nil - @parser.on_comment do |comment| - parsed_comment = comment - end - - @parser << "# status" - assert_nil(parsed_comment) - @parser << "\n" - assert_equal(" status", parsed_comment) - end - - def test_finish - parsed_comment = nil - @parser.on_comment do |comment| - parsed_comment = comment - end - - @parser << "# status" - assert_nil(parsed_comment) - @parser.finish - assert_equal(" status", parsed_comment) - end - end - end - end -end