[Groonga-commit] groonga/groonga-command-parser at 1301be4 [master] Fix a bug that commands after load are ignored in not line based parse mode

Back to archive index

Kouhei Sutou null+****@clear*****
Mon Oct 30 16:53:40 JST 2017


Kouhei Sutou	2017-10-30 16:53:40 +0900 (Mon, 30 Oct 2017)

  New Revision: 1301be4cf18a8e6a17135da70e7d1a894f75be5c
  https://github.com/groonga/groonga-command-parser/commit/1301be4cf18a8e6a17135da70e7d1a894f75be5c

  Message:
    Fix a bug that commands after load are ignored in not line based parse mode

  Modified files:
    lib/groonga/command/parser.rb
    lib/groonga/command/parser/load-values-parser.rb
    test/test-load-value-parser.rb
    test/test-parser.rb

  Modified: lib/groonga/command/parser.rb (+5 -7)
===================================================================
--- lib/groonga/command/parser.rb    2017-10-25 17:37:07 +0900 (36f24d8)
+++ lib/groonga/command/parser.rb    2017-10-30 16:53:40 +0900 (42dbe23)
@@ -219,9 +219,7 @@ module Groonga
 
       def consume_load_values(tag)
         throw(tag) if****@buffe*****?
-        @command.original_source << @buffer
         @load_values_parser << @buffer
-        @buffer.clear
       end
 
       def consume_line(tag)
@@ -359,14 +357,14 @@ module Groonga
             on_load_value(@command, value)
           end
         end
+        @load_values_parser.on_consumed = lambda do |consumed|
+          @command.original_source << consumed if @loading
+        end
         @load_values_parser.on_end = lambda do |rest|
-          if rest
-            original_source_size =****@comma*****_source.size
-            @command.original_source.slice!(original_source_size - rest.size,
-                                            rest.size)
-          end
+          loading = @loading
           on_load_complete(@command)
           reset
+          @buffer << rest if loading and rest
         end
       end
     end

  Modified: lib/groonga/command/parser/load-values-parser.rb (+11 -2)
===================================================================
--- lib/groonga/command/parser/load-values-parser.rb    2017-10-25 17:37:07 +0900 (fb0818d)
+++ lib/groonga/command/parser/load-values-parser.rb    2017-10-30 16:53:40 +0900 (f0a80da)
@@ -21,10 +21,12 @@ module Groonga
     class Parser
       class LoadValuesParser
         attr_writer :on_value
+        attr_writer :on_consumed
         attr_writer :on_end
         def initialize
           initialize_parser
           @on_value = nil
+          @on_consumed = nil
           @on_end = nil
           @containers = []
           @keys = []
@@ -49,9 +51,16 @@ module Groonga
             :continue
           end
 
+          pos =****@parse*****
+          consumed = pos - before_pos
+          if consumed > 0
+            if consumed < data_size
+              @on_consumed.call(data[0, consumed])
+            else
+              @on_consumed.call(data)
+            end
+          end
           if status == :done
-            pos =****@parse*****
-            consumed = pos - before_pos
             if consumed < data_size
               @on_end.call(data[consumed..-1])
             else

  Modified: test/test-load-value-parser.rb (+16 -2)
===================================================================
--- test/test-load-value-parser.rb    2017-10-25 17:37:07 +0900 (8cc6b4e)
+++ test/test-load-value-parser.rb    2017-10-30 16:53:40 +0900 (63d48bf)
@@ -21,7 +21,12 @@ class LoadValuesParserTest < Test::Unit::TestCase
     @parser.on_value = lambda do |value|
       @values << value
     end
+    @parse_consumed = []
+    @parser.on_consumed = lambda do |consumed|
+      @parse_consumed << consumed
+    end
     @parse_done = false
+    @parse_rest = nil
     @parser.on_end = lambda do |rest|
       @parse_done = true
       @parse_rest = rest
@@ -186,8 +191,17 @@ class LoadValuesParserTest < Test::Unit::TestCase
   }
 ]garbage
       JSON
-      assert_equal([true, "garbage\n"],
-                   [@parse_done, @parse_rest])
+      consumed = [
+        "[\n",
+        "  {\n",
+        "    \"object\": {\n",
+        "      \"string\": \"abc\"\n",
+        "    }\n",
+        "  }\n",
+        "]",
+      ]
+      assert_equal([true, consumed, "garbage\n"],
+                   [@parse_done, @parse_consumed, @parse_rest])
     end
   end
 end

  Modified: test/test-parser.rb (+76 -46)
===================================================================
--- test/test-parser.rb    2017-10-25 17:37:07 +0900 (9692a59)
+++ test/test-parser.rb    2017-10-30 16:53:40 +0900 (07e9d89)
@@ -253,108 +253,138 @@ status
         class MultiLineTest < self
           class BracketTest < self
             def test_have_columns
-              @parser << <<-EOC
+              @parser << <<-COMMAND
 load --table Users --columns "_key, name"
 [
 ["alice", "Alice"]
 ]
-EOC
+              COMMAND
+
               expected_events = []
-              expected_events << [:load_start, <<-EOC.chomp]
+              expected_events << [:load_start, <<-COMMAND.chomp]
 load --table Users --columns "_key, name"
-EOC
-              expected_events << [:load_columns, <<-EOC.chomp, ["_key", "name"]]
+              COMMAND
+              expected_events << [:load_columns, <<-COMMAND.chomp, ["_key", "name"]]
 load --table Users --columns "_key, name"
-EOC
-              expected_events << [:load_value, <<-EOC, ["alice", "Alice"]]
+              COMMAND
+              expected_events << [:load_value, <<-COMMAND, ["alice", "Alice"]]
 load --table Users --columns "_key, name"
-[
-["alice", "Alice"]
-]
-EOC
-              expected_events << [:load_complete, <<-EOC.chomp]
+COMMAND
+              expected_events << [:load_complete, <<-COMMAND.chomp]
 load --table Users --columns "_key, name"
 [
 ["alice", "Alice"]
 ]
-EOC
+              COMMAND
               assert_equal(expected_events, @events)
             end
 
             def test_no_columns
-              @parser << <<-EOC
+              @parser << <<-COMMAND
 load --table Users
 [
 ["_key", "name"],
 ["alice", "Alice"]
 ]
-EOC
+              COMMAND
+
               expected_events = []
-              expected_events << [:load_start, <<-EOC.chomp]
+              expected_events << [:load_start, <<-COMMAND.chomp]
 load --table Users
-EOC
-              expected_events << [:load_columns, <<-EOC, ["_key", "name"]]
+              COMMAND
+              expected_events << [:load_columns, <<-COMMAND, ["_key", "name"]]
 load --table Users
-[
-["_key", "name"],
-["alice", "Alice"]
-]
-EOC
-              expected_events << [:load_value, <<-EOC, ["alice", "Alice"]]
+              COMMAND
+              expected_events << [:load_value, <<-COMMAND, ["alice", "Alice"]]
 load --table Users
-[
-["_key", "name"],
-["alice", "Alice"]
-]
-EOC
-              expected_events << [:load_complete, <<-EOC.chomp]
+              COMMAND
+              expected_events << [:load_complete, <<-COMMAND.chomp]
 load --table Users
 [
 ["_key", "name"],
 ["alice", "Alice"]
 ]
-EOC
+              COMMAND
               assert_equal(expected_events, @events)
             end
           end
 
           def test_brace
-            @parser << <<-EOC
+            @parser << <<-COMMAND
 load --table Users
 [
 {"_key": "alice", "name": "Alice"},
 {"_key": "bob",   "name": "Bob"}
 ]
-EOC
+            COMMAND
+
             expected_events = []
-            expected_events << [:load_start, <<-EOC.chomp]
+            expected_events << [:load_start, <<-COMMAND.chomp]
 load --table Users
-EOC
+            COMMAND
             value = {"_key" => "alice", "name" => "Alice"}
-            expected_events << [:load_value, <<-EOC, value]
+            expected_events << [:load_value, <<-COMMAND, value]
+load --table Users
+            COMMAND
+            value = {"_key" => "bob", "name" => "Bob"}
+            expected_events << [:load_value, <<-COMMAND, value]
+load --table Users
+            COMMAND
+            expected_events << [:load_complete, <<-COMMAND.chomp]
 load --table Users
 [
 {"_key": "alice", "name": "Alice"},
 {"_key": "bob",   "name": "Bob"}
 ]
-EOC
-            value = {"_key" => "bob", "name" => "Bob"}
-            expected_events << [:load_value, <<-EOC, value]
+            COMMAND
+            assert_equal(expected_events, @events)
+          end
+        end
+
+        def test_multiple
+          @parser << <<-COMMANDS
+load --table Users
+[
+{"_key": "alice", "name": "Alice"}
+]
+
 load --table Users
 [
-{"_key": "alice", "name": "Alice"},
 {"_key": "bob",   "name": "Bob"}
 ]
-EOC
-            expected_events << [:load_complete, <<-EOC.chomp]
+          COMMANDS
+
+          expected_events = []
+
+          expected_events << [:load_start, <<-SOURCE.chomp]
+load --table Users
+          SOURCE
+          value = {"_key" => "alice", "name" => "Alice"}
+          expected_events << [:load_value, <<-SOURCE, value]
+load --table Users
+          SOURCE
+          expected_events << [:load_complete, <<-SOURCE.chomp]
+load --table Users
+[
+{"_key": "alice", "name": "Alice"}
+]
+          SOURCE
+
+          expected_events << [:load_start, <<-SOURCE.chomp]
+load --table Users
+          SOURCE
+          value = {"_key" => "bob", "name" => "Bob"}
+          expected_events << [:load_value, <<-SOURCE, value]
+load --table Users
+          SOURCE
+          expected_events << [:load_complete, <<-SOURCE.chomp]
 load --table Users
 [
-{"_key": "alice", "name": "Alice"},
 {"_key": "bob",   "name": "Bob"}
 ]
-EOC
-            assert_equal(expected_events, @events)
-          end
+          SOURCE
+
+          assert_equal(expected_events, @events)
         end
 
         class ErrorTest < self
-------------- next part --------------
HTML����������������������������...
URL: https://lists.osdn.me/mailman/archives/groonga-commit/attachments/20171030/92574f96/attachment-0001.htm 



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