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

@ -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

View File

@ -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)

View File

@ -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)

View File

@ -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