tilt 1.0.1 → 1.2.2

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.
@@ -17,48 +17,13 @@ class CompileSiteTest < Test::Unit::TestCase
17
17
  end
18
18
 
19
19
  class Scope
20
- include Tilt::CompileSite
21
20
  end
22
21
 
23
22
  test "compiling template source to a method" do
24
23
  template = CompilingTemplate.new { |t| "Hello World!" }
25
24
  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})"
25
+ method = template.send(:compiled_method, [])
26
+ assert_kind_of UnboundMethod, method
62
27
  end
63
28
 
64
29
  # This test attempts to surface issues with compiling templates from
@@ -1,6 +1,8 @@
1
+ # coding: utf-8
1
2
  require 'contest'
2
3
  require 'tilt'
3
4
  require 'erb'
5
+ require 'tempfile'
4
6
 
5
7
  class ERBTemplateTest < Test::Unit::TestCase
6
8
  test "registered for '.erb' files" do
@@ -17,6 +19,11 @@ class ERBTemplateTest < Test::Unit::TestCase
17
19
  assert_equal "Hello World!", template.render
18
20
  end
19
21
 
22
+ test "can be rendered more than once" do
23
+ template = Tilt::ERBTemplate.new { |t| "Hello World!" }
24
+ 3.times { assert_equal "Hello World!", template.render }
25
+ end
26
+
20
27
  test "passing locals" do
21
28
  template = Tilt::ERBTemplate.new { 'Hey <%= name %>!' }
22
29
  assert_equal "Hey Joe!", template.render(Object.new, :name => 'Joe')
@@ -113,18 +120,13 @@ class CompiledERBTemplateTest < Test::Unit::TestCase
113
120
  end
114
121
 
115
122
  class Scope
116
- include Tilt::CompileSite
117
123
  end
118
124
 
119
125
  test "compiling template source to a method" do
120
126
  template = Tilt::ERBTemplate.new { |t| "Hello World!" }
121
127
  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
+ method = template.send(:compiled_method, [])
129
+ assert_kind_of UnboundMethod, method
128
130
  end
129
131
 
130
132
  test "loading and evaluating templates on #render" do
@@ -199,6 +201,27 @@ class CompiledERBTemplateTest < Test::Unit::TestCase
199
201
  template = Tilt.new('test.erb', :trim => '%') { "\n% if true\nhello\n%end\n" }
200
202
  assert_equal "\nhello\n", template.render(Scope.new)
201
203
  end
204
+
205
+ test "encoding with magic comment" do
206
+ f = Tempfile.open("template")
207
+ f.puts('<%# coding: UTF-8 %>')
208
+ f.puts('ふが <%= @hoge %>')
209
+ f.close()
210
+ @hoge = "ほげ"
211
+ erb = Tilt['erb'].new(f.path)
212
+ 3.times { erb.render(self) }
213
+ f.delete
214
+ end
215
+
216
+ test "encoding with :default_encoding" do
217
+ f = Tempfile.open("template")
218
+ f.puts('ふが <%= @hoge %>')
219
+ f.close()
220
+ @hoge = "ほげ"
221
+ erb = Tilt['erb'].new(f.path, :default_encoding => 'UTF-8')
222
+ 3.times { erb.render(self) }
223
+ f.delete
224
+ end
202
225
  end
203
226
 
204
227
  __END__
@@ -14,6 +14,11 @@ begin
14
14
  assert_equal "Hello World!", template.render
15
15
  end
16
16
 
17
+ test "can be rendered more than once" do
18
+ template = Tilt::ErubisTemplate.new { |t| "Hello World!" }
19
+ 3.times { assert_equal "Hello World!", template.render }
20
+ end
21
+
17
22
  test "passing locals" do
18
23
  template = Tilt::ErubisTemplate.new { 'Hey <%= name %>!' }
19
24
  assert_equal "Hey Joe!", template.render(Object.new, :name => 'Joe')
@@ -17,6 +17,11 @@ begin
17
17
  assert_equal "<p>Hello World!</p>\n", template.render
18
18
  end
19
19
 
20
+ test "can be rendered more than once" do
21
+ template = Tilt::HamlTemplate.new { |t| "%p Hello World!" }
22
+ 3.times { assert_equal "<p>Hello World!</p>\n", template.render }
23
+ end
24
+
20
25
  test "passing locals" do
21
26
  template = Tilt::HamlTemplate.new { "%p= 'Hey ' + name + '!'" }
22
27
  assert_equal "<p>Hey Joe!</p>\n", template.render(Object.new, :name => 'Joe')
@@ -68,18 +73,13 @@ begin
68
73
 
69
74
  class CompiledHamlTemplateTest < Test::Unit::TestCase
70
75
  class Scope
71
- include Tilt::CompileSite
72
76
  end
73
77
 
74
78
  test "compiling template source to a method" do
75
79
  template = Tilt::HamlTemplate.new { |t| "Hello World!" }
76
80
  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})"
81
+ method = template.send(:compiled_method, [])
82
+ assert_kind_of UnboundMethod, method
83
83
  end
84
84
 
85
85
  test "passing locals" do
@@ -13,6 +13,11 @@ begin
13
13
  template = Tilt::LessTemplate.new { |t| ".bg { background-color: #0000ff; } \n#main\n { .bg; }\n" }
14
14
  assert_equal ".bg, #main { background-color: #0000ff; }\n", template.render
15
15
  end
16
+
17
+ test "can be rendered more than once" do
18
+ template = Tilt::LessTemplate.new { |t| ".bg { background-color: #0000ff; } \n#main\n { .bg; }\n" }
19
+ 3.times { assert_equal ".bg, #main { background-color: #0000ff; }\n", template.render }
20
+ end
16
21
  end
17
22
 
18
23
  rescue LoadError => boom
@@ -14,6 +14,11 @@ begin
14
14
  assert_equal "Hello World!", template.render
15
15
  end
16
16
 
17
+ test "can be rendered more than once" do
18
+ template = Tilt::LiquidTemplate.new { |t| "Hello World!" }
19
+ 3.times { assert_equal "Hello World!", template.render }
20
+ end
21
+
17
22
  test "passing locals" do
18
23
  template = Tilt::LiquidTemplate.new { "Hey {{ name }}!" }
19
24
  assert_equal "Hey Joe!", template.render(nil, :name => 'Joe')
@@ -0,0 +1,88 @@
1
+ require 'contest'
2
+ require 'tilt'
3
+
4
+ begin
5
+ require 'markaby'
6
+
7
+ class MarkabyTiltTest < Test::Unit::TestCase
8
+ def setup
9
+ @block = lambda do |t|
10
+ File.read(File.dirname(__FILE__) + "/#{t.file}")
11
+ end
12
+ end
13
+
14
+ test "should be able to render a markaby template with static html" do
15
+ tilt = Tilt::MarkabyTemplate.new("markaby/markaby.mab", &@block)
16
+ assert_equal "hello from markaby!", tilt.render
17
+ end
18
+
19
+ test "should use the contents of the template" do
20
+ tilt = ::Tilt::MarkabyTemplate.new("markaby/markaby_other_static.mab", &@block)
21
+ assert_equal "_why?", tilt.render
22
+ end
23
+
24
+ test "should render from a string (given as data)" do
25
+ tilt = ::Tilt::MarkabyTemplate.new { "html do; end" }
26
+ assert_equal "<html></html>", tilt.render
27
+ end
28
+
29
+ test "can be rendered more than once" do
30
+ tilt = ::Tilt::MarkabyTemplate.new { "html do; end" }
31
+ 3.times { assert_equal "<html></html>", tilt.render }
32
+ end
33
+
34
+ test "should evaluate a template file in the scope given" do
35
+ scope = Object.new
36
+ def scope.foo
37
+ "bar"
38
+ end
39
+
40
+ tilt = ::Tilt::MarkabyTemplate.new("markaby/scope.mab", &@block)
41
+ assert_equal "<li>bar</li>", tilt.render(scope)
42
+ end
43
+
44
+ test "should pass locals to the template" do
45
+ tilt = ::Tilt::MarkabyTemplate.new("markaby/locals.mab", &@block)
46
+ assert_equal "<li>bar</li>", tilt.render(Object.new, { :foo => "bar" })
47
+ end
48
+
49
+ test "should yield to the block given" do
50
+ tilt = ::Tilt::MarkabyTemplate.new("markaby/yielding.mab", &@block)
51
+ eval_scope = Markaby::Builder.new
52
+
53
+ output = tilt.render(Object.new, {}) do
54
+ text("Joe")
55
+ end
56
+
57
+ assert_equal "Hey Joe", output
58
+ end
59
+
60
+ test "should be able to render two templates in a row" do
61
+ tilt = ::Tilt::MarkabyTemplate.new("markaby/render_twice.mab", &@block)
62
+
63
+ assert_equal "foo", tilt.render
64
+ assert_equal "foo", tilt.render
65
+ end
66
+
67
+ test "should retrieve a Tilt::MarkabyTemplate when calling Tilt['hello.mab']" do
68
+ assert_equal Tilt::MarkabyTemplate, ::Tilt['./markaby/markaby.mab']
69
+ end
70
+
71
+ test "should return a new instance of the implementation class (when calling Tilt.new)" do
72
+ assert ::Tilt.new(File.dirname(__FILE__) + "/markaby/markaby.mab").kind_of?(Tilt::MarkabyTemplate)
73
+ end
74
+
75
+ test "should be able to evaluate block style templates" do
76
+ tilt = Tilt::MarkabyTemplate.new { |t| lambda { h1 "Hello World!" }}
77
+ assert_equal "<h1>Hello World!</h1>", tilt.render
78
+ end
79
+
80
+ test "should pass locals to block style templates" do
81
+ tilt = Tilt::MarkabyTemplate.new { |t| lambda { h1 "Hello #{name}!" }}
82
+ assert_equal "<h1>Hello _why!</h1>", tilt.render(nil, :name => "_why")
83
+ end
84
+ end
85
+
86
+ rescue LoadError => boom
87
+ warn "Tilt::MarkabyTemplate (disabled)\n"
88
+ end
@@ -0,0 +1,76 @@
1
+ require 'contest'
2
+ require 'tilt'
3
+
4
+ begin
5
+ require 'nokogiri'
6
+ class NokogiriTemplateTest < Test::Unit::TestCase
7
+ test "registered for '.nokogiri' files" do
8
+ assert_equal Tilt::NokogiriTemplate, Tilt['test.nokogiri']
9
+ assert_equal Tilt::NokogiriTemplate, Tilt['test.xml.nokogiri']
10
+ end
11
+
12
+ test "preparing and evaluating the template on #render" do
13
+ template = Tilt::NokogiriTemplate.new { |t| "xml.em 'Hello World!'" }
14
+ doc = Nokogiri.XML template.render
15
+ assert_equal 'Hello World!', doc.root.text
16
+ assert_equal 'em', doc.root.name
17
+ end
18
+
19
+ test "can be rendered more than once" do
20
+ template = Tilt::NokogiriTemplate.new { |t| "xml.em 'Hello World!'" }
21
+ 3.times do
22
+ doc = Nokogiri.XML template.render
23
+ assert_equal 'Hello World!', doc.root.text
24
+ assert_equal 'em', doc.root.name
25
+ end
26
+ end
27
+
28
+ test "passing locals" do
29
+ template = Tilt::NokogiriTemplate.new { "xml.em('Hey ' + name + '!')" }
30
+ doc = Nokogiri.XML template.render(Object.new, :name => 'Joe')
31
+ assert_equal 'Hey Joe!', doc.root.text
32
+ assert_equal 'em', doc.root.name
33
+ end
34
+
35
+ test "evaluating in an object scope" do
36
+ template = Tilt::NokogiriTemplate.new { "xml.em('Hey ' + @name + '!')" }
37
+ scope = Object.new
38
+ scope.instance_variable_set :@name, 'Joe'
39
+ doc = Nokogiri.XML template.render(scope)
40
+ assert_equal 'Hey Joe!', doc.root.text
41
+ assert_equal 'em', doc.root.name
42
+ end
43
+
44
+ test "passing a block for yield" do
45
+ template = Tilt::NokogiriTemplate.new { "xml.em('Hey ' + yield + '!')" }
46
+ 3.times do
47
+ doc = Nokogiri.XML template.render { 'Joe' }
48
+ assert_equal 'Hey Joe!', doc.root.text
49
+ assert_equal 'em', doc.root.name
50
+ end
51
+ end
52
+
53
+ test "block style templates" do
54
+ template =
55
+ Tilt::NokogiriTemplate.new do |t|
56
+ lambda { |xml| xml.em('Hey Joe!') }
57
+ end
58
+ doc = Nokogiri.XML template.render
59
+ assert_equal 'Hey Joe!', doc.root.text
60
+ assert_equal 'em', doc.root.name
61
+ end
62
+
63
+ test "allows nesting raw XML, API-compatible to Builder" do
64
+ subtemplate = Tilt::NokogiriTemplate.new { "xml.em 'Hello World!'" }
65
+ template = Tilt::NokogiriTemplate.new { "xml.strong { xml << yield }" }
66
+ 3.times do
67
+ options = { :xml => Nokogiri::XML::Builder.new }
68
+ doc = Nokogiri.XML(template.render(options) { subtemplate.render(options) })
69
+ assert_equal 'Hello World!', doc.root.text.strip
70
+ assert_equal 'strong', doc.root.name
71
+ end
72
+ end
73
+ end
74
+ rescue LoadError
75
+ warn "Tilt::NokogiriTemplate (disabled)"
76
+ end
@@ -2,6 +2,10 @@ require 'contest'
2
2
  require 'tilt'
3
3
 
4
4
  begin
5
+ # Disable radius tests under Ruby versions >= 1.9.1 since it's still buggy.
6
+ # Remove when fixed upstream.
7
+ raise LoadError if RUBY_VERSION >= "1.9.1"
8
+
5
9
  require 'radius'
6
10
 
7
11
  class RadiusTemplateTest < Test::Unit::TestCase
@@ -14,6 +18,11 @@ begin
14
18
  assert_equal "Hello World!", template.render
15
19
  end
16
20
 
21
+ test "can be rendered more than once" do
22
+ template = Tilt::RadiusTemplate.new { |t| "Hello World!" }
23
+ 3.times { assert_equal "Hello World!", template.render }
24
+ end
25
+
17
26
  test "passing locals" do
18
27
  template = Tilt::RadiusTemplate.new { "Hey <r:name />!" }
19
28
  assert_equal "Hey Joe!", template.render(nil, :name => 'Joe')
@@ -22,6 +22,11 @@ begin
22
22
  assert_equal "<h1>Hello World!</h1>\n", template.render
23
23
  end
24
24
 
25
+ test "can be rendered more than once" do
26
+ template = Tilt::RDiscountTemplate.new { |t| "# Hello World!" }
27
+ 3.times { assert_equal "<h1>Hello World!</h1>\n", template.render }
28
+ end
29
+
25
30
  test "smartypants when :smart is set" do
26
31
  template = Tilt::RDiscountTemplate.new(:smart => true) { |t|
27
32
  "OKAY -- 'Smarty Pants'" }
@@ -11,7 +11,12 @@ begin
11
11
 
12
12
  test "preparing and evaluating the template with #render" do
13
13
  template = Tilt::RDocTemplate.new { |t| "= Hello World!" }
14
- assert_equal "<h1>Hello World!</h1>\n", template.render
14
+ assert_equal "<h1>Hello World!</h1>", template.render.strip
15
+ end
16
+
17
+ test "can be rendered more than once" do
18
+ template = Tilt::RDocTemplate.new { |t| "= Hello World!" }
19
+ 3.times { assert_equal "<h1>Hello World!</h1>", template.render.strip }
15
20
  end
16
21
  end
17
22
  rescue LoadError => boom
@@ -14,6 +14,10 @@ begin
14
14
  assert_equal "<h1>Hello World!</h1>", template.render
15
15
  end
16
16
 
17
+ test "can be rendered more than once" do
18
+ template = Tilt::RedClothTemplate.new { |t| "h1. Hello World!" }
19
+ 3.times { assert_equal "<h1>Hello World!</h1>", template.render }
20
+ end
17
21
  end
18
22
  rescue LoadError => boom
19
23
  warn "Tilt::RedClothTemplate (disabled)\n"
@@ -14,6 +14,27 @@ begin
14
14
  template = Tilt::SassTemplate.new { |t| "#main\n :background-color #0000f1" }
15
15
  assert_equal "#main {\n background-color: #0000f1; }\n", template.render
16
16
  end
17
+
18
+ test "can be rendered more than once" do
19
+ template = Tilt::SassTemplate.new { |t| "#main\n :background-color #0000f1" }
20
+ 3.times { assert_equal "#main {\n background-color: #0000f1; }\n", template.render }
21
+ end
22
+ end
23
+
24
+ class ScssTemplateTest < Test::Unit::TestCase
25
+ test "is registered for '.scss' files" do
26
+ assert_equal Tilt::ScssTemplate, Tilt['test.scss']
27
+ end
28
+
29
+ test "compiles and evaluates the template on #render" do
30
+ template = Tilt::ScssTemplate.new { |t| "#main {\n background-color: #0000f1;\n}" }
31
+ assert_equal "#main {\n background-color: #0000f1; }\n", template.render
32
+ end
33
+
34
+ test "can be rendered more than once" do
35
+ template = Tilt::ScssTemplate.new { |t| "#main {\n background-color: #0000f1;\n}" }
36
+ 3.times { assert_equal "#main {\n background-color: #0000f1; }\n", template.render }
37
+ end
17
38
  end
18
39
 
19
40
  rescue LoadError => boom
@@ -11,6 +11,11 @@ class StringTemplateTest < Test::Unit::TestCase
11
11
  assert_equal "Hello World!", template.render
12
12
  end
13
13
 
14
+ test "can be rendered more than once" do
15
+ template = Tilt::StringTemplate.new { |t| "Hello World!" }
16
+ 3.times { assert_equal "Hello World!", template.render }
17
+ end
18
+
14
19
  test "passing locals" do
15
20
  template = Tilt::StringTemplate.new { 'Hey #{name}!' }
16
21
  assert_equal "Hey Joe!", template.render(Object.new, :name => 'Joe')
@@ -74,18 +79,13 @@ class CompiledStringTemplateTest < Test::Unit::TestCase
74
79
  end
75
80
 
76
81
  class Scope
77
- include Tilt::CompileSite
78
82
  end
79
83
 
80
84
  test "compiling template source to a method" do
81
85
  template = Tilt::StringTemplate.new { |t| "Hello World!" }
82
86
  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})"
87
+ method = template.send(:compiled_method, [])
88
+ assert_kind_of UnboundMethod, method
89
89
  end
90
90
 
91
91
  test "loading and evaluating templates on #render" do
@@ -145,15 +145,4 @@ class TiltTemplateTest < Test::Unit::TestCase
145
145
  inst = SourceGeneratingMockTemplate.new { |t| 'Hey #{CONSTANT}!' }
146
146
  assert_equal "Hey Bob!", inst.render(Person.new("Joe"))
147
147
  end
148
-
149
- class FastPerson < Person
150
- include Tilt::CompileSite
151
- end
152
-
153
- # FAILING CONSTANT TEST. DISABLED FOR 1.0.
154
-
155
- # test "template which accesses a constant with Tilt::CompileSite" do
156
- # inst = SourceGeneratingMockTemplate.new { |t| 'Hey #{CONSTANT}!' }
157
- # assert_equal "Hey Bob!", inst.render(FastPerson.new("Joe"))
158
- # end
159
148
  end
data/tilt.gemspec CHANGED
@@ -3,8 +3,8 @@ Gem::Specification.new do |s|
3
3
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
4
4
 
5
5
  s.name = 'tilt'
6
- s.version = '1.0.1'
7
- s.date = '2010-06-15'
6
+ s.version = '1.2.2'
7
+ s.date = '2011-01-17'
8
8
 
9
9
  s.description = "Generic interface to multiple Ruby template engines"
10
10
  s.summary = s.description
@@ -21,14 +21,24 @@ Gem::Specification.new do |s|
21
21
  bin/tilt
22
22
  lib/tilt.rb
23
23
  test/contest.rb
24
+ test/markaby/locals.mab
25
+ test/markaby/markaby.mab
26
+ test/markaby/markaby_other_static.mab
27
+ test/markaby/render_twice.mab
28
+ test/markaby/scope.mab
29
+ test/markaby/yielding.mab
30
+ test/tilt_blueclothtemplate_test.rb
24
31
  test/tilt_buildertemplate_test.rb
25
32
  test/tilt_cache_test.rb
33
+ test/tilt_coffeescripttemplate_test.rb
26
34
  test/tilt_compilesite_test.rb
27
35
  test/tilt_erbtemplate_test.rb
28
36
  test/tilt_erubistemplate_test.rb
29
37
  test/tilt_hamltemplate_test.rb
30
38
  test/tilt_lesstemplate_test.rb
31
39
  test/tilt_liquidtemplate_test.rb
40
+ test/tilt_markaby_test.rb
41
+ test/tilt_nokogiritemplate_test.rb
32
42
  test/tilt_radiustemplate_test.rb
33
43
  test/tilt_rdiscounttemplate_test.rb
34
44
  test/tilt_rdoctemplate_test.rb
@@ -53,6 +63,9 @@ Gem::Specification.new do |s|
53
63
  s.add_development_dependency 'liquid'
54
64
  s.add_development_dependency 'less'
55
65
  s.add_development_dependency 'radius'
66
+ s.add_development_dependency 'nokogiri'
67
+ s.add_development_dependency 'markaby'
68
+ s.add_development_dependency 'coffee-script'
56
69
 
57
70
  s.extra_rdoc_files = %w[COPYING]
58
71