1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
|
require File.expand_path('../teststrap', __FILE__)
context "Rabl::Builder" do
helper(:builder) { |*args| Rabl::Builder.new(*args) }
helper(:build_hash) { |*args| builder(*args).to_hash }
setup do
@users = [User.new, User.new]
@user = User.new
builder(nil, nil, {:view_path => '/path/to/views'})
end
context "#initialize" do
asserts_topic.assigns :options
asserts_topic.assigns :_view_path
end
context "#to_hash" do
context "when given a simple object" do
setup { builder(nil, { :attributes => [ { :name => :name } ] }) }
asserts "that the object is set properly" do
topic.to_hash(User.new, nil, :root_name => "user")
end.equivalent_to({ "user" => { :name => "rabl" } })
end
context "when given an object alias" do
setup { builder(nil, { :attributes => [ { :name => :name, :options => { :as => :foo } } ] }) }
asserts "that the object is set properly" do
topic.to_hash(User.new, nil, :root_name => "person")
end.equivalent_to({ "person" => { :foo => "rabl" } })
end
context "when specified with no root" do
setup { builder(nil, { :attributes => [ { :name => :name, :options => { :as => :name } } ] }) }
asserts "that the object is set properly" do
topic.to_hash(User.new, nil, :root => false)
end.equivalent_to({ :name => "rabl" })
end
context "when nil values are replaced with empty strings" do
setup do
Rabl.configuration.replace_nil_values_with_empty_strings = true
builder(nil, { :attributes => [ { :name => :name } ], :node => [{ :name => :extra, :options => {}, :block => lambda { |u| { :twitter => u.twitter } } }] })
end
asserts "that an empty string is returned as the value" do
topic.to_hash(User.new(:name => nil, :twitter => nil))
end.equivalent_to({ :name => '', :extra => { :twitter => '' } })
asserts "that it handles existing non nil values correctly" do
topic.to_hash(User.new(:name => 10, :twitter => 'twitter'))
end.equivalent_to({ :name => 10, :extra => { :twitter => 'twitter' } })
teardown do
Rabl.configuration.replace_nil_values_with_empty_strings = false
end
end
context "when empty string values are replaced with nil values" do
setup do
Rabl.configuration.replace_empty_string_values_with_nil_values = true
builder(nil, { :attributes => [ { :name => :name } ], :node => [{ :name => :extra, :options => {}, :block => lambda { |u| { :twitter => u.twitter } } }] })
end
asserts "that nil is returned as the value" do
topic.to_hash(User.new(:name => "", :twitter => ''))
end.equivalent_to({ :name => nil, :extra => { :twitter => nil } })
asserts "that it handles existing nil values correctly" do
topic.to_hash(User.new(:name => nil, :twitter => nil))
end.equivalent_to({ :name => nil, :extra => { :twitter => nil } })
asserts "that it handles existing non nil values correctly" do
topic.to_hash(User.new(:name => 10, :twitter => 'twitter'))
end.equivalent_to({ :name => 10, :extra => { :twitter => 'twitter' } })
teardown do
Rabl.configuration.replace_empty_string_values_with_nil_values = false
end
end
context "when nil values are excluded" do
setup do
Rabl.configuration.exclude_nil_values = true
builder(nil, { :attributes => [ { :name => :name } ] })
end
asserts "that an nil attribute is not returned" do
topic.to_hash(User.new(:name => nil))
end.equivalent_to({ })
teardown do
Rabl.configuration.exclude_nil_values = false
end
end
context "when keys are camelized" do
setup do
Rabl.configuration.camelize_keys = true
builder(nil, { :attributes => [ { :name => :first_pets_name } ] })
end
asserts "that the key is camelized" do
topic.to_hash(User.new)
end.equivalent_to({ :firstPetsName => 'jack' })
teardown do
Rabl.configuration.camelize_keys = false
end
end
context "when keys are camelized with upper case first letter" do
setup do
Rabl.configuration.camelize_keys = :upper
builder(nil, { :attributes => [ { :name => :first_pets_name } ] })
end
asserts "that the key is camelized with upper case first letter" do
topic.to_hash(User.new)
end.equivalent_to({ :FirstPetsName => 'jack' })
teardown do
Rabl.configuration.camelize_keys = false
end
end
end
context "#attribute" do
asserts "that the node" do
build_hash @user, :attributes => [ { :name => :name }, { :name => :city, :options => { :as => :city } } ]
end.equivalent_to({:name => 'rabl', :city => 'irvine'})
context "that with a non-existent attribute" do
context "when non-existent attributes are allowed by the configuration" do
setup { stub(Rabl.configuration).raise_on_missing_attribute { false } }
asserts "the node" do
build_hash @user, :attributes => [ { :name => :fake } ]
end.equals({})
end
context "when non-existent attributes are forbidden by the configuration" do
setup { stub(Rabl.configuration).raise_on_missing_attribute { true } }
asserts "the node" do
build_hash @user, :attributes => [ { :name => :fake } ]
end.raises(RuntimeError)
end
end
context "that with a string key" do
setup { builder(nil, { :attributes => [ { :name => "name" } ] }) }
asserts "the node name is converted to a symbol" do
topic.to_hash(User.new, :name => "user")
end.equivalent_to({ :name => "rabl" })
end
context "that with the same node names as strings and symbols" do
setup { builder(nil, { :attributes => [ { :name => "name" }, { :name => :name } ] }) }
asserts "the nodes aren't duplicated" do
topic.to_hash(User.new, :name => "user")
end.equivalent_to({ :name => "rabl" })
end
end
context "#node" do
asserts "that it has node :foo" do
build_hash @user, :node => [{ :name => :foo, :options => {}, :block => lambda { |u| "bar" } }]
end.equivalent_to({:foo => 'bar'})
asserts "that it excludes nodes in the :except list" do
build_hash @user, {:node => [{ :name => :foo, :block => lambda { |u| "bar" } }]}, {except: [:foo]}
end.equivalent_to({})
asserts "that it excludes nodes in the :except attribute" do
build_hash @user, {:node => [{ :name => :foo, :block => lambda { |u| "bar" } }]}, {except: :foo}
end.equivalent_to({})
asserts "that it has node not in except list" do
build_hash @user, {:node => [{ :name => :foo, :block => lambda { |u| "bar" } }]}, {except: [:unknown]}
end.equivalent_to({:foo => 'bar'})
asserts "that it has multiple nodes" do
build_hash(@user,
{:node => [{ :name => :foo, :block => lambda { |u| "bar" } },
{:name => :baz, :block => lambda{|_u| "bar"}},
{:name => :bam, :block => lambda{|_u| "boom"}}]},
{except: [:unknown, :baz]})
end.equivalent_to({:foo => 'bar', :bam => 'boom'})
asserts "that using object it has node :boo" do
build_hash @user, :node => [
{ :name => :foo, :options => {}, :block => lambda { |u| "bar" } },
{ :name => :baz, :options => {}, :block => lambda { |u| u.city } }
]
end.equivalent_to({:foo => 'bar', :baz => 'irvine'})
asserts "that it converts the node name to a symbol" do
build_hash @user, :node => [{ :name => "foo", :options => {}, :block => lambda { |u| "bar" } }]
end.equivalent_to({:foo => 'bar'})
asserts "that the same node names as a string and symbol aren't duplicated" do
build_hash @user, :node => [
{ :name => "foo", :options => {}, :block => lambda { |u| "bar" } },
{ :name => :foo, :options => {}, :block => lambda { |u| "bar" } }
]
end.equivalent_to({:foo => 'bar'})
end
context "#child" do
asserts "that it generates if no data present" do
builder(nil, :child => []).to_hash(@user)
end.equals({})
asserts "that it generates with a hash" do
b = builder(nil, :child => [ { :data => { @user => :user }, :options => { }, :block => lambda { |u| attribute :name } } ])
b.to_hash(@user)
end.equivalent_to({ :user => { :name => "rabl" } })
asserts "that it generates with a hash alias" do
b = builder nil, :child => [{ :data => { @user => :person }, :options => {}, :block => lambda { |u| attribute :name } }]
b.to_hash(@user)
end.equivalent_to({ :person => { :name => "rabl" } })
asserts "that it generates with an object" do
b = builder nil, :child => [{ :data => @user, :options => {}, :block => lambda { |u| attribute :name } }]
e = Rabl::Engine.new('')
mock(b).data_name(@user) { :user }
mock(e).render.returns('xyz')
mock(b).object_to_engine(@user, { :root => false }).returns(e).subject
b.to_hash(@user)
end.equivalent_to({ :user => 'xyz'})
asserts "that it generates with an collection and child_root" do
b = builder nil, { :child => [{ :data => @users, :options => {}, :block => lambda { |u| attribute :name } }] }, { :child_root => true }
e = Rabl::Engine.new('')
mock(b).data_name(@users) { :users }
mock(e).render.returns('xyz')
mock(b).object_to_engine(@users, { :root => true, :child_root => true }).returns(e).subject
b.to_hash(@user)
end.equivalent_to({ :users => 'xyz'})
asserts "that it generates with an collection and no child root" do
b = builder nil, { :child => [{ :data => @users, :options => {}, :block => lambda { |u| attribute :name } }] }, { :child_root => false }
e = Rabl::Engine.new('')
mock(b).data_name(@users) { :users }
mock(e).render.returns('xyz')
mock(b).object_to_engine(@users, { :root => false, :child_root => false }).returns(e).subject
b.to_hash(@user)
end.equivalent_to({ :users => 'xyz'})
asserts "that it generates with an collection and a specified object_root_name and root" do
ops = { :object_root => "person", :root => :people }
b = builder nil, { :child => [{ :data => @users, :options => ops, :block => lambda { |u| attribute :name } }] }, { :child_root => true }
e = Rabl::Engine.new('')
mock(e).render.returns('xyz')
mock(b).object_to_engine(@users, { :root => "person", :object_root_name => "person", :child_root => true }).returns(e).subject
b.to_hash(@user)
end.equivalent_to({ :people => 'xyz'})
asserts "that it converts the child name to a symbol" do
b = builder(nil, :child => [ { :data => { @user => "user" }, :options => { }, :block => lambda { |u| attribute :name } } ])
b.to_hash(@user)
end.equivalent_to({ :user => { :name => "rabl" } })
asserts "that it does't duplicate childs with the same name as a string and symbol" do
b = builder(nil, :child => [
{ :data => { @user => "user" }, :options => { }, :block => lambda { |u| attribute :name } },
{ :data => { @user => :user }, :options => { }, :block => lambda { |u| attribute :name } }
])
b.to_hash(@user)
end.equivalent_to({ :user => { :name => "rabl" } })
end
context "#glue" do
asserts "that it generates if no data present" do
builder(nil, :glue => []).to_hash(@user)
end.equals({})
asserts "that it generates the glue attributes" do
b = builder nil, :glue => [{ :data => @user, :options => {}, :block => lambda { |u| attribute :name }}]
e = Rabl::Engine.new('')
mock(e).render.returns({:user => 'xyz'})
mock(b).object_to_engine(@user, :root => false).returns(e).subject
b.to_hash(@user)
end.equivalent_to({ :user => 'xyz' })
asserts "that it appends the glue attributes to result" do
b = builder nil, :glue => [{ :data => @user, :options => {}, :block => lambda { |u| attribute :name => :user_name }}]
b.to_hash(@user)
end.equivalent_to({ :user_name => 'rabl' })
asserts "that it does not generate new attributes if no glue attributes are present" do
b = builder nil, :glue => [{ :data => @user, :options => {}, :block => lambda { |u| attribute :name }}]
e = Rabl::Engine.new('')
mock(e).render.returns({})
mock(b).object_to_engine(@user, :root => false).returns(e).subject
b.to_hash(@user)
end.equals({})
end
context "#extends" do
asserts "that it does not generate if no data is present" do
b = builder nil, :extends => [{ :file => 'users/show', :options => {}, :block => lambda { |u| attribute :name }}]
e = Rabl::Engine.new('users/show')
mock(b).partial_as_engine('users/show',{ :object => @user}).returns(e)
mock(e).render.returns({}).subject
b.to_hash(@user)
end.equals({})
asserts "that it generates if data is present" do
b = builder nil, :extends => [{ :file => 'users/show', :options => {}, :block => lambda { |u| attribute :name }}]
e = Rabl::Engine.new('users/show')
mock(b).partial_as_engine('users/show',{ :object => @user}).returns(e)
mock(e).render.returns({:user => 'xyz'}).subject
b.to_hash(@user)
end.equivalent_to({:user => 'xyz'})
asserts "that it generates if local data is present but object is false" do
b = builder nil, :extends => [{ :file => 'users/show', :options => { :object => @user }, :block => lambda { |u| attribute :name }}]
e = Rabl::Engine.new('users/show')
mock(b).partial_as_engine('users/show',{ :object => @user}).returns(e)
mock(e).render.returns({:user => 'xyz'}).subject
b.to_hash(false)
end.equivalent_to({:user => 'xyz'})
asserts "that it generates with exclude 'except' option" do
b = builder nil, :extends => [{ :file => 'users/show', :options => { :except => :user_id }, :block => lambda { |u| attribute :name }}]
e = Rabl::Engine.new('users/show')
mock(b).partial_as_engine('users/show',{ :object => @user, except: :user_id}).returns(e)
mock(e).render.returns({:user => 'xyz'}).subject
b.to_hash(@user)
end.equivalent_to({:user => 'xyz'})
end
context "#resolve_conditionals" do
class ArbObj
def cool?
false
end
def smooth?
true
end
end
asserts "that it can use symbols on if condition and return false if method returns false" do
scope = Rabl::Builder.new(ArbObj.new)
scope.send(:resolve_condition, { :if => :cool? })
end.equals(false)
asserts "that it can use symbols on if condition and return true if method returns true" do
scope = Rabl::Builder.new(ArbObj.new)
scope.send :resolve_condition, { :if => :smooth? }
end.equals(true)
asserts "that it can use symbols as unless condition and return true if method returns false" do
scope = Rabl::Builder.new(ArbObj.new)
scope.send :resolve_condition, { :unless => :cool? }
end.equals(true)
asserts "that it can use symbols as unless condition and return false if method returns true" do
scope = Rabl::Builder.new(ArbObj.new)
scope.send :resolve_condition, { :unless => :smooth? }
end.equals(false)
asserts "that it can use :unless and :if at the same time and return true when if is true and unless is false" do
scope = Rabl::Builder.new(ArbObj.new)
scope.send :resolve_condition, { :if => true, :unless => false }
end.equals(true)
asserts "that it can use :unless and :if at the same time and return false when if is false and unless is false" do
scope = Rabl::Builder.new(ArbObj.new)
scope.send :resolve_condition, { :if => false, :unless => false }
end.equals(false)
asserts "that it can use :unless and :if at the same time and return false when if is true and unless is true" do
scope = Rabl::Builder.new(ArbObj.new)
scope.send :resolve_condition, { :if => true, :unless => true }
end.equals(false)
asserts "that it can use :unless and :if at the same time and return false when if is false and unless is true" do
scope = Rabl::Builder.new(ArbObj.new)
scope.send :resolve_condition, { :if => false, :unless => true }
end.equals(false)
asserts "that it can use lambda on if condition and return false if lambda returns false" do
scope = Rabl::Builder.new(ArbObj.new)
scope.send(:resolve_condition, { :if => lambda { |obj| false } })
end.equals(false)
asserts "that it can use lambda on if condition and return true if lambda returns true" do
scope = Rabl::Builder.new(ArbObj.new)
scope.send(:resolve_condition, { :if => lambda { |obj| true } })
end.equals(true)
asserts "that it can use proc on if condition and return false if proc returns false" do
scope = Rabl::Builder.new(ArbObj.new)
scope.send(:resolve_condition, { :if => proc { false } })
end.equals(false)
asserts "that it can use proc on if condition and return true if proc returns true" do
scope = Rabl::Builder.new(ArbObj.new)
scope.send(:resolve_condition, { :if => proc { true } })
end.equals(true)
asserts "that it can use a variable on if condition and return true if variable is truthy" do
scope = Rabl::Builder.new(ArbObj.new)
scope.send(:resolve_condition, { :if => 'Im truthy' })
end.equals('Im truthy')
asserts "that it can use a variable on if condition and return false if variable is falsy" do
scope = Rabl::Builder.new(ArbObj.new)
scope.send(:resolve_condition, { :if => nil })
end.equals(nil)
asserts "that it can use a hash variable on if condition and return true" do
scope = Rabl::Builder.new(ArbObj.new)
scope.send(:resolve_condition, { :if => { some: 'data' } })
end.equals({some: 'data'})
end
end
|