test/hexapdf/layout/test_inline_box.rb in hexapdf-0.31.0 vs test/hexapdf/layout/test_inline_box.rb in hexapdf-0.32.0

- old
+ new

@@ -1,33 +1,53 @@ # -*- encoding: utf-8 -*- require 'test_helper' require 'hexapdf/layout/inline_box' +require 'hexapdf/document' describe HexaPDF::Layout::InlineBox do + def inline_box(box, valign: :baseline) + HexaPDF::Layout::InlineBox.new(box, valign: valign) + end + before do @box = HexaPDF::Layout::InlineBox.create(width: 10, height: 15, margin: [15, 10]) end - it "needs a box to wrap and an optional alignment on initialization" do - ibox = HexaPDF::Layout::InlineBox.new(@box) - assert_equal(@box, ibox.box) - assert_equal(:baseline, ibox.valign) + describe "initialize" do + it "needs a box to wrap and an optional alignment on initialization" do + box = HexaPDF::Layout::Box.create(width: 10, height: 15) + ibox = inline_box(box) + assert_equal(box, ibox.box) + assert_equal(:baseline, ibox.valign) - ibox = HexaPDF::Layout::InlineBox.new(@box, valign: :top) - assert_equal(:top, ibox.valign) - end + ibox = inline_box(box, valign: :top) + assert_equal(:top, ibox.valign) + end - it "draws the wrapped box at the correct position" do - canvas = Object.new - block = lambda do |c, x, y| - assert_equal(canvas, c) - assert_equal(10, x) - assert_equal(15, y) + it "automatically fits the provided box into a frame" do + ibox = inline_box(HexaPDF::Document.new.layout.text("test is going good", width: 20)) + assert_equal(20, ibox.width) + assert_equal(45, ibox.height) end - @box.box.stub(:draw, block) do - @box.draw(canvas, 0, 0) + + it "fails if the wrapped box could not be fit" do + box = HexaPDF::Document.new.layout.text("test is not going good", width: 1) + assert_raises(HexaPDF::Error) { inline_box(box) } end + + it "fails if the height is not set explicitly and during fitting" do + assert_raises(HexaPDF::Error) do + inline_box(HexaPDF::Layout::Box.create(width: 10)) + end + end + end + + it "draws the wrapped box at the correct position" do + doc = HexaPDF::Document.new + canvas = doc.pages.add.canvas + inline_box(doc.layout.text("", width: 20, margin: [15, 10])).draw(canvas, 100, 200) + assert_equal("q\n1 0 0 1 110 -99785 cm\nQ\n", canvas.contents) end it "returns true if the inline box is empty with no drawing operations" do assert(@box.empty?) refute(HexaPDF::Layout::InlineBox.create(width: 10, height: 15) {}.empty?)