[Groonga-commit] ranguba/chupa-text at d7e476f [master] Add CaptureLogger that is convenient for testing

Back to archive index
Kouhei Sutou null+****@clear*****
Sun Mar 3 06:03:42 JST 2019


Kouhei Sutou	2019-03-03 06:03:42 +0900 (Sun, 03 Mar 2019)

  Revision: d7e476fac356176f8d44fe53108848ab159d5403
  https://github.com/ranguba/chupa-text/commit/d7e476fac356176f8d44fe53108848ab159d5403

  Message:
    Add CaptureLogger that is convenient for testing

  Copied files:
    lib/chupa-text/capture-logger.rb
      (from test/helper.rb)
  Modified files:
    lib/chupa-text.rb
    test/helper.rb

  Modified: lib/chupa-text.rb (+1 -0)
===================================================================
--- lib/chupa-text.rb    2019-03-03 06:03:33 +0900 (09c1b40)
+++ lib/chupa-text.rb    2019-03-03 06:03:42 +0900 (4d567a8)
@@ -20,6 +20,7 @@ require "chupa-text/error"
 
 require "chupa-text/size-parser"
 require "chupa-text/default-logger"
+require "chupa-text/capture-logger"
 require "chupa-text/logger"
 
 require "chupa-text/loggable"

  Copied: lib/chupa-text/capture-logger.rb (+29 -37) 54%
===================================================================
--- test/helper.rb    2019-03-03 06:03:33 +0900 (68f814e)
+++ lib/chupa-text/capture-logger.rb    2019-03-03 06:03:42 +0900 (6390de5)
@@ -1,4 +1,4 @@
-# Copyright (C) 2013-2019  Kouhei Sutou <kou****@clear*****>
+# Copyright (C) 2019  Kouhei Sutou <kou****@clear*****>
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -14,52 +14,44 @@
 # 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 "pathname"
-require "tempfile"
-require "uri"
-
-module Helper
-  def fixture_path(*components)
-    base_path = Pathname(__FILE__).dirname + "fixture"
-    base_path.join(*components)
-  end
+module ChupaText
+  class CaptureLogger
+    class << self
+      def capture
+        original_logger = ChupaText.logger
+        begin
+          output = []
+          ChupaText.logger = new(output)
+          yield
+          output
+        ensure
+          ChupaText.logger = original_logger
+        end
+      end
+    end
 
-  def fixture_uri(*components)
-    path = fixture_path(*components)
-    file_uri(path)
-  end
+    def initialize(output)
+      @output = output
+    end
 
-  def file_uri(path)
-    URI.parse("file://#{path}")
-  end
+    def debug(message=nil)
+      @output << [:debu, message || yield]
+    end
 
+    def info(message=nil)
+      @output << [:info, message || yield]
+    end
 
-  class CaptureLogger
-    def initialize(output)
-      @output = output
+    def warn(message=nil)
+      @output << [:warn, message || yield]
     end
 
     def error(message=nil)
       @output << [:error, message || yield]
     end
-  end
-
-  def capture_log
-    original_logger = ChupaText.logger
-    begin
-      output = []
-      ChupaText.logger = CaptureLogger.new(output)
-      yield
-      normalize_log(output)
-    ensure
-      ChupaText.logger = original_logger
-    end
-  end
 
-  def normalize_log(log)
-    log.collect do |level, message|
-      message = message.split("\n", 2)[0]
-      [level, message]
+    def fatal(message=nil)
+      @output << [:fatal, message || yield]
     end
   end
 end

  Modified: test/helper.rb (+2 -25)
===================================================================
--- test/helper.rb    2019-03-03 06:03:33 +0900 (68f814e)
+++ test/helper.rb    2019-03-03 06:03:42 +0900 (9e068e6)
@@ -33,31 +33,8 @@ module Helper
     URI.parse("file://#{path}")
   end
 
-
-  class CaptureLogger
-    def initialize(output)
-      @output = output
-    end
-
-    def error(message=nil)
-      @output << [:error, message || yield]
-    end
-  end
-
-  def capture_log
-    original_logger = ChupaText.logger
-    begin
-      output = []
-      ChupaText.logger = CaptureLogger.new(output)
-      yield
-      normalize_log(output)
-    ensure
-      ChupaText.logger = original_logger
-    end
-  end
-
-  def normalize_log(log)
-    log.collect do |level, message|
+  def capture_log(&block)
+    ChupaText::CaptureLogger.capture(&block).collect do |level, message|
       message = message.split("\n", 2)[0]
       [level, message]
     end
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.osdn.me/mailman/archives/groonga-commit/attachments/20190303/379816d9/attachment-0001.html>


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