[Groonga-commit] groonga/groonga-command [master] Use test utils

Back to archive index

Kouhei Sutou null+****@clear*****
Sun Nov 25 19:26:05 JST 2012


Kouhei Sutou	2012-11-25 19:26:05 +0900 (Sun, 25 Nov 2012)

  New Revision: 8d933635cfef5390ea5dde52ddba046d822ae818
  https://github.com/groonga/groonga-command/commit/8d933635cfef5390ea5dde52ddba046d822ae818

  Log:
    Use test utils

  Modified files:
    test/command/test-table-create.rb
    test/command/test-table-remove.rb
    test/command/test-table-rename.rb
    test/groonga-command-test-utils.rb

  Modified: test/command/test-table-create.rb (+5 -5)
===================================================================
--- test/command/test-table-create.rb    2012-11-25 19:15:47 +0900 (422eb9f)
+++ test/command/test-table-create.rb    2012-11-25 19:26:05 +0900 (9fdcf53)
@@ -18,6 +18,8 @@
 
 class TableCreateCommandTest < Test::Unit::TestCase
   class CommandLineTest < self
+    include GroongaCommandTestUtils::CommandLineCommandParser
+
     def test_ordered_arguments
       name              = "Users"
       flags             = "TABLE_PAT_KEY"
@@ -25,8 +27,7 @@ class TableCreateCommandTest < Test::Unit::TestCase
       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({
@@ -40,9 +41,8 @@ class TableCreateCommandTest < Test::Unit::TestCase
     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

  Modified: test/command/test-table-remove.rb (+5 -4)
===================================================================
--- test/command/test-table-remove.rb    2012-11-25 19:15:47 +0900 (9598082)
+++ test/command/test-table-remove.rb    2012-11-25 19:26:05 +0900 (6b519d8)
@@ -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,
@@ -30,9 +32,8 @@ class TableRemoveCommandTest < Test::Unit::TestCase
     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

  Modified: test/command/test-table-rename.rb (+5 -4)
===================================================================
--- test/command/test-table-rename.rb    2012-11-25 19:15:47 +0900 (f213929)
+++ test/command/test-table-rename.rb    2012-11-25 19:26:05 +0900 (403f316)
@@ -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,
@@ -32,9 +34,8 @@ class TableRenameCommandTest < Test::Unit::TestCase
     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

  Modified: test/groonga-command-test-utils.rb (+42 -14)
===================================================================
--- test/groonga-command-test-utils.rb    2012-11-25 19:15:47 +0900 (28c696a)
+++ test/groonga-command-test-utils.rb    2012-11-25 19:26:05 +0900 (0412f03)
@@ -28,8 +28,16 @@ module GroongaCommandTestUtils
       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("=")
@@ -37,29 +45,49 @@ module GroongaCommandTestUtils
         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
 
@@ -67,8 +95,8 @@ module GroongaCommandTestUtils
     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
-------------- next part --------------
HTML����������������������������...
다운로드 



More information about the Groonga-commit mailing list
Back to archive index