rabl-rails/test/compiler_test.rb

193 lines
6.5 KiB
Ruby
Raw Normal View History

2012-02-22 18:14:00 +00:00
require 'test_helper'
class CompilerTest < ActiveSupport::TestCase
setup do
@user = User.new
2012-04-20 14:28:34 +00:00
@compiler = RablRails::Compiler.new
2012-02-22 18:14:00 +00:00
end
test "compiler return a compiled template" do
2012-04-20 14:28:34 +00:00
assert_instance_of RablRails::CompiledTemplate, @compiler.compile_source("")
2012-02-22 18:14:00 +00:00
end
2012-04-15 16:17:49 +00:00
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
2012-09-19 15:36:57 +00:00
test "root can be defined via keyword" do
t = @compiler.compile_source(%{ root :author })
assert_equal :author, t.root_name
end
test "root keyword override object root" do
t = @compiler.compile_source(%{ object :@user ; root :author })
assert_equal :author, t.root_name
end
2012-04-15 16:17:49 +00:00
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
t = @compiler.compile_source(%{ collection :@user, :root => :users })
assert_equal :@user, t.data
assert_equal :users, t.root_name
end
test "root can be set to false via options" do
t = @compiler.compile_source(%( object :@user, root: false))
assert_equal false, t.root_name
end
2012-04-15 16:17:49 +00:00
# Compilation
2012-02-22 18:14:00 +00:00
test "simple attributes are compiled to hash" do
t = @compiler.compile_source(%{ attributes :id, :name })
assert_equal({ :id => :id, :name => :name}, t.source)
end
test "attributes appeared only once even if called mutiple times" do
t = @compiler.compile_source(%{ attribute :id ; attribute :id })
assert_equal({ :id => :id }, t.source)
end
2012-04-08 21:26:18 +00:00
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
2012-02-22 18:14:00 +00:00
t = @compiler.compile_source(%{ attribute :foo => :bar })
assert_equal({ :bar => :foo }, t.source)
end
test "multiple attributes can be aliased" do
t = @compiler.compile_source(%{ attributes :foo => :bar, :id => :uid })
assert_equal({ :bar => :foo, :uid => :id }, t.source)
end
2012-02-23 11:04:55 +00:00
test "child with association use association name as data" do
t = @compiler.compile_source(%{ child :address do attributes :foo end})
assert_equal({ :address => { :_data => :address, :foo => :foo } }, t.source)
end
test "child with association can be aliased" do
t = @compiler.compile_source(%{ child :address => :bar do attributes :foo end})
assert_equal({ :bar => { :_data => :address, :foo => :foo } }, t.source)
end
2012-03-02 13:39:20 +00:00
test "child with root name defined as option" do
t = @compiler.compile_source(%{ child(:user, :root => :author) do attributes :foo end })
assert_equal({ :author => { :_data => :user, :foo => :foo } }, t.source)
end
2012-02-23 11:04:55 +00:00
test "child with arbitrary source store the data with the template" do
2012-02-27 13:49:34 +00:00
t = @compiler.compile_source(%{ child :@user => :author do attribute :name end })
assert_equal({ :author => { :_data => :@user, :name => :name } }, t.source)
2012-02-23 11:04:55 +00:00
end
2012-03-02 13:39:20 +00:00
test "child with succint partial notation" do
2012-04-20 14:28:34 +00:00
mock_template = RablRails::CompiledTemplate.new
2012-04-15 16:17:49 +00:00
mock_template.source = { :id => :id }
2012-04-20 14:28:34 +00:00
RablRails::Library.reset_instance
2012-07-15 15:53:20 +00:00
RablRails::Library.instance.stub(:compile_template_from_path).with('users/base').and_return(mock_template)
2012-03-02 13:39:20 +00:00
t = @compiler.compile_source(%{child(:user, :partial => 'users/base') })
assert_equal( {:user => { :_data => :user, :id => :id } }, t.source)
assert_equal( {:id => :id}, mock_template.source)
2012-03-02 13:39:20 +00:00
end
2012-02-23 11:56:45 +00:00
test "glue is compiled as a child but with anonymous name" do
2012-02-27 16:25:06 +00:00
t = @compiler.compile_source(%{ glue(:@user) do attribute :name end })
2012-02-27 13:49:34 +00:00
assert_equal({ :_glue0 => { :_data => :@user, :name => :name } }, t.source)
2012-02-23 11:56:45 +00:00
end
test "multiple glue don't come with name collisions" do
t = @compiler.compile_source(%{
2012-02-27 13:49:34 +00:00
glue :@user do attribute :name end
glue :@user do attribute :foo end
2012-02-23 11:56:45 +00:00
})
assert_equal({
2012-02-27 13:49:34 +00:00
:_glue0 => { :_data => :@user, :name => :name},
:_glue1 => { :_data => :@user, :foo => :foo}
2012-02-23 11:56:45 +00:00
}, t.source)
end
test "glue accepts all dsl in its body" do
t = @compiler.compile_source(%{
glue :@user do node(:foo) { |u| u.name } end
})
assert_not_nil(t.source[:_glue0])
s = t.source[:_glue0]
assert_equal(:@user, s[:_data])
assert_instance_of(Proc, s[:foo])
end
2012-03-02 10:32:11 +00:00
test "extends use other template source as itself" do
2012-02-27 13:49:34 +00:00
template = mock('template', :source => { :id => :id })
2012-04-20 14:28:34 +00:00
RablRails::Library.reset_instance
2012-07-15 15:53:20 +00:00
RablRails::Library.instance.stub(:compile_template_from_path).with('users/base').and_return(template)
2012-02-27 13:49:34 +00:00
t = @compiler.compile_source(%{ extends 'users/base' })
assert_equal({ :id => :id }, t.source)
end
2012-02-23 11:04:55 +00:00
test "node are compiled without evaluating the block" do
t = @compiler.compile_source(%{ node(:foo) { bar } })
assert_not_nil t.source[:foo]
assert_instance_of Proc, t.source[:foo]
end
test "node with condition are compiled as an array of procs" do
t = @compiler.compile_source(%{ node(:foo, :if => lambda { |m| m.foo.present? }) do |m| m.foo end })
assert_not_nil t.source[:foo]
assert_instance_of Array, t.source[:foo]
assert_equal 2, t.source[:foo].size
end
2012-04-08 21:26:18 +00:00
test "conditionnal block compile nicely" do
t = @compiler.compile_source(%{ condition(->(u) {}) do attributes :secret end })
assert_instance_of RablRails::Condition, t.source[:_if0]
assert_equal({ :secret => :secret }, t.source[:_if0].source)
end
2012-03-15 14:12:52 +00:00
test "compile with no object" do
t = @compiler.compile_source(%{
object false
child(:@user => :user) do
attribute :id
end
2012-04-08 21:26:18 +00:00
})
2012-03-15 14:12:52 +00:00
assert_equal({ :user => { :_data => :@user, :id => :id } }, t.source)
2012-03-27 15:07:30 +00:00
assert_equal false, t.data
2012-03-15 14:12:52 +00:00
end
test "name extraction from argument" do
assert_equal [:@users, 'users'], @compiler.send(:extract_data_and_name, :@users)
assert_equal [:users, :users], @compiler.send(:extract_data_and_name, :users)
assert_equal [:@users, :authors], @compiler.send(:extract_data_and_name, :@users => :authors)
end
end