Add tests about compiled templates
This commit is contained in:
parent
bdc5148c42
commit
844b2703e2
@ -37,7 +37,6 @@ module RablFastJson
|
|||||||
return unless block_given?
|
return unless block_given?
|
||||||
name = :"_glue#{@glue_count}"
|
name = :"_glue#{@glue_count}"
|
||||||
@glue_count += 1
|
@glue_count += 1
|
||||||
Rails.logger.warn "[RABL] glue called with data = #{data.inspect}"
|
|
||||||
_compile_sub_template(name, data, &block)
|
_compile_sub_template(name, data, &block)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -19,12 +19,11 @@ module RablFastJson
|
|||||||
end
|
end
|
||||||
|
|
||||||
def render_resource(data = nil, source = nil)
|
def render_resource(data = nil, source = nil)
|
||||||
output = {}
|
|
||||||
|
|
||||||
data ||= @object
|
data ||= @object
|
||||||
source ||= @source
|
source ||= @source
|
||||||
|
|
||||||
source.each_pair { |key, value|
|
source.inject({}) { |output, current|
|
||||||
|
key, value = current
|
||||||
out = case value
|
out = case value
|
||||||
when Symbol
|
when Symbol
|
||||||
data.send(value) # attributes
|
data.send(value) # attributes
|
||||||
@ -37,22 +36,19 @@ module RablFastJson
|
|||||||
value.each_pair { |k, v|
|
value.each_pair { |k, v|
|
||||||
output[k] = object.send(v)
|
output[k] = object.send(v)
|
||||||
}
|
}
|
||||||
|
next output
|
||||||
next
|
|
||||||
else # child
|
else # child
|
||||||
object.respond_to?(:each) ? render_collection(object, value) : render_resource(object, value)
|
object.respond_to?(:each) ? render_collection(object, value) : render_resource(object, value)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
output[key] = out
|
output[key] = out
|
||||||
|
output
|
||||||
}
|
}
|
||||||
output
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def render_collection(collection = nil, source = nil)
|
def render_collection(collection = nil, source = nil)
|
||||||
output = []
|
|
||||||
collection ||= @object
|
collection ||= @object
|
||||||
collection.each { |o| output << render_resource(o, source) }
|
collection.inject([]) { |output, o| output << render_resource(o, source) }
|
||||||
output
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
@ -4,9 +4,36 @@ class TestCompiledTemplate < ActiveSupport::TestCase
|
|||||||
|
|
||||||
setup do
|
setup do
|
||||||
@context = Context.new
|
@context = Context.new
|
||||||
@user = User.new
|
@data = User.new(1, 'foobar', 'male')
|
||||||
@context.set_assign('user', @user)
|
@data.stub(:respond_to?).with(:each).and_return(false)
|
||||||
|
@context.stub(:instance_variable_get).with(:@data).and_return(@data)
|
||||||
@template = RablFastJson::CompiledTemplate.new
|
@template = RablFastJson::CompiledTemplate.new
|
||||||
@template.context = @context
|
@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
|
||||||
end
|
end
|
@ -49,7 +49,7 @@ class CompilerTest < ActiveSupport::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
test "glue is compiled as a child but with anonymous name" do
|
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)
|
assert_equal({ :_glue0 => { :_data => :@user, :name => :name } }, t.source)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -31,6 +31,4 @@ class Context
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class User
|
User = Struct.new(:id, :name, :sex)
|
||||||
attr_accessor :name
|
|
||||||
end
|
|
Loading…
Reference in New Issue
Block a user