[Groonga-commit] ranguba/chupa-text at b080bda [master] Support chupa-text from stdin

Back to archive index

Kouhei Sutou null+****@clear*****
Thu Jan 2 11:51:40 JST 2014


Kouhei Sutou	2014-01-02 11:51:40 +0900 (Thu, 02 Jan 2014)

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

  Message:
    Support chupa-text from stdin

  Added files:
    test/command/test-chupa-text.rb
    test/fixture/command/chupa-text/hello.txt
  Modified files:
    lib/chupa-text/command/chupa-text.rb

  Modified: lib/chupa-text/command/chupa-text.rb (+37 -7)
===================================================================
--- lib/chupa-text/command/chupa-text.rb    2014-01-02 00:49:40 +0900 (6fac5a6)
+++ lib/chupa-text/command/chupa-text.rb    2014-01-02 11:51:40 +0900 (a18f502)
@@ -14,6 +14,8 @@
 # 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 "optparse"
+
 module ChupaText
   module Command
     class ChupaText
@@ -25,22 +27,50 @@ module ChupaText
       end
 
       def initialize
+        @path = nil
       end
 
       def run(*arguments)
-        paths = arguments
+        return false unless parse_arguments(arguments)
+
         extractor = create_extractor
-        paths.each do |path|
-          data = Data.new
-          data.path = path
-          extractor.extract(data) do |extracted|
-            puts(extracted.body)
-          end
+        data = Data.new
+        if****@path*****?
+          data.body = $stdin.read
+        else
+          data.path = @path
+        end
+        extractor.extract(data) do |extracted|
+          puts(extracted.body)
         end
         true
       end
 
       private
+      def parse_arguments(arguments)
+        parser = create_option_parser
+        rest = nil
+        begin
+          rest = parser.parse!(arguments)
+        rescue OptionParser::ParseError
+          puts($!.message)
+          return false
+        end
+        if rest.size > 1
+          puts(parser.help)
+          return false
+        end
+        @path, = rest
+        true
+      end
+
+      def create_option_parser
+        parser = OptionParser.new
+        parser.banner += " [FILE]"
+        parser.version = VERSION
+        parser
+      end
+
       def create_extractor
         Decomposer.load
         extractor = Extractor.new

  Added: test/command/test-chupa-text.rb (+69 -0) 100644
===================================================================
--- /dev/null
+++ test/command/test-chupa-text.rb    2014-01-02 11:51:40 +0900 (657dcef)
@@ -0,0 +1,69 @@
+# Copyright (C) 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
+
+class TestCommandChupaText < Test::Unit::TestCase
+  include Helper
+
+  def setup
+    setup_io
+  end
+
+  def teardown
+    teardown_io
+  end
+
+  def setup_io
+    @original_stdin  = $stdin
+    @original_stdout = $stdout
+    @stdin  = StringIO.new
+    @stdout = StringIO.new
+    $stdin  = @stdin
+    $stdout = @stdout
+  end
+
+  def teardown_io
+    $stdin  = @original_stdin
+    $stdout = @original_stdout
+  end
+
+  private
+  def run_command(*arguments)
+    succeeded = ChupaText::Command::ChupaText.run(*arguments)
+    [succeeded, @stdout.string]
+  end
+
+  def fixture_path(*components)
+    super("command", "chupa-text", *components)
+  end
+
+  sub_test_case("output") do
+    sub_test_case("file") do
+      def test_single
+        assert_equal([true, "Hello\n"],
+                     run_command(fixture_path("hello.txt")))
+      end
+    end
+
+    sub_test_case("standard input") do
+      def test_single
+        @stdin << "Hello\n"
+        @stdin.rewind
+        assert_equal([true, "Hello\n"],
+                     run_command)
+      end
+    end
+  end
+end

  Added: test/fixture/command/chupa-text/hello.txt (+1 -0) 100644
===================================================================
--- /dev/null
+++ test/fixture/command/chupa-text/hello.txt    2014-01-02 11:51:40 +0900 (e965047)
@@ -0,0 +1 @@
+Hello
-------------- next part --------------
HTML����������������������������...
Download 



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