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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
require_relative 'test_helper'
checked_describe 'tilt/yajl' do
it "is registered for '.yajl' files" do
assert_equal Tilt::YajlTemplate, Tilt['test.yajl']
end
it "compiles and evaluates the template on #render" do
template = Tilt::YajlTemplate.new { "json = { :integer => 3, :string => 'hello' }" }
expect = {"integer" => 3,"string" => "hello"}
3.times do
output = template.render
result = Yajl::Parser.parse(output)
assert_equal expect, result
end
end
it "evaluating ruby code" do
template = Tilt::YajlTemplate.new { "json = { :integer => (3 * 2) }" }
assert_equal '{"integer":6}', template.render
end
it "evaluating in an object scope" do
template = Tilt::YajlTemplate.new { "json = { :string => 'Hey ' + @name + '!' }" }
scope = Object.new
scope.instance_variable_set :@name, 'Joe'
assert_equal '{"string":"Hey Joe!"}', template.render(scope)
end
it "passing locals with :json option" do
template = Tilt::YajlTemplate.new { "json[:string] = 'Hey ' + name + '!'" }
assert_equal '{"a":1,"string":"Hey Joe!"}', template.render(Object.new, :name => 'Joe', :json => {'a'=>1})
end
it "passing locals" do
template = Tilt::YajlTemplate.new { "json = { :string => 'Hey ' + name + '!' }" }
assert_equal '{"string":"Hey Joe!"}', template.render(Object.new, :name => 'Joe')
end
it "passing a block for yield" do
template = Tilt::YajlTemplate.new { "json = { :string => 'Hey ' + yield + '!' }" }
assert_equal '{"string":"Hey Joe!"}', template.render { 'Joe' }
assert_equal '{"string":"Hey Moe!"}', template.render { 'Moe' }
end
it "template multiline" do
template = Tilt::YajlTemplate.new { %Q{
json = {
:string => "hello"
}
} }
assert_equal '{"string":"hello"}', template.render
end
it "template can reuse existing json buffer" do
template = Tilt::YajlTemplate.new { "json.merge! :string => 'hello'" }
assert_equal '{"string":"hello"}', template.render
end
it "template can end with any statement" do
template = Tilt::YajlTemplate.new { %Q{
json = {
:string => "hello"
}
four = 2 * 2
json[:integer] = four
a = a = nil
} }
result = template.render
assert( (result == '{"string":"hello","integer":4}') || (result == '{"integer":4,"string":"hello"}') )
end
it "option callback" do
options = { :callback => 'foo' }
template = Tilt::YajlTemplate.new(nil, options) { "json = { :string => 'hello' }" }
assert_equal 'foo({"string":"hello"});', template.render
end
it "option variable" do
options = { :variable => 'output' }
template = Tilt::YajlTemplate.new(nil, options) { "json = { :string => 'hello' }" }
assert_equal 'var output = {"string":"hello"};', template.render
end
it "option callback and variable" do
options = { :callback => 'foo', :variable => 'output' }
template = Tilt::YajlTemplate.new(nil, options) { "json = { :string => 'hello' }" }
assert_equal 'var output = {"string":"hello"}; foo(output);', template.render
end
end
|