tilt 1.2.2 → 1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,25 @@
1
+ require 'tilt/template'
2
+
3
+ module Tilt
4
+ # RedCloth implementation. See:
5
+ # http://redcloth.org/
6
+ class RedClothTemplate < Template
7
+ def self.engine_initialized?
8
+ defined? ::RedCloth
9
+ end
10
+
11
+ def initialize_engine
12
+ require_template_library 'redcloth'
13
+ end
14
+
15
+ def prepare
16
+ @engine = RedCloth.new(data)
17
+ @output = nil
18
+ end
19
+
20
+ def evaluate(scope, locals, &block)
21
+ @output ||= @engine.to_html
22
+ end
23
+ end
24
+ end
25
+
@@ -5,35 +5,16 @@ begin
5
5
  require 'bluecloth'
6
6
 
7
7
  class BlueClothTemplateTest < Test::Unit::TestCase
8
- setup do
9
- Tilt.register('markdown', Tilt::BlueClothTemplate)
10
- Tilt.register('md', Tilt::BlueClothTemplate)
11
- Tilt.register('mkd', Tilt::BlueClothTemplate)
8
+ test "registered for '.md' files" do
9
+ assert Tilt.mappings['md'].include?(Tilt::BlueClothTemplate)
12
10
  end
13
11
 
14
- teardown do
15
- # Need to revert to RDiscount, otherwise the RDiscount test will fail
16
- Tilt.register('markdown', Tilt::RDiscountTemplate)
17
- Tilt.register('md', Tilt::RDiscountTemplate)
18
- Tilt.register('mkd', Tilt::RDiscountTemplate)
12
+ test "registered for '.mkd' files" do
13
+ assert Tilt.mappings['mkd'].include?(Tilt::BlueClothTemplate)
19
14
  end
20
15
 
21
- test "registered for '.markdown' files unless RDiscount is loaded" do
22
- unless defined?(RDiscount)
23
- assert_equal Tilt::BlueClothTemplate, Tilt['test.markdown']
24
- end
25
- end
26
-
27
- test "registered for '.md' files unless RDiscount is loaded" do
28
- unless defined?(RDiscount)
29
- assert_equal Tilt::BlueClothTemplate, Tilt['test.md']
30
- end
31
- end
32
-
33
- test "registered for '.mkd' files unless RDiscount is loaded" do
34
- unless defined?(RDiscount)
35
- assert_equal Tilt::BlueClothTemplate, Tilt['test.mkd']
36
- end
16
+ test "registered for '.markdown' files" do
17
+ assert Tilt.mappings['markdown'].include?(Tilt::BlueClothTemplate)
37
18
  end
38
19
 
39
20
  test "preparing and evaluating templates on #render" do
@@ -20,9 +20,39 @@ begin
20
20
  end
21
21
 
22
22
  test "disabling coffee-script wrapper" do
23
- template = Tilt::CoffeeScriptTemplate.new(:no_wrap => true) { |t| "puts 'Hello, World!'\n" }
23
+ str = "puts 'Hello, World!'\n"
24
+
25
+ template = Tilt::CoffeeScriptTemplate.new(:bare => true) { str }
26
+ assert_equal "puts('Hello, World!');", template.render
27
+
28
+ template2 = Tilt::CoffeeScriptTemplate.new(:no_wrap => true) { str}
24
29
  assert_equal "puts('Hello, World!');", template.render
25
30
  end
31
+
32
+ context "disabling coffee-script wrapper globally" do
33
+ setup do
34
+ @bare = Tilt::CoffeeScriptTemplate.default_bare
35
+ end
36
+
37
+ teardown do
38
+ Tilt::CoffeeScriptTemplate.default_bare = @bare
39
+ end
40
+
41
+ test "no options" do
42
+ template = Tilt::CoffeeScriptTemplate.new { |t| "puts 'Hello, World!'\n" }
43
+ assert_equal "puts('Hello, World!');", template.render
44
+ end
45
+
46
+ test "overridden by :bare" do
47
+ template = Tilt::CoffeeScriptTemplate.new(:bare => false) { "puts 'Hello, World!'\n" }
48
+ assert_not_equal "puts('Hello, World!');", template.render
49
+ end
50
+
51
+ test "overridden by :no_wrap" do
52
+ template = Tilt::CoffeeScriptTemplate.new(:no_wrap => false) { "puts 'Hello, World!'\n" }
53
+ assert_not_equal "puts('Hello, World!');", template.render
54
+ end
55
+ end
26
56
  end
27
57
 
28
58
  rescue LoadError => boom
@@ -0,0 +1,24 @@
1
+ require 'contest'
2
+ require 'tilt'
3
+
4
+ begin
5
+ require 'creole'
6
+
7
+ class CreoleTemplateTest < Test::Unit::TestCase
8
+ test "is registered for '.creole' files" do
9
+ assert_equal Tilt::CreoleTemplate, Tilt['test.creole']
10
+ end
11
+
12
+ test "compiles and evaluates the template on #render" do
13
+ template = Tilt::CreoleTemplate.new { |t| "= Hello World!" }
14
+ assert_equal "<h1>Hello World!</h1>", template.render
15
+ end
16
+
17
+ test "can be rendered more than once" do
18
+ template = Tilt::CreoleTemplate.new { |t| "= Hello World!" }
19
+ 3.times { assert_equal "<h1>Hello World!</h1>", template.render }
20
+ end
21
+ end
22
+ rescue LoadError => boom
23
+ warn "Tilt::CreoleTemplate (disabled)\n"
24
+ end
@@ -6,12 +6,11 @@ require 'tempfile'
6
6
 
7
7
  class ERBTemplateTest < Test::Unit::TestCase
8
8
  test "registered for '.erb' files" do
9
- assert_equal Tilt::ERBTemplate, Tilt['test.erb']
10
- assert_equal Tilt::ERBTemplate, Tilt['test.html.erb']
9
+ assert Tilt.mappings['erb'].include?(Tilt::ERBTemplate)
11
10
  end
12
11
 
13
12
  test "registered for '.rhtml' files" do
14
- assert_equal Tilt::ERBTemplate, Tilt['test.rhtml']
13
+ assert Tilt.mappings['rhtml'].include?(Tilt::ERBTemplate)
15
14
  end
16
15
 
17
16
  test "loading and evaluating templates on #render" do
@@ -67,9 +66,9 @@ class ERBTemplateTest < Test::Unit::TestCase
67
66
  fail 'should have raised an exception'
68
67
  rescue => boom
69
68
  assert_kind_of NameError, boom
70
- line = boom.backtrace.first
69
+ line = boom.backtrace.grep(/^test\.erb:/).first
70
+ assert line, "Backtrace didn't contain test.erb"
71
71
  file, line, meth = line.split(":")
72
- assert_equal 'test.erb', file
73
72
  assert_equal '13', line
74
73
  end
75
74
  end
@@ -90,18 +89,18 @@ class ERBTemplateTest < Test::Unit::TestCase
90
89
  end
91
90
  end
92
91
 
93
- test "default non-stripping trim mode" do
94
- template = Tilt.new('test.erb', 1) { "\n<%= 1 + 1 %>\n" }
95
- assert_equal "\n2\n", template.render
92
+ test "default stripping trim mode" do
93
+ template = Tilt::ERBTemplate.new('test.erb', 1) { "\n<%= 1 + 1 %>\n" }
94
+ assert_equal "\n2", template.render
96
95
  end
97
96
 
98
97
  test "stripping trim mode" do
99
- template = Tilt.new('test.erb', 1, :trim => '-') { "\n<%= 1 + 1 -%>\n" }
98
+ template = Tilt::ERBTemplate.new('test.erb', 1, :trim => '-') { "\n<%= 1 + 1 -%>\n" }
100
99
  assert_equal "\n2", template.render
101
100
  end
102
101
 
103
102
  test "shorthand whole line syntax trim mode" do
104
- template = Tilt.new('test.erb', :trim => '%') { "\n% if true\nhello\n%end\n" }
103
+ template = Tilt::ERBTemplate.new('test.erb', :trim => '%') { "\n% if true\nhello\n%end\n" }
105
104
  assert_equal "\nhello\n", template.render
106
105
  end
107
106
 
@@ -164,9 +163,9 @@ class CompiledERBTemplateTest < Test::Unit::TestCase
164
163
  fail 'should have raised an exception'
165
164
  rescue => boom
166
165
  assert_kind_of NameError, boom
167
- line = boom.backtrace.first
166
+ line = boom.backtrace.grep(/^test\.erb:/).first
167
+ assert line, "Backtrace didn't contain test.erb"
168
168
  file, line, meth = line.split(":")
169
- assert_equal 'test.erb', file
170
169
  assert_equal '13', line
171
170
  end
172
171
  end
@@ -187,18 +186,18 @@ class CompiledERBTemplateTest < Test::Unit::TestCase
187
186
  end
188
187
  end
189
188
 
190
- test "default non-stripping trim mode" do
191
- template = Tilt.new('test.erb') { "\n<%= 1 + 1 %>\n" }
192
- assert_equal "\n2\n", template.render(Scope.new)
189
+ test "default stripping trim mode" do
190
+ template = Tilt::ERBTemplate.new('test.erb') { "\n<%= 1 + 1 %>\n" }
191
+ assert_equal "\n2", template.render(Scope.new)
193
192
  end
194
193
 
195
194
  test "stripping trim mode" do
196
- template = Tilt.new('test.erb', :trim => '-') { "\n<%= 1 + 1 -%>\n" }
195
+ template = Tilt::ERBTemplate.new('test.erb', :trim => '-') { "\n<%= 1 + 1 -%>\n" }
197
196
  assert_equal "\n2", template.render(Scope.new)
198
197
  end
199
198
 
200
199
  test "shorthand whole line syntax trim mode" do
201
- template = Tilt.new('test.erb', :trim => '%') { "\n% if true\nhello\n%end\n" }
200
+ template = Tilt::ERBTemplate.new('test.erb', :trim => '%') { "\n% if true\nhello\n%end\n" }
202
201
  assert_equal "\nhello\n", template.render(Scope.new)
203
202
  end
204
203
 
@@ -208,7 +207,7 @@ class CompiledERBTemplateTest < Test::Unit::TestCase
208
207
  f.puts('ふが <%= @hoge %>')
209
208
  f.close()
210
209
  @hoge = "ほげ"
211
- erb = Tilt['erb'].new(f.path)
210
+ erb = Tilt::ERBTemplate.new(f.path)
212
211
  3.times { erb.render(self) }
213
212
  f.delete
214
213
  end
@@ -218,7 +217,7 @@ class CompiledERBTemplateTest < Test::Unit::TestCase
218
217
  f.puts('ふが <%= @hoge %>')
219
218
  f.close()
220
219
  @hoge = "ほげ"
221
- erb = Tilt['erb'].new(f.path, :default_encoding => 'UTF-8')
220
+ erb = Tilt::ERBTemplate.new(f.path, :default_encoding => 'UTF-8')
222
221
  3.times { erb.render(self) }
223
222
  f.delete
224
223
  end
@@ -9,6 +9,16 @@ begin
9
9
  assert_equal Tilt::ErubisTemplate, Tilt['test.html.erubis']
10
10
  end
11
11
 
12
+ test "registered above ERB" do
13
+ %w[erb rhtml].each do |ext|
14
+ mappings = Tilt.mappings[ext]
15
+ erubis_idx = mappings.index(Tilt::ErubisTemplate)
16
+ erb_idx = mappings.index(Tilt::ERBTemplate)
17
+ assert erubis_idx < erb_idx,
18
+ "#{erubis_idx} should be lower than #{erb_idx}"
19
+ end
20
+ end
21
+
12
22
  test "preparing and evaluating templates on #render" do
13
23
  template = Tilt::ErubisTemplate.new { |t| "Hello World!" }
14
24
  assert_equal "Hello World!", template.render
@@ -62,9 +72,9 @@ begin
62
72
  fail 'should have raised an exception'
63
73
  rescue => boom
64
74
  assert_kind_of NameError, boom
65
- line = boom.backtrace.first
75
+ line = boom.backtrace.grep(/^test\.erubis:/).first
76
+ assert line, "Backtrace didn't contain test.erubis"
66
77
  file, line, meth = line.split(":")
67
- assert_equal 'test.erubis', file
68
78
  assert_equal '13', line
69
79
  end
70
80
  end
@@ -0,0 +1,122 @@
1
+ require 'contest'
2
+ require 'tilt'
3
+
4
+ class TiltFallbackTest < Test::Unit::TestCase
5
+ class FailTemplate < Tilt::Template
6
+ def self.engine_initialized?; false end
7
+ def prepare; end
8
+
9
+ def initialize_engine
10
+ raise LoadError, "can't load #{self.class}"
11
+ end
12
+ end
13
+
14
+ class WinTemplate < Tilt::Template
15
+ def self.engine_initialized?; true end
16
+ def prepare; end
17
+ end
18
+
19
+ FailTemplate2 = Class.new(FailTemplate)
20
+ WinTemplate2 = Class.new(WinTemplate)
21
+
22
+ def set_ivar(obj, name, value)
23
+ obj.instance_variable_set("@#{name}", value)
24
+ end
25
+
26
+ def clear_ivar(obj, name)
27
+ ivar = "@#{name}"
28
+ value = obj.instance_variable_get(ivar)
29
+ ensure
30
+ obj.instance_variable_set(ivar, value.dup.clear)
31
+ end
32
+
33
+ setup do
34
+ # Make sure every test have no mappings.
35
+ @p = clear_ivar(Tilt, :preferred_mappings)
36
+ @t = clear_ivar(Tilt, :template_mappings)
37
+ end
38
+
39
+ teardown do
40
+ set_ivar(Tilt, :preferred_mappings, @p)
41
+ set_ivar(Tilt, :template_mappings, @t)
42
+ end
43
+
44
+ test "returns nil on unregistered extensions" do
45
+ template = Tilt["md"]
46
+ assert_equal nil, template
47
+ end
48
+
49
+ test "returns the last registered template" do
50
+ Tilt.register("md", WinTemplate)
51
+ Tilt.register("md", WinTemplate2)
52
+
53
+ template = Tilt["md"]
54
+ assert_equal WinTemplate2, template
55
+ end
56
+
57
+ test "returns the last registered working template" do
58
+ Tilt.register("md", WinTemplate)
59
+ Tilt.register("md", FailTemplate)
60
+
61
+ template = Tilt["md"]
62
+ assert_equal WinTemplate, template
63
+ end
64
+
65
+ test "if every template fails, raise the exception from the first template" do
66
+ Tilt.register("md", FailTemplate)
67
+ Tilt.register("md", FailTemplate2)
68
+
69
+ exc = assert_raise(LoadError) { Tilt["md"] }
70
+ assert_match /FailTemplate2/, exc.message
71
+ end
72
+
73
+ test ".prefer should also register the template" do
74
+ Tilt.prefer(WinTemplate, "md")
75
+ assert Tilt.registered?("md")
76
+ end
77
+
78
+ test ".prefer always win" do
79
+ Tilt.register("md", FailTemplate)
80
+ Tilt.register("md", WinTemplate)
81
+ Tilt.prefer(FailTemplate, "md")
82
+
83
+ template = Tilt["md"]
84
+ assert_equal FailTemplate, template
85
+ end
86
+
87
+ test ".prefer accepts multiple extensions" do
88
+ extensions = %w[md mkd markdown]
89
+ Tilt.prefer(FailTemplate, *extensions)
90
+
91
+ extensions.each do |ext|
92
+ template = Tilt[ext]
93
+ assert_equal FailTemplate, template
94
+ end
95
+ end
96
+
97
+ test ".prefer with no extension should use already registered extensions" do
98
+ extensions = %w[md mkd markdown]
99
+
100
+ extensions.each do |ext|
101
+ Tilt.register(ext, FailTemplate)
102
+ Tilt.register(ext, WinTemplate)
103
+ end
104
+
105
+ Tilt.prefer(FailTemplate)
106
+
107
+ extensions.each do |ext|
108
+ template = Tilt[ext]
109
+ assert_equal FailTemplate, template
110
+ end
111
+ end
112
+
113
+ test ".prefer should only override extensions the preferred library is registered for" do
114
+ Tilt.register("md", WinTemplate)
115
+ Tilt.register("mkd", FailTemplate)
116
+ Tilt.register("mkd", WinTemplate)
117
+ Tilt.prefer(FailTemplate)
118
+ assert_equal FailTemplate, Tilt["mkd"]
119
+ assert_equal WinTemplate, Tilt["md"]
120
+ end
121
+ end
122
+
@@ -48,9 +48,9 @@ begin
48
48
  fail 'should have raised an exception'
49
49
  rescue => boom
50
50
  assert_kind_of NameError, boom
51
- line = boom.backtrace.first
51
+ line = boom.backtrace.grep(/^test\.haml:/).first
52
+ assert line, "Backtrace didn't contain test.haml"
52
53
  file, line, meth = line.split(":")
53
- assert_equal 'test.haml', file
54
54
  assert_equal '12', line
55
55
  end
56
56
  end
@@ -108,9 +108,9 @@ begin
108
108
  fail 'should have raised an exception'
109
109
  rescue => boom
110
110
  assert_kind_of NameError, boom
111
- line = boom.backtrace.first
111
+ line = boom.backtrace.grep(/^test\.haml:/).first
112
+ assert line, "Backtrace didn't contain test.haml"
112
113
  file, line, meth = line.split(":")
113
- assert_equal 'test.haml', file
114
114
  assert_equal '12', line
115
115
  end
116
116
  end
@@ -0,0 +1,42 @@
1
+ require 'contest'
2
+ require 'tilt'
3
+
4
+ begin
5
+ require 'kramdown'
6
+
7
+ class MarukuTemplateTest < Test::Unit::TestCase
8
+ test "registered for '.md' files" do
9
+ assert Tilt.mappings['md'].include?(Tilt::KramdownTemplate)
10
+ end
11
+
12
+ test "registered for '.mkd' files" do
13
+ assert Tilt.mappings['mkd'].include?(Tilt::KramdownTemplate)
14
+ end
15
+
16
+ test "registered for '.markdown' files" do
17
+ assert Tilt.mappings['markdown'].include?(Tilt::KramdownTemplate)
18
+ end
19
+
20
+ test "registered above MarukuTemplate" do
21
+ %w[md mkd markdown].each do |ext|
22
+ mappings = Tilt.mappings[ext]
23
+ kram_idx = mappings.index(Tilt::KramdownTemplate)
24
+ maru_idx = mappings.index(Tilt::MarukuTemplate)
25
+ assert kram_idx < maru_idx,
26
+ "#{kram_idx} should be lower than #{maru_idx}"
27
+ end
28
+ end
29
+
30
+ test "preparing and evaluating templates on #render" do
31
+ template = Tilt::KramdownTemplate.new { |t| "# Hello World!" }
32
+ assert_equal "<h1 id='hello_world'>Hello World!</h1>", template.render
33
+ end
34
+
35
+ test "can be rendered more than once" do
36
+ template = Tilt::KramdownTemplate.new { |t| "# Hello World!" }
37
+ 3.times { assert_equal "<h1 id='hello_world'>Hello World!</h1>", template.render }
38
+ end
39
+ end
40
+ rescue LoadError => boom
41
+ warn "Tilt::KramdownTemplate (disabled)\n"
42
+ end