tilt 0.6 → 0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/COPYING +1 -1
- data/README.md +51 -3
- data/Rakefile +31 -48
- data/TEMPLATES.md +26 -4
- data/bin/tilt +45 -16
- data/lib/tilt.rb +348 -139
- data/test/tilt_buildertemplate_test.rb +1 -1
- data/test/tilt_compilesite_test.rb +86 -0
- data/test/tilt_erbtemplate_test.rb +104 -2
- data/test/tilt_erubistemplate_test.rb +35 -1
- data/test/tilt_hamltemplate_test.rb +65 -1
- data/test/tilt_liquidtemplate_test.rb +1 -1
- data/test/tilt_mustachetemplate_test.rb +1 -4
- data/test/tilt_rdiscounttemplate_test.rb +3 -3
- data/test/tilt_rdoctemplate_test.rb +1 -1
- data/test/tilt_stringtemplate_test.rb +87 -1
- data/test/tilt_template_test.rb +29 -24
- data/test/tilt_test.rb +9 -1
- data/tilt.gemspec +6 -4
- metadata +38 -7
|
@@ -9,7 +9,7 @@ begin
|
|
|
9
9
|
assert_equal Tilt::BuilderTemplate, Tilt['test.xml.builder']
|
|
10
10
|
end
|
|
11
11
|
|
|
12
|
-
test "
|
|
12
|
+
test "preparing and evaluating the template on #render" do
|
|
13
13
|
template = Tilt::BuilderTemplate.new { |t| "xml.em 'Hello World!'" }
|
|
14
14
|
assert_equal "<em>Hello World!</em>\n", template.render
|
|
15
15
|
end
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
require 'contest'
|
|
2
|
+
require 'tilt'
|
|
3
|
+
require 'thread'
|
|
4
|
+
|
|
5
|
+
class CompileSiteTest < Test::Unit::TestCase
|
|
6
|
+
def setup
|
|
7
|
+
GC.start
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
class CompilingTemplate < Tilt::Template
|
|
11
|
+
def prepare
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def precompiled_template(locals)
|
|
15
|
+
@data.inspect
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
class Scope
|
|
20
|
+
include Tilt::CompileSite
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
test "compiling template source to a method" do
|
|
24
|
+
template = CompilingTemplate.new { |t| "Hello World!" }
|
|
25
|
+
template.render(Scope.new)
|
|
26
|
+
method_name = template.send(:compiled_method_name, [])
|
|
27
|
+
method_name = method_name.to_sym if Symbol === Kernel.methods.first
|
|
28
|
+
assert Tilt::CompileSite.instance_methods.include?(method_name),
|
|
29
|
+
"CompileSite.instance_methods.include?(#{method_name.inspect})"
|
|
30
|
+
assert Scope.new.respond_to?(method_name),
|
|
31
|
+
"scope.respond_to?(#{method_name.inspect})"
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
test 'garbage collecting compiled methods' do
|
|
35
|
+
template = CompilingTemplate.new { '' }
|
|
36
|
+
method_name = template.send(:compiled_method_name, [])
|
|
37
|
+
template.render(Scope.new)
|
|
38
|
+
assert Scope.new.respond_to?(method_name)
|
|
39
|
+
Tilt::Template.send(
|
|
40
|
+
:garbage_collect_compiled_template_method,
|
|
41
|
+
Tilt::CompileSite,
|
|
42
|
+
method_name
|
|
43
|
+
)
|
|
44
|
+
assert !Scope.new.respond_to?(method_name), "compiled method not removed"
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def self.create_and_destroy_template
|
|
48
|
+
template = CompilingTemplate.new { 'Hello World' }
|
|
49
|
+
template.render(Scope.new)
|
|
50
|
+
method_name = template.send(:compiled_method_name, [])
|
|
51
|
+
method_name = method_name.to_sym if Symbol === Kernel.methods.first
|
|
52
|
+
[template.object_id, method_name]
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
finalized_object_id, finalized_method_name = create_and_destroy_template
|
|
56
|
+
|
|
57
|
+
test "triggering compiled method gc finalizer" do
|
|
58
|
+
assert !Tilt::CompileSite.instance_methods.include?(finalized_method_name),
|
|
59
|
+
"CompileSite.instance_methods.include?(#{finalized_method_name.inspect})"
|
|
60
|
+
assert !Scope.new.respond_to?(finalized_method_name),
|
|
61
|
+
"Scope.new.respond_to?(#{finalized_method_name.inspect})"
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# This test attempts to surface issues with compiling templates from
|
|
65
|
+
# multiple threads.
|
|
66
|
+
test "using compiled templates from multiple threads" do
|
|
67
|
+
template = CompilingTemplate.new { 'template' }
|
|
68
|
+
main_thread = Thread.current
|
|
69
|
+
10.times do |i|
|
|
70
|
+
threads =
|
|
71
|
+
(1..50).map do |j|
|
|
72
|
+
Thread.new {
|
|
73
|
+
begin
|
|
74
|
+
locals = { "local#{i}" => 'value' }
|
|
75
|
+
res = template.render(self, locals)
|
|
76
|
+
thread_id = Thread.current.object_id
|
|
77
|
+
res = template.render(self, "local#{thread_id.to_s}" => 'value')
|
|
78
|
+
rescue => boom
|
|
79
|
+
main_thread.raise(boom)
|
|
80
|
+
end
|
|
81
|
+
}
|
|
82
|
+
end
|
|
83
|
+
threads.each { |t| t.join }
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
end
|
|
@@ -12,7 +12,7 @@ class ERBTemplateTest < Test::Unit::TestCase
|
|
|
12
12
|
assert_equal Tilt::ERBTemplate, Tilt['test.rhtml']
|
|
13
13
|
end
|
|
14
14
|
|
|
15
|
-
test "
|
|
15
|
+
test "loading and evaluating templates on #render" do
|
|
16
16
|
template = Tilt::ERBTemplate.new { |t| "Hello World!" }
|
|
17
17
|
assert_equal "Hello World!", template.render
|
|
18
18
|
end
|
|
@@ -77,9 +77,111 @@ class ERBTemplateTest < Test::Unit::TestCase
|
|
|
77
77
|
end
|
|
78
78
|
|
|
79
79
|
test "shorthand whole line syntax trim mode" do
|
|
80
|
-
template = Tilt.new('test.erb',
|
|
80
|
+
template = Tilt.new('test.erb', :trim => '%') { "\n% if true\nhello\n%end\n" }
|
|
81
81
|
assert_equal "\nhello\n", template.render
|
|
82
82
|
end
|
|
83
|
+
|
|
84
|
+
test "using an instance variable as the outvar" do
|
|
85
|
+
template = Tilt::ERBTemplate.new(nil, :outvar => '@buf') { "<%= 1 + 1 %>" }
|
|
86
|
+
scope = Object.new
|
|
87
|
+
scope.instance_variable_set(:@buf, 'original value')
|
|
88
|
+
assert_equal '2', template.render(scope)
|
|
89
|
+
assert_equal 'original value', scope.instance_variable_get(:@buf)
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
class CompiledERBTemplateTest < Test::Unit::TestCase
|
|
94
|
+
def teardown
|
|
95
|
+
GC.start
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
class Scope
|
|
99
|
+
include Tilt::CompileSite
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
test "compiling template source to a method" do
|
|
103
|
+
template = Tilt::ERBTemplate.new { |t| "Hello World!" }
|
|
104
|
+
template.render(Scope.new)
|
|
105
|
+
method_name = template.send(:compiled_method_name, [])
|
|
106
|
+
method_name = method_name.to_sym if Symbol === Kernel.methods.first
|
|
107
|
+
assert Tilt::CompileSite.instance_methods.include?(method_name),
|
|
108
|
+
"CompileSite.instance_methods.include?(#{method_name.inspect})"
|
|
109
|
+
assert Scope.new.respond_to?(method_name),
|
|
110
|
+
"scope.respond_to?(#{method_name.inspect})"
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
test "loading and evaluating templates on #render" do
|
|
114
|
+
template = Tilt::ERBTemplate.new { |t| "Hello World!" }
|
|
115
|
+
assert_equal "Hello World!", template.render(Scope.new)
|
|
116
|
+
assert_equal "Hello World!", template.render(Scope.new)
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
test "passing locals" do
|
|
120
|
+
template = Tilt::ERBTemplate.new { 'Hey <%= name %>!' }
|
|
121
|
+
assert_equal "Hey Joe!", template.render(Scope.new, :name => 'Joe')
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
test "evaluating in an object scope" do
|
|
125
|
+
template = Tilt::ERBTemplate.new { 'Hey <%= @name %>!' }
|
|
126
|
+
scope = Scope.new
|
|
127
|
+
scope.instance_variable_set :@name, 'Joe'
|
|
128
|
+
assert_equal "Hey Joe!", template.render(scope)
|
|
129
|
+
scope.instance_variable_set :@name, 'Jane'
|
|
130
|
+
assert_equal "Hey Jane!", template.render(scope)
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
test "passing a block for yield" do
|
|
134
|
+
template = Tilt::ERBTemplate.new { 'Hey <%= yield %>!' }
|
|
135
|
+
assert_equal "Hey Joe!", template.render(Scope.new) { 'Joe' }
|
|
136
|
+
assert_equal "Hey Jane!", template.render(Scope.new) { 'Jane' }
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
test "backtrace file and line reporting without locals" do
|
|
140
|
+
data = File.read(__FILE__).split("\n__END__\n").last
|
|
141
|
+
fail unless data[0] == ?<
|
|
142
|
+
template = Tilt::ERBTemplate.new('test.erb', 11) { data }
|
|
143
|
+
begin
|
|
144
|
+
template.render(Scope.new)
|
|
145
|
+
fail 'should have raised an exception'
|
|
146
|
+
rescue => boom
|
|
147
|
+
assert_kind_of NameError, boom
|
|
148
|
+
line = boom.backtrace.first
|
|
149
|
+
file, line, meth = line.split(":")
|
|
150
|
+
assert_equal 'test.erb', file
|
|
151
|
+
assert_equal '13', line
|
|
152
|
+
end
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
test "backtrace file and line reporting with locals" do
|
|
156
|
+
data = File.read(__FILE__).split("\n__END__\n").last
|
|
157
|
+
fail unless data[0] == ?<
|
|
158
|
+
template = Tilt::ERBTemplate.new('test.erb') { data }
|
|
159
|
+
begin
|
|
160
|
+
template.render(Scope.new, :name => 'Joe', :foo => 'bar')
|
|
161
|
+
fail 'should have raised an exception'
|
|
162
|
+
rescue => boom
|
|
163
|
+
assert_kind_of RuntimeError, boom
|
|
164
|
+
line = boom.backtrace.first
|
|
165
|
+
file, line, meth = line.split(":")
|
|
166
|
+
assert_equal 'test.erb', file
|
|
167
|
+
assert_equal '6', line
|
|
168
|
+
end
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
test "default non-stripping trim mode" do
|
|
172
|
+
template = Tilt.new('test.erb') { "\n<%= 1 + 1 %>\n" }
|
|
173
|
+
assert_equal "\n2\n", template.render(Scope.new)
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
test "stripping trim mode" do
|
|
177
|
+
template = Tilt.new('test.erb', :trim => '-') { "\n<%= 1 + 1 -%>\n" }
|
|
178
|
+
assert_equal "\n2", template.render(Scope.new)
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
test "shorthand whole line syntax trim mode" do
|
|
182
|
+
template = Tilt.new('test.erb', :trim => '%') { "\n% if true\nhello\n%end\n" }
|
|
183
|
+
assert_equal "\nhello\n", template.render(Scope.new)
|
|
184
|
+
end
|
|
83
185
|
end
|
|
84
186
|
|
|
85
187
|
__END__
|
|
@@ -9,7 +9,7 @@ begin
|
|
|
9
9
|
assert_equal Tilt::ErubisTemplate, Tilt['test.html.erubis']
|
|
10
10
|
end
|
|
11
11
|
|
|
12
|
-
test "
|
|
12
|
+
test "preparing and evaluating templates on #render" do
|
|
13
13
|
template = Tilt::ErubisTemplate.new { |t| "Hello World!" }
|
|
14
14
|
assert_equal "Hello World!", template.render
|
|
15
15
|
end
|
|
@@ -69,6 +69,40 @@ begin
|
|
|
69
69
|
scope.instance_variable_set :@name, 'Joe'
|
|
70
70
|
assert_equal "Hey Joe!", template.render(scope)
|
|
71
71
|
end
|
|
72
|
+
|
|
73
|
+
test "using an instance variable as the outvar" do
|
|
74
|
+
template = Tilt::ErubisTemplate.new(nil, :outvar => '@buf') { "<%= 1 + 1 %>" }
|
|
75
|
+
scope = Object.new
|
|
76
|
+
scope.instance_variable_set(:@buf, 'original value')
|
|
77
|
+
assert_equal '2', template.render(scope)
|
|
78
|
+
assert_equal 'original value', scope.instance_variable_get(:@buf)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
test "using Erubis::EscapedEruby subclass via :engine_class option" do
|
|
82
|
+
template = Tilt::ErubisTemplate.new(nil, :engine_class => ::Erubis::EscapedEruby) { |t| %(<%= "<p>Hello World!</p>" %>) }
|
|
83
|
+
assert_equal "<p>Hello World!</p>", template.render
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
test "using :escape_html => true option" do
|
|
87
|
+
template = Tilt::ErubisTemplate.new(nil, :escape_html => true) { |t| %(<%= "<p>Hello World!</p>" %>) }
|
|
88
|
+
assert_equal "<p>Hello World!</p>", template.render
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
test "using :escape_html => false option" do
|
|
92
|
+
template = Tilt::ErubisTemplate.new(nil, :escape_html => false) { |t| %(<%= "<p>Hello World!</p>" %>) }
|
|
93
|
+
assert_equal "<p>Hello World!</p>", template.render
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
test "erubis default does not escape html" do
|
|
97
|
+
template = Tilt::ErubisTemplate.new { |t| %(<%= "<p>Hello World!</p>" %>) }
|
|
98
|
+
assert_equal "<p>Hello World!</p>", template.render
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
test "does not modify options argument" do
|
|
102
|
+
options_hash = {:escape_html => true}
|
|
103
|
+
template = Tilt::ErubisTemplate.new(nil, options_hash) { |t| "Hello World!" }
|
|
104
|
+
assert_equal({:escape_html => true}, options_hash)
|
|
105
|
+
end
|
|
72
106
|
end
|
|
73
107
|
rescue LoadError => boom
|
|
74
108
|
warn "Tilt::ErubisTemplate (disabled)\n"
|
|
@@ -12,7 +12,7 @@ begin
|
|
|
12
12
|
assert_equal Tilt::HamlTemplate, Tilt['test.haml']
|
|
13
13
|
end
|
|
14
14
|
|
|
15
|
-
test "
|
|
15
|
+
test "preparing and evaluating templates on #render" do
|
|
16
16
|
template = Tilt::HamlTemplate.new { |t| "%p Hello World!" }
|
|
17
17
|
assert_equal "<p>Hello World!</p>\n", template.render
|
|
18
18
|
end
|
|
@@ -66,6 +66,70 @@ begin
|
|
|
66
66
|
end
|
|
67
67
|
end
|
|
68
68
|
|
|
69
|
+
class CompiledHamlTemplateTest < Test::Unit::TestCase
|
|
70
|
+
class Scope
|
|
71
|
+
include Tilt::CompileSite
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
test "compiling template source to a method" do
|
|
75
|
+
template = Tilt::HamlTemplate.new { |t| "Hello World!" }
|
|
76
|
+
template.render(Scope.new)
|
|
77
|
+
method_name = template.send(:compiled_method_name, [])
|
|
78
|
+
method_name = method_name.to_sym if Symbol === Kernel.methods.first
|
|
79
|
+
assert Tilt::CompileSite.instance_methods.include?(method_name),
|
|
80
|
+
"CompileSite.instance_methods.include?(#{method_name.inspect})"
|
|
81
|
+
assert Scope.new.respond_to?(method_name),
|
|
82
|
+
"scope.respond_to?(#{method_name.inspect})"
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
test "passing locals" do
|
|
86
|
+
template = Tilt::HamlTemplate.new { "%p= 'Hey ' + name + '!'" }
|
|
87
|
+
assert_equal "<p>Hey Joe!</p>\n", template.render(Scope.new, :name => 'Joe')
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
test "evaluating in an object scope" do
|
|
91
|
+
template = Tilt::HamlTemplate.new { "%p= 'Hey ' + @name + '!'" }
|
|
92
|
+
scope = Scope.new
|
|
93
|
+
scope.instance_variable_set :@name, 'Joe'
|
|
94
|
+
assert_equal "<p>Hey Joe!</p>\n", template.render(scope)
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
test "passing a block for yield" do
|
|
98
|
+
template = Tilt::HamlTemplate.new { "%p= 'Hey ' + yield + '!'" }
|
|
99
|
+
assert_equal "<p>Hey Joe!</p>\n", template.render(Scope.new) { 'Joe' }
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
test "backtrace file and line reporting without locals" do
|
|
103
|
+
data = File.read(__FILE__).split("\n__END__\n").last
|
|
104
|
+
fail unless data[0] == ?%
|
|
105
|
+
template = Tilt::HamlTemplate.new('test.haml', 10) { data }
|
|
106
|
+
begin
|
|
107
|
+
template.render(Scope.new)
|
|
108
|
+
fail 'should have raised an exception'
|
|
109
|
+
rescue => boom
|
|
110
|
+
assert_kind_of NameError, boom
|
|
111
|
+
line = boom.backtrace.first
|
|
112
|
+
file, line, meth = line.split(":")
|
|
113
|
+
assert_equal 'test.haml', file
|
|
114
|
+
assert_equal '12', line
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
test "backtrace file and line reporting with locals" do
|
|
119
|
+
data = File.read(__FILE__).split("\n__END__\n").last
|
|
120
|
+
fail unless data[0] == ?%
|
|
121
|
+
template = Tilt::HamlTemplate.new('test.haml') { data }
|
|
122
|
+
begin
|
|
123
|
+
res = template.render(Scope.new, :name => 'Joe', :foo => 'bar')
|
|
124
|
+
rescue => boom
|
|
125
|
+
assert_kind_of MockError, boom
|
|
126
|
+
line = boom.backtrace.first
|
|
127
|
+
file, line, meth = line.split(":")
|
|
128
|
+
assert_equal 'test.haml', file
|
|
129
|
+
assert_equal '5', line
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
end
|
|
69
133
|
rescue LoadError => boom
|
|
70
134
|
warn "Tilt::HamlTemplate (disabled)\n"
|
|
71
135
|
end
|
|
@@ -9,7 +9,7 @@ begin
|
|
|
9
9
|
assert_equal Tilt::LiquidTemplate, Tilt['test.liquid']
|
|
10
10
|
end
|
|
11
11
|
|
|
12
|
-
test "
|
|
12
|
+
test "preparing and evaluating templates on #render" do
|
|
13
13
|
template = Tilt::LiquidTemplate.new { |t| "Hello World!" }
|
|
14
14
|
assert_equal "Hello World!", template.render
|
|
15
15
|
end
|
|
@@ -16,7 +16,7 @@ begin
|
|
|
16
16
|
assert_equal Tilt::MustacheTemplate, Tilt['test.mustache']
|
|
17
17
|
end
|
|
18
18
|
|
|
19
|
-
test "
|
|
19
|
+
test "preparing and evaluating templates on #render" do
|
|
20
20
|
template = Tilt::MustacheTemplate.new { |t| "Hello World!" }
|
|
21
21
|
assert_equal "Hello World!", template.render
|
|
22
22
|
end
|
|
@@ -33,7 +33,6 @@ begin
|
|
|
33
33
|
|
|
34
34
|
test "locating views defined at the top-level" do
|
|
35
35
|
template = Tilt::MustacheTemplate.new('foo.mustache') { "<p>Hey {{foo}}!</p>" }
|
|
36
|
-
template.compile
|
|
37
36
|
assert_equal Views::Foo, template.engine
|
|
38
37
|
end
|
|
39
38
|
|
|
@@ -46,7 +45,6 @@ begin
|
|
|
46
45
|
|
|
47
46
|
test "locating views defined in a custom namespace" do
|
|
48
47
|
template = Tilt::MustacheTemplate.new('bizzle.mustache', :namespace => Bar) { "<p>Hello World!</p>" }
|
|
49
|
-
template.compile
|
|
50
48
|
assert_equal Bar::Views::Bizzle, template.engine
|
|
51
49
|
assert_equal "<p>Hello World!</p>", template.render
|
|
52
50
|
end
|
|
@@ -54,7 +52,6 @@ begin
|
|
|
54
52
|
test "locating views in files" do
|
|
55
53
|
view_path = File.expand_path('../tilt_mustache_views', __FILE__)
|
|
56
54
|
template = Tilt::MustacheTemplate.new('external.mustache', :view_path => view_path) { "<p>{{hello}}!</p>" }
|
|
57
|
-
template.compile
|
|
58
55
|
assert defined?(Views::External), "external.rb should have been required"
|
|
59
56
|
assert_equal Views::External, template.engine
|
|
60
57
|
assert_equal "<p>Stached!</p>", template.render
|
|
@@ -17,20 +17,20 @@ begin
|
|
|
17
17
|
assert_equal Tilt::RDiscountTemplate, Tilt['test.mkd']
|
|
18
18
|
end
|
|
19
19
|
|
|
20
|
-
test "
|
|
20
|
+
test "preparing and evaluating templates on #render" do
|
|
21
21
|
template = Tilt::RDiscountTemplate.new { |t| "# Hello World!" }
|
|
22
22
|
assert_equal "<h1>Hello World!</h1>\n", template.render
|
|
23
23
|
end
|
|
24
24
|
|
|
25
25
|
test "smartypants when :smart is set" do
|
|
26
|
-
template = Tilt::RDiscountTemplate.new(
|
|
26
|
+
template = Tilt::RDiscountTemplate.new(:smart => true) { |t|
|
|
27
27
|
"OKAY -- 'Smarty Pants'" }
|
|
28
28
|
assert_equal "<p>OKAY — ‘Smarty Pants’</p>\n",
|
|
29
29
|
template.render
|
|
30
30
|
end
|
|
31
31
|
|
|
32
32
|
test "stripping HTML when :filter_html is set" do
|
|
33
|
-
template = Tilt::RDiscountTemplate.new(
|
|
33
|
+
template = Tilt::RDiscountTemplate.new(:filter_html => true) { |t|
|
|
34
34
|
"HELLO <blink>WORLD</blink>" }
|
|
35
35
|
assert_equal "<p>HELLO <blink>WORLD</blink></p>\n", template.render
|
|
36
36
|
end
|
|
@@ -9,7 +9,7 @@ begin
|
|
|
9
9
|
assert_equal Tilt::RDocTemplate, Tilt['test.rdoc']
|
|
10
10
|
end
|
|
11
11
|
|
|
12
|
-
test "
|
|
12
|
+
test "preparing and evaluating the template with #render" do
|
|
13
13
|
template = Tilt::RDocTemplate.new { |t| "= Hello World!" }
|
|
14
14
|
assert_equal "<h1>Hello World!</h1>\n", template.render
|
|
15
15
|
end
|
|
@@ -6,7 +6,7 @@ class StringTemplateTest < Test::Unit::TestCase
|
|
|
6
6
|
assert_equal Tilt::StringTemplate, Tilt['test.str']
|
|
7
7
|
end
|
|
8
8
|
|
|
9
|
-
test "
|
|
9
|
+
test "loading and evaluating templates on #render" do
|
|
10
10
|
template = Tilt::StringTemplate.new { |t| "Hello World!" }
|
|
11
11
|
assert_equal "Hello World!", template.render
|
|
12
12
|
end
|
|
@@ -26,6 +26,7 @@ class StringTemplateTest < Test::Unit::TestCase
|
|
|
26
26
|
test "passing a block for yield" do
|
|
27
27
|
template = Tilt::StringTemplate.new { 'Hey #{yield}!' }
|
|
28
28
|
assert_equal "Hey Joe!", template.render { 'Joe' }
|
|
29
|
+
assert_equal "Hey Moe!", template.render { 'Moe' }
|
|
29
30
|
end
|
|
30
31
|
|
|
31
32
|
test "multiline templates" do
|
|
@@ -66,6 +67,91 @@ class StringTemplateTest < Test::Unit::TestCase
|
|
|
66
67
|
end
|
|
67
68
|
end
|
|
68
69
|
|
|
70
|
+
|
|
71
|
+
class CompiledStringTemplateTest < Test::Unit::TestCase
|
|
72
|
+
def teardown
|
|
73
|
+
GC.start
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
class Scope
|
|
77
|
+
include Tilt::CompileSite
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
test "compiling template source to a method" do
|
|
81
|
+
template = Tilt::StringTemplate.new { |t| "Hello World!" }
|
|
82
|
+
template.render(Scope.new)
|
|
83
|
+
method_name = template.send(:compiled_method_name, [])
|
|
84
|
+
method_name = method_name.to_sym if Symbol === Kernel.methods.first
|
|
85
|
+
assert Tilt::CompileSite.instance_methods.include?(method_name),
|
|
86
|
+
"CompileSite.instance_methods.include?(#{method_name.inspect})"
|
|
87
|
+
assert Scope.new.respond_to?(method_name),
|
|
88
|
+
"scope.respond_to?(#{method_name.inspect})"
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
test "loading and evaluating templates on #render" do
|
|
92
|
+
template = Tilt::StringTemplate.new { |t| "Hello World!" }
|
|
93
|
+
assert_equal "Hello World!", template.render(Scope.new)
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
test "passing locals" do
|
|
97
|
+
template = Tilt::StringTemplate.new { 'Hey #{name}!' }
|
|
98
|
+
assert_equal "Hey Joe!", template.render(Scope.new, :name => 'Joe')
|
|
99
|
+
assert_equal "Hey Moe!", template.render(Scope.new, :name => 'Moe')
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
test "evaluating in an object scope" do
|
|
103
|
+
template = Tilt::StringTemplate.new { 'Hey #{@name}!' }
|
|
104
|
+
scope = Scope.new
|
|
105
|
+
scope.instance_variable_set :@name, 'Joe'
|
|
106
|
+
assert_equal "Hey Joe!", template.render(scope)
|
|
107
|
+
scope.instance_variable_set :@name, 'Moe'
|
|
108
|
+
assert_equal "Hey Moe!", template.render(scope)
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
test "passing a block for yield" do
|
|
112
|
+
template = Tilt::StringTemplate.new { 'Hey #{yield}!' }
|
|
113
|
+
assert_equal "Hey Joe!", template.render(Scope.new) { 'Joe' }
|
|
114
|
+
assert_equal "Hey Moe!", template.render(Scope.new) { 'Moe' }
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
test "multiline templates" do
|
|
118
|
+
template = Tilt::StringTemplate.new { "Hello\nWorld!\n" }
|
|
119
|
+
assert_equal "Hello\nWorld!\n", template.render(Scope.new)
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
test "backtrace file and line reporting without locals" do
|
|
123
|
+
data = File.read(__FILE__).split("\n__END__\n").last
|
|
124
|
+
fail unless data[0] == ?<
|
|
125
|
+
template = Tilt::StringTemplate.new('test.str', 11) { data }
|
|
126
|
+
begin
|
|
127
|
+
template.render(Scope.new)
|
|
128
|
+
fail 'should have raised an exception'
|
|
129
|
+
rescue => boom
|
|
130
|
+
assert_kind_of NameError, boom
|
|
131
|
+
line = boom.backtrace.first
|
|
132
|
+
file, line, meth = line.split(":")
|
|
133
|
+
assert_equal 'test.str', file
|
|
134
|
+
assert_equal '13', line
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
test "backtrace file and line reporting with locals" do
|
|
139
|
+
data = File.read(__FILE__).split("\n__END__\n").last
|
|
140
|
+
fail unless data[0] == ?<
|
|
141
|
+
template = Tilt::StringTemplate.new('test.str') { data }
|
|
142
|
+
begin
|
|
143
|
+
template.render(Scope.new, :name => 'Joe', :foo => 'bar')
|
|
144
|
+
fail 'should have raised an exception'
|
|
145
|
+
rescue => boom
|
|
146
|
+
assert_kind_of RuntimeError, boom
|
|
147
|
+
line = boom.backtrace.first
|
|
148
|
+
file, line, meth = line.split(":")
|
|
149
|
+
assert_equal 'test.str', file
|
|
150
|
+
assert_equal '6', line
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
end
|
|
154
|
+
|
|
69
155
|
__END__
|
|
70
156
|
<html>
|
|
71
157
|
<body>
|
data/test/tilt_template_test.rb
CHANGED
|
@@ -2,44 +2,50 @@ require 'contest'
|
|
|
2
2
|
require 'tilt'
|
|
3
3
|
|
|
4
4
|
class TiltTemplateTest < Test::Unit::TestCase
|
|
5
|
+
|
|
6
|
+
class MockTemplate < Tilt::Template
|
|
7
|
+
def prepare
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
|
|
5
11
|
test "needs a file or block" do
|
|
6
12
|
assert_raise(ArgumentError) { Tilt::Template.new }
|
|
7
13
|
end
|
|
8
14
|
|
|
9
15
|
test "initializing with a file" do
|
|
10
|
-
inst =
|
|
16
|
+
inst = MockTemplate.new('foo.erb') {}
|
|
11
17
|
assert_equal 'foo.erb', inst.file
|
|
12
18
|
end
|
|
13
19
|
|
|
14
20
|
test "initializing with a file and line" do
|
|
15
|
-
inst =
|
|
21
|
+
inst = MockTemplate.new('foo.erb', 55) {}
|
|
16
22
|
assert_equal 'foo.erb', inst.file
|
|
17
23
|
assert_equal 55, inst.line
|
|
18
24
|
end
|
|
19
25
|
|
|
20
26
|
test "uses correct eval_file" do
|
|
21
|
-
inst =
|
|
27
|
+
inst = MockTemplate.new('foo.erb', 55) {}
|
|
22
28
|
assert_equal 'foo.erb', inst.eval_file
|
|
23
29
|
end
|
|
24
30
|
|
|
25
31
|
test "uses a default filename for #eval_file when no file provided" do
|
|
26
|
-
inst =
|
|
32
|
+
inst = MockTemplate.new { 'Hi' }
|
|
27
33
|
assert_not_nil inst.eval_file
|
|
28
34
|
assert !inst.eval_file.include?("\n")
|
|
29
35
|
end
|
|
30
36
|
|
|
31
37
|
test "calculating template's #basename" do
|
|
32
|
-
inst =
|
|
38
|
+
inst = MockTemplate.new('/tmp/templates/foo.html.erb') {}
|
|
33
39
|
assert_equal 'foo.html.erb', inst.basename
|
|
34
40
|
end
|
|
35
41
|
|
|
36
42
|
test "calculating the template's #name" do
|
|
37
|
-
inst =
|
|
43
|
+
inst = MockTemplate.new('/tmp/templates/foo.html.erb') {}
|
|
38
44
|
assert_equal 'foo', inst.name
|
|
39
45
|
end
|
|
40
46
|
|
|
41
47
|
test "initializing with a data loading block" do
|
|
42
|
-
|
|
48
|
+
MockTemplate.new { |template| "Hello World!" }
|
|
43
49
|
end
|
|
44
50
|
|
|
45
51
|
class InitializingMockTemplate < Tilt::Template
|
|
@@ -52,7 +58,7 @@ class TiltTemplateTest < Test::Unit::TestCase
|
|
|
52
58
|
@@initialized_count += 1
|
|
53
59
|
end
|
|
54
60
|
|
|
55
|
-
def
|
|
61
|
+
def prepare
|
|
56
62
|
end
|
|
57
63
|
end
|
|
58
64
|
|
|
@@ -68,44 +74,43 @@ class TiltTemplateTest < Test::Unit::TestCase
|
|
|
68
74
|
assert_equal 1, InitializingMockTemplate.initialized_count
|
|
69
75
|
end
|
|
70
76
|
|
|
71
|
-
class
|
|
77
|
+
class PreparingMockTemplate < Tilt::Template
|
|
72
78
|
include Test::Unit::Assertions
|
|
73
|
-
def
|
|
79
|
+
def prepare
|
|
74
80
|
assert !data.nil?
|
|
75
|
-
@
|
|
81
|
+
@prepared = true
|
|
76
82
|
end
|
|
77
|
-
def
|
|
83
|
+
def prepared? ; @prepared ; end
|
|
78
84
|
end
|
|
79
85
|
|
|
80
|
-
test "raises NotImplementedError when #
|
|
81
|
-
|
|
82
|
-
assert_raise(NotImplementedError) { inst.render }
|
|
86
|
+
test "raises NotImplementedError when #prepare not defined" do
|
|
87
|
+
assert_raise(NotImplementedError) { Tilt::Template.new { |template| "Hello World!" } }
|
|
83
88
|
end
|
|
84
89
|
|
|
85
90
|
test "raises NotImplementedError when #evaluate or #template_source not defined" do
|
|
86
|
-
inst =
|
|
91
|
+
inst = PreparingMockTemplate.new { |t| "Hello World!" }
|
|
87
92
|
assert_raise(NotImplementedError) { inst.render }
|
|
88
|
-
assert inst.
|
|
93
|
+
assert inst.prepared?
|
|
89
94
|
end
|
|
90
95
|
|
|
91
|
-
class SimpleMockTemplate <
|
|
96
|
+
class SimpleMockTemplate < PreparingMockTemplate
|
|
92
97
|
include Test::Unit::Assertions
|
|
93
98
|
def evaluate(scope, locals, &block)
|
|
94
|
-
assert
|
|
99
|
+
assert prepared?
|
|
95
100
|
assert !scope.nil?
|
|
96
101
|
assert !locals.nil?
|
|
97
102
|
"<em>#{@data}</em>"
|
|
98
103
|
end
|
|
99
104
|
end
|
|
100
105
|
|
|
101
|
-
test "
|
|
106
|
+
test "prepares and evaluates the template on #render" do
|
|
102
107
|
inst = SimpleMockTemplate.new { |t| "Hello World!" }
|
|
103
108
|
assert_equal "<em>Hello World!</em>", inst.render
|
|
104
|
-
assert inst.
|
|
109
|
+
assert inst.prepared?
|
|
105
110
|
end
|
|
106
111
|
|
|
107
|
-
class SourceGeneratingMockTemplate <
|
|
108
|
-
def
|
|
112
|
+
class SourceGeneratingMockTemplate < PreparingMockTemplate
|
|
113
|
+
def precompiled_template(locals)
|
|
109
114
|
"foo = [] ; foo << %Q{#{data}} ; foo.join"
|
|
110
115
|
end
|
|
111
116
|
end
|
|
@@ -113,7 +118,7 @@ class TiltTemplateTest < Test::Unit::TestCase
|
|
|
113
118
|
test "template_source with locals" do
|
|
114
119
|
inst = SourceGeneratingMockTemplate.new { |t| 'Hey #{name}!' }
|
|
115
120
|
assert_equal "Hey Joe!", inst.render(Object.new, :name => 'Joe')
|
|
116
|
-
assert inst.
|
|
121
|
+
assert inst.prepared?
|
|
117
122
|
end
|
|
118
123
|
|
|
119
124
|
class Person
|