[Groonga-commit] droonga/drntest at ea07129 [master] Extract JSON loading code as a class

Back to archive index

Kouhei Sutou null+****@clear*****
Tue Dec 10 19:04:56 JST 2013


Kouhei Sutou	2013-12-10 19:04:56 +0900 (Tue, 10 Dec 2013)

  New Revision: ea071293aa3f68c551cad4124971c92e9acba2b1
  https://github.com/droonga/drntest/commit/ea071293aa3f68c551cad4124971c92e9acba2b1

  Message:
    Extract JSON loading code as a class

  Added files:
    lib/drntest/json-loader.rb
  Modified files:
    lib/drntest/test-runner.rb

  Added: lib/drntest/json-loader.rb (+56 -0) 100644
===================================================================
--- /dev/null
+++ lib/drntest/json-loader.rb    2013-12-10 19:04:56 +0900 (073d3e2)
@@ -0,0 +1,56 @@
+# Copyright (C) 2013  Droonga Project
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program 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 General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+require "yajl"
+
+module Drntest
+  class JSONLoader
+    attr_reader :objects
+
+    def initialize
+      @parser = Yajl::Parser.new
+      @objects = []
+      @parser.on_parse_complete = lambda do |object|
+        @objects << object
+      end
+    end
+
+    def <<(data)
+      @parser << data
+    end
+
+    def load(path)
+      path.open do |file|
+        data = ""
+        file.each_line do |line|
+          data << line
+          begin
+            self << line
+          rescue Yajl::ParseError => error
+            marker = "-" * 60
+            puts("Failed to load JSONs file: #{path}")
+            puts(marker)
+            puts(data)
+            puts(marker)
+            puts(error)
+            puts(marker)
+            break
+          end
+        end
+      end
+      @objects
+    end
+  end
+end

  Modified: lib/drntest/test-runner.rb (+4 -15)
===================================================================
--- lib/drntest/test-runner.rb    2013-12-10 16:56:41 +0900 (fac6c19)
+++ lib/drntest/test-runner.rb    2013-12-10 19:04:56 +0900 (840759a)
@@ -22,6 +22,7 @@ require "fileutils"
 require "drntest/path"
 require "drntest/test-results"
 require "drntest/test-executor"
+require "drntest/json-loader"
 
 module Drntest
   class TestRunner
@@ -161,21 +162,9 @@ module Drntest
       load_jsons(expected_path)
     end
 
-    def load_jsons(path, options={})
-      parser = Yajl::Parser.new
-      json_objects = []
-      parser.on_parse_complete = Proc.new do |json_object|
-        json_objects << json_object
-      end
-      Pathname(path).read.each_line do |line|
-        begin
-          parser << line
-        rescue StandardError => error
-          p "Failed to load JSONs file: #{path.to_s}"
-          raise error
-        end
-      end
-      json_objects
+    def load_jsons(path)
+      loader = JSONLoader.new
+      loader.load(path)
     end
 
     def expected_exist?
-------------- next part --------------
HTML����������������������������...
Download 



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