test/hexapdf/document/test_pages.rb in hexapdf-0.25.0 vs test/hexapdf/document/test_pages.rb in hexapdf-0.26.0

- old
+ new

@@ -134,6 +134,92 @@ @doc.pages.add @doc.pages.add assert_equal(3, @doc.pages.count) end end + + describe "page_label" do + it "returns the page label object for the given range start index" do + 11.times { @doc.pages.add } + @doc.catalog[:PageLabels] = {Nums: [0, {S: :D}, 5, {S: :r, St: 2}, 10, {P: 'A-', S: :a}]} + assert_equal("1", @doc.pages.page_label(0)) + assert_equal("5", @doc.pages.page_label(4)) + assert_equal("ii", @doc.pages.page_label(5)) + assert_equal("vi", @doc.pages.page_label(9)) + assert_equal("A-a", @doc.pages.page_label(10)) + end + + it "fails if the page index is out of range" do + assert_raises(ArgumentError) { @doc.pages.page_label(-1) } + assert_raises(ArgumentError) { @doc.pages.page_label(0) } + end + end + + describe "each_labelling_range" do + before do + 10.times { @doc.pages.add } + end + + it "returns no entries for an empty or non-existing /PageLabels entry" do + assert(@doc.pages.each_labelling_range.to_a.empty?) + end + + it "works for a single page label entry" do + @doc.catalog[:PageLabels] = {Nums: [0, {S: :r}]} + result = @doc.pages.each_labelling_range.to_a + assert_equal([[0, 10, {S: :r}]], result.map {|s, c, l| [s, c, l.value]}) + assert_equal(:lowercase_roman, result[0].last.numbering_style) + end + + it "works for multiple page label entries" do + @doc.catalog[:PageLabels] = {Nums: [0, {S: :r}, 2, {S: :d}, 7, {S: :A}]} + result = @doc.pages.each_labelling_range.to_a + assert_equal([[0, 2, {S: :r}], [2, 5, {S: :d}], [7, 3, {S: :A}]], + result.map {|s, c, l| [s, c, l.value]}) + end + + it "returns a zero or negative count for the last range if there aren't enough pages" do + assert_equal(10, @doc.pages.count) + @doc.catalog[:PageLabels] = {Nums: [0, {S: :d}, 10, {S: :r}]} + assert_equal(0, @doc.pages.each_labelling_range.to_a[-1][1]) + @doc.catalog[:PageLabels][:Nums][2] = 11 + assert_equal(-1, @doc.pages.each_labelling_range.to_a[-1][1]) + end + end + + describe "add_labelling_range" do + it "creates a new page label object for the given arguments" do + label = @doc.pages.add_labelling_range(5, numbering_style: :lowercase_roman, + start_number: 5, prefix: 'a') + assert_equal({S: :r, St: 5, P: 'a'}, label.value) + assert_equal(label, @doc.catalog.page_labels.find_entry(5)) + end + + it "adds an entry for the range starting at 0 if it doesn't exist" do + label = @doc.pages.add_labelling_range(5) + assert_equal([{S: :d}, label], + @doc.catalog.page_labels[:Nums].value.values_at(1, 3)) + end + end + + describe "delete_labelling_range" do + before do + @doc.catalog[:PageLabels] = {Nums: [0, {S: :r}, 5, {S: :d}]} + end + + it "deletes the labelling range for a given start index" do + label = @doc.pages.delete_labelling_range(5) + assert_equal({S: :d}, label) + end + + it "deletes the labelling range for 0 if it is the last, together with the number tree" do + @doc.pages.delete_labelling_range(5) + label = @doc.pages.delete_labelling_range(0) + assert_equal({S: :r}, label) + assert_nil(@doc.catalog[:PageLabels]) + end + + it "fails if the range starting at zero is deleted when other ranges still exist" do + assert_raises(HexaPDF::Error) { @doc.pages.delete_labelling_range(0) } + end + end end