2012-02-22 18:14:00 +00:00
|
|
|
require 'test_helper'
|
|
|
|
|
2012-02-27 13:49:34 +00:00
|
|
|
class TestCompiledTemplate < ActiveSupport::TestCase
|
2012-02-22 18:14:00 +00:00
|
|
|
|
2012-02-27 13:49:34 +00:00
|
|
|
setup do
|
|
|
|
@context = Context.new
|
2012-02-27 16:25:06 +00:00
|
|
|
@data = User.new(1, 'foobar', 'male')
|
|
|
|
@data.stub(:respond_to?).with(:each).and_return(false)
|
|
|
|
@context.stub(:instance_variable_get).with(:@data).and_return(@data)
|
2012-03-15 14:12:52 +00:00
|
|
|
@context.stub(:instance_variable_get).with(:@_assigns).and_return({})
|
2012-02-27 13:49:34 +00:00
|
|
|
@template = RablFastJson::CompiledTemplate.new
|
|
|
|
@template.context = @context
|
2012-02-27 16:25:06 +00:00
|
|
|
@template.data = :@data
|
|
|
|
end
|
|
|
|
|
|
|
|
test "render single object attributes" do
|
|
|
|
@template.source = { :id => :id, :name => :name }
|
|
|
|
assert_equal({ :id => 1, :name => 'foobar'}, @template.render)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "render object as a child" do
|
|
|
|
@template.source = { :author => { :_data => :@data, :name => :name } }
|
|
|
|
assert_equal({ :author => { :name => 'foobar' } }, @template.render)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "render glued attributes from single object" do
|
|
|
|
@template.source = { :_glue0 => { :_data => :@data, :name => :name } }
|
|
|
|
assert_equal({ :name => 'foobar' }, @template.render)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "render collection with attributes" do
|
|
|
|
@data = [User.new(1, 'foo', 'male'), User.new(2, 'bar', 'female')]
|
|
|
|
@context.stub(:instance_variable_get).with(:@data).and_return(@data)
|
|
|
|
@template.source = { :uid => :id, :name => :name, :gender => :sex }
|
|
|
|
assert_equal([
|
|
|
|
{ :uid => 1, :name => 'foo', :gender => 'male'},
|
|
|
|
{ :uid => 2, :name => 'bar', :gender => 'female'}
|
|
|
|
], @template.render)
|
2012-02-27 13:49:34 +00:00
|
|
|
end
|
2012-03-05 14:33:25 +00:00
|
|
|
|
|
|
|
test "render object with node property" do
|
|
|
|
proc = lambda { |object| object.sex }
|
|
|
|
@template.source = { :sex => proc }
|
|
|
|
assert_equal({ :sex => 'male' }, @template.render)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "render obejct with conditionnal node property" do
|
|
|
|
condition = lambda { |u| u.name.present? }
|
|
|
|
proc = lambda { |object| object.name }
|
|
|
|
@template.source = { :name => [condition, proc] }
|
|
|
|
assert_equal({ :name => 'foobar' }, @template.render)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "render node property with false condition" do
|
|
|
|
condition = lambda { |u| false }
|
|
|
|
proc = lambda { |object| object.name }
|
|
|
|
@template.source = { :name => [condition, proc] }
|
|
|
|
assert_equal({}, @template.render)
|
|
|
|
end
|
2012-03-29 08:58:20 +00:00
|
|
|
|
|
|
|
test "partial should be evaluated at rendering time" do
|
|
|
|
# Set assigns
|
|
|
|
@context.stub(:instance_variable_get).with(:@_assigns).and_return({'user' => @data})
|
|
|
|
@data.stub(:respond_to?).with(:empty?).and_return(false)
|
|
|
|
|
|
|
|
# Stub Library#get
|
|
|
|
t = RablFastJson::CompiledTemplate.new
|
|
|
|
t.source, t.context = { :name => :name }, @context
|
|
|
|
RablFastJson::Library.reset_instance
|
|
|
|
RablFastJson::Library.instance.should_receive(:get).with('users/base').and_return(t)
|
|
|
|
|
|
|
|
@template.data = false
|
|
|
|
@template.source = { :user => ->(s) { partial('users/base', :object => @user) } }
|
|
|
|
|
|
|
|
assert_equal({ :user => { :name => 'foobar' } }, @template.render)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "partial with nil values should raise an error" do
|
|
|
|
@template.data = false
|
|
|
|
@template.source = { :user => ->(s) { partial('users/base') } }
|
|
|
|
|
|
|
|
assert_raises(RuntimeError) { @template.render }
|
|
|
|
end
|
|
|
|
|
|
|
|
test "partial with empty values should not raise an error" do
|
|
|
|
@template.data = false
|
|
|
|
@template.source = { :users => ->(s) { partial('users/base', :object => []) } }
|
|
|
|
|
|
|
|
assert_equal({ :users => [] }, @template.render)
|
|
|
|
end
|
2012-02-22 18:14:00 +00:00
|
|
|
end
|