Created
October 25, 2013 12:14
-
-
Save phoet/7153728 to your computer and use it in GitHub Desktop.
RSpec PDF content matcher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module PDF | |
class Loader | |
def self.content_for(body) | |
temp_pdf = Tempfile.new('pdf') | |
temp_pdf << body.force_encoding('UTF-8') | |
temp_pdf.close | |
receiver = PDF::SimplePageTextReceiver.new | |
PDF::Reader.file(temp_pdf.path, receiver) | |
receiver.content | |
end | |
end | |
class SimplePageTextReceiver | |
attr_accessor :content | |
def initialize | |
@content = [] | |
end | |
# Called when page parsing starts | |
def begin_page(arg = nil) | |
@content << "" | |
end | |
# record text that is drawn on the page | |
def show_text(string, *params) | |
@content.last << string.strip | |
end | |
# there's a few text callbacks, so make sure we process them all | |
alias :super_show_text :show_text | |
alias :move_to_next_line_and_show_text :show_text | |
alias :set_spacing_next_line_show_text :show_text | |
# this final text callback takes slightly different arguments | |
def show_text_with_positioning(*params) | |
params = params.first | |
params.each { |str| show_text(str) if str.kind_of?(String)} | |
end | |
end | |
end | |
RSpec::Matchers.define :have_pdf_content do |text| | |
match do |body| | |
PDF::Loader.content_for(body).join == text.gsub(/ /, '') | |
end | |
failure_message_for_should do |body| | |
"pdf should have content:\n'#{text}'" | |
end | |
failure_message_for_should_not do |body| | |
"pdf should not have content:\n'#{text}'" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment