require File.expand_path('../teststrap', __FILE__) require 'rabl/template' context "Rabl::Engine" do helper(:rabl) { |t| RablTemplate.new("code", :format => 'plist') { t } } context "with plist defaults" do setup do Rabl.configure do |config| # Comment this line out because include_plist_root is default. #config.include_plist_root = true end end context "#object" do asserts "that it sets data source" do template = rabl %q{ object @user } scope = Object.new scope.instance_variable_set :@user, User.new template.render(scope) end.matches "\n\n\n\n\tuser\n\t\n\n\n" asserts "that it can set root node" do template = rabl %q{ object @user => :person } scope = Object.new scope.instance_variable_set :@user, User.new template.render(scope).split("").sort end.equals "\n\n\n\n\tperson\n\t\n\n\n".split("").sort end context "#collection" do asserts "that it sets object to be casted as a simple array" do template = rabl %{ collection @users } scope = Object.new scope.instance_variable_set :@users, [User.new, User.new] template.render(scope).split("").sort end.equals "\n\n\n\n\t\n\t\tuser\n\t\t\n\t\n\t\n\t\tuser\n\t\t\n\t\n\n\n".split("").sort asserts "that it sets root node for objects" do template = rabl %{ collection @users => :person } scope = Object.new scope.instance_variable_set :@users, [User.new, User.new] template.render(scope).split("").sort end.equals "\n\n\n\n\tperson\n\t\n\t\t\n\t\t\tperson\n\t\t\t\n\t\t\n\t\t\n\t\t\tperson\n\t\t\t\n\t\t\n\t\n\n\n".split("").sort end context "#attribute" do asserts "that it adds an attribute or method to be included in output" do template = rabl %{ object @user attribute :name } scope = Object.new scope.instance_variable_set :@user, User.new(:name => 'irvine') template.render(scope).split("").sort end.equals "\n\n\n\n\tuser\n\t\n\t\tname\n\t\tirvine\n\t\n\n\n".split("").sort asserts "that it can add attribute under a different key name through :as" do template = rabl %{ object @user attribute :name, :as => 'city' } scope = Object.new scope.instance_variable_set :@user, User.new(:name => 'irvine') template.render(scope).split("").sort end.equals "\n\n\n\n\tuser\n\t\n\t\tcity\n\t\tirvine\n\t\n\n\n".split("").sort asserts "that it can add attribute under a different key name through hash" do template = rabl %{ object @user attribute :name => :city } scope = Object.new scope.instance_variable_set :@user, User.new(:name => 'irvine') template.render(scope).split("").sort end.equals "\n\n\n\n\tuser\n\t\n\t\tcity\n\t\tirvine\n\t\n\n\n".split("").sort end context "#code" do asserts "that it can create an arbitraty code node" do template = rabl %{ code(:foo) { 'bar' } } template.render(Object.new).split("").sort end.equals "\n\n\n\n\tfoo\n\tbar\n\n\n".split("").sort asserts "that it can be passed conditionals" do template = rabl %{ code(:foo, :if => lambda { |i| false }) { 'bar' } } template.render(Object.new).split("").sort end.equals "\n\n\n\n\n".split("").sort end context "#child" do asserts "that it can create a child node" do template = rabl %{ object @user child(@user) { attribute :city } } scope = Object.new scope.instance_variable_set :@user, User.new(:name => 'leo', :city => 'LA') template.render(scope).split("").sort end.equals "\n\n\n\n\tuser\n\t\n\t\tuser\n\t\t\n\t\t\tcity\n\t\t\tLA\n\t\t\n\t\n\n\n".split("").sort asserts "that it can create a child node with different key" do template = rabl %{ object @user attribute :name child(@user => :person) { attribute :city } } scope = Object.new scope.instance_variable_set :@user, User.new(:name => 'leo', :city => 'LA') template.render(scope) end.equals "\n\n\n\n\tuser\n\t\n\t\tname\n\t\tleo\n\t\tperson\n\t\t\n\t\t\tcity\n\t\t\tLA\n\t\t\n\t\n\n\n" end context "#glue" do asserts "that it glues data from a child node" do template = rabl %{ object @user attribute :name glue(@user) { attribute :city } glue(@user) { attribute :age } } scope = Object.new scope.instance_variable_set :@user, User.new(:name => 'leo', :city => 'LA', :age => 12) template.render(scope) end.equals "\n\n\n\n\tuser\n\t\n\t\tage\n\t\t12\n\t\tcity\n\t\tLA\n\t\tname\n\t\tleo\n\t\n\n\n" end teardown do Rabl.reset_configuration! end end context "with plist_engine" do setup do class CustomPlistEncodeEngine def self.dump string 42 end end Rabl.configure do |config| config.plist_engine = CustomPlistEncodeEngine end end asserts 'that it returns process by custom to_json' do template = rabl %q{ object @user } scope = Object.new scope.instance_variable_set :@user, User.new template.render(scope) end.equals 42 teardown do Rabl.reset_configuration! end end context "without plist root" do setup do Rabl.configure do |config| config.include_plist_root = false end end context "#object" do asserts "that it sets data source" do template = rabl %q{ object @user } scope = Object.new scope.instance_variable_set :@user, User.new template.render(scope) end.matches "\n\n\n\n\n" asserts "that it can set root node" do template = rabl %q{ object @user => :person } scope = Object.new scope.instance_variable_set :@user, User.new template.render(scope) end.equals "\n\n\n\n\n" end context "#collection" do asserts "that it sets object to be casted as a simple array" do template = rabl %{ collection @users } scope = Object.new scope.instance_variable_set :@users, [User.new, User.new] template.render(scope) end.equals "\n\n\n\n\t\n\t\n\n\n" asserts "that it sets root node for objects" do template = rabl %{ collection @users => :person } scope = Object.new scope.instance_variable_set :@users, [User.new, User.new] template.render(scope) end.equals "\n\n\n\n\tperson\n\t\n\t\t\n\t\t\n\t\n\n\n" end context "#attribute" do asserts "that it adds an attribute or method to be included in output" do template = rabl %{ object @user attribute :name } scope = Object.new scope.instance_variable_set :@user, User.new(:name => 'irvine') template.render(scope) end.equals "\n\n\n\n\tname\n\tirvine\n\n\n" asserts "that it can add attribute under a different key name through :as" do template = rabl %{ object @user attribute :name, :as => 'city' } scope = Object.new scope.instance_variable_set :@user, User.new(:name => 'irvine') template.render(scope) end.equals "\n\n\n\n\tcity\n\tirvine\n\n\n" asserts "that it can add attribute under a different key name through hash" do template = rabl %{ object @user attribute :name => :city } scope = Object.new scope.instance_variable_set :@user, User.new(:name => 'irvine') template.render(scope) end.equals "\n\n\n\n\tcity\n\tirvine\n\n\n" end context "#code" do asserts "that it can create an arbitrary code node" do template = rabl %{ code(:foo) { 'bar' } } template.render(Object.new) end.equals "\n\n\n\n\tfoo\n\tbar\n\n\n" asserts "that it can be passed conditionals" do template = rabl %{ code(:foo, :if => lambda { |i| false }) { 'bar' } } template.render(Object.new) end.equals "\n\n\n\n\n" end context "#child" do asserts "that it can create a child node" do template = rabl %{ object @user child(@user) { attribute :city } } scope = Object.new scope.instance_variable_set :@user, User.new(:name => 'leo', :city => 'LA') template.render(scope).split("").sort end.equals "\n\n\n\n\tuser\n\t\n\t\tcity\n\t\tLA\n\t\n\n\n".split("").sort asserts "that it can create a child node with different key" do template = rabl %{ object @user attribute :name child(@user => :person) { attribute :city } } scope = Object.new scope.instance_variable_set :@user, User.new(:name => 'leo', :city => 'LA') template.render(scope) end.equals "\n\n\n\n\tname\n\tleo\n\tperson\n\t\n\t\tcity\n\t\tLA\n\t\n\n\n" end context "#glue" do asserts "that it glues data from a child node" do template = rabl %{ object @user attribute :name glue(@user) { attribute :city } glue(@user) { attribute :age } } scope = Object.new scope.instance_variable_set :@user, User.new(:name => 'leo', :city => 'LA', :age => 12) template.render(scope) end.equals "\n\n\n\n\tage\n\t12\n\tcity\n\tLA\n\tname\n\tleo\n\n\n" end teardown do Rabl.reset_configuration! end end end