test/hexapdf/test_revision.rb in hexapdf-0.19.3 vs test/hexapdf/test_revision.rb in hexapdf-0.20.0

- old
+ new

@@ -1,27 +1,38 @@ # -*- encoding: utf-8 -*- require 'test_helper' require 'hexapdf/revision' -require 'hexapdf/object' +require 'hexapdf/dictionary' require 'hexapdf/reference' require 'hexapdf/xref_section' +require 'hexapdf/type/catalog' require 'stringio' describe HexaPDF::Revision do before do @xref_section = HexaPDF::XRefSection.new @xref_section.add_in_use_entry(2, 0, 5000) @xref_section.add_free_entry(3, 0) + @xref_section.add_in_use_entry(4, 0, 1000) + @xref_section.add_in_use_entry(5, 0, 1000) + @xref_section.add_in_use_entry(6, 0, 5000) + @xref_section.add_in_use_entry(7, 0, 5000) @obj = HexaPDF::Object.new(:val, oid: 1, gen: 0) @ref = HexaPDF::Reference.new(1, 0) @loader = lambda do |entry| if entry.type == :free HexaPDF::Object.new(nil, oid: entry.oid, gen: entry.gen) else - HexaPDF::Object.new(:Test, oid: entry.oid, gen: entry.gen) + case entry.oid + when 4 then HexaPDF::Dictionary.new({Type: :XRef}, oid: entry.oid, gen: entry.gen) + when 5 then HexaPDF::Dictionary.new({Type: :ObjStm}, oid: entry.oid, gen: entry.gen) + when 7 then HexaPDF::Type::Catalog.new({Type: :Catalog}, oid: entry.oid, gen: entry.gen, + document: self) + else HexaPDF::Object.new(:Test, oid: entry.oid, gen: entry.gen) + end end end @rev = HexaPDF::Revision.new({}, xref_section: @xref_section, loader: @loader) end @@ -34,14 +45,14 @@ rev = HexaPDF::Revision.new({}, loader: @loader, xref_section: @xref_section) assert_equal(:Test, rev.object(2).value) end it "returns the next free object number" do - assert_equal(4, @rev.next_free_oid) - @obj.oid = 4 + assert_equal(8, @rev.next_free_oid) + @obj.oid = 8 @rev.add(@obj) - assert_equal(5, @rev.next_free_oid) + assert_equal(9, @rev.next_free_oid) end describe "add" do it "works correctly" do @rev.add(@obj) @@ -149,19 +160,18 @@ refute(@obj.null?) @rev.delete(@ref) assert(@rev.object(@ref).null?) assert(@obj.null?) assert_raises(HexaPDF::Error) { @obj.document } + assert_same(@obj.data, @rev.object(@ref).data) end end describe "object iteration" do it "iterates over all objects via each" do @rev.add(@obj) - obj2 = @rev.object(2) - obj3 = @rev.object(3) - assert_equal([@obj, obj2, obj3], @rev.each.to_a) + assert_equal([@obj, *(2..7).map {|i| @rev.object(i) }], @rev.each.to_a) end it "iterates only over loaded objects" do obj = @rev.object(2) assert_equal([obj], @rev.each(only_loaded: true).to_a) @@ -176,13 +186,33 @@ assert_equal([@obj], rev.each.to_a) rev.delete(@ref, mark_as_free: false) refute(rev.object?(@ref)) end - it "can iterate over all modified objects" do - obj = @rev.object(2) - assert_equal([], @rev.each_modified_object.to_a) - obj.value = :Other - @rev.add(@obj) - assert_equal([obj, @obj], @rev.each_modified_object.to_a) + describe "each_modified_object" do + it "returns modified objects" do + obj = @rev.object(2) + obj.value = :Other + @rev.add(@obj) + deleted = @rev.object(6) + @rev.delete(6) + assert_equal([obj, @obj, deleted], @rev.each_modified_object.to_a) + end + + it "ignores object and xref streams that were deleted" do + @rev.delete(4) + @rev.delete(5) + assert_equal([], @rev.each_modified_object.to_a) + end + + it "doesn't return non-modified objects" do + @rev.object(2) + assert_equal([], @rev.each_modified_object.to_a) + end + + it "doesn't return objects that have modified values just because of reading" do + obj = @rev.object(7) + obj.delete(:Type) + assert_equal([], @rev.each_modified_object.to_a) + end end end