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

Class Method Details

.capture_log { ... } ⇒ String

Capture Pdfrb logger output during a block. Returns the captured string. Useful for asserting on warnings.

Yields:

  • block to execute.

Returns:

  • (String)

    captured log output.



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.

Parameters:

  • pdf_bytes (String)

Returns:

  • (Integer)


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.

Parameters:

Returns:

  • (String)

    binary PDF bytes.



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.

Parameters:

  • relative_path (String)

    e.g., "pdfs/sample.pdf".

Returns:

  • (String)

    absolute path.



43
44
45
# File 'lib/pdfrb/test_utils.rb', line 43

def fixture_path(relative_path)
  File.expand_path(File.join("spec", "fixtures", relative_path))
end

.load_fixture(relative_path) ⇒ String

Load a fixture file from a relative path under spec/fixtures.

Parameters:

  • relative_path (String)

    e.g., "images/test.png".

Returns:

  • (String)

    binary contents.



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.

Parameters:

  • size (Array<Float, Float>) (defaults to: [612, 792])

    page dimensions in points.

Returns:



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.

Parameters:

  • size (Array<Float, Float>) (defaults to: [612, 792])

    page dimensions in points.

Returns:

  • (String)

    PDF bytes (binary encoded).



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.

Parameters:

  • text (String)

    the text to render.

  • font (Symbol) (defaults to: :Helvetica)

    font resource name.

  • size (Integer) (defaults to: 12)

    point size.

Returns:

  • (String)

    binary PDF bytes.



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.

Parameters:

Returns:



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.

Parameters:

  • left (String)

    first PDF bytes.

  • right (String)

    second PDF bytes.

Returns:

  • (Boolean)


72
73
74
# File 'lib/pdfrb/test_utils.rb', line 72

def same_page_count?(left, right)
  count_pages(left) == count_pages(right)
end