From: Kouhei S. <nu...@co...> - 2017-07-31 00:36:37
|
Kouhei Sutou 2017-07-31 09:35:51 +0900 (Mon, 31 Jul 2017) New Revision: 6f7bb0eb49068865bd7fd773a0f042b45eeaa14d https://github.com/ruby-gnome2/ruby-gnome2/commit/6f7bb0eb49068865bd7fd773a0f042b45eeaa14d Message: poppler: make Document enumerable Added files: poppler/test/fixtures/multiple-pages.odt poppler/test/fixtures/multiple-pages.pdf Modified files: poppler/lib/poppler/document.rb poppler/test/poppler-test-utils.rb poppler/test/test-document.rb Modified: poppler/lib/poppler/document.rb (+3 -1) =================================================================== --- poppler/lib/poppler/document.rb 2017-07-31 01:03:47 +0900 (5ce8f69) +++ poppler/lib/poppler/document.rb 2017-07-31 09:35:51 +0900 (6418483) @@ -16,6 +16,8 @@ module Poppler class Document + include Enumerable + alias_method :initialize_raw, :initialize def initialize(*args) if args.size == 1 and args[0].is_a?(Hash) @@ -67,7 +69,7 @@ module Poppler alias_method :[], :get_page def each - return enum_for(__method__) unless block_given? + return to_enum(__method__) unless block_given? n_pages.times do |i| yield get_page(i) Added: poppler/test/fixtures/multiple-pages.odt (+0 -0) 100644 =================================================================== (Binary files differ) Added: poppler/test/fixtures/multiple-pages.pdf (+0 -0) 100644 =================================================================== (Binary files differ) Modified: poppler/test/poppler-test-utils.rb (+12 -4) =================================================================== --- poppler/test/poppler-test-utils.rb 2017-07-31 01:03:47 +0900 (9f557d7) +++ poppler/test/poppler-test-utils.rb 2017-07-31 09:35:51 +0900 (e404ae7) @@ -15,12 +15,16 @@ module PopplerTestUtils ensure_dir(File.join(test_dir, "fixtures")) end + def fixture_path(*components) + File.join(fixtures_dir, *components) + end + def tmp_dir ensure_dir(File.join(test_dir, "tmp")) end def form_pdf - path = File.join(fixtures_dir, "form.pdf") + path = fixture_path("form.pdf") unless File.exist?(path) pdf = open("https://www.irs.gov/pub/irs-pdf/fw9.pdf").read File.open(path, "wb") do |output| @@ -31,15 +35,19 @@ module PopplerTestUtils end def image_pdf - File.join(fixtures_dir, "image.pdf") + fixture_path("image.pdf") end def image_png - File.join(fixtures_dir, "image.png") + fixture_path("image.png") end def outline_pdf - File.join(fixtures_dir, "outline.pdf") + fixture_path("outline.pdf") + end + + def multiple_pages_pdf + fixture_path("multiple-pages.pdf") end def only_poppler_version(major, minor, micro=nil) Modified: poppler/test/test-document.rb (+23 -0) =================================================================== --- poppler/test/test-document.rb 2017-07-31 01:03:47 +0900 (a083074) +++ poppler/test/test-document.rb 2017-07-31 09:35:51 +0900 (1ca9f36) @@ -23,11 +23,34 @@ class TestDocument < Test::Unit::TestCase assert_equal(default_text, find_first_text_field(reread_document).text) end + def test_each + document = Poppler::Document.new(multiple_pages_pdf) + texts = [] + document.each do |page| + texts << page.text + end + assert_equal(["The first page", "The second page"], + texts) + end + + def test_each_enumerator + document = Poppler::Document.new(multiple_pages_pdf) + assert_equal(["The first page", "The second page"], + document.each.collect(&:text)) + end + + def test_enumerable + document = Poppler::Document.new(multiple_pages_pdf) + assert_equal(["The first page", "The second page"], + document.collect(&:text)) + end + private def find_first_text_field(document) document.each do |page| page.form_field_mapping.each do |mapping| field = mapping.field + p field.class return field if field.field_type == Poppler::FormFieldType::TEXT end end |