Update tests
This commit is contained in:
parent
7f66973b83
commit
1f36248f13
@ -9,7 +9,7 @@ module RablFastJson
|
|||||||
def initialize
|
def initialize
|
||||||
@glue_count = 0
|
@glue_count = 0
|
||||||
end
|
end
|
||||||
|
|
||||||
#
|
#
|
||||||
# Compile from source code and return the CompiledTemplate
|
# Compile from source code and return the CompiledTemplate
|
||||||
# created.
|
# created.
|
||||||
@ -31,7 +31,7 @@ module RablFastJson
|
|||||||
|
|
||||||
#
|
#
|
||||||
# Sets the object to be used as the data for the template
|
# Sets the object to be used as the data for the template
|
||||||
# Example:
|
# Example:
|
||||||
# object :@user
|
# object :@user
|
||||||
# object :@user, :root => :author
|
# object :@user, :root => :author
|
||||||
#
|
#
|
||||||
@ -51,7 +51,7 @@ module RablFastJson
|
|||||||
object(data)
|
object(data)
|
||||||
@template.root_name = options[:root] if root_given?(options)
|
@template.root_name = options[:root] if root_given?(options)
|
||||||
end
|
end
|
||||||
|
|
||||||
#
|
#
|
||||||
# Includes the attribute or method in the output
|
# Includes the attribute or method in the output
|
||||||
# Example:
|
# Example:
|
||||||
@ -62,7 +62,11 @@ module RablFastJson
|
|||||||
if args.first.is_a?(Hash)
|
if args.first.is_a?(Hash)
|
||||||
args.first.each_pair { |k, v| @template[v] = k }
|
args.first.each_pair { |k, v| @template[v] = k }
|
||||||
else
|
else
|
||||||
args.each { |name| @template[name] = name }
|
options = args.extract_options!
|
||||||
|
args.each { |name|
|
||||||
|
key = options[:as] || name
|
||||||
|
@template[key] = name
|
||||||
|
}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
alias_method :attributes, :attribute
|
alias_method :attributes, :attribute
|
||||||
@ -100,7 +104,7 @@ module RablFastJson
|
|||||||
end
|
end
|
||||||
|
|
||||||
#
|
#
|
||||||
# Creates an arbitrary node in the json output.
|
# Creates an arbitrary node in the json output.
|
||||||
# It accepts :if option to create conditionnal nodes. The current data will
|
# It accepts :if option to create conditionnal nodes. The current data will
|
||||||
# be passed to the block so it is advised to use it instead of ivars.
|
# be passed to the block so it is advised to use it instead of ivars.
|
||||||
# Example:
|
# Example:
|
||||||
@ -134,7 +138,7 @@ module RablFastJson
|
|||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
#
|
#
|
||||||
# Extract data root_name and root name
|
# Extract data root_name and root name
|
||||||
# Example:
|
# Example:
|
||||||
# :@users -> [:@users, nil]
|
# :@users -> [:@users, nil]
|
||||||
|
@ -17,7 +17,7 @@ module RablFastJson
|
|||||||
compiled_template = get_compiled_template(path, source)
|
compiled_template = get_compiled_template(path, source)
|
||||||
compiled_template.context = context
|
compiled_template.context = context
|
||||||
body = compiled_template.render
|
body = compiled_template.render
|
||||||
ActiveSupport::JSON.encode(compiled_template.has_root_name? ? { compiled_template.root_name => body } : body)
|
ActiveSupport::JSON.encode(compiled_template.root_name ? { compiled_template.root_name => body } : body)
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_compiled_template(path, source)
|
def get_compiled_template(path, source)
|
||||||
|
@ -13,6 +13,17 @@ class TestCompiledTemplate < ActiveSupport::TestCase
|
|||||||
@template.data = :@data
|
@template.data = :@data
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "render object wth empty template" do
|
||||||
|
@template.source = {}
|
||||||
|
assert_equal({}, @template.render)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "render collection with empty template" do
|
||||||
|
@context.stub(:instance_variable_get).with(:@data).and_return([@data])
|
||||||
|
@template.source = {}
|
||||||
|
assert_equal([{}], @template.render)
|
||||||
|
end
|
||||||
|
|
||||||
test "render single object attributes" do
|
test "render single object attributes" do
|
||||||
@template.source = { :id => :id, :name => :name }
|
@template.source = { :id => :id, :name => :name }
|
||||||
assert_equal({ :id => 1, :name => 'foobar'}, @template.render)
|
assert_equal({ :id => 1, :name => 'foobar'}, @template.render)
|
||||||
@ -38,14 +49,14 @@ class TestCompiledTemplate < ActiveSupport::TestCase
|
|||||||
], @template.render)
|
], @template.render)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "render object with node property" do
|
test "render node property" do
|
||||||
proc = lambda { |object| object.sex }
|
proc = lambda { |object| object.sex }
|
||||||
@template.source = { :sex => proc }
|
@template.source = { :sex => proc }
|
||||||
assert_equal({ :sex => 'male' }, @template.render)
|
assert_equal({ :sex => 'male' }, @template.render)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "render obejct with conditionnal node property" do
|
test "render node property with true condition" do
|
||||||
condition = lambda { |u| u.name.present? }
|
condition = lambda { |u| true }
|
||||||
proc = lambda { |object| object.name }
|
proc = lambda { |object| object.name }
|
||||||
@template.source = { :name => [condition, proc] }
|
@template.source = { :name => [condition, proc] }
|
||||||
assert_equal({ :name => 'foobar' }, @template.render)
|
assert_equal({ :name => 'foobar' }, @template.render)
|
||||||
@ -57,12 +68,12 @@ class TestCompiledTemplate < ActiveSupport::TestCase
|
|||||||
@template.source = { :name => [condition, proc] }
|
@template.source = { :name => [condition, proc] }
|
||||||
assert_equal({}, @template.render)
|
assert_equal({}, @template.render)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "partial should be evaluated at rendering time" do
|
test "partial should be evaluated at rendering time" do
|
||||||
# Set assigns
|
# Set assigns
|
||||||
@context.stub(:instance_variable_get).with(:@_assigns).and_return({'user' => @data})
|
@context.stub(:instance_variable_get).with(:@_assigns).and_return({'user' => @data})
|
||||||
@data.stub(:respond_to?).with(:empty?).and_return(false)
|
@data.stub(:respond_to?).with(:empty?).and_return(false)
|
||||||
|
|
||||||
# Stub Library#get
|
# Stub Library#get
|
||||||
t = RablFastJson::CompiledTemplate.new
|
t = RablFastJson::CompiledTemplate.new
|
||||||
t.source, t.context = { :name => :name }, @context
|
t.source, t.context = { :name => :name }, @context
|
||||||
@ -71,21 +82,21 @@ class TestCompiledTemplate < ActiveSupport::TestCase
|
|||||||
|
|
||||||
@template.data = false
|
@template.data = false
|
||||||
@template.source = { :user => ->(s) { partial('users/base', :object => @user) } }
|
@template.source = { :user => ->(s) { partial('users/base', :object => @user) } }
|
||||||
|
|
||||||
assert_equal({ :user => { :name => 'foobar' } }, @template.render)
|
assert_equal({ :user => { :name => 'foobar' } }, @template.render)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "partial with nil values should raise an error" do
|
test "partial with nil values should raise an error" do
|
||||||
@template.data = false
|
@template.data = false
|
||||||
@template.source = { :user => ->(s) { partial('users/base') } }
|
@template.source = { :user => ->(s) { partial('users/base') } }
|
||||||
|
|
||||||
assert_raises(RuntimeError) { @template.render }
|
assert_raises(RuntimeError) { @template.render }
|
||||||
end
|
end
|
||||||
|
|
||||||
test "partial with empty values should not raise an error" do
|
test "partial with empty values should not raise an error" do
|
||||||
@template.data = false
|
@template.data = false
|
||||||
@template.source = { :users => ->(s) { partial('users/base', :object => []) } }
|
@template.source = { :users => ->(s) { partial('users/base', :object => []) } }
|
||||||
|
|
||||||
assert_equal({ :users => [] }, @template.render)
|
assert_equal({ :users => [] }, @template.render)
|
||||||
end
|
end
|
||||||
end
|
end
|
@ -21,7 +21,12 @@ class CompilerTest < ActiveSupport::TestCase
|
|||||||
assert_equal({ :id => :id }, t.source)
|
assert_equal({ :id => :id }, t.source)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "attribute can be aliased" do
|
test "attribute can be aliased through :as option" do
|
||||||
|
t = @compiler.compile_source(%{ attribute :foo, :as => :bar })
|
||||||
|
assert_equal({ :bar => :foo}, t.source)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "attribute can be aliased through hash" do
|
||||||
t = @compiler.compile_source(%{ attribute :foo => :bar })
|
t = @compiler.compile_source(%{ attribute :foo => :bar })
|
||||||
assert_equal({ :bar => :foo }, t.source)
|
assert_equal({ :bar => :foo }, t.source)
|
||||||
end
|
end
|
||||||
@ -82,23 +87,27 @@ class CompilerTest < ActiveSupport::TestCase
|
|||||||
test "object set data for the template" do
|
test "object set data for the template" do
|
||||||
t = @compiler.compile_source(%{ object :@user })
|
t = @compiler.compile_source(%{ object :@user })
|
||||||
assert_equal :@user, t.data
|
assert_equal :@user, t.data
|
||||||
|
assert_equal({}, t.source)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "object property can define root name" do
|
test "object property can define root name" do
|
||||||
t = @compiler.compile_source(%{ object :@user => :author })
|
t = @compiler.compile_source(%{ object :@user => :author })
|
||||||
assert_equal :@user, t.data
|
assert_equal :@user, t.data
|
||||||
assert_equal :author, t.root_name
|
assert_equal :author, t.root_name
|
||||||
|
assert_equal({}, t.source)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "collection set the data for the template" do
|
test "collection set the data for the template" do
|
||||||
t = @compiler.compile_source(%{ collection :@user })
|
t = @compiler.compile_source(%{ collection :@user })
|
||||||
assert_equal :@user, t.data
|
assert_equal :@user, t.data
|
||||||
|
assert_equal({}, t.source)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "collection property can define root name" do
|
test "collection property can define root name" do
|
||||||
t = @compiler.compile_source(%{ collection :@user => :users })
|
t = @compiler.compile_source(%{ collection :@user => :users })
|
||||||
assert_equal :@user, t.data
|
assert_equal :@user, t.data
|
||||||
assert_equal :users, t.root_name
|
assert_equal :users, t.root_name
|
||||||
|
assert_equal({}, t.source)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "collection property can define root name via options" do
|
test "collection property can define root name via options" do
|
||||||
@ -127,15 +136,15 @@ class CompilerTest < ActiveSupport::TestCase
|
|||||||
assert_instance_of Array, t.source[:foo]
|
assert_instance_of Array, t.source[:foo]
|
||||||
assert_equal 2, t.source[:foo].size
|
assert_equal 2, t.source[:foo].size
|
||||||
end
|
end
|
||||||
|
|
||||||
test "compile with no object" do
|
test "compile with no object" do
|
||||||
t = @compiler.compile_source(%{
|
t = @compiler.compile_source(%{
|
||||||
object false
|
object false
|
||||||
child(:@user => :user) do
|
child(:@user => :user) do
|
||||||
attribute :id
|
attribute :id
|
||||||
end
|
end
|
||||||
})
|
})
|
||||||
|
|
||||||
assert_equal({ :user => { :_data => :@user, :id => :id } }, t.source)
|
assert_equal({ :user => { :_data => :@user, :id => :id } }, t.source)
|
||||||
assert_equal false, t.data
|
assert_equal false, t.data
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user