foodsoft/vendor/plugins/prawnto/test/raw_template_handler_test.rb

164 lines
5.3 KiB
Ruby

# uncomment and edit below if you want to get off gem version
#$LOAD_PATH.unshift '~/cracklabs/vendor/gems/prawn-0.0.0.1/lib/' #to force picup of latest prawn (instead of stable gem)
require 'rubygems'
require 'action_controller'
require 'action_view'
require 'test/unit'
require File.dirname(__FILE__) + '/../lib/prawnto'
require File.dirname(__FILE__) + '/template_handler_test_mocks'
class RawTemplateHandlerTest < Test::Unit::TestCase
include TemplateHandlerTestMocks
class ::ApplicationHelper
end
def setup
@view = ActionView.new
@handler = Prawnto::TemplateHandlers::Raw.new(@view)
end
def test_massage_template_source_header_comments
expected_commented_lines = [0,2,3]
source = <<EOS
require 'prawn'
require 'hello'
require "rubygems"
$LOAD_PATH.unshift blah blah
LOAD_PATH.unshift blah blah
EOS
output_lines = @handler.send(:massage_template_source, Template.new(source)).split("\n")
output_lines.each_with_index do |line, i|
method = expected_commented_lines.include?(i) ? :assert_match : :assert_no_match
self.send method, /^\s*\#/, line
end
end
def test_massage_template_source_generate
@handler.pull_prawnto_options
changed_lines = [0,2,3]
source = <<EOS
Prawn::Document.generate('hello.pdf') do |pdf|
end
EOS
output_lines = @handler.send(:massage_template_source, Template.new(source)).split("\n")
assert_match(/^\s*(\S+)\s*\=\s*Prawn\:\:Document\.new\(?\s*\)?\s*do\s*\|pdf\|/, output_lines.first)
variable = $1
assert_match(/^\s*\[(\S+)\.render\s*\,\s*\'hello\.pdf\'\s*\]\s*$/, output_lines.last)
assert_equal variable, $1
end
def test_massage_template_source_new
@handler.pull_prawnto_options
unchanged_lines = [0,1,2]
source = <<EOS
x = Prawn::Document.new do |pdf|
text.blah blah blah
end
x.render_file('hello.pdf')
EOS
source_lines = source.split("\n")
output_lines = @handler.send(:massage_template_source, Template.new(source)).split("\n")
output_lines.each_with_index do |line, i|
method = unchanged_lines.include?(i) ? :assert_equal : :assert_not_equal
self.send method, source_lines[i], line
end
assert_match(/^\s*\#\s*x\.render\_file\(\'hello.pdf\'\)/, output_lines[3])
assert_match(/^\s*\[\s*x\.render\s*\,\s*\'hello\.pdf\'\s*\]\s*$/, output_lines.last)
end
def test_massage_template_source_classes_methods
source = <<EOS
class Foo
def initialize
@foo = true
end
end
def bar(*args)
if args[0]==true
z = false
end
end
EOS
@handler.send :setup_run_environment
output_lines = @handler.send(:massage_template_source, Template.new(source)).split("\n")
output_lines.pop
output_lines.each {|l| assert_match(/^\s*$/, l)}
assert @handler.run_environment.methods(false).include?('bar')
assert class <<@handler.run_environment; self; end.constants.include?('Foo')
end
CURRENT_PATH = Pathname('.').realpath
PRAWN_PATH = Pathname(Prawn::BASEDIR).realpath
REFERENCE_PATH = Pathname('reference_pdfs').realpath
INPUT_PATH = PRAWN_PATH + 'examples'
IGNORE_LIST = %w(table_bench ruport_formatter page_geometry)
INPUTS = INPUT_PATH.children.select {|p| p.extname==".rb" && !IGNORE_LIST.include?(p.basename('.rb').to_s)}
def self.ensure_reference_pdfs_are_recent
head_lines = (INPUT_PATH + "../.git/HEAD").read.split("\n")
head_hash = Hash[*head_lines.map {|line| line.split(':').map{|v| v.strip}}.flatten]
head_version = (INPUT_PATH + "../.git" + head_hash['ref'])
REFERENCE_PATH.mkpath
current_version = REFERENCE_PATH + 'HEAD'
if !current_version.exist? || current_version.read!=head_version.read
puts "\n!!!! reference pdfs are determined to be old-- repopulating...\n\n"
require 'fileutils'
FileUtils.instance_eval do
rm REFERENCE_PATH + '*', :force=>true
INPUTS.each do |path|
pre_brood = INPUT_PATH.children
cd INPUT_PATH
system("ruby #{path.basename}")
post_brood = INPUT_PATH.children
new_kids = post_brood - pre_brood
new_kids.each {|p| mv p, REFERENCE_PATH + p.basename}
cd CURRENT_PATH
end
cp head_version, current_version
end
else
puts "\n reference pdfs are current-- continuing...\n"
end
end
#TODO: ruby 1.9: uncomment below line when on 1.9
#ensure_reference_pdfs_are_recent
def assert_renders_correctly(name, path)
input_source = path.read
output_source = @handler.compile(Template.new(input_source))
value = @view.instance_eval output_source
reference = (REFERENCE_PATH + @view.prawnto_options[:filename]).read
message = "template: #{name}\n"
message += ">"*30 + " original template: " + ">"*20 + "\n"
message += input_source + "\n"*2
message += ">"*30 + " manipulated template: " + ">"*20 + "\n"
message += output_source + "\n" + "<"*60 + "\n"
assert_equal reference, value, message
end
#!!! Can't actually verify pdf equality until ruby 1.9
# (cuz hash orders are messed up otherwise and no other way to test equality at the moment)
INPUTS.each do |path|
name = path.basename('.rb')
define_method "test_template_should_render_correctly [template: #{name}] " do
# assert_renders_correctly name, path
assert true
end
end
end