tilt 0.6 → 1.0.1
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 +53 -4
- data/Rakefile +33 -49
- data/TEMPLATES.md +80 -53
- data/bin/tilt +45 -16
- data/lib/tilt.rb +374 -188
- data/test/contest.rb +68 -0
- data/test/tilt_buildertemplate_test.rb +1 -1
- data/test/tilt_compilesite_test.rb +86 -0
- data/test/tilt_erbtemplate_test.rb +121 -2
- data/test/tilt_erubistemplate_test.rb +52 -1
- data/test/tilt_hamltemplate_test.rb +65 -1
- data/test/tilt_liquidtemplate_test.rb +1 -1
- data/test/tilt_radiustemplate_test.rb +66 -0
- data/test/tilt_rdiscounttemplate_test.rb +3 -3
- data/test/tilt_rdoctemplate_test.rb +1 -1
- data/test/tilt_sasstemplate_test.rb +2 -2
- data/test/tilt_stringtemplate_test.rb +87 -1
- data/test/tilt_template_test.rb +47 -24
- data/test/tilt_test.rb +9 -1
- data/tilt.gemspec +11 -8
- metadata +57 -18
- data/test/tilt_coffeetemplate_test.rb +0 -20
- data/test/tilt_mustache_views/external.rb +0 -9
- data/test/tilt_mustachetemplate_test.rb +0 -73
data/test/contest.rb
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
require "test/unit"
|
|
2
|
+
|
|
3
|
+
# Test::Unit loads a default test if the suite is empty, whose purpose is to
|
|
4
|
+
# fail. Since having empty contexts is a common practice, we decided to
|
|
5
|
+
# overwrite TestSuite#empty? in order to allow them. Having a failure when no
|
|
6
|
+
# tests have been defined seems counter-intuitive.
|
|
7
|
+
class Test::Unit::TestSuite
|
|
8
|
+
def empty?
|
|
9
|
+
false
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
# Contest adds +teardown+, +test+ and +context+ as class methods, and the
|
|
14
|
+
# instance methods +setup+ and +teardown+ now iterate on the corresponding
|
|
15
|
+
# blocks. Note that all setup and teardown blocks must be defined with the
|
|
16
|
+
# block syntax. Adding setup or teardown instance methods defeats the purpose
|
|
17
|
+
# of this library.
|
|
18
|
+
class Test::Unit::TestCase
|
|
19
|
+
def self.setup(&block)
|
|
20
|
+
define_method :setup do
|
|
21
|
+
super(&block)
|
|
22
|
+
instance_eval(&block)
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def self.teardown(&block)
|
|
27
|
+
define_method :teardown do
|
|
28
|
+
instance_eval(&block)
|
|
29
|
+
super(&block)
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def self.context(name, &block)
|
|
34
|
+
subclass = Class.new(self)
|
|
35
|
+
remove_tests(subclass)
|
|
36
|
+
subclass.class_eval(&block) if block_given?
|
|
37
|
+
const_set(context_name(name), subclass)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def self.test(name, &block)
|
|
41
|
+
define_method(test_name(name), &block)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
class << self
|
|
45
|
+
alias_method :should, :test
|
|
46
|
+
alias_method :describe, :context
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
private
|
|
50
|
+
|
|
51
|
+
def self.context_name(name)
|
|
52
|
+
"Test#{sanitize_name(name).gsub(/(^| )(\w)/) { $2.upcase }}".to_sym
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def self.test_name(name)
|
|
56
|
+
"test_#{sanitize_name(name).gsub(/\s+/,'_')}".to_sym
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def self.sanitize_name(name)
|
|
60
|
+
name.gsub(/\W+/, ' ').strip
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def self.remove_tests(subclass)
|
|
64
|
+
subclass.public_instance_methods.grep(/^test_/).each do |meth|
|
|
65
|
+
subclass.send(:undef_method, meth.to_sym)
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
@@ -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
|
|
@@ -29,6 +29,23 @@ class ERBTemplateTest < Test::Unit::TestCase
|
|
|
29
29
|
assert_equal "Hey Joe!", template.render(scope)
|
|
30
30
|
end
|
|
31
31
|
|
|
32
|
+
class MockOutputVariableScope
|
|
33
|
+
attr_accessor :exposed_buffer
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
test "exposing the buffer to the template by default" do
|
|
37
|
+
begin
|
|
38
|
+
Tilt::ERBTemplate.default_output_variable = '@_out_buf'
|
|
39
|
+
template = Tilt::ERBTemplate.new { '<% self.exposed_buffer = @_out_buf %>hey' }
|
|
40
|
+
scope = MockOutputVariableScope.new
|
|
41
|
+
template.render(scope)
|
|
42
|
+
assert_not_nil scope.exposed_buffer
|
|
43
|
+
assert_equal scope.exposed_buffer, 'hey'
|
|
44
|
+
ensure
|
|
45
|
+
Tilt::ERBTemplate.default_output_variable = '_erbout'
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
32
49
|
test "passing a block for yield" do
|
|
33
50
|
template = Tilt::ERBTemplate.new { 'Hey <%= yield %>!' }
|
|
34
51
|
assert_equal "Hey Joe!", template.render { 'Joe' }
|
|
@@ -77,9 +94,111 @@ class ERBTemplateTest < Test::Unit::TestCase
|
|
|
77
94
|
end
|
|
78
95
|
|
|
79
96
|
test "shorthand whole line syntax trim mode" do
|
|
80
|
-
template = Tilt.new('test.erb',
|
|
97
|
+
template = Tilt.new('test.erb', :trim => '%') { "\n% if true\nhello\n%end\n" }
|
|
81
98
|
assert_equal "\nhello\n", template.render
|
|
82
99
|
end
|
|
100
|
+
|
|
101
|
+
test "using an instance variable as the outvar" do
|
|
102
|
+
template = Tilt::ERBTemplate.new(nil, :outvar => '@buf') { "<%= 1 + 1 %>" }
|
|
103
|
+
scope = Object.new
|
|
104
|
+
scope.instance_variable_set(:@buf, 'original value')
|
|
105
|
+
assert_equal '2', template.render(scope)
|
|
106
|
+
assert_equal 'original value', scope.instance_variable_get(:@buf)
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
class CompiledERBTemplateTest < Test::Unit::TestCase
|
|
111
|
+
def teardown
|
|
112
|
+
GC.start
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
class Scope
|
|
116
|
+
include Tilt::CompileSite
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
test "compiling template source to a method" do
|
|
120
|
+
template = Tilt::ERBTemplate.new { |t| "Hello World!" }
|
|
121
|
+
template.render(Scope.new)
|
|
122
|
+
method_name = template.send(:compiled_method_name, [])
|
|
123
|
+
method_name = method_name.to_sym if Symbol === Kernel.methods.first
|
|
124
|
+
assert Tilt::CompileSite.instance_methods.include?(method_name),
|
|
125
|
+
"CompileSite.instance_methods.include?(#{method_name.inspect})"
|
|
126
|
+
assert Scope.new.respond_to?(method_name),
|
|
127
|
+
"scope.respond_to?(#{method_name.inspect})"
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
test "loading and evaluating templates on #render" do
|
|
131
|
+
template = Tilt::ERBTemplate.new { |t| "Hello World!" }
|
|
132
|
+
assert_equal "Hello World!", template.render(Scope.new)
|
|
133
|
+
assert_equal "Hello World!", template.render(Scope.new)
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
test "passing locals" do
|
|
137
|
+
template = Tilt::ERBTemplate.new { 'Hey <%= name %>!' }
|
|
138
|
+
assert_equal "Hey Joe!", template.render(Scope.new, :name => 'Joe')
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
test "evaluating in an object scope" do
|
|
142
|
+
template = Tilt::ERBTemplate.new { 'Hey <%= @name %>!' }
|
|
143
|
+
scope = Scope.new
|
|
144
|
+
scope.instance_variable_set :@name, 'Joe'
|
|
145
|
+
assert_equal "Hey Joe!", template.render(scope)
|
|
146
|
+
scope.instance_variable_set :@name, 'Jane'
|
|
147
|
+
assert_equal "Hey Jane!", template.render(scope)
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
test "passing a block for yield" do
|
|
151
|
+
template = Tilt::ERBTemplate.new { 'Hey <%= yield %>!' }
|
|
152
|
+
assert_equal "Hey Joe!", template.render(Scope.new) { 'Joe' }
|
|
153
|
+
assert_equal "Hey Jane!", template.render(Scope.new) { 'Jane' }
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
test "backtrace file and line reporting without locals" do
|
|
157
|
+
data = File.read(__FILE__).split("\n__END__\n").last
|
|
158
|
+
fail unless data[0] == ?<
|
|
159
|
+
template = Tilt::ERBTemplate.new('test.erb', 11) { data }
|
|
160
|
+
begin
|
|
161
|
+
template.render(Scope.new)
|
|
162
|
+
fail 'should have raised an exception'
|
|
163
|
+
rescue => boom
|
|
164
|
+
assert_kind_of NameError, boom
|
|
165
|
+
line = boom.backtrace.first
|
|
166
|
+
file, line, meth = line.split(":")
|
|
167
|
+
assert_equal 'test.erb', file
|
|
168
|
+
assert_equal '13', line
|
|
169
|
+
end
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
test "backtrace file and line reporting with locals" do
|
|
173
|
+
data = File.read(__FILE__).split("\n__END__\n").last
|
|
174
|
+
fail unless data[0] == ?<
|
|
175
|
+
template = Tilt::ERBTemplate.new('test.erb') { data }
|
|
176
|
+
begin
|
|
177
|
+
template.render(Scope.new, :name => 'Joe', :foo => 'bar')
|
|
178
|
+
fail 'should have raised an exception'
|
|
179
|
+
rescue => boom
|
|
180
|
+
assert_kind_of RuntimeError, boom
|
|
181
|
+
line = boom.backtrace.first
|
|
182
|
+
file, line, meth = line.split(":")
|
|
183
|
+
assert_equal 'test.erb', file
|
|
184
|
+
assert_equal '6', line
|
|
185
|
+
end
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
test "default non-stripping trim mode" do
|
|
189
|
+
template = Tilt.new('test.erb') { "\n<%= 1 + 1 %>\n" }
|
|
190
|
+
assert_equal "\n2\n", template.render(Scope.new)
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
test "stripping trim mode" do
|
|
194
|
+
template = Tilt.new('test.erb', :trim => '-') { "\n<%= 1 + 1 -%>\n" }
|
|
195
|
+
assert_equal "\n2", template.render(Scope.new)
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
test "shorthand whole line syntax trim mode" do
|
|
199
|
+
template = Tilt.new('test.erb', :trim => '%') { "\n% if true\nhello\n%end\n" }
|
|
200
|
+
assert_equal "\nhello\n", template.render(Scope.new)
|
|
201
|
+
end
|
|
83
202
|
end
|
|
84
203
|
|
|
85
204
|
__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
|
|
@@ -26,6 +26,23 @@ begin
|
|
|
26
26
|
assert_equal "Hey Joe!", template.render(scope)
|
|
27
27
|
end
|
|
28
28
|
|
|
29
|
+
class MockOutputVariableScope
|
|
30
|
+
attr_accessor :exposed_buffer
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
test "exposing the buffer to the template by default" do
|
|
34
|
+
begin
|
|
35
|
+
Tilt::ErubisTemplate.default_output_variable = '@_out_buf'
|
|
36
|
+
template = Tilt::ErubisTemplate.new { '<% self.exposed_buffer = @_out_buf %>hey' }
|
|
37
|
+
scope = MockOutputVariableScope.new
|
|
38
|
+
template.render(scope)
|
|
39
|
+
assert_not_nil scope.exposed_buffer
|
|
40
|
+
assert_equal scope.exposed_buffer, 'hey'
|
|
41
|
+
ensure
|
|
42
|
+
Tilt::ErubisTemplate.default_output_variable = '_erbout'
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
29
46
|
test "passing a block for yield" do
|
|
30
47
|
template = Tilt::ErubisTemplate.new { 'Hey <%= yield %>!' }
|
|
31
48
|
assert_equal "Hey Joe!", template.render { 'Joe' }
|
|
@@ -69,6 +86,40 @@ begin
|
|
|
69
86
|
scope.instance_variable_set :@name, 'Joe'
|
|
70
87
|
assert_equal "Hey Joe!", template.render(scope)
|
|
71
88
|
end
|
|
89
|
+
|
|
90
|
+
test "using an instance variable as the outvar" do
|
|
91
|
+
template = Tilt::ErubisTemplate.new(nil, :outvar => '@buf') { "<%= 1 + 1 %>" }
|
|
92
|
+
scope = Object.new
|
|
93
|
+
scope.instance_variable_set(:@buf, 'original value')
|
|
94
|
+
assert_equal '2', template.render(scope)
|
|
95
|
+
assert_equal 'original value', scope.instance_variable_get(:@buf)
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
test "using Erubis::EscapedEruby subclass via :engine_class option" do
|
|
99
|
+
template = Tilt::ErubisTemplate.new(nil, :engine_class => ::Erubis::EscapedEruby) { |t| %(<%= "<p>Hello World!</p>" %>) }
|
|
100
|
+
assert_equal "<p>Hello World!</p>", template.render
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
test "using :escape_html => true option" do
|
|
104
|
+
template = Tilt::ErubisTemplate.new(nil, :escape_html => true) { |t| %(<%= "<p>Hello World!</p>" %>) }
|
|
105
|
+
assert_equal "<p>Hello World!</p>", template.render
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
test "using :escape_html => false option" do
|
|
109
|
+
template = Tilt::ErubisTemplate.new(nil, :escape_html => false) { |t| %(<%= "<p>Hello World!</p>" %>) }
|
|
110
|
+
assert_equal "<p>Hello World!</p>", template.render
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
test "erubis default does not escape html" do
|
|
114
|
+
template = Tilt::ErubisTemplate.new { |t| %(<%= "<p>Hello World!</p>" %>) }
|
|
115
|
+
assert_equal "<p>Hello World!</p>", template.render
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
test "does not modify options argument" do
|
|
119
|
+
options_hash = {:escape_html => true}
|
|
120
|
+
template = Tilt::ErubisTemplate.new(nil, options_hash) { |t| "Hello World!" }
|
|
121
|
+
assert_equal({:escape_html => true}, options_hash)
|
|
122
|
+
end
|
|
72
123
|
end
|
|
73
124
|
rescue LoadError => boom
|
|
74
125
|
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
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
require 'contest'
|
|
2
|
+
require 'tilt'
|
|
3
|
+
|
|
4
|
+
begin
|
|
5
|
+
require 'radius'
|
|
6
|
+
|
|
7
|
+
class RadiusTemplateTest < Test::Unit::TestCase
|
|
8
|
+
test "registered for '.radius' files" do
|
|
9
|
+
assert_equal Tilt::RadiusTemplate, Tilt['test.radius']
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
test "preparing and evaluating templates on #render" do
|
|
13
|
+
template = Tilt::RadiusTemplate.new { |t| "Hello World!" }
|
|
14
|
+
assert_equal "Hello World!", template.render
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
test "passing locals" do
|
|
18
|
+
template = Tilt::RadiusTemplate.new { "Hey <r:name />!" }
|
|
19
|
+
assert_equal "Hey Joe!", template.render(nil, :name => 'Joe')
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
class ExampleRadiusScope
|
|
23
|
+
def beer; 'wet'; end
|
|
24
|
+
def whisky; 'wetter'; end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
test "combining scope and locals when scope responds" do
|
|
28
|
+
template = Tilt::RadiusTemplate.new {
|
|
29
|
+
'Beer is <r:beer /> but Whisky is <r:whisky />.'
|
|
30
|
+
}
|
|
31
|
+
scope = ExampleRadiusScope.new
|
|
32
|
+
assert_equal "Beer is wet but Whisky is wetter.", template.render(scope)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
test "precedence when locals and scope define same variables" do
|
|
36
|
+
template = Tilt::RadiusTemplate.new {
|
|
37
|
+
'Beer is <r:beer /> but Whisky is <r:whisky />.'
|
|
38
|
+
}
|
|
39
|
+
scope = ExampleRadiusScope.new
|
|
40
|
+
assert_equal "Beer is great but Whisky is greater.",
|
|
41
|
+
template.render(scope, :beer => 'great', :whisky => 'greater')
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
#test "handles local scope" do
|
|
45
|
+
# beer = 'wet'
|
|
46
|
+
# whisky = 'wetter'
|
|
47
|
+
#
|
|
48
|
+
# template = Tilt::RadiusTemplate.new {
|
|
49
|
+
# 'Beer is <r:beer /> but Whisky is <r:whisky />.'
|
|
50
|
+
# }
|
|
51
|
+
# assert_equal "Beer is wet but Whisky is wetter.", template.render(self)
|
|
52
|
+
#end
|
|
53
|
+
|
|
54
|
+
test "passing a block for yield" do
|
|
55
|
+
template = Tilt::RadiusTemplate.new {
|
|
56
|
+
'Beer is <r:yield /> but Whisky is <r:yield />ter.'
|
|
57
|
+
}
|
|
58
|
+
assert_equal "Beer is wet but Whisky is wetter.",
|
|
59
|
+
template.render({}) { 'wet' }
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
rescue LoadError => boom
|
|
64
|
+
warn "Tilt::RadiusTemplate (disabled)\n"
|
|
65
|
+
end
|
|
66
|
+
|
|
@@ -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
|
|
@@ -11,8 +11,8 @@ begin
|
|
|
11
11
|
end
|
|
12
12
|
|
|
13
13
|
test "compiles and evaluates the template on #render" do
|
|
14
|
-
template = Tilt::SassTemplate.new { |t| "#main\n :background-color #
|
|
15
|
-
assert_equal "#main {\n background-color: #
|
|
14
|
+
template = Tilt::SassTemplate.new { |t| "#main\n :background-color #0000f1" }
|
|
15
|
+
assert_equal "#main {\n background-color: #0000f1; }\n", template.render
|
|
16
16
|
end
|
|
17
17
|
end
|
|
18
18
|
|