[Groonga-commit] ranguba/chupa-text-decomposer-mail at 8f914a6 [master] Add test

Back to archive index

Kouhei Sutou null+****@clear*****
Sun Feb 19 00:44:48 JST 2017


Kouhei Sutou	2017-02-19 00:44:48 +0900 (Sun, 19 Feb 2017)

  New Revision: 8f914a65203507bdda4b39196624ae57b51322c4
  https://github.com/ranguba/chupa-text-decomposer-mail/commit/8f914a65203507bdda4b39196624ae57b51322c4

  Message:
    Add test

  Added files:
    test/fixture/attributes.eml
    test/fixture/multipart.eml
    test/fixture/text.eml
    test/helper.rb
    test/run-test.rb
    test/test-mail.rb
  Modified files:
    lib/chupa-text/decomposers/mail.rb

  Modified: lib/chupa-text/decomposers/mail.rb (+26 -7)
===================================================================
--- lib/chupa-text/decomposers/mail.rb    2017-02-18 01:04:36 +0900 (54d1e59)
+++ lib/chupa-text/decomposers/mail.rb    2017-02-19 00:44:48 +0900 (91b2741)
@@ -34,22 +34,41 @@ module ChupaText
 
       def decompose(data)
         mail = ::Mail.new(data.body)
-        mail.body.parts.each_with_index do |part, i|
+        decompose_attributes(mail, data)
+
+        if mail.multipart?
+          parts = mail.body.parts
+        else
+          parts = [mail]
+        end
+        parts.each_with_index do |part, i|
           body = part.body.decoded
           body.force_encoding(part.charset)
 
-          part_data = TextData.new(body)
+          part_data = TextData.new(body, :source_data => data)
           part_data.uri = "#{data.uri}\##{i}"
           part_data.mime_type = part.mime_type
-          data.attributes.each do |name, value|
-            part_data[name] = value
-          end
           part_data[:encoding] = body.encoding.to_s
-          part_data[:subject] = mail.subject
-          part_data[:author] = mail[:from].formatted | mail[:from].addresses
           yield(part_data)
         end
       end
+
+      private
+      def decompose_attributes(mail, data)
+        data[:subject] = mail.subject
+
+        from = mail[:from]
+        if from
+          data[:from] = from.formatted | from.addresses
+        end
+
+        to = mail[:to]
+        if to
+          data[:to] = to.formatted | to.addresses
+        end
+
+        data[:date] = mail.date
+      end
     end
   end
 end

  Added: test/fixture/attributes.eml (+7 -0) 100644
===================================================================
--- /dev/null
+++ test/fixture/attributes.eml    2017-02-19 00:44:48 +0900 (b51888d)
@@ -0,0 +1,7 @@
+Mime-Version: 1.0
+Subject: Hello
+From: Sender <from �� example.com>
+To: Recipient1 <to1 �� example.com>, Recipient2 <to2 �� example.com>
+Date: Sun, 19 Feb 2017 00:27:55 +0900 (JST)
+
+World

  Added: test/fixture/multipart.eml (+23 -0) 100644
===================================================================
--- /dev/null
+++ test/fixture/multipart.eml    2017-02-19 00:44:48 +0900 (0ede64e)
@@ -0,0 +1,23 @@
+Mime-Version: 1.0
+Subject: Hello
+From: Sender <from �� example.com>
+To: Recipient <to �� example.com>
+Date: Sun, 19 Feb 2017 00:27:55 +0900 (JST)
+Content-Type: multipart/alternative;
+ boundary="++++";
+ charset=UTF-8
+Content-Transfer-Encoding: 8bit
+
+--++++
+Content-Type: text/plain;
+ charset=UTF-8
+Content-Transfer-Encoding: 8bit
+
+World
+--++++
+Content-Type: text/html;
+ charset=UTF-8
+Content-Transfer-Encoding: 8bit
+
+<p>World</p>
+--++++

  Added: test/fixture/text.eml (+9 -0) 100644
===================================================================
--- /dev/null
+++ test/fixture/text.eml    2017-02-19 00:44:48 +0900 (3c6eb86)
@@ -0,0 +1,9 @@
+Mime-Version: 1.0
+Subject: Hello
+From: Sender <from �� example.com>
+To: Recipient <to �� example.com>
+Date: Sun, 19 Feb 2017 00:27:55 +0900 (JST)
+Content-Type: Text/Plain; charset=UTF-8
+Content-Transfer-Encoding: 8bit
+
+World

  Added: test/helper.rb (+22 -0) 100644
===================================================================
--- /dev/null
+++ test/helper.rb    2017-02-19 00:44:48 +0900 (2e69b52)
@@ -0,0 +1,22 @@
+# Copyright (C) 2017  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
+
+module Helper
+  def fixture_path(*components)
+    base_dir = File.expand_path(File.dirname(__FILE__))
+    File.join(base_dir, "fixture", *components)
+  end
+end

  Added: test/run-test.rb (+33 -0) 100755
===================================================================
--- /dev/null
+++ test/run-test.rb    2017-02-19 00:44:48 +0900 (6f1cb61)
@@ -0,0 +1,33 @@
+#!/usr/bin/env ruby
+#
+# Copyright (C) 2017  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
+
+$VERBOSE = true
+
+ENV["TZ"] = "JST"
+
+require "bundler/setup"
+
+require "test-unit"
+
+require "chupa-text"
+
+ChupaText::Decomposers.load
+
+require_relative "helper"
+
+exit(Test::Unit::AutoRunner.run(true))

  Added: test/test-mail.rb (+151 -0) 100644
===================================================================
--- /dev/null
+++ test/test-mail.rb    2017-02-19 00:44:48 +0900 (41eb2ef)
@@ -0,0 +1,151 @@
+# Copyright (C) 2017  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
+
+require "pathname"
+
+class TestMail < Test::Unit::TestCase
+  include Helper
+
+  def setup
+    @options = {}
+  end
+
+  private
+  def decomposer
+    ChupaText::Decomposers::Mail.new(@options)
+  end
+
+  sub_test_case("target?") do
+    sub_test_case("extension") do
+      def create_data(uri)
+        data = ChupaText::Data.new
+        data.body = ""
+        data.uri = uri
+        data
+      end
+
+      def test_eml
+        assert do
+          decomposer.target?(create_data("0.eml"))
+        end
+      end
+
+      def test_mew
+        assert do
+          decomposer.target?(create_data("0.mew"))
+        end
+      end
+
+      def test_txt
+        assert do
+          not decomposer.target?(create_data("0.txt"))
+        end
+      end
+    end
+
+    sub_test_case("mime-type") do
+      def create_data(mime_type)
+        data = ChupaText::Data.new
+        data.mime_type = mime_type
+        data
+      end
+
+      def test_rfc822
+        assert do
+          decomposer.target?(create_data("message/rfc822"))
+        end
+      end
+
+      def test_html
+        assert do
+          not decomposer.target?(create_data("text/html"))
+        end
+      end
+    end
+  end
+
+  sub_test_case("decompose") do
+    private
+    def decompose(path)
+      data = ChupaText::InputData.new(path)
+      data.mime_type = "message/rfc822"
+
+      decomposed = []
+      decomposer.decompose(data) do |decomposed_data|
+        decomposed << decomposed_data
+      end
+      decomposed
+    end
+
+    sub_test_case("attributes") do
+      def test_subject
+        assert_equal(["Hello"], decompose("subject"))
+      end
+
+      def test_from
+        assert_equal([["Sender <from �� example.com>", "from �� example.com"]],
+                     decompose("from"))
+      end
+
+      def test_to
+        assert_equal([
+                       [
+                         "Recipient1 <to1 �� example.com>",
+                         "Recipient2 <to2 �� example.com>",
+                         "to1 �� example.com",
+                         "to2 �� example.com",
+                       ]
+                     ],
+                     decompose("to"))
+      end
+
+      def test_date
+        assert_equal([DateTime.parse("2017-02-19T00:27:55+09:00")],
+                     decompose("date"))
+      end
+
+      private
+      def decompose(attribute_name)
+        super(fixture_path("attributes.eml")).collect do |data|
+          data[attribute_name]
+        end
+      end
+    end
+
+    sub_test_case("one page") do
+      def test_body
+        assert_equal(["World\n"], decompose.collect(&:body))
+      end
+
+      private
+      def decompose
+        super(fixture_path("text.eml"))
+      end
+    end
+
+    sub_test_case("multipart") do
+      def test_body
+        assert_equal(["World", "<p>World</p>"],
+                     decompose.collect(&:body))
+      end
+
+      private
+      def decompose
+        super(fixture_path("multipart.eml"))
+      end
+    end
+  end
+end
-------------- next part --------------
HTML����������������������������...
下载 



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