jbuilder 2.0.7 → 2.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,6 +2,10 @@ require 'test_helper'
2
2
  require 'active_support/inflector'
3
3
  require 'jbuilder'
4
4
 
5
+ def jbuild(*args, &block)
6
+ Jbuilder.new(*args, &block).attributes!
7
+ end
8
+
5
9
  Comment = Struct.new(:content, :id)
6
10
 
7
11
  class NonEnumerable
@@ -14,218 +18,221 @@ class NonEnumerable
14
18
  end
15
19
  end
16
20
 
21
+ # This is not Struct, because structs are Enumerable
22
+ class Person
23
+ attr_reader :name, :age
24
+
25
+ def initialize(name, age)
26
+ @name, @age = name, age
27
+ end
28
+ end
29
+
30
+ class RelationMock
31
+ include Enumerable
32
+
33
+ def each(&block)
34
+ [Person.new('Bob', 30), Person.new('Frank', 50)].each(&block)
35
+ end
36
+
37
+ def empty?
38
+ false
39
+ end
40
+ end
41
+
42
+
17
43
  class JbuilderTest < ActiveSupport::TestCase
18
44
  test 'single key' do
19
- json = Jbuilder.encode do |json|
45
+ result = jbuild do |json|
20
46
  json.content 'hello'
21
47
  end
22
48
 
23
- assert_equal 'hello', MultiJson.load(json)['content']
49
+ assert_equal 'hello', result['content']
24
50
  end
25
51
 
26
52
  test 'single key with false value' do
27
- json = Jbuilder.encode do |json|
53
+ result = jbuild do |json|
28
54
  json.content false
29
55
  end
30
56
 
31
- assert_equal false, MultiJson.load(json)['content']
57
+ assert_equal false, result['content']
32
58
  end
33
59
 
34
60
  test 'single key with nil value' do
35
- json = Jbuilder.encode do |json|
61
+ result = jbuild do |json|
36
62
  json.content nil
37
63
  end
38
64
 
39
- assert MultiJson.load(json).has_key?('content')
40
- assert_equal nil, MultiJson.load(json)['content']
65
+ assert result.has_key?('content')
66
+ assert_equal nil, result['content']
41
67
  end
42
68
 
43
69
  test 'multiple keys' do
44
- json = Jbuilder.encode do |json|
70
+ result = jbuild do |json|
45
71
  json.title 'hello'
46
72
  json.content 'world'
47
73
  end
48
74
 
49
- parsed = MultiJson.load(json)
50
- assert_equal 'hello', parsed['title']
51
- assert_equal 'world', parsed['content']
75
+ assert_equal 'hello', result['title']
76
+ assert_equal 'world', result['content']
52
77
  end
53
78
 
54
79
  test 'extracting from object' do
55
80
  person = Struct.new(:name, :age).new('David', 32)
56
81
 
57
- json = Jbuilder.encode do |json|
82
+ result = jbuild do |json|
58
83
  json.extract! person, :name, :age
59
84
  end
60
85
 
61
- parsed = MultiJson.load(json)
62
- assert_equal 'David', parsed['name']
63
- assert_equal 32, parsed['age']
86
+ assert_equal 'David', result['name']
87
+ assert_equal 32, result['age']
64
88
  end
65
89
 
66
90
  test 'extracting from object using call style for 1.9' do
67
91
  person = Struct.new(:name, :age).new('David', 32)
68
92
 
69
- json = Jbuilder.encode do |json|
93
+ result = jbuild do |json|
70
94
  json.(person, :name, :age)
71
95
  end
72
96
 
73
- parsed = MultiJson.load(json)
74
- assert_equal 'David', parsed['name']
75
- assert_equal 32, parsed['age']
97
+ assert_equal 'David', result['name']
98
+ assert_equal 32, result['age']
76
99
  end
77
100
 
78
101
  test 'extracting from hash' do
79
102
  person = {:name => 'Jim', :age => 34}
80
103
 
81
- json = Jbuilder.encode do |json|
104
+ result = jbuild do |json|
82
105
  json.extract! person, :name, :age
83
106
  end
84
107
 
85
- parsed = MultiJson.load(json)
86
- assert_equal 'Jim', parsed['name']
87
- assert_equal 34, parsed['age']
108
+ assert_equal 'Jim', result['name']
109
+ assert_equal 34, result['age']
88
110
  end
89
111
 
90
112
  test 'nesting single child with block' do
91
- json = Jbuilder.encode do |json|
113
+ result = jbuild do |json|
92
114
  json.author do
93
115
  json.name 'David'
94
116
  json.age 32
95
117
  end
96
118
  end
97
119
 
98
- parsed = MultiJson.load(json)
99
- assert_equal 'David', parsed['author']['name']
100
- assert_equal 32, parsed['author']['age']
120
+ assert_equal 'David', result['author']['name']
121
+ assert_equal 32, result['author']['age']
101
122
  end
102
123
 
103
124
  test 'nesting multiple children with block' do
104
- json = Jbuilder.encode do |json|
125
+ result = jbuild do |json|
105
126
  json.comments do
106
127
  json.child! { json.content 'hello' }
107
128
  json.child! { json.content 'world' }
108
129
  end
109
130
  end
110
131
 
111
- parsed = MultiJson.load(json)
112
- assert_equal 'hello', parsed['comments'].first['content']
113
- assert_equal 'world', parsed['comments'].second['content']
132
+ assert_equal 'hello', result['comments'].first['content']
133
+ assert_equal 'world', result['comments'].second['content']
114
134
  end
115
135
 
116
136
  test 'nesting single child with inline extract' do
117
- person = Class.new do
118
- attr_reader :name, :age
119
-
120
- def initialize(name, age)
121
- @name, @age = name, age
122
- end
123
- end.new('David', 32)
137
+ person = Person.new('David', 32)
124
138
 
125
- json = Jbuilder.encode do |json|
139
+ result = jbuild do |json|
126
140
  json.author person, :name, :age
127
141
  end
128
142
 
129
- parsed = MultiJson.load(json)
130
- assert_equal 'David', parsed['author']['name']
131
- assert_equal 32, parsed['author']['age']
143
+ assert_equal 'David', result['author']['name']
144
+ assert_equal 32, result['author']['age']
132
145
  end
133
146
 
134
147
  test 'nesting multiple children from array' do
135
148
  comments = [ Comment.new('hello', 1), Comment.new('world', 2) ]
136
149
 
137
- json = Jbuilder.encode do |json|
150
+ result = jbuild do |json|
138
151
  json.comments comments, :content
139
152
  end
140
153
 
141
- parsed = MultiJson.load(json)
142
- assert_equal ['content'], parsed['comments'].first.keys
143
- assert_equal 'hello', parsed['comments'].first['content']
144
- assert_equal 'world', parsed['comments'].second['content']
154
+ assert_equal ['content'], result['comments'].first.keys
155
+ assert_equal 'hello', result['comments'].first['content']
156
+ assert_equal 'world', result['comments'].second['content']
145
157
  end
146
158
 
147
159
  test 'nesting multiple children from array when child array is empty' do
148
160
  comments = []
149
161
 
150
- json = Jbuilder.encode do |json|
162
+ result = jbuild do |json|
151
163
  json.name 'Parent'
152
164
  json.comments comments, :content
153
165
  end
154
166
 
155
- parsed = MultiJson.load(json)
156
- assert_equal 'Parent', parsed['name']
157
- assert_equal [], parsed['comments']
167
+ assert_equal 'Parent', result['name']
168
+ assert_equal [], result['comments']
158
169
  end
159
170
 
160
171
  test 'nesting multiple children from array with inline loop' do
161
172
  comments = [ Comment.new('hello', 1), Comment.new('world', 2) ]
162
173
 
163
- json = Jbuilder.encode do |json|
174
+ result = jbuild do |json|
164
175
  json.comments comments do |comment|
165
176
  json.content comment.content
166
177
  end
167
178
  end
168
179
 
169
- parsed = MultiJson.load(json)
170
- assert_equal ['content'], parsed['comments'].first.keys
171
- assert_equal 'hello', parsed['comments'].first['content']
172
- assert_equal 'world', parsed['comments'].second['content']
180
+ assert_equal ['content'], result['comments'].first.keys
181
+ assert_equal 'hello', result['comments'].first['content']
182
+ assert_equal 'world', result['comments'].second['content']
173
183
  end
174
184
 
175
185
  test 'handles nil-collections as empty arrays' do
176
- json = Jbuilder.encode do |json|
186
+ result = jbuild do |json|
177
187
  json.comments nil do |comment|
178
188
  json.content comment.content
179
189
  end
180
190
  end
181
191
 
182
- assert_equal [], MultiJson.load(json)['comments']
192
+ assert_equal [], result['comments']
183
193
  end
184
194
 
185
195
  test 'nesting multiple children from a non-Enumerable that responds to #map' do
186
196
  comments = NonEnumerable.new([ Comment.new('hello', 1), Comment.new('world', 2) ])
187
197
 
188
- json = Jbuilder.encode do |json|
198
+ result = jbuild do |json|
189
199
  json.comments comments, :content
190
200
  end
191
201
 
192
- parsed = MultiJson.load(json)
193
- assert_equal ['content'], parsed['comments'].first.keys
194
- assert_equal 'hello', parsed['comments'].first['content']
195
- assert_equal 'world', parsed['comments'].second['content']
202
+ assert_equal ['content'], result['comments'].first.keys
203
+ assert_equal 'hello', result['comments'].first['content']
204
+ assert_equal 'world', result['comments'].second['content']
196
205
  end
197
206
 
198
207
  test 'nesting multiple chilren from a non-Enumerable that responds to #map with inline loop' do
199
208
  comments = NonEnumerable.new([ Comment.new('hello', 1), Comment.new('world', 2) ])
200
209
 
201
- json = Jbuilder.encode do |json|
210
+ result = jbuild do |json|
202
211
  json.comments comments do |comment|
203
212
  json.content comment.content
204
213
  end
205
214
  end
206
215
 
207
- parsed = MultiJson.load(json)
208
- assert_equal ['content'], parsed['comments'].first.keys
209
- assert_equal 'hello', parsed['comments'].first['content']
210
- assert_equal 'world', parsed['comments'].second['content']
216
+ assert_equal ['content'], result['comments'].first.keys
217
+ assert_equal 'hello', result['comments'].first['content']
218
+ assert_equal 'world', result['comments'].second['content']
211
219
  end
212
220
 
213
221
  test 'nesting multiple children from array with inline loop on root' do
214
222
  comments = [ Comment.new('hello', 1), Comment.new('world', 2) ]
215
223
 
216
- json = Jbuilder.encode do |json|
224
+ result = jbuild do |json|
217
225
  json.call(comments) do |comment|
218
226
  json.content comment.content
219
227
  end
220
228
  end
221
229
 
222
- parsed = MultiJson.load(json)
223
- assert_equal 'hello', parsed.first['content']
224
- assert_equal 'world', parsed.second['content']
230
+ assert_equal 'hello', result.first['content']
231
+ assert_equal 'world', result.second['content']
225
232
  end
226
233
 
227
234
  test 'array nested inside nested hash' do
228
- json = Jbuilder.encode do |json|
235
+ result = jbuild do |json|
229
236
  json.author do
230
237
  json.name 'David'
231
238
  json.age 32
@@ -237,13 +244,12 @@ class JbuilderTest < ActiveSupport::TestCase
237
244
  end
238
245
  end
239
246
 
240
- parsed = MultiJson.load(json)
241
- assert_equal 'hello', parsed['author']['comments'].first['content']
242
- assert_equal 'world', parsed['author']['comments'].second['content']
247
+ assert_equal 'hello', result['author']['comments'].first['content']
248
+ assert_equal 'world', result['author']['comments'].second['content']
243
249
  end
244
250
 
245
251
  test 'array nested inside array' do
246
- json = Jbuilder.encode do |json|
252
+ result = jbuild do |json|
247
253
  json.comments do
248
254
  json.child! do
249
255
  json.authors do
@@ -255,12 +261,13 @@ class JbuilderTest < ActiveSupport::TestCase
255
261
  end
256
262
  end
257
263
 
258
- assert_equal 'david', MultiJson.load(json)['comments'].first['authors'].first['name']
264
+ assert_equal 'david', result['comments'].first['authors'].first['name']
259
265
  end
260
266
 
261
267
  test 'directly set an array nested in another array' do
262
268
  data = [ { :department => 'QA', :not_in_json => 'hello', :names => ['John', 'David'] } ]
263
- json = Jbuilder.encode do |json|
269
+
270
+ result = jbuild do |json|
264
271
  json.array! data do |object|
265
272
  json.department object[:department]
266
273
  json.names do
@@ -269,271 +276,259 @@ class JbuilderTest < ActiveSupport::TestCase
269
276
  end
270
277
  end
271
278
 
272
- assert_equal 'David', MultiJson.load(json)[0]['names'].last
273
- assert_not_equal 'hello', MultiJson.load(json)[0]['not_in_json']
279
+ assert_equal 'David', result[0]['names'].last
280
+ assert !result[0].key?('not_in_json')
274
281
  end
275
282
 
276
283
  test 'nested jbuilder objects' do
277
- to_nest = Jbuilder.new
278
- to_nest.nested_value 'Nested Test'
279
- json = Jbuilder.encode do |json|
284
+ to_nest = Jbuilder.new{ |json| json.nested_value 'Nested Test' }
285
+
286
+ result = jbuild do |json|
280
287
  json.value 'Test'
281
288
  json.nested to_nest
282
289
  end
283
290
 
284
- result = {'value' => 'Test', 'nested' => {'nested_value' => 'Nested Test'}}
285
- assert_equal result, MultiJson.load(json)
291
+ expected = {'value' => 'Test', 'nested' => {'nested_value' => 'Nested Test'}}
292
+ assert_equal expected, result
286
293
  end
287
294
 
288
295
  test 'nested jbuilder object via set!' do
289
- to_nest = Jbuilder.new
290
- to_nest.nested_value 'Nested Test'
291
- json = Jbuilder.encode do |json|
296
+ to_nest = Jbuilder.new{ |json| json.nested_value 'Nested Test' }
297
+
298
+ result = jbuild do |json|
292
299
  json.value 'Test'
293
300
  json.set! :nested, to_nest
294
301
  end
295
302
 
296
- result = {'value' => 'Test', 'nested' => {'nested_value' => 'Nested Test'}}
297
- assert_equal result, MultiJson.load(json)
303
+ expected = {'value' => 'Test', 'nested' => {'nested_value' => 'Nested Test'}}
304
+ assert_equal expected, result
298
305
  end
299
306
 
300
307
  test 'top-level array' do
301
308
  comments = [ Comment.new('hello', 1), Comment.new('world', 2) ]
302
309
 
303
- json = Jbuilder.encode do |json|
304
- json.array!(comments) do |comment|
310
+ result = jbuild do |json|
311
+ json.array! comments do |comment|
305
312
  json.content comment.content
306
313
  end
307
314
  end
308
315
 
309
- parsed = MultiJson.load(json)
310
- assert_equal 'hello', parsed.first['content']
311
- assert_equal 'world', parsed.second['content']
316
+ assert_equal 'hello', result.first['content']
317
+ assert_equal 'world', result.second['content']
312
318
  end
313
319
 
314
320
  test 'extract attributes directly from array' do
315
321
  comments = [ Comment.new('hello', 1), Comment.new('world', 2) ]
316
322
 
317
- json = Jbuilder.encode do |json|
323
+ result = jbuild do |json|
318
324
  json.array! comments, :content, :id
319
325
  end
320
326
 
321
- parsed = MultiJson.load(json)
322
- assert_equal 'hello', parsed.first['content']
323
- assert_equal 1, parsed.first['id']
324
- assert_equal 'world', parsed.second['content']
325
- assert_equal 2, parsed.second['id']
327
+ assert_equal 'hello', result.first['content']
328
+ assert_equal 1, result.first['id']
329
+ assert_equal 'world', result.second['content']
330
+ assert_equal 2, result.second['id']
326
331
  end
327
332
 
328
333
  test 'empty top-level array' do
329
334
  comments = []
330
335
 
331
- json = Jbuilder.encode do |json|
332
- json.array!(comments) do |comment|
336
+ result = jbuild do |json|
337
+ json.array! comments do |comment|
333
338
  json.content comment.content
334
339
  end
335
340
  end
336
341
 
337
- assert_equal [], MultiJson.load(json)
342
+ assert_equal [], result
338
343
  end
339
344
 
340
345
  test 'dynamically set a key/value' do
341
- json = Jbuilder.encode do |json|
346
+ result = jbuild do |json|
342
347
  json.set! :each, 'stuff'
343
348
  end
344
349
 
345
- assert_equal 'stuff', MultiJson.load(json)['each']
350
+ assert_equal 'stuff', result['each']
346
351
  end
347
352
 
348
353
  test 'dynamically set a key/nested child with block' do
349
- json = Jbuilder.encode do |json|
350
- json.set!(:author) do
354
+ result = jbuild do |json|
355
+ json.set! :author do
351
356
  json.name 'David'
352
357
  json.age 32
353
358
  end
354
359
  end
355
360
 
356
- parsed = MultiJson.load(json)
357
- assert_equal 'David', parsed['author']['name']
358
- assert_equal 32, parsed['author']['age']
361
+ assert_equal 'David', result['author']['name']
362
+ assert_equal 32, result['author']['age']
359
363
  end
360
364
 
361
365
  test 'dynamically sets a collection' do
362
366
  comments = [ Comment.new('hello', 1), Comment.new('world', 2) ]
363
367
 
364
- json = Jbuilder.encode do |json|
368
+ result = jbuild do |json|
365
369
  json.set! :comments, comments, :content
366
370
  end
367
371
 
368
- parsed = MultiJson.load(json)
369
- assert_equal ['content'], parsed['comments'].first.keys
370
- assert_equal 'hello', parsed['comments'].first['content']
371
- assert_equal 'world', parsed['comments'].second['content']
372
+ assert_equal ['content'], result['comments'].first.keys
373
+ assert_equal 'hello', result['comments'].first['content']
374
+ assert_equal 'world', result['comments'].second['content']
372
375
  end
373
376
 
374
377
  test 'query like object' do
375
- class Person
376
- attr_reader :name, :age
377
-
378
- def initialize(name, age)
379
- @name, @age = name, age
380
- end
381
- end
382
- class RelationMock
383
- include Enumerable
384
-
385
- def each(&block)
386
- [Person.new('Bob', 30), Person.new('Frank', 50)].each(&block)
387
- end
388
- def empty?
389
- false
390
- end
391
- end
392
-
393
- result = Jbuilder.encode do |json|
378
+ result = jbuild do |json|
394
379
  json.relations RelationMock.new, :name, :age
395
380
  end
396
381
 
397
- parsed = MultiJson.load(result)
398
- assert_equal 2, parsed['relations'].length
399
- assert_equal 'Bob', parsed['relations'][0]['name']
400
- assert_equal 50, parsed['relations'][1]['age']
382
+ assert_equal 2, result['relations'].length
383
+ assert_equal 'Bob', result['relations'][0]['name']
384
+ assert_equal 50, result['relations'][1]['age']
401
385
  end
402
386
 
403
387
  test 'initialize via options hash' do
404
- jbuilder = Jbuilder.new(:key_formatter => 1, :ignore_nil => 2)
388
+ jbuilder = Jbuilder.new(key_formatter: 1, ignore_nil: 2)
405
389
  assert_equal 1, jbuilder.instance_eval{ @key_formatter }
406
390
  assert_equal 2, jbuilder.instance_eval{ @ignore_nil }
407
391
  end
408
392
 
409
393
  test 'key_format! with parameter' do
410
- json = Jbuilder.new
411
- json.key_format! :camelize => [:lower]
412
- json.camel_style 'for JS'
394
+ result = jbuild do |json|
395
+ json.key_format! camelize: [:lower]
396
+ json.camel_style 'for JS'
397
+ end
413
398
 
414
- assert_equal ['camelStyle'], json.attributes!.keys
399
+ assert_equal ['camelStyle'], result.keys
415
400
  end
416
401
 
417
402
  test 'key_format! with parameter not as an array' do
418
- json = Jbuilder.new
419
- json.key_format! :camelize => :lower
420
- json.camel_style 'for JS'
403
+ result = jbuild do |json|
404
+ json.key_format! :camelize => :lower
405
+ json.camel_style 'for JS'
406
+ end
421
407
 
422
- assert_equal ['camelStyle'], json.attributes!.keys
408
+ assert_equal ['camelStyle'], result.keys
423
409
  end
424
410
 
425
411
  test 'key_format! propagates to child elements' do
426
- json = Jbuilder.new
427
- json.key_format! :upcase
428
- json.level1 'one'
429
- json.level2 do
430
- json.value 'two'
412
+ result = jbuild do |json|
413
+ json.key_format! :upcase
414
+ json.level1 'one'
415
+ json.level2 do
416
+ json.value 'two'
417
+ end
431
418
  end
432
419
 
433
- result = json.attributes!
434
420
  assert_equal 'one', result['LEVEL1']
435
421
  assert_equal 'two', result['LEVEL2']['VALUE']
436
422
  end
437
423
 
438
424
  test 'key_format! resets after child element' do
439
- json = Jbuilder.new
440
- json.level2 do
441
- json.key_format! :upcase
442
- json.value 'two'
425
+ result = jbuild do |json|
426
+ json.level2 do
427
+ json.key_format! :upcase
428
+ json.value 'two'
429
+ end
430
+ json.level1 'one'
443
431
  end
444
- json.level1 'one'
445
432
 
446
- result = json.attributes!
447
433
  assert_equal 'two', result['level2']['VALUE']
448
434
  assert_equal 'one', result['level1']
449
435
  end
450
436
 
451
437
  test 'key_format! with no parameter' do
452
- json = Jbuilder.new
453
- json.key_format! :upcase
454
- json.lower 'Value'
438
+ result = jbuild do |json|
439
+ json.key_format! :upcase
440
+ json.lower 'Value'
441
+ end
455
442
 
456
- assert_equal ['LOWER'], json.attributes!.keys
443
+ assert_equal ['LOWER'], result.keys
457
444
  end
458
445
 
459
446
  test 'key_format! with multiple steps' do
460
- json = Jbuilder.new
461
- json.key_format! :upcase, :pluralize
462
- json.pill ''
447
+ result = jbuild do |json|
448
+ json.key_format! :upcase, :pluralize
449
+ json.pill 'foo'
450
+ end
463
451
 
464
- assert_equal ['PILLs'], json.attributes!.keys
452
+ assert_equal ['PILLs'], result.keys
465
453
  end
466
454
 
467
455
  test 'key_format! with lambda/proc' do
468
- json = Jbuilder.new
469
- json.key_format! lambda { |key| key + ' and friends' }
470
- json.oats ''
456
+ result = jbuild do |json|
457
+ json.key_format! ->(key){ key + ' and friends' }
458
+ json.oats 'foo'
459
+ end
471
460
 
472
- assert_equal ['oats and friends'], json.attributes!.keys
461
+ assert_equal ['oats and friends'], result.keys
473
462
  end
474
463
 
475
464
  test 'default key_format!' do
476
- Jbuilder.key_format :camelize => :lower
477
- json = Jbuilder.new
478
- json.camel_style 'for JS'
479
-
480
- assert_equal ['camelStyle'], json.attributes!.keys
481
- Jbuilder.send(:class_variable_set, '@@key_formatter', Jbuilder::KeyFormatter.new)
465
+ Jbuilder.key_format camelize: :lower
466
+ result = jbuild{ |json| json.camel_style 'for JS' }
467
+ assert_equal ['camelStyle'], result.keys
468
+ Jbuilder.send :class_variable_set, '@@key_formatter', Jbuilder::KeyFormatter.new
482
469
  end
483
470
 
484
471
  test 'do not use default key formatter directly' do
485
- json = Jbuilder.new
486
- json.key 'value'
487
-
488
- assert_equal [], Jbuilder.send(:class_variable_get, '@@key_formatter').instance_variable_get('@cache').keys
472
+ jbuild{ |json| json.key 'value' }
473
+ cache = Jbuilder.send(:class_variable_get, '@@key_formatter').instance_variable_get('@cache')
474
+ assert_empty cache
489
475
  end
490
476
 
491
477
  test 'ignore_nil! without a parameter' do
492
- json = Jbuilder.new
493
- json.ignore_nil!
494
- json.test nil
478
+ result = jbuild do |json|
479
+ json.ignore_nil!
480
+ json.test nil
481
+ end
495
482
 
496
- assert_equal [], json.attributes!.keys
483
+ assert_empty result.keys
497
484
  end
498
485
 
499
486
  test 'ignore_nil! with parameter' do
500
- json = Jbuilder.new
501
- json.ignore_nil! true
502
- json.name 'Bob'
503
- json.dne nil
487
+ result = jbuild do |json|
488
+ json.ignore_nil! true
489
+ json.name 'Bob'
490
+ json.dne nil
491
+ end
504
492
 
505
- assert_equal ['name'], json.attributes!.keys
493
+ assert_equal ['name'], result.keys
506
494
 
507
- json = Jbuilder.new
508
- json.ignore_nil! false
509
- json.name 'Bob'
510
- json.dne nil
495
+ result = jbuild do |json|
496
+ json.ignore_nil! false
497
+ json.name 'Bob'
498
+ json.dne nil
499
+ end
511
500
 
512
- assert_equal ['name', 'dne'], json.attributes!.keys
501
+ assert_equal ['name', 'dne'], result.keys
513
502
  end
514
503
 
515
504
  test 'default ignore_nil!' do
516
505
  Jbuilder.ignore_nil
517
- json = Jbuilder.new
518
- json.name 'Bob'
519
- json.dne nil
520
506
 
521
- assert_equal ['name'], json.attributes!.keys
507
+ result = jbuild do |json|
508
+ json.name 'Bob'
509
+ json.dne nil
510
+ end
511
+
512
+ assert_equal ['name'], result.keys
522
513
  Jbuilder.send(:class_variable_set, '@@ignore_nil', false)
523
514
  end
524
515
 
525
516
  test 'nil!' do
526
- json = Jbuilder.new
527
- json.key 'value'
528
- json.nil!
529
- assert_nil json.attributes!
517
+ result = jbuild do |json|
518
+ json.key 'value'
519
+ json.nil!
520
+ end
521
+
522
+ assert_nil result
530
523
  end
531
524
 
532
525
  test 'null!' do
533
- json = Jbuilder.new
534
- json.key 'value'
535
- json.null!
536
- assert_nil json.attributes!
526
+ result = jbuild do |json|
527
+ json.key 'value'
528
+ json.null!
529
+ end
530
+
531
+ assert_nil result
537
532
  end
538
533
 
539
534
  test 'throws meaningfull error when on trying to add properties to null' do