test/test-pdf.rb in chupa-text-decomposer-pdf-1.0.1 vs test/test-pdf.rb in chupa-text-decomposer-pdf-1.0.2

- old
+ new

@@ -1,6 +1,6 @@ -# Copyright (C) 2013 Kouhei Sutou <kou@clear-code.com> +# Copyright (C) 2013-2014 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. @@ -16,14 +16,18 @@ require "pathname" class TestPDF < Test::Unit::TestCase def setup - @decomposer = ChupaText::Decomposers::PDF.new({}) + @options = {} end private + def decomposer + ChupaText::Decomposers::PDF.new(@options) + end + def fixture_path(*components) base_path = Pathname(__FILE__).dirname + "fixture" base_path.join(*components) end @@ -35,15 +39,15 @@ data.uri = uri data end def test_pdf - assert_true(@decomposer.target?(create_data("index.pdf"))) + assert_true(decomposer.target?(create_data("index.pdf"))) end def test_html - assert_false(@decomposer.target?(create_data("index.html"))) + assert_false(decomposer.target?(create_data("index.html"))) end end sub_test_case("mime-type") do def create_data(mime_type) @@ -51,15 +55,15 @@ data.mime_type = mime_type data end def test_pdf - assert_true(@decomposer.target?(create_data("application/pdf"))) + assert_true(decomposer.target?(create_data("application/pdf"))) end def test_html - assert_false(@decomposer.target?(create_data("text/html"))) + assert_false(decomposer.target?(create_data("text/html"))) end end end sub_test_case("decompose") do @@ -67,11 +71,11 @@ def decompose(path) data = ChupaText::InputData.new(path) data.mime_type = "text/pdf" decomposed = [] - @decomposer.decompose(data) do |decomposed_data| + decomposer.decompose(data) do |decomposed_data| decomposed << decomposed_data end decomposed end @@ -98,12 +102,13 @@ def test_producer assert_equal(["LibreOffice 4.1"], decompose("producer")) end - def test_creation_date - assert_equal([nil], decompose("creation_date")) + def test_created_time + assert_equal([Time.parse("2014-01-06T00:52:45+09:00")], + decompose("created_time")) end private def decompose(attribute_name) super(fixture_path("attributes.pdf")).collect do |data| @@ -112,26 +117,51 @@ end end sub_test_case("one page") do def test_body - assert_equal(["Page1"], decompose.collect(&:body)) + assert_equal(["Page1\n"], decompose.collect(&:body)) end private def decompose super(fixture_path("one-page.pdf")) end end sub_test_case("multi pages") do def test_body - assert_equal(["Page1\nPage2"], decompose.collect(&:body)) + assert_equal(["Page1\nPage2\n"], decompose.collect(&:body)) end private def decompose super(fixture_path("multi-pages.pdf")) + end + end + + sub_test_case("encrypted") do + def test_with_password + @options = {:password => "encrypted"} + assert_equal(["Password is 'encrypted'.\n"], + decompose.collect(&:body)) + end + + def test_with_password_block + @options = {:password => lambda {|data| "encrypted"}} + assert_equal(["Password is 'encrypted'.\n"], + decompose.collect(&:body)) + end + + def test_without_password + assert_raise(ChupaText::EncryptedError) do + decompose + end + end + + private + def decompose + super(fixture_path("encrypted.pdf")) end end end end