Module: Pdfrb::TestUtils
- Defined in:
- lib/pdfrb/test_utils.rb
Overview
Test utilities for downstream projects and our own specs. Provides helpers for creating minimal PDFs, loading fixtures, and round-tripping documents in tests.
Class Method Summary collapse
-
.capture_log { ... } ⇒ String
Capture Pdfrb logger output during a block.
-
.count_pages(pdf_bytes) ⇒ Integer
Count pages in a PDF byte string.
-
.document_to_string(document) ⇒ String
Write a Document to a String.
-
.fixture_path(relative_path) ⇒ String
Resolve a fixture path under spec/fixtures.
-
.load_fixture(relative_path) ⇒ String
Load a fixture file from a relative path under spec/fixtures.
-
.minimal_document(size: [612, 792]) ⇒ Pdfrb::Document
Build a minimal valid PDF Document with one blank page.
-
.minimal_pdf(size: [612, 792]) ⇒ String
Build a minimal PDF byte string with one blank page.
-
.pdf_with_text(text, font: :Helvetica, size: 12) ⇒ String
Generate a one-page PDF with a single text string.
-
.roundtrip(document) ⇒ Pdfrb::Document
Round-trip a Document through serialize → parse and return the re-parsed Document.
-
.same_page_count?(left, right) ⇒ Boolean
Assert that two PDF byte strings have the same page count.
Class Method Details
.capture_log { ... } ⇒ String
Capture Pdfrb logger output during a block. Returns the captured string. Useful for asserting on warnings.
101 102 103 104 105 106 107 108 109 110 |
# File 'lib/pdfrb/test_utils.rb', line 101 def capture_log require "stringio" original = Pdfrb.logger buffer = StringIO.new Pdfrb.logger = Logger.new(buffer) yield buffer.string ensure Pdfrb.logger = original end |
.count_pages(pdf_bytes) ⇒ Integer
Count pages in a PDF byte string.
79 80 81 |
# File 'lib/pdfrb/test_utils.rb', line 79 def count_pages(pdf_bytes) Pdfrb::Document.new(io: StringIO.new(pdf_bytes)).pages.count end |
.document_to_string(document) ⇒ String
Write a Document to a String.
61 62 63 64 65 66 |
# File 'lib/pdfrb/test_utils.rb', line 61 def document_to_string(document) require "stringio" io = StringIO.new document.write(io: io) io.string end |
.fixture_path(relative_path) ⇒ String
Resolve a fixture path under spec/fixtures.
43 44 45 |
# File 'lib/pdfrb/test_utils.rb', line 43 def fixture_path(relative_path) File.(File.join("spec", "fixtures", relative_path)) end |
.load_fixture(relative_path) ⇒ String
Load a fixture file from a relative path under spec/fixtures.
33 34 35 36 37 38 |
# File 'lib/pdfrb/test_utils.rb', line 33 def load_fixture(relative_path) path = fixture_path(relative_path) raise "fixture not found: #{path}" unless File.exist?(path) File.binread(path) end |
.minimal_document(size: [612, 792]) ⇒ Pdfrb::Document
Build a minimal valid PDF Document with one blank page.
13 14 15 16 17 18 |
# File 'lib/pdfrb/test_utils.rb', line 13 def minimal_document(size: [612, 792]) doc = Pdfrb::Document.new page = doc.pages.add page.value[:MediaBox] = [0, 0, size[0], size[1]] doc end |
.minimal_pdf(size: [612, 792]) ⇒ String
Build a minimal PDF byte string with one blank page.
23 24 25 26 27 28 |
# File 'lib/pdfrb/test_utils.rb', line 23 def minimal_pdf(size: [612, 792]) require "stringio" io = StringIO.new minimal_document(size: size).write(io: io) io.string end |
.pdf_with_text(text, font: :Helvetica, size: 12) ⇒ String
Generate a one-page PDF with a single text string.
88 89 90 91 92 93 94 95 |
# File 'lib/pdfrb/test_utils.rb', line 88 def pdf_with_text(text, font: :Helvetica, size: 12) require "stringio" doc = Pdfrb::Document.new doc.pages.add.canvas.text(text, at: [50, 750], font: font, size: size) io = StringIO.new doc.write(io: io) io.string end |
.roundtrip(document) ⇒ Pdfrb::Document
Round-trip a Document through serialize → parse and return the re-parsed Document. Useful for verifying write correctness.
51 52 53 54 55 56 |
# File 'lib/pdfrb/test_utils.rb', line 51 def roundtrip(document) require "stringio" io = StringIO.new document.write(io: io) Pdfrb::Document.new(io: StringIO.new(io.string)) end |
.same_page_count?(left, right) ⇒ Boolean
Assert that two PDF byte strings have the same page count.
72 73 74 |
# File 'lib/pdfrb/test_utils.rb', line 72 def same_page_count?(left, right) count_pages(left) == count_pages(right) end |