1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
require_relative 'test_helper'
checked_describe 'tilt/builder' do
it "registered for '.builder' files" do
assert_equal Tilt::BuilderTemplate, Tilt['test.builder']
assert_equal Tilt::BuilderTemplate, Tilt['test.xml.builder']
end
it "preparing and evaluating the template on #render" do
template = Tilt::BuilderTemplate.new { |t| "xml.em 'Hello World!'" }
3.times { assert_equal "<em>Hello World!</em>\n", template.render }
end
it "passing locals" do
template = Tilt::BuilderTemplate.new { "xml.em('Hey ' + name + '!')" }
assert_equal "<em>Hey Joe!</em>\n", template.render(Object.new, :name => 'Joe')
end
it "passing :xml in locals" do
template = Tilt::BuilderTemplate.new { "xml.div { xml.em('Hey') }" }
assert_equal "<div><em>Hey</em></div>", template.render(nil, xml: ::Builder::XmlMarkup.new(:indent => 0))
end
it "evaluating in an object scope" do
template = Tilt::BuilderTemplate.new { "xml.em('Hey ' + @name + '!')" }
scope = Object.new
scope.instance_variable_set :@name, 'Joe'
assert_equal "<em>Hey Joe!</em>\n", template.render(scope)
end
it "passing a block for yield" do
template = Tilt::BuilderTemplate.new { "xml.em('Hey ' + yield + '!')" }
3.times { assert_equal "<em>Hey Joe!</em>\n", template.render { 'Joe' }}
end
it "block style templates" do
template =
Tilt::BuilderTemplate.new do |t|
lambda { |xml| xml.em('Hey Joe!') }
end
assert_equal "<em>Hey Joe!</em>\n", template.render
end
it "options can be overridden" do
template = Tilt::BuilderTemplate.new(:indent => 0) { "xml.div { xml.em('Hey') }" }
assert_equal "<div><em>Hey</em></div>", template.render
end
it "can re-use locals for multiple calls" do
locals = { :name => "world" }
template = Tilt::BuilderTemplate.new(:indent => 0) { "xml.em name" }
3.times do
assert_equal "<em>world</em>", template.render(self, locals)
end
end
it "allows nesting raw XML" do
subtemplate = Tilt::BuilderTemplate.new { "xml.em 'Hello World!'" }
template = Tilt::BuilderTemplate.new { "xml.strong { xml << yield }" }
3.times do
options = { :xml => Builder::XmlMarkup.new }
assert_equal "<strong>\n<em>Hello World!</em>\n</strong>\n",
template.render(options) { subtemplate.render(options) }
end
end
end
|