Skip to content

Commit

Permalink
Update tests to reflect changes to object_node
Browse files Browse the repository at this point in the history
  • Loading branch information
nesquena committed Apr 6, 2012
1 parent ea0fac2 commit 098512f
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 95 deletions.
80 changes: 46 additions & 34 deletions test/bson_engine_test.rb
Expand Up @@ -62,8 +62,9 @@

asserts "that it adds an attribute or method to be included in output" do
template = rabl %{
object @user
attribute :name
object @user do
attribute :name
end
}
scope = Object.new
scope.instance_variable_set :@user, User.new(:name => 'irvine')
Expand All @@ -72,8 +73,9 @@

asserts "that it can add attribute under a different key name through :as" do
template = rabl %{
object @user
attribute :name, :as => 'city'
object @user do
attribute :name, :as => 'city'
end
}
scope = Object.new
scope.instance_variable_set :@user, User.new(:name => 'irvine')
Expand All @@ -82,8 +84,9 @@

asserts "that it can add attribute under a different key name through hash" do
template = rabl %{
object @user
attribute :name => :city
object @user do
attribute :name => :city
end
}
scope = Object.new
scope.instance_variable_set :@user, User.new(:name => 'irvine')
Expand All @@ -94,7 +97,7 @@

context "#code" do

asserts "that it can create an arbitraty code node" do
asserts "that it can create an arbitrary code node" do
template = rabl %{
code(:foo) { 'bar' }
}
Expand All @@ -114,9 +117,10 @@

asserts "that it can create a child node" do
template = rabl %{
object @user
attribute :name
child(@user) { attribute :city }
object @user do
attribute :name
child(@user) { attribute :city }
end
}
scope = Object.new
scope.instance_variable_set :@user, User.new(:name => 'leo', :city => 'LA')
Expand All @@ -125,9 +129,10 @@

asserts "that it can create a child node with different key" do
template = rabl %{
object @user
attribute :name
child(@user => :person) { attribute :city }
object @user do
attribute :name
child(@user => :person) { attribute :city }
end
}
scope = Object.new
scope.instance_variable_set :@user, User.new(:name => 'leo', :city => 'LA')
Expand All @@ -139,10 +144,11 @@

asserts "that it glues data from a child node" do
template = rabl %{
object @user
attribute :name
glue(@user) { attribute :city }
glue(@user) { attribute :age }
object @user do
attribute :name
glue(@user) { attribute :city }
glue(@user) { attribute :age }
end
}
scope = Object.new
scope.instance_variable_set :@user, User.new(:name => 'leo', :city => 'LA', :age => 12)
Expand Down Expand Up @@ -236,8 +242,9 @@ def self.serialize string

asserts "that it adds an attribute or method to be included in output" do
template = rabl %{
object @user
attribute :name
object @user do
attribute :name
end
}
scope = Object.new
scope.instance_variable_set :@user, User.new(:name => 'irvine')
Expand All @@ -246,8 +253,9 @@ def self.serialize string

asserts "that it can add attribute under a different key name through :as" do
template = rabl %{
object @user
attribute :name, :as => 'city'
object @user do
attribute :name, :as => 'city'
end
}
scope = Object.new
scope.instance_variable_set :@user, User.new(:name => 'irvine')
Expand All @@ -256,8 +264,9 @@ def self.serialize string

asserts "that it can add attribute under a different key name through hash" do
template = rabl %{
object @user
attribute :name => :city
object @user do
attribute :name => :city
end
}
scope = Object.new
scope.instance_variable_set :@user, User.new(:name => 'irvine')
Expand All @@ -268,7 +277,7 @@ def self.serialize string

context "#code" do

asserts "that it can create an arbitraty code node" do
asserts "that it can create an arbitrary code node" do
template = rabl %{
code(:foo) { 'bar' }
}
Expand All @@ -288,9 +297,10 @@ def self.serialize string

asserts "that it can create a child node" do
template = rabl %{
object @user
attribute :name
child(@user) { attribute :city }
object @user do
attribute :name
child(@user) { attribute :city }
end
}
scope = Object.new
scope.instance_variable_set :@user, User.new(:name => 'leo', :city => 'LA')
Expand All @@ -299,9 +309,10 @@ def self.serialize string

asserts "that it can create a child node with different key" do
template = rabl %{
object @user
attribute :name
child(@user => :person) { attribute :city }
object @user do
attribute :name
child(@user => :person) { attribute :city }
end
}
scope = Object.new
scope.instance_variable_set :@user, User.new(:name => 'leo', :city => 'LA')
Expand All @@ -313,10 +324,11 @@ def self.serialize string

asserts "that it glues data from a child node" do
template = rabl %{
object @user
attribute :name
glue(@user) { attribute :city }
glue(@user) { attribute :age }
object @user do
attribute :name
glue(@user) { attribute :city }
glue(@user) { attribute :age }
end
}
scope = Object.new
scope.instance_variable_set :@user, User.new(:name => 'leo', :city => 'LA', :age => 12)
Expand Down
17 changes: 6 additions & 11 deletions test/builder_test.rb
Expand Up @@ -85,24 +85,21 @@

asserts "that it generates with an object" do
b = builder :child => [{ :data => @user, :options => {}, :block => lambda { |u| attribute :name } }]
mock(b).data_name(@user) { :user }
mock(b).object_to_hash(@user, { :root => false }).returns('xyz').subject
mock(b).data_name(@user) { :person }
b.build(@user)
end.equivalent_to({ :user => 'xyz'})
end.equivalent_to({:person=>{:name=>"rabl"}})

asserts "that it generates with an collection and child_root" do
b = builder :child => [{ :data => @users, :options => {}, :block => lambda { |u| attribute :name } }], :child_root => true
mock(b).data_name(@users) { :users }
mock(b).object_to_hash(@users, { :root => true, :child_root => true }).returns('xyz').subject
b.build(@user)
end.equivalent_to({ :users => 'xyz'})
end.equivalent_to({:users => [{"user"=> {:name=>"rabl"}}, {"user"=>{:name=>"rabl"}}] })

asserts "that it generates with an collection and no child root" do
b = builder :child => [{ :data => @users, :options => {}, :block => lambda { |u| attribute :name } }], :child_root => false
mock(b).data_name(@users) { :users }
mock(b).object_to_hash(@users, { :root => false, :child_root => false }).returns('xyz').subject
b.build(@user)
end.equivalent_to({ :users => 'xyz'})
end.equivalent_to({:users => [{:name=>"rabl"}, {:name=>"rabl"}] })
end

context "#glue" do
Expand All @@ -112,18 +109,16 @@

asserts "that it generates the glue attributes" do
b = builder :glue => [{ :data => @user, :block => lambda { |u| attribute :name }}]
mock(b).object_to_hash(@user, { :root => false }).returns({:user => 'xyz'}).subject
b.build(@user)
end.equivalent_to({ :user => 'xyz' })
end.equivalent_to({:name=>"rabl"})

asserts "that it appends the glue attributes to result" do
b = builder :glue => [{ :data => @user, :block => lambda { |u| attribute :name => :user_name }}]
b.build(@user)
end.equivalent_to({ :user_name => 'rabl' })

asserts "that it does not generate new attributes if no glue attributes are present" do
b = builder :glue => [{ :data => @user, :block => lambda { |u| attribute :name }}]
mock(b).object_to_hash(@user,{ :root => false }).returns({}).subject
b = builder :glue => [{ :data => @user, :block => lambda { |u| }}]
b.build(@user)
end.equals({})
end
Expand Down

0 comments on commit 098512f

Please sign in to comment.