Skip to content

Commit

Permalink
Simplify prawn test checking now that checked_describe can take multi…
Browse files Browse the repository at this point in the history
…ple arguments
  • Loading branch information
jeremyevans committed May 22, 2024
1 parent e6f8b7f commit adc1ba3
Showing 1 changed file with 46 additions and 53 deletions.
99 changes: 46 additions & 53 deletions test/tilt_prawntemplate_test.rb
Original file line number Diff line number Diff line change
@@ -1,66 +1,59 @@
require_relative 'test_helper'

begin
require 'tilt/prawn'
require 'pdf-reader'
rescue LoadError => e
warn "skipping tests of tilt/prawn: #{e.class}: #{e.message}"
else
describe 'tilt/prawn' do
_PdfOutput = Class.new do
def initialize(pdf_raw)
@reader = PDF::Reader.new(StringIO.new(pdf_raw))
end

def text
@reader.pages.map(&:text).join
end

def page_attributes(page_num=1)
@reader.page(page_num).attributes
end
end

it "is registered for '.prawn' files" do
assert_equal Tilt::PrawnTemplate, Tilt['test.prawn']
end

it "renders inline prawn templates" do
template = Tilt::PrawnTemplate.new { |t| "pdf.text \"Hello PDF!\"" }
3.times do
output = _PdfOutput.new(template.render)
assert_includes output.text, "Hello PDF!"
end
checked_describe('tilt/prawn', 'pdf-reader') do
_PdfOutput = Class.new do
def initialize(pdf_raw)
@reader = PDF::Reader.new(StringIO.new(pdf_raw))
end

it "supports yielding to render" do
template = Tilt::PrawnTemplate.new { |t| "yield pdf" }
3.times do
output = _PdfOutput.new(template.render{|pdf| pdf.text 'Hello PDF!'})
assert_includes output.text, "Hello PDF!"
end
def text
@reader.pages.map(&:text).join
end

it "renders templates from a file" do
template = Tilt::PrawnTemplate.new("test/tilt_prawntemplate.prawn")
3.times do
output = _PdfOutput.new(template.render)
assert_includes output.text, "Hello Template!"
end
def page_attributes(page_num=1)
@reader.page(page_num).attributes
end

it "defaults to A4 page size & portrait layout settings" do
template = Tilt::PrawnTemplate.new { |t| "pdf.text \"Hello A4 portrait!\"" }
end

it "is registered for '.prawn' files" do
assert_equal Tilt::PrawnTemplate, Tilt['test.prawn']
end

it "renders inline prawn templates" do
template = Tilt::PrawnTemplate.new { |t| "pdf.text \"Hello PDF!\"" }
3.times do
output = _PdfOutput.new(template.render)
assert_includes output.text, "Hello A4 portrait!"
assert_equal [0, 0, 595.28, 841.89], output.page_attributes(1)[:MediaBox]
assert_includes output.text, "Hello PDF!"
end

it "allows page size & layout settings - A3 landscape" do
template = Tilt::PrawnTemplate.new( :page_size => 'A3', :page_layout => :landscape) { |t| "pdf.text \"Hello A3 landscape!\"" }
end

it "supports yielding to render" do
template = Tilt::PrawnTemplate.new { |t| "yield pdf" }
3.times do
output = _PdfOutput.new(template.render{|pdf| pdf.text 'Hello PDF!'})
assert_includes output.text, "Hello PDF!"
end
end

it "renders templates from a file" do
template = Tilt::PrawnTemplate.new("test/tilt_prawntemplate.prawn")
3.times do
output = _PdfOutput.new(template.render)
assert_includes output.text, "Hello A3 landscape!"
assert_equal [0, 0, 1190.55, 841.89], output.page_attributes(1)[:MediaBox]
assert_includes output.text, "Hello Template!"
end
end

it "defaults to A4 page size & portrait layout settings" do
template = Tilt::PrawnTemplate.new { |t| "pdf.text \"Hello A4 portrait!\"" }
output = _PdfOutput.new(template.render)
assert_includes output.text, "Hello A4 portrait!"
assert_equal [0, 0, 595.28, 841.89], output.page_attributes(1)[:MediaBox]
end

it "allows page size & layout settings - A3 landscape" do
template = Tilt::PrawnTemplate.new( :page_size => 'A3', :page_layout => :landscape) { |t| "pdf.text \"Hello A3 landscape!\"" }
output = _PdfOutput.new(template.render)
assert_includes output.text, "Hello A3 landscape!"
assert_equal [0, 0, 1190.55, 841.89], output.page_attributes(1)[:MediaBox]
end
end

0 comments on commit adc1ba3

Please sign in to comment.