2012-02-22 18:14:00 +00:00
|
|
|
require 'test_helper'
|
|
|
|
|
2012-04-15 16:17:49 +00:00
|
|
|
class TestJsonRenderer < ActiveSupport::TestCase
|
2012-02-22 18:14:00 +00:00
|
|
|
|
2012-02-27 13:49:34 +00:00
|
|
|
setup do
|
2012-02-27 16:25:06 +00:00
|
|
|
@data = User.new(1, 'foobar', 'male')
|
|
|
|
@data.stub(:respond_to?).with(:each).and_return(false)
|
2012-04-15 16:17:49 +00:00
|
|
|
|
|
|
|
@context = Context.new
|
2012-02-27 16:25:06 +00:00
|
|
|
@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-04-15 16:17:49 +00:00
|
|
|
|
2012-04-20 14:28:34 +00:00
|
|
|
@template = RablRails::CompiledTemplate.new
|
2012-02-27 16:25:06 +00:00
|
|
|
@template.data = :@data
|
|
|
|
end
|
|
|
|
|
2012-04-09 23:03:07 +00:00
|
|
|
def render_json_output
|
2012-04-20 14:28:34 +00:00
|
|
|
RablRails::Renderers::JSON.new(@context).render(@template).to_s
|
2012-04-09 23:03:07 +00:00
|
|
|
end
|
|
|
|
|
2012-04-08 21:26:18 +00:00
|
|
|
test "render object wth empty template" do
|
|
|
|
@template.source = {}
|
2012-04-15 16:17:49 +00:00
|
|
|
assert_equal %q({}), render_json_output
|
2012-04-08 21:26:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "render collection with empty template" do
|
|
|
|
@context.stub(:instance_variable_get).with(:@data).and_return([@data])
|
|
|
|
@template.source = {}
|
2012-04-15 16:17:49 +00:00
|
|
|
assert_equal %q([{}]), render_json_output
|
2012-04-08 21:26:18 +00:00
|
|
|
end
|
|
|
|
|
2012-02-27 16:25:06 +00:00
|
|
|
test "render single object attributes" do
|
|
|
|
@template.source = { :id => :id, :name => :name }
|
2012-04-15 16:17:49 +00:00
|
|
|
assert_equal %q({"id":1,"name":"foobar"}), render_json_output
|
2012-02-27 16:25:06 +00:00
|
|
|
end
|
|
|
|
|
2012-04-15 16:17:49 +00:00
|
|
|
test "render child with object association" do
|
|
|
|
@data.stub(:address).and_return(mock(:city => 'Paris'))
|
|
|
|
@template.source = { :address => { :_data => :address, :city => :city } }
|
|
|
|
assert_equal %q({"address":{"city":"Paris"}}), render_json_output
|
|
|
|
end
|
|
|
|
|
|
|
|
test "render child with arbitrary data source" do
|
2012-02-27 16:25:06 +00:00
|
|
|
@template.source = { :author => { :_data => :@data, :name => :name } }
|
2012-04-15 16:17:49 +00:00
|
|
|
assert_equal %q({"author":{"name":"foobar"}}), render_json_output
|
2012-02-27 16:25:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "render glued attributes from single object" do
|
|
|
|
@template.source = { :_glue0 => { :_data => :@data, :name => :name } }
|
2012-04-15 16:17:49 +00:00
|
|
|
assert_equal %q({"name":"foobar"}), render_json_output
|
2012-02-27 16:25:06 +00:00
|
|
|
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 }
|
2012-04-15 16:17:49 +00:00
|
|
|
assert_equal %q([{"uid":1,"name":"foo","gender":"male"},{"uid":2,"name":"bar","gender":"female"}]), render_json_output
|
2012-02-27 13:49:34 +00:00
|
|
|
end
|
2012-03-05 14:33:25 +00:00
|
|
|
|
2012-04-08 21:26:18 +00:00
|
|
|
test "render node property" do
|
2012-04-15 16:17:49 +00:00
|
|
|
proc = lambda { |object| object.name }
|
|
|
|
@template.source = { :name => proc }
|
|
|
|
assert_equal %q({"name":"foobar"}), render_json_output
|
2012-03-05 14:33:25 +00:00
|
|
|
end
|
|
|
|
|
2012-04-08 21:26:18 +00:00
|
|
|
test "render node property with true condition" do
|
|
|
|
condition = lambda { |u| true }
|
2012-03-05 14:33:25 +00:00
|
|
|
proc = lambda { |object| object.name }
|
|
|
|
@template.source = { :name => [condition, proc] }
|
2012-04-15 16:17:49 +00:00
|
|
|
assert_equal %q({"name":"foobar"}), render_json_output
|
2012-03-05 14:33:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "render node property with false condition" do
|
|
|
|
condition = lambda { |u| false }
|
|
|
|
proc = lambda { |object| object.name }
|
|
|
|
@template.source = { :name => [condition, proc] }
|
2012-04-15 16:17:49 +00:00
|
|
|
assert_equal %q({}), render_json_output
|
2012-03-05 14:33:25 +00:00
|
|
|
end
|
2012-05-02 15:53:13 +00:00
|
|
|
|
|
|
|
test "node with context method call" do
|
|
|
|
@context.stub(:respond_to?).with(:context_method).and_return(true)
|
|
|
|
@context.stub(:context_method).and_return('marty')
|
|
|
|
proc = lambda { |object| context_method }
|
|
|
|
@template.source = { :name => proc }
|
|
|
|
assert_equal %q({"name":"marty"}), render_json_output
|
|
|
|
end
|
2012-04-08 21:26:18 +00:00
|
|
|
|
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)
|
2012-04-08 21:26:18 +00:00
|
|
|
|
2012-03-29 08:58:20 +00:00
|
|
|
# Stub Library#get
|
2012-04-20 14:28:34 +00:00
|
|
|
t = RablRails::CompiledTemplate.new
|
2012-04-15 16:17:49 +00:00
|
|
|
t.source = { :name => :name }
|
2012-04-20 14:28:34 +00:00
|
|
|
RablRails::Library.reset_instance
|
|
|
|
RablRails::Library.instance.should_receive(:get).with('users/base').and_return(t)
|
2012-03-29 08:58:20 +00:00
|
|
|
|
|
|
|
@template.data = false
|
|
|
|
@template.source = { :user => ->(s) { partial('users/base', :object => @user) } }
|
2012-04-08 21:26:18 +00:00
|
|
|
|
2012-04-15 16:17:49 +00:00
|
|
|
assert_equal %q({"user":{"name":"foobar"}}), render_json_output
|
2012-03-29 08:58:20 +00:00
|
|
|
end
|
2012-04-08 21:26:18 +00:00
|
|
|
|
2012-04-15 16:17:49 +00:00
|
|
|
test "partial with no values should raise an error" do
|
2012-03-29 08:58:20 +00:00
|
|
|
@template.data = false
|
|
|
|
@template.source = { :user => ->(s) { partial('users/base') } }
|
2012-04-08 21:26:18 +00:00
|
|
|
|
2012-04-20 14:28:34 +00:00
|
|
|
assert_raises(RablRails::Renderers::PartialError) { render_json_output }
|
2012-03-29 08:58:20 +00:00
|
|
|
end
|
2012-04-08 21:26:18 +00:00
|
|
|
|
2012-03-29 08:58:20 +00:00
|
|
|
test "partial with empty values should not raise an error" do
|
|
|
|
@template.data = false
|
|
|
|
@template.source = { :users => ->(s) { partial('users/base', :object => []) } }
|
2012-04-08 21:26:18 +00:00
|
|
|
|
2012-04-15 16:17:49 +00:00
|
|
|
assert_equal %q({"users":[]}), render_json_output
|
2012-03-29 08:58:20 +00:00
|
|
|
end
|
2012-07-13 17:15:04 +00:00
|
|
|
|
|
|
|
test "render object with root node" do
|
|
|
|
@template.root_name = :author
|
|
|
|
@template.source = { :id => :id, :name => :name }
|
|
|
|
assert_equal %q({"author":{"id":1,"name":"foobar"}}), render_json_output
|
|
|
|
end
|
|
|
|
|
|
|
|
test "render object with root options set to false" do
|
|
|
|
RablRails.include_json_root = false
|
|
|
|
@template.root_name = :author
|
|
|
|
@template.source = { :id => :id, :name => :name }
|
|
|
|
assert_equal %q({"id":1,"name":"foobar"}), render_json_output
|
|
|
|
end
|
2012-02-22 18:14:00 +00:00
|
|
|
end
|