From adc1ba34fff8a866201eb9b1ca6c6406017cfae9 Mon Sep 17 00:00:00 2001 From: Jeremy Evans Date: Wed, 22 May 2024 12:42:04 -0700 Subject: [PATCH] Simplify prawn test checking now that checked_describe can take multiple arguments --- test/tilt_prawntemplate_test.rb | 99 +++++++++++++++------------------ 1 file changed, 46 insertions(+), 53 deletions(-) diff --git a/test/tilt_prawntemplate_test.rb b/test/tilt_prawntemplate_test.rb index 220cb4c..abd0840 100644 --- a/test/tilt_prawntemplate_test.rb +++ b/test/tilt_prawntemplate_test.rb @@ -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