Skip to content
Browse files

Add tests

  • Loading branch information...
1 parent 8f40a08 commit 54302ab63df8d293344699537952c61b9409c12d @inkstak committed
Showing with 43 additions and 1 deletion.
  1. +1 −1 lib/rabl/engine.rb
  2. +42 −0 test/engine_test.rb
View
2 lib/rabl/engine.rb
@@ -55,7 +55,7 @@ def result(options={})
data = to_hash(options)
data = { root_name => data } if root_name
- data.merge! Hash[@_options[:siblings].map { |k,v| [k, @_data.send(v)] }] if data.is_a?(Hash)
+ data.merge! Hash[@_options[:siblings].map { |k,v| [v, @_data.send(k)] }] if data.is_a?(Hash)
data
end
View
42 test/engine_test.rb
@@ -234,6 +234,48 @@
end.equals "{\"user\":{\"name\":\"leo\",\"city\":\"LA\",\"age\":12}}".split('').sort
end
+ context "#sibling" do
+ asserts "that it adds a sibling method in output" do
+ template = rabl %{
+ collection @users, :root => :users
+ sibling :size
+ }
+ scope = Object.new
+ scope.instance_variable_set :@users, [User.new, User.new]
+ template.render(scope)
+ end.equals %{{"users":[{"user":{}},{"user":{}}],"size":2}}
+
+ asserts "that it can add sibling with :as option" do
+ template = rabl %{
+ collection @users, :root => :users
+ sibling :size, :as => 'total_count'
+ }
+ scope = Object.new
+ scope.instance_variable_set :@users, [User.new, User.new]
+ template.render(scope)
+ end.equals %{{"users":[{"user":{}},{"user":{}}],"total_count":2}}
+
+ asserts "that it can add sibling as hash" do
+ template = rabl %{
+ collection @users, :root => :users
+ sibling :size => 'total_count'
+ }
+ scope = Object.new
+ scope.instance_variable_set :@users, [User.new, User.new]
+ template.render(scope)
+ end.equals %{{"users":[{"user":{}},{"user":{}}],"total_count":2}}
+
+ asserts "that it doesn't add sibling methods on a simple array" do
+ template = rabl %{
+ collection @users
+ sibling :size
+ }
+ scope = Object.new
+ scope.instance_variable_set :@users, [User.new, User.new]
+ template.render(scope)
+ end.equals %{[{"user":{}},{"user":{}}]}
+ end
+
teardown do
Rabl.reset_configuration!
end

0 comments on commit 54302ab

Please sign in to comment.
Something went wrong with that request. Please try again.