-
Notifications
You must be signed in to change notification settings - Fork 101
/
raw_template_handler_test.rb
163 lines (138 loc) · 5.34 KB
/
raw_template_handler_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# 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::TemplateHandler::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