Skip to content

Commit

Permalink
Add better specs
Browse files Browse the repository at this point in the history
  • Loading branch information
bradgessler committed Feb 28, 2024
1 parent 36625a7 commit 0cc6ed2
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 4 deletions.
13 changes: 13 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,20 @@ PATH
GEM
remote: https://rubygems.org/
specs:
Ascii85 (1.1.0)
afm (0.2.2)
diff-lcs (1.5.1)
hashery (2.1.2)
matrix (0.4.2)
pdf-core (0.9.0)
pdf-inspector (1.3.0)
pdf-reader (>= 1.0, < 3.0.a)
pdf-reader (2.12.0)
Ascii85 (~> 1.0)
afm (~> 0.2.1)
hashery (~> 2.0)
ruby-rc4
ttfunk
prawn (2.4.0)
pdf-core (~> 0.9.0)
ttfunk (~> 1.7)
Expand All @@ -28,13 +39,15 @@ GEM
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-support (3.13.0)
ruby-rc4 (0.1.5)
ttfunk (1.7.0)

PLATFORMS
arm64-darwin-23
ruby

DEPENDENCIES
pdf-inspector (~> 1.0)
phlex-pdf!
rake (~> 13.0)
rspec (~> 3.0)
Expand Down
5 changes: 2 additions & 3 deletions lib/phlex/pdf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class PDF

def document = @document

def call(document = self.class.blank, &block)
def call(document, &block)
@document = document
around_template do
if block_given?
Expand Down Expand Up @@ -45,8 +45,7 @@ def after_template
end

def to_pdf(...)
call
@document.render(...)
self.class.blank.tap{ |doc| call doc }.render(...)
end

def yield_content(&block)
Expand Down
2 changes: 2 additions & 0 deletions phlex-pdf.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ Gem::Specification.new do |spec|
spec.add_dependency "prawn", "~> 2.0"
spec.add_dependency "matrix", "~> 0.4"

spec.add_development_dependency("pdf-inspector", "~> 1.0")

# For more information and examples about making a new gem, check out our
# guide at: https://bundler.io/guides/creating_gem.html
end
13 changes: 12 additions & 1 deletion spec/phlex/pdf_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,18 @@ def view_template
end

RSpec.describe Phlex::PDF do
let(:pages) do
PDF::Inspector::Page.analyze(PDFDocument.new.to_pdf).pages
end

it "generates a PDF" do
PDFDocument.new.to_pdf
expect(pages.size).to eq(3)
expect(pages[0][:strings]).to include("Hi", "There")
expect(pages[1][:strings]).to include("Friendly", "Pal")
expect(pages[2][:strings]).to include("Whats", "Up")

pages.each do |page|
expect(page[:strings]).to include("Header", "Rendered from Proc.new", "Rendered from proc", "Rendered from lambda", "Rendered from ->", "Rendered from Method", "Rendered from lambda", "Rendered from ->", "Rendered from Method", "Hello Brad", "Danger!", "Don't Panic!")
end
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require "phlex/pdf"
require "pdf/inspector"

RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
Expand Down

0 comments on commit 0cc6ed2

Please sign in to comment.