jwtbuilder 0.0.1 → 2.2.12.jwt
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +5 -0
- data/.travis.yml +28 -0
- data/Appraisals +32 -0
- data/CHANGELOG.md +174 -0
- data/Gemfile +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +260 -0
- data/Rakefile +23 -0
- data/gemfiles/rails_3_0.gemfile +12 -0
- data/gemfiles/rails_3_1.gemfile +12 -0
- data/gemfiles/rails_3_2.gemfile +12 -0
- data/gemfiles/rails_4_0.gemfile +11 -0
- data/gemfiles/rails_4_1.gemfile +11 -0
- data/gemfiles/rails_4_2.gemfile +11 -0
- data/jbuilder.gemspec +18 -0
- data/lib/generators/rails/jbuilder_generator.rb +48 -0
- data/lib/generators/rails/scaffold_controller_generator.rb +12 -0
- data/lib/generators/rails/templates/controller.rb +84 -0
- data/lib/generators/rails/templates/index.json.jbuilder +5 -0
- data/lib/generators/rails/templates/show.json.jbuilder +3 -0
- data/lib/jbuilder/blank.rb +11 -0
- data/lib/jbuilder/dependency_tracker.rb +61 -0
- data/lib/jbuilder/errors.rb +17 -0
- data/lib/jbuilder/jbuilder.rb +7 -0
- data/lib/jbuilder/jbuilder_template.rb +145 -0
- data/lib/jbuilder/key_formatter.rb +34 -0
- data/lib/jbuilder/railtie.rb +21 -0
- data/lib/jbuilder.rb +313 -0
- data/test/jbuilder_dependency_tracker_test.rb +72 -0
- data/test/jbuilder_generator_test.rb +32 -0
- data/test/jbuilder_template_test.rb +343 -0
- data/test/jbuilder_test.rb +686 -0
- data/test/scaffold_controller_generator_test.rb +57 -0
- data/test/test_helper.rb +14 -0
- metadata +48 -8
@@ -0,0 +1,686 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'active_support/inflector'
|
3
|
+
require 'jbuilder'
|
4
|
+
|
5
|
+
def jbuild(*args, &block)
|
6
|
+
Jbuilder.new(*args, &block).attributes!
|
7
|
+
end
|
8
|
+
|
9
|
+
Comment = Struct.new(:content, :id)
|
10
|
+
|
11
|
+
class NonEnumerable
|
12
|
+
def initialize(collection)
|
13
|
+
@collection = collection
|
14
|
+
end
|
15
|
+
|
16
|
+
def map(&block)
|
17
|
+
@collection.map(&block)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class VeryBasicWrapper < BasicObject
|
22
|
+
def initialize(thing)
|
23
|
+
@thing = thing
|
24
|
+
end
|
25
|
+
|
26
|
+
def method_missing(name, *args, &block)
|
27
|
+
@thing.send name, *args, &block
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
# This is not Struct, because structs are Enumerable
|
32
|
+
class Person
|
33
|
+
attr_reader :name, :age
|
34
|
+
|
35
|
+
def initialize(name, age)
|
36
|
+
@name, @age = name, age
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
class RelationMock
|
41
|
+
include Enumerable
|
42
|
+
|
43
|
+
def each(&block)
|
44
|
+
[Person.new('Bob', 30), Person.new('Frank', 50)].each(&block)
|
45
|
+
end
|
46
|
+
|
47
|
+
def empty?
|
48
|
+
false
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
|
53
|
+
class JbuilderTest < ActiveSupport::TestCase
|
54
|
+
test 'single key' do
|
55
|
+
result = jbuild do |json|
|
56
|
+
json.content 'hello'
|
57
|
+
end
|
58
|
+
|
59
|
+
assert_equal 'hello', result['content']
|
60
|
+
end
|
61
|
+
|
62
|
+
test 'single key with false value' do
|
63
|
+
result = jbuild do |json|
|
64
|
+
json.content false
|
65
|
+
end
|
66
|
+
|
67
|
+
assert_equal false, result['content']
|
68
|
+
end
|
69
|
+
|
70
|
+
test 'single key with nil value' do
|
71
|
+
result = jbuild do |json|
|
72
|
+
json.content nil
|
73
|
+
end
|
74
|
+
|
75
|
+
assert result.has_key?('content')
|
76
|
+
assert_equal nil, result['content']
|
77
|
+
end
|
78
|
+
|
79
|
+
test 'multiple keys' do
|
80
|
+
result = jbuild do |json|
|
81
|
+
json.title 'hello'
|
82
|
+
json.content 'world'
|
83
|
+
end
|
84
|
+
|
85
|
+
assert_equal 'hello', result['title']
|
86
|
+
assert_equal 'world', result['content']
|
87
|
+
end
|
88
|
+
|
89
|
+
test 'extracting from object' do
|
90
|
+
person = Struct.new(:name, :age).new('David', 32)
|
91
|
+
|
92
|
+
result = jbuild do |json|
|
93
|
+
json.extract! person, :name, :age
|
94
|
+
end
|
95
|
+
|
96
|
+
assert_equal 'David', result['name']
|
97
|
+
assert_equal 32, result['age']
|
98
|
+
end
|
99
|
+
|
100
|
+
test 'extracting from object using call style for 1.9' do
|
101
|
+
person = Struct.new(:name, :age).new('David', 32)
|
102
|
+
|
103
|
+
result = jbuild do |json|
|
104
|
+
json.(person, :name, :age)
|
105
|
+
end
|
106
|
+
|
107
|
+
assert_equal 'David', result['name']
|
108
|
+
assert_equal 32, result['age']
|
109
|
+
end
|
110
|
+
|
111
|
+
test 'extracting from hash' do
|
112
|
+
person = {:name => 'Jim', :age => 34}
|
113
|
+
|
114
|
+
result = jbuild do |json|
|
115
|
+
json.extract! person, :name, :age
|
116
|
+
end
|
117
|
+
|
118
|
+
assert_equal 'Jim', result['name']
|
119
|
+
assert_equal 34, result['age']
|
120
|
+
end
|
121
|
+
|
122
|
+
test 'nesting single child with block' do
|
123
|
+
result = jbuild do |json|
|
124
|
+
json.author do
|
125
|
+
json.name 'David'
|
126
|
+
json.age 32
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
assert_equal 'David', result['author']['name']
|
131
|
+
assert_equal 32, result['author']['age']
|
132
|
+
end
|
133
|
+
|
134
|
+
test 'empty block handling' do
|
135
|
+
result = jbuild do |json|
|
136
|
+
json.foo 'bar'
|
137
|
+
json.author do
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
assert_equal 'bar', result['foo']
|
142
|
+
assert !result.key?('author')
|
143
|
+
end
|
144
|
+
|
145
|
+
test 'blocks are additive' do
|
146
|
+
result = jbuild do |json|
|
147
|
+
json.author do
|
148
|
+
json.name 'David'
|
149
|
+
end
|
150
|
+
|
151
|
+
json.author do
|
152
|
+
json.age 32
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
assert_equal 'David', result['author']['name']
|
157
|
+
assert_equal 32, result['author']['age']
|
158
|
+
end
|
159
|
+
|
160
|
+
test 'support merge! method' do
|
161
|
+
result = jbuild do |json|
|
162
|
+
json.merge! 'foo' => 'bar'
|
163
|
+
end
|
164
|
+
|
165
|
+
assert_equal 'bar', result['foo']
|
166
|
+
end
|
167
|
+
|
168
|
+
test 'support merge! method in a block' do
|
169
|
+
result = jbuild do |json|
|
170
|
+
json.author do
|
171
|
+
json.merge! 'name' => 'Pavel'
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
assert_equal 'Pavel', result['author']['name']
|
176
|
+
end
|
177
|
+
|
178
|
+
test 'blocks are additive via extract syntax' do
|
179
|
+
person = Person.new('Pavel', 27)
|
180
|
+
|
181
|
+
result = jbuild do |json|
|
182
|
+
json.author person, :age
|
183
|
+
json.author person, :name
|
184
|
+
end
|
185
|
+
|
186
|
+
assert_equal 'Pavel', result['author']['name']
|
187
|
+
assert_equal 27, result['author']['age']
|
188
|
+
end
|
189
|
+
|
190
|
+
test 'arrays are additive' do
|
191
|
+
result = jbuild do |json|
|
192
|
+
json.array! %w[foo]
|
193
|
+
json.array! %w[bar]
|
194
|
+
end
|
195
|
+
|
196
|
+
assert_equal %w[foo bar], result
|
197
|
+
end
|
198
|
+
|
199
|
+
test 'nesting multiple children with block' do
|
200
|
+
result = jbuild do |json|
|
201
|
+
json.comments do
|
202
|
+
json.child! { json.content 'hello' }
|
203
|
+
json.child! { json.content 'world' }
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
207
|
+
assert_equal 'hello', result['comments'].first['content']
|
208
|
+
assert_equal 'world', result['comments'].second['content']
|
209
|
+
end
|
210
|
+
|
211
|
+
test 'nesting single child with inline extract' do
|
212
|
+
person = Person.new('David', 32)
|
213
|
+
|
214
|
+
result = jbuild do |json|
|
215
|
+
json.author person, :name, :age
|
216
|
+
end
|
217
|
+
|
218
|
+
assert_equal 'David', result['author']['name']
|
219
|
+
assert_equal 32, result['author']['age']
|
220
|
+
end
|
221
|
+
|
222
|
+
test 'nesting multiple children from array' do
|
223
|
+
comments = [ Comment.new('hello', 1), Comment.new('world', 2) ]
|
224
|
+
|
225
|
+
result = jbuild do |json|
|
226
|
+
json.comments comments, :content
|
227
|
+
end
|
228
|
+
|
229
|
+
assert_equal ['content'], result['comments'].first.keys
|
230
|
+
assert_equal 'hello', result['comments'].first['content']
|
231
|
+
assert_equal 'world', result['comments'].second['content']
|
232
|
+
end
|
233
|
+
|
234
|
+
test 'nesting multiple children from array when child array is empty' do
|
235
|
+
comments = []
|
236
|
+
|
237
|
+
result = jbuild do |json|
|
238
|
+
json.name 'Parent'
|
239
|
+
json.comments comments, :content
|
240
|
+
end
|
241
|
+
|
242
|
+
assert_equal 'Parent', result['name']
|
243
|
+
assert_equal [], result['comments']
|
244
|
+
end
|
245
|
+
|
246
|
+
test 'nesting multiple children from array with inline loop' do
|
247
|
+
comments = [ Comment.new('hello', 1), Comment.new('world', 2) ]
|
248
|
+
|
249
|
+
result = jbuild do |json|
|
250
|
+
json.comments comments do |comment|
|
251
|
+
json.content comment.content
|
252
|
+
end
|
253
|
+
end
|
254
|
+
|
255
|
+
assert_equal ['content'], result['comments'].first.keys
|
256
|
+
assert_equal 'hello', result['comments'].first['content']
|
257
|
+
assert_equal 'world', result['comments'].second['content']
|
258
|
+
end
|
259
|
+
|
260
|
+
test 'handles nil-collections as empty arrays' do
|
261
|
+
result = jbuild do |json|
|
262
|
+
json.comments nil do |comment|
|
263
|
+
json.content comment.content
|
264
|
+
end
|
265
|
+
end
|
266
|
+
|
267
|
+
assert_equal [], result['comments']
|
268
|
+
end
|
269
|
+
|
270
|
+
test 'nesting multiple children from a non-Enumerable that responds to #map' do
|
271
|
+
comments = NonEnumerable.new([ Comment.new('hello', 1), Comment.new('world', 2) ])
|
272
|
+
|
273
|
+
result = jbuild do |json|
|
274
|
+
json.comments comments, :content
|
275
|
+
end
|
276
|
+
|
277
|
+
assert_equal ['content'], result['comments'].first.keys
|
278
|
+
assert_equal 'hello', result['comments'].first['content']
|
279
|
+
assert_equal 'world', result['comments'].second['content']
|
280
|
+
end
|
281
|
+
|
282
|
+
test 'nesting multiple chilren from a non-Enumerable that responds to #map with inline loop' do
|
283
|
+
comments = NonEnumerable.new([ Comment.new('hello', 1), Comment.new('world', 2) ])
|
284
|
+
|
285
|
+
result = jbuild do |json|
|
286
|
+
json.comments comments do |comment|
|
287
|
+
json.content comment.content
|
288
|
+
end
|
289
|
+
end
|
290
|
+
|
291
|
+
assert_equal ['content'], result['comments'].first.keys
|
292
|
+
assert_equal 'hello', result['comments'].first['content']
|
293
|
+
assert_equal 'world', result['comments'].second['content']
|
294
|
+
end
|
295
|
+
|
296
|
+
test 'array! casts array-like objects to array before merging' do
|
297
|
+
wrapped_array = VeryBasicWrapper.new(%w[foo bar])
|
298
|
+
|
299
|
+
result = jbuild do |json|
|
300
|
+
json.array! wrapped_array
|
301
|
+
end
|
302
|
+
|
303
|
+
assert_equal %w[foo bar], result
|
304
|
+
end
|
305
|
+
|
306
|
+
test 'nesting multiple children from array with inline loop on root' do
|
307
|
+
comments = [ Comment.new('hello', 1), Comment.new('world', 2) ]
|
308
|
+
|
309
|
+
result = jbuild do |json|
|
310
|
+
json.call(comments) do |comment|
|
311
|
+
json.content comment.content
|
312
|
+
end
|
313
|
+
end
|
314
|
+
|
315
|
+
assert_equal 'hello', result.first['content']
|
316
|
+
assert_equal 'world', result.second['content']
|
317
|
+
end
|
318
|
+
|
319
|
+
test 'array nested inside nested hash' do
|
320
|
+
result = jbuild do |json|
|
321
|
+
json.author do
|
322
|
+
json.name 'David'
|
323
|
+
json.age 32
|
324
|
+
|
325
|
+
json.comments do
|
326
|
+
json.child! { json.content 'hello' }
|
327
|
+
json.child! { json.content 'world' }
|
328
|
+
end
|
329
|
+
end
|
330
|
+
end
|
331
|
+
|
332
|
+
assert_equal 'hello', result['author']['comments'].first['content']
|
333
|
+
assert_equal 'world', result['author']['comments'].second['content']
|
334
|
+
end
|
335
|
+
|
336
|
+
test 'array nested inside array' do
|
337
|
+
result = jbuild do |json|
|
338
|
+
json.comments do
|
339
|
+
json.child! do
|
340
|
+
json.authors do
|
341
|
+
json.child! do
|
342
|
+
json.name 'david'
|
343
|
+
end
|
344
|
+
end
|
345
|
+
end
|
346
|
+
end
|
347
|
+
end
|
348
|
+
|
349
|
+
assert_equal 'david', result['comments'].first['authors'].first['name']
|
350
|
+
end
|
351
|
+
|
352
|
+
test 'directly set an array nested in another array' do
|
353
|
+
data = [ { :department => 'QA', :not_in_json => 'hello', :names => ['John', 'David'] } ]
|
354
|
+
|
355
|
+
result = jbuild do |json|
|
356
|
+
json.array! data do |object|
|
357
|
+
json.department object[:department]
|
358
|
+
json.names do
|
359
|
+
json.array! object[:names]
|
360
|
+
end
|
361
|
+
end
|
362
|
+
end
|
363
|
+
|
364
|
+
assert_equal 'David', result[0]['names'].last
|
365
|
+
assert !result[0].key?('not_in_json')
|
366
|
+
end
|
367
|
+
|
368
|
+
test 'nested jbuilder objects' do
|
369
|
+
to_nest = Jbuilder.new{ |json| json.nested_value 'Nested Test' }
|
370
|
+
|
371
|
+
result = jbuild do |json|
|
372
|
+
json.value 'Test'
|
373
|
+
json.nested to_nest
|
374
|
+
end
|
375
|
+
|
376
|
+
expected = {'value' => 'Test', 'nested' => {'nested_value' => 'Nested Test'}}
|
377
|
+
assert_equal expected, result
|
378
|
+
end
|
379
|
+
|
380
|
+
test 'nested jbuilder object via set!' do
|
381
|
+
to_nest = Jbuilder.new{ |json| json.nested_value 'Nested Test' }
|
382
|
+
|
383
|
+
result = jbuild do |json|
|
384
|
+
json.value 'Test'
|
385
|
+
json.set! :nested, to_nest
|
386
|
+
end
|
387
|
+
|
388
|
+
expected = {'value' => 'Test', 'nested' => {'nested_value' => 'Nested Test'}}
|
389
|
+
assert_equal expected, result
|
390
|
+
end
|
391
|
+
|
392
|
+
test 'top-level array' do
|
393
|
+
comments = [ Comment.new('hello', 1), Comment.new('world', 2) ]
|
394
|
+
|
395
|
+
result = jbuild do |json|
|
396
|
+
json.array! comments do |comment|
|
397
|
+
json.content comment.content
|
398
|
+
end
|
399
|
+
end
|
400
|
+
|
401
|
+
assert_equal 'hello', result.first['content']
|
402
|
+
assert_equal 'world', result.second['content']
|
403
|
+
end
|
404
|
+
|
405
|
+
test 'it allows using next in array block to skip value' do
|
406
|
+
comments = [ Comment.new('hello', 1), Comment.new('skip', 2), Comment.new('world', 3) ]
|
407
|
+
result = jbuild do |json|
|
408
|
+
json.array! comments do |comment|
|
409
|
+
next if comment.id == 2
|
410
|
+
json.content comment.content
|
411
|
+
end
|
412
|
+
end
|
413
|
+
|
414
|
+
assert_equal 2, result.length
|
415
|
+
assert_equal 'hello', result.first['content']
|
416
|
+
assert_equal 'world', result.second['content']
|
417
|
+
end
|
418
|
+
|
419
|
+
test 'extract attributes directly from array' do
|
420
|
+
comments = [ Comment.new('hello', 1), Comment.new('world', 2) ]
|
421
|
+
|
422
|
+
result = jbuild do |json|
|
423
|
+
json.array! comments, :content, :id
|
424
|
+
end
|
425
|
+
|
426
|
+
assert_equal 'hello', result.first['content']
|
427
|
+
assert_equal 1, result.first['id']
|
428
|
+
assert_equal 'world', result.second['content']
|
429
|
+
assert_equal 2, result.second['id']
|
430
|
+
end
|
431
|
+
|
432
|
+
test 'empty top-level array' do
|
433
|
+
comments = []
|
434
|
+
|
435
|
+
result = jbuild do |json|
|
436
|
+
json.array! comments do |comment|
|
437
|
+
json.content comment.content
|
438
|
+
end
|
439
|
+
end
|
440
|
+
|
441
|
+
assert_equal [], result
|
442
|
+
end
|
443
|
+
|
444
|
+
test 'dynamically set a key/value' do
|
445
|
+
result = jbuild do |json|
|
446
|
+
json.set! :each, 'stuff'
|
447
|
+
end
|
448
|
+
|
449
|
+
assert_equal 'stuff', result['each']
|
450
|
+
end
|
451
|
+
|
452
|
+
test 'dynamically set a key/nested child with block' do
|
453
|
+
result = jbuild do |json|
|
454
|
+
json.set! :author do
|
455
|
+
json.name 'David'
|
456
|
+
json.age 32
|
457
|
+
end
|
458
|
+
end
|
459
|
+
|
460
|
+
assert_equal 'David', result['author']['name']
|
461
|
+
assert_equal 32, result['author']['age']
|
462
|
+
end
|
463
|
+
|
464
|
+
test 'dynamically sets a collection' do
|
465
|
+
comments = [ Comment.new('hello', 1), Comment.new('world', 2) ]
|
466
|
+
|
467
|
+
result = jbuild do |json|
|
468
|
+
json.set! :comments, comments, :content
|
469
|
+
end
|
470
|
+
|
471
|
+
assert_equal ['content'], result['comments'].first.keys
|
472
|
+
assert_equal 'hello', result['comments'].first['content']
|
473
|
+
assert_equal 'world', result['comments'].second['content']
|
474
|
+
end
|
475
|
+
|
476
|
+
test 'query like object' do
|
477
|
+
result = jbuild do |json|
|
478
|
+
json.relations RelationMock.new, :name, :age
|
479
|
+
end
|
480
|
+
|
481
|
+
assert_equal 2, result['relations'].length
|
482
|
+
assert_equal 'Bob', result['relations'][0]['name']
|
483
|
+
assert_equal 50, result['relations'][1]['age']
|
484
|
+
end
|
485
|
+
|
486
|
+
test 'initialize via options hash' do
|
487
|
+
jbuilder = Jbuilder.new(key_formatter: 1, ignore_nil: 2)
|
488
|
+
assert_equal 1, jbuilder.instance_eval{ @key_formatter }
|
489
|
+
assert_equal 2, jbuilder.instance_eval{ @ignore_nil }
|
490
|
+
end
|
491
|
+
|
492
|
+
test 'key_format! with parameter' do
|
493
|
+
result = jbuild do |json|
|
494
|
+
json.key_format! camelize: [:lower]
|
495
|
+
json.camel_style 'for JS'
|
496
|
+
end
|
497
|
+
|
498
|
+
assert_equal ['camelStyle'], result.keys
|
499
|
+
end
|
500
|
+
|
501
|
+
test 'key_format! with parameter not as an array' do
|
502
|
+
result = jbuild do |json|
|
503
|
+
json.key_format! :camelize => :lower
|
504
|
+
json.camel_style 'for JS'
|
505
|
+
end
|
506
|
+
|
507
|
+
assert_equal ['camelStyle'], result.keys
|
508
|
+
end
|
509
|
+
|
510
|
+
test 'key_format! propagates to child elements' do
|
511
|
+
result = jbuild do |json|
|
512
|
+
json.key_format! :upcase
|
513
|
+
json.level1 'one'
|
514
|
+
json.level2 do
|
515
|
+
json.value 'two'
|
516
|
+
end
|
517
|
+
end
|
518
|
+
|
519
|
+
assert_equal 'one', result['LEVEL1']
|
520
|
+
assert_equal 'two', result['LEVEL2']['VALUE']
|
521
|
+
end
|
522
|
+
|
523
|
+
test 'key_format! resets after child element' do
|
524
|
+
result = jbuild do |json|
|
525
|
+
json.level2 do
|
526
|
+
json.key_format! :upcase
|
527
|
+
json.value 'two'
|
528
|
+
end
|
529
|
+
json.level1 'one'
|
530
|
+
end
|
531
|
+
|
532
|
+
assert_equal 'two', result['level2']['VALUE']
|
533
|
+
assert_equal 'one', result['level1']
|
534
|
+
end
|
535
|
+
|
536
|
+
test 'key_format! with no parameter' do
|
537
|
+
result = jbuild do |json|
|
538
|
+
json.key_format! :upcase
|
539
|
+
json.lower 'Value'
|
540
|
+
end
|
541
|
+
|
542
|
+
assert_equal ['LOWER'], result.keys
|
543
|
+
end
|
544
|
+
|
545
|
+
test 'key_format! with multiple steps' do
|
546
|
+
result = jbuild do |json|
|
547
|
+
json.key_format! :upcase, :pluralize
|
548
|
+
json.pill 'foo'
|
549
|
+
end
|
550
|
+
|
551
|
+
assert_equal ['PILLs'], result.keys
|
552
|
+
end
|
553
|
+
|
554
|
+
test 'key_format! with lambda/proc' do
|
555
|
+
result = jbuild do |json|
|
556
|
+
json.key_format! ->(key){ key + ' and friends' }
|
557
|
+
json.oats 'foo'
|
558
|
+
end
|
559
|
+
|
560
|
+
assert_equal ['oats and friends'], result.keys
|
561
|
+
end
|
562
|
+
|
563
|
+
test 'default key_format!' do
|
564
|
+
Jbuilder.key_format camelize: :lower
|
565
|
+
result = jbuild{ |json| json.camel_style 'for JS' }
|
566
|
+
assert_equal ['camelStyle'], result.keys
|
567
|
+
Jbuilder.send :class_variable_set, '@@key_formatter', Jbuilder::KeyFormatter.new
|
568
|
+
end
|
569
|
+
|
570
|
+
test 'do not use default key formatter directly' do
|
571
|
+
jbuild{ |json| json.key 'value' }
|
572
|
+
cache = Jbuilder.send(:class_variable_get, '@@key_formatter').instance_variable_get('@cache')
|
573
|
+
assert_empty cache
|
574
|
+
end
|
575
|
+
|
576
|
+
test 'ignore_nil! without a parameter' do
|
577
|
+
result = jbuild do |json|
|
578
|
+
json.ignore_nil!
|
579
|
+
json.test nil
|
580
|
+
end
|
581
|
+
|
582
|
+
assert_empty result.keys
|
583
|
+
end
|
584
|
+
|
585
|
+
test 'ignore_nil! with parameter' do
|
586
|
+
result = jbuild do |json|
|
587
|
+
json.ignore_nil! true
|
588
|
+
json.name 'Bob'
|
589
|
+
json.dne nil
|
590
|
+
end
|
591
|
+
|
592
|
+
assert_equal ['name'], result.keys
|
593
|
+
|
594
|
+
result = jbuild do |json|
|
595
|
+
json.ignore_nil! false
|
596
|
+
json.name 'Bob'
|
597
|
+
json.dne nil
|
598
|
+
end
|
599
|
+
|
600
|
+
assert_equal ['name', 'dne'], result.keys
|
601
|
+
end
|
602
|
+
|
603
|
+
test 'default ignore_nil!' do
|
604
|
+
Jbuilder.ignore_nil
|
605
|
+
|
606
|
+
result = jbuild do |json|
|
607
|
+
json.name 'Bob'
|
608
|
+
json.dne nil
|
609
|
+
end
|
610
|
+
|
611
|
+
assert_equal ['name'], result.keys
|
612
|
+
Jbuilder.send(:class_variable_set, '@@ignore_nil', false)
|
613
|
+
end
|
614
|
+
|
615
|
+
test 'nil!' do
|
616
|
+
result = jbuild do |json|
|
617
|
+
json.key 'value'
|
618
|
+
json.nil!
|
619
|
+
end
|
620
|
+
|
621
|
+
assert_nil result
|
622
|
+
end
|
623
|
+
|
624
|
+
test 'null!' do
|
625
|
+
result = jbuild do |json|
|
626
|
+
json.key 'value'
|
627
|
+
json.null!
|
628
|
+
end
|
629
|
+
|
630
|
+
assert_nil result
|
631
|
+
end
|
632
|
+
|
633
|
+
test 'null! in a block' do
|
634
|
+
result = jbuild do |json|
|
635
|
+
json.author do
|
636
|
+
json.name 'David'
|
637
|
+
end
|
638
|
+
|
639
|
+
json.author do
|
640
|
+
json.null!
|
641
|
+
end
|
642
|
+
end
|
643
|
+
|
644
|
+
assert result.key?('author')
|
645
|
+
assert_nil result['author']
|
646
|
+
end
|
647
|
+
|
648
|
+
test 'empty attributes respond to empty?' do
|
649
|
+
attributes = Jbuilder.new.attributes!
|
650
|
+
assert attributes.empty?
|
651
|
+
assert attributes.blank?
|
652
|
+
assert !attributes.present?
|
653
|
+
end
|
654
|
+
|
655
|
+
test 'throws ArrayError when trying to add a key to an array' do
|
656
|
+
assert_raise Jbuilder::ArrayError do
|
657
|
+
jbuild do |json|
|
658
|
+
json.array! %w[foo bar]
|
659
|
+
json.fizz "buzz"
|
660
|
+
end
|
661
|
+
end
|
662
|
+
end
|
663
|
+
|
664
|
+
test 'throws NullError when trying to add properties to null' do
|
665
|
+
assert_raise Jbuilder::NullError do
|
666
|
+
jbuild do |json|
|
667
|
+
json.null!
|
668
|
+
json.foo 'bar'
|
669
|
+
end
|
670
|
+
end
|
671
|
+
end
|
672
|
+
|
673
|
+
test 'throws NullError when trying to add properties to null using block syntax' do
|
674
|
+
assert_raise Jbuilder::NullError do
|
675
|
+
jbuild do |json|
|
676
|
+
json.author do
|
677
|
+
json.null!
|
678
|
+
end
|
679
|
+
|
680
|
+
json.author do
|
681
|
+
json.name "Pavel"
|
682
|
+
end
|
683
|
+
end
|
684
|
+
end
|
685
|
+
end
|
686
|
+
end
|