tilt 1.4.1 → 2.0.11
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.
- checksums.yaml +7 -0
- data/COPYING +1 -1
- data/bin/tilt +18 -8
- data/lib/tilt/asciidoc.rb +1 -8
- data/lib/tilt/babel.rb +16 -0
- data/lib/tilt/bluecloth.rb +24 -0
- data/lib/tilt/builder.rb +12 -15
- data/lib/tilt/coffee.rb +10 -6
- data/lib/tilt/commonmarker.rb +88 -0
- data/lib/tilt/creole.rb +25 -0
- data/lib/tilt/csv.rb +12 -18
- data/lib/tilt/dummy.rb +3 -0
- data/lib/tilt/erb.rb +9 -56
- data/lib/tilt/erubi.rb +32 -0
- data/lib/tilt/erubis.rb +43 -0
- data/lib/tilt/haml.rb +66 -44
- data/lib/tilt/kramdown.rb +25 -0
- data/lib/tilt/less.rb +30 -0
- data/lib/tilt/liquid.rb +9 -10
- data/lib/tilt/livescript.rb +23 -0
- data/lib/tilt/mapping.rb +293 -0
- data/lib/tilt/markaby.rb +1 -8
- data/lib/tilt/maruku.rb +22 -0
- data/lib/tilt/nokogiri.rb +1 -8
- data/lib/tilt/pandoc.rb +57 -0
- data/lib/tilt/plain.rb +0 -4
- data/lib/tilt/prawn.rb +43 -0
- data/lib/tilt/radius.rb +1 -8
- data/lib/tilt/rdiscount.rb +39 -0
- data/lib/tilt/rdoc.rb +3 -10
- data/lib/tilt/redcarpet.rb +86 -0
- data/lib/tilt/{textile.rb → redcloth.rb} +1 -8
- data/lib/tilt/rst-pandoc.rb +23 -0
- data/lib/tilt/sass.rb +78 -0
- data/lib/tilt/sigil.rb +34 -0
- data/lib/tilt/string.rb +1 -1
- data/lib/tilt/template.rb +121 -105
- data/lib/tilt/typescript.rb +26 -0
- data/lib/tilt/wikicloth.rb +22 -0
- data/lib/tilt/yajl.rb +1 -8
- data/lib/tilt.rb +118 -155
- metadata +38 -469
- data/CHANGELOG.md +0 -44
- data/Gemfile +0 -32
- data/HACKING +0 -16
- data/README.md +0 -232
- data/Rakefile +0 -104
- data/TEMPLATES.md +0 -516
- data/lib/tilt/css.rb +0 -80
- data/lib/tilt/markdown.rb +0 -214
- data/lib/tilt/wiki.rb +0 -58
- data/test/contest.rb +0 -68
- data/test/markaby/locals.mab +0 -1
- data/test/markaby/markaby.mab +0 -1
- data/test/markaby/markaby_other_static.mab +0 -1
- data/test/markaby/render_twice.mab +0 -1
- data/test/markaby/scope.mab +0 -1
- data/test/markaby/yielding.mab +0 -2
- data/test/tilt_asciidoctor_test.rb +0 -44
- data/test/tilt_blueclothtemplate_test.rb +0 -45
- data/test/tilt_buildertemplate_test.rb +0 -59
- data/test/tilt_cache_test.rb +0 -32
- data/test/tilt_coffeescripttemplate_test.rb +0 -114
- data/test/tilt_compilesite_test.rb +0 -51
- data/test/tilt_creoletemplate_test.rb +0 -28
- data/test/tilt_csv_test.rb +0 -69
- data/test/tilt_erbtemplate_test.rb +0 -239
- data/test/tilt_erubistemplate_test.rb +0 -151
- data/test/tilt_etannitemplate_test.rb +0 -173
- data/test/tilt_fallback_test.rb +0 -122
- data/test/tilt_hamltemplate_test.rb +0 -144
- data/test/tilt_kramdown_test.rb +0 -42
- data/test/tilt_lesstemplate_test.less +0 -1
- data/test/tilt_lesstemplate_test.rb +0 -42
- data/test/tilt_liquidtemplate_test.rb +0 -78
- data/test/tilt_markaby_test.rb +0 -88
- data/test/tilt_markdown_test.rb +0 -172
- data/test/tilt_marukutemplate_test.rb +0 -48
- data/test/tilt_nokogiritemplate_test.rb +0 -87
- data/test/tilt_radiustemplate_test.rb +0 -75
- data/test/tilt_rdiscounttemplate_test.rb +0 -55
- data/test/tilt_rdoctemplate_test.rb +0 -31
- data/test/tilt_redcarpettemplate_test.rb +0 -71
- data/test/tilt_redclothtemplate_test.rb +0 -36
- data/test/tilt_sasstemplate_test.rb +0 -41
- data/test/tilt_stringtemplate_test.rb +0 -170
- data/test/tilt_template_test.rb +0 -323
- data/test/tilt_test.rb +0 -65
- data/test/tilt_wikiclothtemplate_test.rb +0 -32
- data/test/tilt_yajltemplate_test.rb +0 -101
- data/tilt.gemspec +0 -120
@@ -1,170 +0,0 @@
|
|
1
|
-
require 'contest'
|
2
|
-
require 'tilt'
|
3
|
-
|
4
|
-
class StringTemplateTest < Test::Unit::TestCase
|
5
|
-
test "registered for '.str' files" do
|
6
|
-
assert_equal Tilt::StringTemplate, Tilt['test.str']
|
7
|
-
end
|
8
|
-
|
9
|
-
test "loading and evaluating templates on #render" do
|
10
|
-
template = Tilt::StringTemplate.new { |t| "Hello World!" }
|
11
|
-
assert_equal "Hello World!", template.render
|
12
|
-
end
|
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
|
-
|
19
|
-
test "passing locals" do
|
20
|
-
template = Tilt::StringTemplate.new { 'Hey #{name}!' }
|
21
|
-
assert_equal "Hey Joe!", template.render(Object.new, :name => 'Joe')
|
22
|
-
end
|
23
|
-
|
24
|
-
test "evaluating in an object scope" do
|
25
|
-
template = Tilt::StringTemplate.new { 'Hey #{@name}!' }
|
26
|
-
scope = Object.new
|
27
|
-
scope.instance_variable_set :@name, 'Joe'
|
28
|
-
assert_equal "Hey Joe!", template.render(scope)
|
29
|
-
end
|
30
|
-
|
31
|
-
test "passing a block for yield" do
|
32
|
-
template = Tilt::StringTemplate.new { 'Hey #{yield}!' }
|
33
|
-
assert_equal "Hey Joe!", template.render { 'Joe' }
|
34
|
-
assert_equal "Hey Moe!", template.render { 'Moe' }
|
35
|
-
end
|
36
|
-
|
37
|
-
test "multiline templates" do
|
38
|
-
template = Tilt::StringTemplate.new { "Hello\nWorld!\n" }
|
39
|
-
assert_equal "Hello\nWorld!\n", template.render
|
40
|
-
end
|
41
|
-
|
42
|
-
test "backtrace file and line reporting without locals" do
|
43
|
-
data = File.read(__FILE__).split("\n__END__\n").last
|
44
|
-
fail unless data[0] == ?<
|
45
|
-
template = Tilt::StringTemplate.new('test.str', 11) { data }
|
46
|
-
begin
|
47
|
-
template.render
|
48
|
-
fail 'should have raised an exception'
|
49
|
-
rescue => boom
|
50
|
-
assert_kind_of NameError, boom
|
51
|
-
line = boom.backtrace.grep(/^test\.str:/).first
|
52
|
-
assert line, "Backtrace didn't contain test.str"
|
53
|
-
file, line, meth = line.split(":")
|
54
|
-
assert_equal '13', line
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
test "backtrace file and line reporting with locals" do
|
59
|
-
data = File.read(__FILE__).split("\n__END__\n").last
|
60
|
-
fail unless data[0] == ?<
|
61
|
-
template = Tilt::StringTemplate.new('test.str', 1) { data }
|
62
|
-
begin
|
63
|
-
template.render(nil, :name => 'Joe', :foo => 'bar')
|
64
|
-
fail 'should have raised an exception'
|
65
|
-
rescue => boom
|
66
|
-
assert_kind_of RuntimeError, boom
|
67
|
-
line = boom.backtrace.first
|
68
|
-
file, line, meth = line.split(":")
|
69
|
-
assert_equal 'test.str', file
|
70
|
-
assert_equal '6', line
|
71
|
-
end
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|
75
|
-
|
76
|
-
class CompiledStringTemplateTest < Test::Unit::TestCase
|
77
|
-
def teardown
|
78
|
-
GC.start
|
79
|
-
end
|
80
|
-
|
81
|
-
class Scope
|
82
|
-
end
|
83
|
-
|
84
|
-
test "compiling template source to a method" do
|
85
|
-
template = Tilt::StringTemplate.new { |t| "Hello World!" }
|
86
|
-
template.render(Scope.new)
|
87
|
-
method = template.send(:compiled_method, [])
|
88
|
-
assert_kind_of UnboundMethod, method
|
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
|
-
|
123
|
-
test "template with '}'" do
|
124
|
-
template = Tilt::StringTemplate.new { "Hello }" }
|
125
|
-
assert_equal "Hello }", template.render
|
126
|
-
end
|
127
|
-
|
128
|
-
test "backtrace file and line reporting without locals" do
|
129
|
-
data = File.read(__FILE__).split("\n__END__\n").last
|
130
|
-
fail unless data[0] == ?<
|
131
|
-
template = Tilt::StringTemplate.new('test.str', 11) { data }
|
132
|
-
begin
|
133
|
-
template.render(Scope.new)
|
134
|
-
fail 'should have raised an exception'
|
135
|
-
rescue => boom
|
136
|
-
assert_kind_of NameError, boom
|
137
|
-
line = boom.backtrace.first
|
138
|
-
line = boom.backtrace.grep(/^test\.str:/).first
|
139
|
-
assert line, "Backtrace didn't contain test.str"
|
140
|
-
file, line, meth = line.split(":")
|
141
|
-
assert_equal '13', line
|
142
|
-
end
|
143
|
-
end
|
144
|
-
|
145
|
-
test "backtrace file and line reporting with locals" do
|
146
|
-
data = File.read(__FILE__).split("\n__END__\n").last
|
147
|
-
fail unless data[0] == ?<
|
148
|
-
template = Tilt::StringTemplate.new('test.str') { data }
|
149
|
-
begin
|
150
|
-
template.render(Scope.new, :name => 'Joe', :foo => 'bar')
|
151
|
-
fail 'should have raised an exception'
|
152
|
-
rescue => boom
|
153
|
-
assert_kind_of RuntimeError, boom
|
154
|
-
line = boom.backtrace.first
|
155
|
-
file, line, meth = line.split(":")
|
156
|
-
assert_equal 'test.str', file
|
157
|
-
assert_equal '6', line
|
158
|
-
end
|
159
|
-
end
|
160
|
-
end
|
161
|
-
|
162
|
-
__END__
|
163
|
-
<html>
|
164
|
-
<body>
|
165
|
-
<h1>Hey #{name}!</h1>
|
166
|
-
|
167
|
-
|
168
|
-
<p>#{fail}</p>
|
169
|
-
</body>
|
170
|
-
</html>
|
data/test/tilt_template_test.rb
DELETED
@@ -1,323 +0,0 @@
|
|
1
|
-
# coding: utf-8
|
2
|
-
require 'contest'
|
3
|
-
require 'tilt'
|
4
|
-
require 'tempfile'
|
5
|
-
|
6
|
-
class TiltTemplateTest < Test::Unit::TestCase
|
7
|
-
|
8
|
-
class MockTemplate < Tilt::Template
|
9
|
-
def prepare
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
test "needs a file or block" do
|
14
|
-
assert_raise(ArgumentError) { Tilt::Template.new }
|
15
|
-
end
|
16
|
-
|
17
|
-
test "initializing with a file" do
|
18
|
-
inst = MockTemplate.new('foo.erb') {}
|
19
|
-
assert_equal 'foo.erb', inst.file
|
20
|
-
end
|
21
|
-
|
22
|
-
test "initializing with a file and line" do
|
23
|
-
inst = MockTemplate.new('foo.erb', 55) {}
|
24
|
-
assert_equal 'foo.erb', inst.file
|
25
|
-
assert_equal 55, inst.line
|
26
|
-
end
|
27
|
-
|
28
|
-
test "initializing with a tempfile" do
|
29
|
-
tempfile = Tempfile.new('tilt_template_test')
|
30
|
-
inst = MockTemplate.new(tempfile)
|
31
|
-
assert_equal File.basename(tempfile.path), inst.basename
|
32
|
-
end
|
33
|
-
|
34
|
-
class SillyHash < Hash
|
35
|
-
def path(arg)
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
test "initialize with hash that implements #path" do
|
40
|
-
options = SillyHash[:key => :value]
|
41
|
-
inst = MockTemplate.new(options) {}
|
42
|
-
assert_equal :value, inst.options[:key]
|
43
|
-
end
|
44
|
-
|
45
|
-
test "uses correct eval_file" do
|
46
|
-
inst = MockTemplate.new('foo.erb', 55) {}
|
47
|
-
assert_equal 'foo.erb', inst.eval_file
|
48
|
-
end
|
49
|
-
|
50
|
-
test "uses a default filename for #eval_file when no file provided" do
|
51
|
-
inst = MockTemplate.new { 'Hi' }
|
52
|
-
assert_not_nil inst.eval_file
|
53
|
-
assert !inst.eval_file.include?("\n")
|
54
|
-
end
|
55
|
-
|
56
|
-
test "calculating template's #basename" do
|
57
|
-
inst = MockTemplate.new('/tmp/templates/foo.html.erb') {}
|
58
|
-
assert_equal 'foo.html.erb', inst.basename
|
59
|
-
end
|
60
|
-
|
61
|
-
test "calculating the template's #name" do
|
62
|
-
inst = MockTemplate.new('/tmp/templates/foo.html.erb') {}
|
63
|
-
assert_equal 'foo', inst.name
|
64
|
-
end
|
65
|
-
|
66
|
-
test "initializing with a data loading block" do
|
67
|
-
MockTemplate.new { |template| "Hello World!" }
|
68
|
-
end
|
69
|
-
|
70
|
-
class InitializingMockTemplate < Tilt::Template
|
71
|
-
@@initialized_count = 0
|
72
|
-
def self.initialized_count
|
73
|
-
@@initialized_count
|
74
|
-
end
|
75
|
-
|
76
|
-
def initialize_engine
|
77
|
-
@@initialized_count += 1
|
78
|
-
end
|
79
|
-
|
80
|
-
def prepare
|
81
|
-
end
|
82
|
-
end
|
83
|
-
|
84
|
-
test "one-time template engine initialization" do
|
85
|
-
assert_nil InitializingMockTemplate.engine_initialized
|
86
|
-
assert_equal 0, InitializingMockTemplate.initialized_count
|
87
|
-
|
88
|
-
InitializingMockTemplate.new { "Hello World!" }
|
89
|
-
assert InitializingMockTemplate.engine_initialized
|
90
|
-
assert_equal 1, InitializingMockTemplate.initialized_count
|
91
|
-
|
92
|
-
InitializingMockTemplate.new { "Hello World!" }
|
93
|
-
assert_equal 1, InitializingMockTemplate.initialized_count
|
94
|
-
end
|
95
|
-
|
96
|
-
class PreparingMockTemplate < Tilt::Template
|
97
|
-
include Test::Unit::Assertions
|
98
|
-
def prepare
|
99
|
-
assert !data.nil?
|
100
|
-
@prepared = true
|
101
|
-
end
|
102
|
-
def prepared? ; @prepared ; end
|
103
|
-
end
|
104
|
-
|
105
|
-
test "raises NotImplementedError when #prepare not defined" do
|
106
|
-
assert_raise(NotImplementedError) { Tilt::Template.new { |template| "Hello World!" } }
|
107
|
-
end
|
108
|
-
|
109
|
-
test "raises NotImplementedError when #evaluate or #template_source not defined" do
|
110
|
-
inst = PreparingMockTemplate.new { |t| "Hello World!" }
|
111
|
-
assert_raise(NotImplementedError) { inst.render }
|
112
|
-
assert inst.prepared?
|
113
|
-
end
|
114
|
-
|
115
|
-
class SimpleMockTemplate < PreparingMockTemplate
|
116
|
-
include Test::Unit::Assertions
|
117
|
-
def evaluate(scope, locals, &block)
|
118
|
-
assert prepared?
|
119
|
-
assert !scope.nil?
|
120
|
-
assert !locals.nil?
|
121
|
-
"<em>#{@data}</em>"
|
122
|
-
end
|
123
|
-
end
|
124
|
-
|
125
|
-
test "prepares and evaluates the template on #render" do
|
126
|
-
inst = SimpleMockTemplate.new { |t| "Hello World!" }
|
127
|
-
assert_equal "<em>Hello World!</em>", inst.render
|
128
|
-
assert inst.prepared?
|
129
|
-
end
|
130
|
-
|
131
|
-
class SourceGeneratingMockTemplate < PreparingMockTemplate
|
132
|
-
def precompiled_template(locals)
|
133
|
-
"foo = [] ; foo << %Q{#{data}} ; foo.join"
|
134
|
-
end
|
135
|
-
end
|
136
|
-
|
137
|
-
test "template_source with locals" do
|
138
|
-
inst = SourceGeneratingMockTemplate.new { |t| 'Hey #{name}!' }
|
139
|
-
assert_equal "Hey Joe!", inst.render(Object.new, :name => 'Joe')
|
140
|
-
assert inst.prepared?
|
141
|
-
end
|
142
|
-
|
143
|
-
test "template_source with locals of strings" do
|
144
|
-
inst = SourceGeneratingMockTemplate.new { |t| 'Hey #{name}!' }
|
145
|
-
assert_equal "Hey Joe!", inst.render(Object.new, 'name' => 'Joe')
|
146
|
-
assert inst.prepared?
|
147
|
-
end
|
148
|
-
|
149
|
-
test "template_source with locals having non-variable keys raises error" do
|
150
|
-
inst = SourceGeneratingMockTemplate.new { |t| '1 + 2 = #{_answer}' }
|
151
|
-
err = assert_raise(RuntimeError) { inst.render(Object.new, 'ANSWER' => 3) }
|
152
|
-
assert_equal "invalid locals key: \"ANSWER\" (keys must be variable names)", err.message
|
153
|
-
assert_equal "1 + 2 = 3", inst.render(Object.new, '_answer' => 3)
|
154
|
-
end
|
155
|
-
|
156
|
-
class CustomGeneratingMockTemplate < PreparingMockTemplate
|
157
|
-
def precompiled_template(locals)
|
158
|
-
data
|
159
|
-
end
|
160
|
-
|
161
|
-
def precompiled_preamble(locals)
|
162
|
-
options.fetch(:preamble)
|
163
|
-
end
|
164
|
-
|
165
|
-
def precompiled_postamble(locals)
|
166
|
-
options.fetch(:postamble)
|
167
|
-
end
|
168
|
-
end
|
169
|
-
|
170
|
-
test "supports pre/postamble" do
|
171
|
-
inst = CustomGeneratingMockTemplate.new(
|
172
|
-
:preamble => 'buf = []',
|
173
|
-
:postamble => 'buf.join'
|
174
|
-
) { 'buf << 1' }
|
175
|
-
|
176
|
-
assert_equal "1", inst.render
|
177
|
-
end
|
178
|
-
|
179
|
-
# Special-case for Haml
|
180
|
-
# https://github.com/rtomayko/tilt/issues/193
|
181
|
-
test "supports Array pre/postambles" do
|
182
|
-
inst = CustomGeneratingMockTemplate.new(
|
183
|
-
:preamble => ['buf = ', '[]'],
|
184
|
-
:postamble => ['buf.', 'join']
|
185
|
-
) { 'buf << 1' }
|
186
|
-
|
187
|
-
# TODO: Use assert_output when we swicth to MiniTest
|
188
|
-
warns = <<-EOF
|
189
|
-
precompiled_preamble should return String (not Array)
|
190
|
-
precompiled_postamble should return String (not Array)
|
191
|
-
EOF
|
192
|
-
|
193
|
-
begin
|
194
|
-
require 'stringio'
|
195
|
-
$stderr = StringIO.new
|
196
|
-
assert_equal "1", inst.render
|
197
|
-
assert_equal warns, $stderr.string
|
198
|
-
ensure
|
199
|
-
$stderr = STDERR
|
200
|
-
end
|
201
|
-
end
|
202
|
-
|
203
|
-
class Person
|
204
|
-
CONSTANT = "Bob"
|
205
|
-
|
206
|
-
attr_accessor :name
|
207
|
-
def initialize(name)
|
208
|
-
@name = name
|
209
|
-
end
|
210
|
-
end
|
211
|
-
|
212
|
-
test "template_source with an object scope" do
|
213
|
-
inst = SourceGeneratingMockTemplate.new { |t| 'Hey #{@name}!' }
|
214
|
-
scope = Person.new('Joe')
|
215
|
-
assert_equal "Hey Joe!", inst.render(scope)
|
216
|
-
end
|
217
|
-
|
218
|
-
test "template_source with a block for yield" do
|
219
|
-
inst = SourceGeneratingMockTemplate.new { |t| 'Hey #{yield}!' }
|
220
|
-
assert_equal "Hey Joe!", inst.render(Object.new){ 'Joe' }
|
221
|
-
end
|
222
|
-
|
223
|
-
test "template which accesses a constant" do
|
224
|
-
inst = SourceGeneratingMockTemplate.new { |t| 'Hey #{CONSTANT}!' }
|
225
|
-
assert_equal "Hey Bob!", inst.render(Person.new("Joe"))
|
226
|
-
end
|
227
|
-
|
228
|
-
##
|
229
|
-
# Encodings
|
230
|
-
|
231
|
-
class DynamicMockTemplate < MockTemplate
|
232
|
-
def precompiled_template(locals)
|
233
|
-
options[:code]
|
234
|
-
end
|
235
|
-
end
|
236
|
-
|
237
|
-
class UTF8Template < MockTemplate
|
238
|
-
def default_encoding
|
239
|
-
Encoding::UTF_8
|
240
|
-
end
|
241
|
-
end
|
242
|
-
|
243
|
-
if ''.respond_to?(:encoding)
|
244
|
-
original_encoding = Encoding.default_external
|
245
|
-
|
246
|
-
setup do
|
247
|
-
@file = Tempfile.open('template')
|
248
|
-
@file.puts "stuff"
|
249
|
-
@file.close
|
250
|
-
@template = @file.path
|
251
|
-
end
|
252
|
-
|
253
|
-
teardown do
|
254
|
-
Encoding.default_external = original_encoding
|
255
|
-
Encoding.default_internal = nil
|
256
|
-
@file.delete
|
257
|
-
end
|
258
|
-
|
259
|
-
test "reading from file assumes default external encoding" do
|
260
|
-
Encoding.default_external = 'Big5'
|
261
|
-
inst = MockTemplate.new(@template)
|
262
|
-
assert_equal 'Big5', inst.data.encoding.to_s
|
263
|
-
end
|
264
|
-
|
265
|
-
test "reading from file with a :default_encoding overrides default external" do
|
266
|
-
Encoding.default_external = 'Big5'
|
267
|
-
inst = MockTemplate.new(@template, :default_encoding => 'GBK')
|
268
|
-
assert_equal 'GBK', inst.data.encoding.to_s
|
269
|
-
end
|
270
|
-
|
271
|
-
test "reading from file with default_internal set does no transcoding" do
|
272
|
-
Encoding.default_internal = 'utf-8'
|
273
|
-
Encoding.default_external = 'Big5'
|
274
|
-
inst = MockTemplate.new(@template)
|
275
|
-
assert_equal 'Big5', inst.data.encoding.to_s
|
276
|
-
end
|
277
|
-
|
278
|
-
test "using provided template data verbatim when given as string" do
|
279
|
-
Encoding.default_internal = 'Big5'
|
280
|
-
inst = MockTemplate.new(@template) { "blah".force_encoding('GBK') }
|
281
|
-
assert_equal 'GBK', inst.data.encoding.to_s
|
282
|
-
end
|
283
|
-
|
284
|
-
test "uses the template from the generated source code" do
|
285
|
-
tmpl = "ふが"
|
286
|
-
code = tmpl.inspect.encode('Shift_JIS')
|
287
|
-
inst = DynamicMockTemplate.new(:code => code) { '' }
|
288
|
-
res = inst.render
|
289
|
-
assert_equal 'Shift_JIS', res.encoding.to_s
|
290
|
-
assert_equal tmpl, res.encode(tmpl.encoding)
|
291
|
-
end
|
292
|
-
|
293
|
-
test "uses the magic comment from the generated source code" do
|
294
|
-
tmpl = "ふが"
|
295
|
-
code = ("# coding: Shift_JIS\n" + tmpl.inspect).encode('Shift_JIS')
|
296
|
-
# Set it to an incorrect encoding
|
297
|
-
code.force_encoding('UTF-8')
|
298
|
-
|
299
|
-
inst = DynamicMockTemplate.new(:code => code) { '' }
|
300
|
-
res = inst.render
|
301
|
-
assert_equal 'Shift_JIS', res.encoding.to_s
|
302
|
-
assert_equal tmpl, res.encode(tmpl.encoding)
|
303
|
-
end
|
304
|
-
|
305
|
-
test "uses #default_encoding instead of default_external" do
|
306
|
-
Encoding.default_external = 'Big5'
|
307
|
-
inst = UTF8Template.new(@template)
|
308
|
-
assert_equal 'UTF-8', inst.data.encoding.to_s
|
309
|
-
end
|
310
|
-
|
311
|
-
test "uses #default_encoding instead of current encoding" do
|
312
|
-
tmpl = "".force_encoding('Big5')
|
313
|
-
inst = UTF8Template.new(@template) { tmpl }
|
314
|
-
assert_equal 'UTF-8', inst.data.encoding.to_s
|
315
|
-
end
|
316
|
-
|
317
|
-
test "raises error if the encoding is not valid" do
|
318
|
-
assert_raises(Encoding::InvalidByteSequenceError) do
|
319
|
-
UTF8Template.new(@template) { "\xe4" }
|
320
|
-
end
|
321
|
-
end
|
322
|
-
end
|
323
|
-
end
|
data/test/tilt_test.rb
DELETED
@@ -1,65 +0,0 @@
|
|
1
|
-
require 'contest'
|
2
|
-
require 'tilt'
|
3
|
-
|
4
|
-
class TiltTest < Test::Unit::TestCase
|
5
|
-
class MockTemplate
|
6
|
-
attr_reader :args, :block
|
7
|
-
def initialize(*args, &block)
|
8
|
-
@args = args
|
9
|
-
@block = block
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
test "registering template implementation classes by file extension" do
|
14
|
-
Tilt.register('mock', MockTemplate)
|
15
|
-
end
|
16
|
-
|
17
|
-
test "an extension is registered if explicit handle is found" do
|
18
|
-
Tilt.register('mock', MockTemplate)
|
19
|
-
assert Tilt.registered?('mock')
|
20
|
-
end
|
21
|
-
|
22
|
-
test "registering template classes by symbol file extension" do
|
23
|
-
Tilt.register(:mock, MockTemplate)
|
24
|
-
end
|
25
|
-
|
26
|
-
test "looking up template classes by exact file extension" do
|
27
|
-
Tilt.register('mock', MockTemplate)
|
28
|
-
impl = Tilt['mock']
|
29
|
-
assert_equal MockTemplate, impl
|
30
|
-
end
|
31
|
-
|
32
|
-
test "looking up template classes by implicit file extension" do
|
33
|
-
Tilt.register('mock', MockTemplate)
|
34
|
-
impl = Tilt['.mock']
|
35
|
-
assert_equal MockTemplate, impl
|
36
|
-
end
|
37
|
-
|
38
|
-
test "looking up template classes with multiple file extensions" do
|
39
|
-
Tilt.register('mock', MockTemplate)
|
40
|
-
impl = Tilt['index.html.mock']
|
41
|
-
assert_equal MockTemplate, impl
|
42
|
-
end
|
43
|
-
|
44
|
-
test "looking up template classes by file name" do
|
45
|
-
Tilt.register('mock', MockTemplate)
|
46
|
-
impl = Tilt['templates/test.mock']
|
47
|
-
assert_equal MockTemplate, impl
|
48
|
-
end
|
49
|
-
|
50
|
-
test "looking up non-existant template class" do
|
51
|
-
assert_nil Tilt['none']
|
52
|
-
end
|
53
|
-
|
54
|
-
test "accessing template class mappings at Tilt::mappings" do
|
55
|
-
assert Tilt.respond_to?(:mappings)
|
56
|
-
assert Tilt.mappings.respond_to?(:[])
|
57
|
-
end
|
58
|
-
|
59
|
-
test "creating new template instance with a filename" do
|
60
|
-
Tilt.register('mock', MockTemplate)
|
61
|
-
template = Tilt.new('foo.mock', 1, :key => 'val') { 'Hello World!' }
|
62
|
-
assert_equal ['foo.mock', 1, {:key => 'val'}], template.args
|
63
|
-
assert_equal 'Hello World!', template.block.call
|
64
|
-
end
|
65
|
-
end
|
@@ -1,32 +0,0 @@
|
|
1
|
-
require 'contest'
|
2
|
-
require 'tilt'
|
3
|
-
|
4
|
-
begin
|
5
|
-
require 'wikicloth'
|
6
|
-
|
7
|
-
class WikiClothTemplateTest < Test::Unit::TestCase
|
8
|
-
test "is registered for '.mediawiki' files" do
|
9
|
-
assert_equal Tilt::WikiClothTemplate, Tilt['test.mediawiki']
|
10
|
-
end
|
11
|
-
|
12
|
-
test "is registered for '.mw' files" do
|
13
|
-
assert_equal Tilt::WikiClothTemplate, Tilt['test.mw']
|
14
|
-
end
|
15
|
-
|
16
|
-
test "is registered for '.wiki' files" do
|
17
|
-
assert_equal Tilt::WikiClothTemplate, Tilt['test.wiki']
|
18
|
-
end
|
19
|
-
|
20
|
-
test "compiles and evaluates the template on #render" do
|
21
|
-
template = Tilt::WikiClothTemplate.new { |t| "= Hello World! =" }
|
22
|
-
assert_match /<h1>.*Hello World!.*<\/h1>/, template.render
|
23
|
-
end
|
24
|
-
|
25
|
-
test "can be rendered more than once" do
|
26
|
-
template = Tilt::WikiClothTemplate.new { |t| "= Hello World! =" }
|
27
|
-
3.times { assert_match /<h1>.*Hello World!.*<\/h1>/, template.render }
|
28
|
-
end
|
29
|
-
end
|
30
|
-
rescue LoadError => boom
|
31
|
-
warn "Tilt::WikiClothTemplate (disabled)"
|
32
|
-
end
|
@@ -1,101 +0,0 @@
|
|
1
|
-
require 'contest'
|
2
|
-
require 'tilt'
|
3
|
-
|
4
|
-
begin
|
5
|
-
require 'yajl'
|
6
|
-
|
7
|
-
class YajlTemplateTest < Test::Unit::TestCase
|
8
|
-
test "is registered for '.yajl' files" do
|
9
|
-
assert_equal Tilt::YajlTemplate, Tilt['test.yajl']
|
10
|
-
end
|
11
|
-
|
12
|
-
test "compiles and evaluates the template on #render" do
|
13
|
-
template = Tilt::YajlTemplate.new { "json = { :integer => 3, :string => 'hello' }" }
|
14
|
-
output = template.render
|
15
|
-
result = Yajl::Parser.parse(output)
|
16
|
-
expect = {"integer" => 3,"string" => "hello"}
|
17
|
-
assert_equal expect, result
|
18
|
-
end
|
19
|
-
|
20
|
-
test "can be rendered more than once" do
|
21
|
-
template = Tilt::YajlTemplate.new { "json = { :integer => 3, :string => 'hello' }" }
|
22
|
-
expect = {"integer" => 3,"string" => "hello"}
|
23
|
-
3.times do
|
24
|
-
output = template.render
|
25
|
-
result = Yajl::Parser.parse(output)
|
26
|
-
assert_equal expect, result
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
test "evaluating ruby code" do
|
31
|
-
template = Tilt::YajlTemplate.new { "json = { :integer => (3 * 2) }" }
|
32
|
-
assert_equal '{"integer":6}', template.render
|
33
|
-
end
|
34
|
-
|
35
|
-
test "evaluating in an object scope" do
|
36
|
-
template = Tilt::YajlTemplate.new { "json = { :string => 'Hey ' + @name + '!' }" }
|
37
|
-
scope = Object.new
|
38
|
-
scope.instance_variable_set :@name, 'Joe'
|
39
|
-
assert_equal '{"string":"Hey Joe!"}', template.render(scope)
|
40
|
-
end
|
41
|
-
|
42
|
-
test "passing locals" do
|
43
|
-
template = Tilt::YajlTemplate.new { "json = { :string => 'Hey ' + name + '!' }" }
|
44
|
-
assert_equal '{"string":"Hey Joe!"}', template.render(Object.new, :name => 'Joe')
|
45
|
-
end
|
46
|
-
|
47
|
-
test "passing a block for yield" do
|
48
|
-
template = Tilt::YajlTemplate.new { "json = { :string => 'Hey ' + yield + '!' }" }
|
49
|
-
assert_equal '{"string":"Hey Joe!"}', template.render { 'Joe' }
|
50
|
-
assert_equal '{"string":"Hey Moe!"}', template.render { 'Moe' }
|
51
|
-
end
|
52
|
-
|
53
|
-
test "template multiline" do
|
54
|
-
template = Tilt::YajlTemplate.new { %Q{
|
55
|
-
json = {
|
56
|
-
:string => "hello"
|
57
|
-
}
|
58
|
-
} }
|
59
|
-
assert_equal '{"string":"hello"}', template.render
|
60
|
-
end
|
61
|
-
|
62
|
-
test "template can reuse existing json buffer" do
|
63
|
-
template = Tilt::YajlTemplate.new { "json.merge! :string => 'hello'" }
|
64
|
-
assert_equal '{"string":"hello"}', template.render
|
65
|
-
end
|
66
|
-
|
67
|
-
test "template can end with any statement" do
|
68
|
-
template = Tilt::YajlTemplate.new { %Q{
|
69
|
-
json = {
|
70
|
-
:string => "hello"
|
71
|
-
}
|
72
|
-
four = 2 * 2
|
73
|
-
json[:integer] = four
|
74
|
-
nil
|
75
|
-
} }
|
76
|
-
result = template.render
|
77
|
-
assert( (result == '{"string":"hello","integer":4}') || (result == '{"integer":4,"string":"hello"}') )
|
78
|
-
end
|
79
|
-
|
80
|
-
test "option callback" do
|
81
|
-
options = { :callback => 'foo' }
|
82
|
-
template = Tilt::YajlTemplate.new(nil, options) { "json = { :string => 'hello' }" }
|
83
|
-
assert_equal 'foo({"string":"hello"});', template.render
|
84
|
-
end
|
85
|
-
|
86
|
-
test "option variable" do
|
87
|
-
options = { :variable => 'output' }
|
88
|
-
template = Tilt::YajlTemplate.new(nil, options) { "json = { :string => 'hello' }" }
|
89
|
-
assert_equal 'var output = {"string":"hello"};', template.render
|
90
|
-
end
|
91
|
-
|
92
|
-
test "option callback and variable" do
|
93
|
-
options = { :callback => 'foo', :variable => 'output' }
|
94
|
-
template = Tilt::YajlTemplate.new(nil, options) { "json = { :string => 'hello' }" }
|
95
|
-
assert_equal 'var output = {"string":"hello"}; foo(output);', template.render
|
96
|
-
end
|
97
|
-
|
98
|
-
end
|
99
|
-
rescue LoadError
|
100
|
-
warn "Tilt::YajlTemplateTest (disabled)"
|
101
|
-
end
|