Update tests

This commit is contained in:
ccocchi 2012-04-08 23:26:18 +02:00
parent 7f66973b83
commit 1f36248f13
4 changed files with 45 additions and 21 deletions

View File

@ -9,7 +9,7 @@ module RablFastJson
def initialize
@glue_count = 0
end
#
# Compile from source code and return the CompiledTemplate
# created.
@ -31,7 +31,7 @@ module RablFastJson
#
# Sets the object to be used as the data for the template
# Example:
# Example:
# object :@user
# object :@user, :root => :author
#
@ -51,7 +51,7 @@ module RablFastJson
object(data)
@template.root_name = options[:root] if root_given?(options)
end
#
# Includes the attribute or method in the output
# Example:
@ -62,7 +62,11 @@ module RablFastJson
if args.first.is_a?(Hash)
args.first.each_pair { |k, v| @template[v] = k }
else
args.each { |name| @template[name] = name }
options = args.extract_options!
args.each { |name|
key = options[:as] || name
@template[key] = name
}
end
end
alias_method :attributes, :attribute
@ -100,7 +104,7 @@ module RablFastJson
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
# be passed to the block so it is advised to use it instead of ivars.
# Example:
@ -134,7 +138,7 @@ module RablFastJson
protected
#
#
# Extract data root_name and root name
# Example:
# :@users -> [:@users, nil]

View File

@ -17,7 +17,7 @@ module RablFastJson
compiled_template = get_compiled_template(path, source)
compiled_template.context = context
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
def get_compiled_template(path, source)

View File

@ -13,6 +13,17 @@ class TestCompiledTemplate < ActiveSupport::TestCase
@template.data = :@data
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
@template.source = { :id => :id, :name => :name }
assert_equal({ :id => 1, :name => 'foobar'}, @template.render)
@ -38,14 +49,14 @@ class TestCompiledTemplate < ActiveSupport::TestCase
], @template.render)
end
test "render object with node property" do
test "render 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? }
test "render node property with true condition" do
condition = lambda { |u| true }
proc = lambda { |object| object.name }
@template.source = { :name => [condition, proc] }
assert_equal({ :name => 'foobar' }, @template.render)
@ -57,12 +68,12 @@ class TestCompiledTemplate < ActiveSupport::TestCase
@template.source = { :name => [condition, proc] }
assert_equal({}, @template.render)
end
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
@ -71,21 +82,21 @@ class TestCompiledTemplate < ActiveSupport::TestCase
@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
end

View File

@ -21,7 +21,12 @@ class CompilerTest < ActiveSupport::TestCase
assert_equal({ :id => :id }, t.source)
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 })
assert_equal({ :bar => :foo }, t.source)
end
@ -82,23 +87,27 @@ class CompilerTest < ActiveSupport::TestCase
test "object set data for the template" do
t = @compiler.compile_source(%{ object :@user })
assert_equal :@user, t.data
assert_equal({}, t.source)
end
test "object property can define root name" do
t = @compiler.compile_source(%{ object :@user => :author })
assert_equal :@user, t.data
assert_equal :author, t.root_name
assert_equal({}, t.source)
end
test "collection set the data for the template" do
t = @compiler.compile_source(%{ collection :@user })
assert_equal :@user, t.data
assert_equal({}, t.source)
end
test "collection property can define root name" do
t = @compiler.compile_source(%{ collection :@user => :users })
assert_equal :@user, t.data
assert_equal :users, t.root_name
assert_equal({}, t.source)
end
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_equal 2, t.source[:foo].size
end
test "compile with no object" do
t = @compiler.compile_source(%{
object false
child(:@user => :user) do
attribute :id
end
})
})
assert_equal({ :user => { :_data => :@user, :id => :id } }, t.source)
assert_equal false, t.data
end