Add tests about compiled templates

This commit is contained in:
ccocchi 2012-02-27 17:25:06 +01:00
parent bdc5148c42
commit 844b2703e2
5 changed files with 36 additions and 16 deletions

View File

@ -37,7 +37,6 @@ module RablFastJson
return unless block_given?
name = :"_glue#{@glue_count}"
@glue_count += 1
Rails.logger.warn "[RABL] glue called with data = #{data.inspect}"
_compile_sub_template(name, data, &block)
end

View File

@ -19,12 +19,11 @@ module RablFastJson
end
def render_resource(data = nil, source = nil)
output = {}
data ||= @object
source ||= @source
source.each_pair { |key, value|
source.inject({}) { |output, current|
key, value = current
out = case value
when Symbol
data.send(value) # attributes
@ -37,22 +36,19 @@ module RablFastJson
value.each_pair { |k, v|
output[k] = object.send(v)
}
next
next output
else # child
object.respond_to?(:each) ? render_collection(object, value) : render_resource(object, value)
end
end
output[key] = out
}
output
}
end
def render_collection(collection = nil, source = nil)
output = []
collection ||= @object
collection.each { |o| output << render_resource(o, source) }
output
collection.inject([]) { |output, o| output << render_resource(o, source) }
end
end
end

View File

@ -4,9 +4,36 @@ class TestCompiledTemplate < ActiveSupport::TestCase
setup do
@context = Context.new
@user = User.new
@context.set_assign('user', @user)
@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)
@template = RablFastJson::CompiledTemplate.new
@template.context = @context
@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)
end
end

View File

@ -49,7 +49,7 @@ class CompilerTest < ActiveSupport::TestCase
end
test "glue is compiled as a child but with anonymous name" do
t = @compiler.compile_source(%{ glue :@user do attribute :name end })
t = @compiler.compile_source(%{ glue(:@user) do attribute :name end })
assert_equal({ :_glue0 => { :_data => :@user, :name => :name } }, t.source)
end

View File

@ -31,6 +31,4 @@ class Context
end
end
class User
attr_accessor :name
end
User = Struct.new(:id, :name, :sex)