forked from nesquena/rabl
-
Notifications
You must be signed in to change notification settings - Fork 11
/
engine_test.rb
405 lines (341 loc) · 12.4 KB
/
engine_test.rb
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
require File.expand_path('../teststrap', __FILE__)
require File.expand_path('../../lib/rabl', __FILE__)
require File.expand_path('../../lib/rabl/template', __FILE__)
require File.expand_path('../models/user', __FILE__)
require File.expand_path('../models/ormless', __FILE__)
context "Rabl::Engine" do
helper(:rabl) { |t| RablTemplate.new { t } }
context "#initialize" do
setup do
Rabl::Engine.new("...source...", { :format => 'xml', :root => true, :view_path => '/path/to/views' })
end
asserts_topic.assigns :_source
asserts_topic.assigns :_options
end
context "with defaults" do
setup do
Rabl.configure do |config|
config.include_json_root = true
config.include_xml_root = false
config.enable_json_callbacks = 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 "{\"user\":{}}"
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 "{\"person\":{}}"
asserts "that it can use non-ORM objects" do
template = rabl %q{
object @other
}
scope = Object.new
scope.instance_variable_set :@other, Ormless.new
template.render(scope)
end.equals "{\"ormless\":{}}"
asserts "that it works with nested controllers" do
template = rabl ""
scope = NestedScope::User.new
scope.instance_variable_set :@user, User.new
template.render(scope)
end.matches "{}"
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 "[{\"user\":{}},{\"user\":{}}]"
# TODO fix this test
# asserts "that it sets root node for objects" do
# template = rabl %{
# collection @users => :people
# }
# scope = Object.new
# scope.instance_variable_set :@users, [User.new, User.new]
# template.render(scope)
# end.equals "{\"people\":[{\"person\":{}},{\"person\":{}}]}"
asserts "that it can use non-ORM objects" do
template = rabl %q{
object @others
}
scope = Object.new
scope.instance_variable_set :@others, [Ormless.new, Ormless.new]
template.render(scope)
end.equals "[{\"ormless\":{}},{\"ormless\":{}}]"
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 "{\"user\":{\"name\":\"irvine\"}}".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 "{\"user\":{\"city\":\"irvine\"}}".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 "{\"user\":{\"city\":\"irvine\"}}".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 "{\"foo\":\"bar\"}".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 "{}".split('').sort
asserts "that it can merge the result with a collection element given no name" do
template = rabl %{
collection @users
code do |user|
{:name => user.name}
end
}
scope = Object.new
scope.instance_variable_set :@users, [User.new(:name => 'a'), User.new(:name => 'b')]
template.render(scope).split('').sort
end.equals "[{\"user\":{\"name\":\"a\"}},{\"user\":{\"name\":\"b\"}}]".split('').sort
asserts "that it can merge the result on a child node given no name" do
template = rabl %{
object @user
attribute :name
child(@user) do
code do |user|
{:city => user.city}
end
end
}
scope = Object.new
scope.instance_variable_set :@user, User.new(:name => 'leo', :city => 'LA')
template.render(scope).split('').sort
end.equals "{\"user\":{\"name\":\"leo\",\"user\":{\"city\":\"LA\"}}}".split('').sort
end
context "#child" do
asserts "that it can create a child node" do
template = rabl %{
object @user
attribute :name
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 "{\"user\":{\"name\":\"leo\",\"user\":{\"city\":\"LA\"}}}".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).split('').sort
end.equals "{\"user\":{\"name\":\"leo\",\"person\":{\"city\":\"LA\"}}}".split('').sort
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).split('').sort
end.equals "{\"user\":{\"name\":\"leo\",\"city\":\"LA\",\"age\":12}}".split('').sort
end
teardown do
Rabl.reset_configuration!
end
end
context "without json root" do
setup do
Rabl.configure do |config|
config.include_json_root = false
config.include_xml_root = false
config.enable_json_callbacks = 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 "{}"
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 "{}"
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 "[{},{}]"
asserts "that it sets root node for objects using hash" do
template = rabl %{
collection @users => :people
}
scope = Object.new
scope.instance_variable_set :@users, [User.new, User.new]
template.render(scope)
end.equals "{\"people\":[{},{}]}"
asserts "that it sets root node for objects using root option" do
template = rabl %{
collection @users, :root => :people
}
scope = Object.new
scope.instance_variable_set :@users, [User.new, User.new]
template.render(scope)
end.equals "{\"people\":[{},{}]}"
asserts "that it sets root node for objects using object_root option" do
template = rabl %{
collection @users, :root => :humans, :object_root => :person
}
scope = Object.new
scope.instance_variable_set :@users, [User.new, User.new]
template.render(scope)
end.equals %Q^{"humans":[{"person":{}},{"person":{}}]}^
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 "{\"name\":\"irvine\"}"
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 "{\"city\":\"irvine\"}"
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 "{\"city\":\"irvine\"}"
end
context "#code" do
asserts "that it can create an arbitraty code node" do
template = rabl %{
code(:foo) { 'bar' }
}
template.render(Object.new)
end.equals "{\"foo\":\"bar\"}"
asserts "that it can be passed conditionals" do
template = rabl %{
code(:foo, :if => lambda { |i| false }) { 'bar' }
}
template.render(Object.new)
end.equals "{}"
end
context "#child" do
asserts "that it can create a child node" do
template = rabl %{
object @user
attribute :name
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 "{\"name\":\"leo\",\"user\":{\"city\":\"LA\"}}".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).split('').sort
end.equals "{\"name\":\"leo\",\"person\":{\"city\":\"LA\"}}".split('').sort
asserts "that it can be passed conditionals" do
template = rabl %{
object @user
attribute :name
child({:children => :children}, {:if => lambda { |user| user.respond_to?('children') }}) { attribute :test }
}
scope = Object.new
scope.instance_variable_set :@user, User.new(:name => 'leo', :city => 'LA')
template.render(scope).split('').sort
end.equals "{\"name\":\"leo\"}".split('').sort
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).split('').sort
end.equals "{\"name\":\"leo\",\"city\":\"LA\",\"age\":12}".split('').sort
end
teardown do
Rabl.reset_configuration!
end
end
end