test/hexapdf/document/test_destinations.rb in hexapdf-0.24.2 vs test/hexapdf/document/test_destinations.rb in hexapdf-0.25.0

- old
+ new

@@ -6,19 +6,44 @@ describe HexaPDF::Document::Destinations::Destination do def destination(dest) HexaPDF::Document::Destinations::Destination.new(dest) end + describe "self.valid?" do + before do + @klass = HexaPDF::Document::Destinations::Destination + end + + it "validates the type" do + assert(@klass.valid?([5, :Fit])) + refute(@klass.valid?([5, :FitNone])) + end + + it "validates the page entry" do + assert(@klass.valid?([5, :Fit])) + refute(@klass.valid?([HexaPDF::Dictionary.new({Type: :Page}), :FitNone])) + end + + it "validates the arguments" do + assert(@klass.valid?([5, :FitH, 5])) + refute(@klass.valid?([5, :FitH, :other])) + end + end + it "can be asked whether the referenced page is in a remote document" do assert(destination([5, :Fit]).remote?) refute(destination([HexaPDF::Dictionary.new({}), :Fit]).remote?) end it "returns the page object" do assert_equal(:page, destination([:page, :Fit]).page) end + it "can validate a destination" do + assert(destination([5, :Fit]).valid?) + end + describe "type :xyz" do before do @dest = destination([:page, :XYZ, :left, :top, :zoom]) end @@ -197,9 +222,71 @@ describe HexaPDF::Document::Destinations do before do @doc = HexaPDF::Document.new @page = @doc.pages.add + end + + describe "use_or_create" do + it "uses the given destination name if it exists" do + @doc.destinations.create(:fit_page, @page, name: "test") + assert_equal("test", @doc.destinations.use_or_create("test")) + end + + it "fails if the given destination name doesn't exist" do + assert_raises(HexaPDF::Error) { @doc.destinations.use_or_create("test") } + end + + it "uses the given destination array" do + dest = [@page, :Fit] + assert_same(dest, @doc.destinations.use_or_create(dest)) + end + + it "fails if the given destination array is not valid" do + assert_raises(HexaPDF::Error) { @doc.destinations.use_or_create([@page, :FitNone]) } + end + + it "creates a fit page destination for a given page" do + assert_equal([@page, :Fit], @doc.destinations.use_or_create(@page)) + end + + it "fails if the given dictionary object is not a page object" do + assert_raises(HexaPDF::Error) { @doc.destinations.use_or_create(@doc.catalog) } + end + + it "creates a fit page destination for a given page index" do + assert_equal([@page, :Fit], @doc.destinations.use_or_create(0)) + end + + it "fails if the given index is no a valid page index" do + assert_raises(ArgumentError) { @doc.destinations.use_or_create(-1) } + assert_raises(ArgumentError) { @doc.destinations.use_or_create(1) } + end + + it "creates the destination using the provided details" do + dest = @doc.destinations.use_or_create(type: :fit_page_horizontal, page: @page, top: 10) + assert_equal([@page, :FitH, 10], dest) + end + + it "fails creating a destination if the :type key is missing" do + assert_raises(ArgumentError) { @doc.destinations.use_or_create(page: @page) } + end + + it "fails creating a destination if the :page key is missing" do + assert_raises(ArgumentError) { @doc.destinations.use_or_create(type: :fit_page) } + end + + it "fails if the provided argument has an invalid type" do + assert_raises(ArgumentError) { @doc.destinations.use_or_create(:value) } + end + end + + describe "create" do + it "creates the destination based on the given type" do + @doc.destinations.stub(:create_fit_page, [5, :Fit]) do + assert_equal([5, :Fit], @doc.destinations.create(:fit_page, 5)) + end + end end describe "create_xyz" do it "creates the destination" do dest = @doc.destinations.create_xyz(@page, left: 1, top: 2, zoom: 3)