micro-lite-hub 0.0.1

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.
Files changed (42) hide show
  1. checksums.yaml +7 -0
  2. data/jbuilder-2.15.1/Appraisals +26 -0
  3. data/jbuilder-2.15.1/CONTRIBUTING.md +100 -0
  4. data/jbuilder-2.15.1/Gemfile +9 -0
  5. data/jbuilder-2.15.1/MIT-LICENSE +20 -0
  6. data/jbuilder-2.15.1/README.md +394 -0
  7. data/jbuilder-2.15.1/Rakefile +21 -0
  8. data/jbuilder-2.15.1/bin/release +14 -0
  9. data/jbuilder-2.15.1/bin/test +6 -0
  10. data/jbuilder-2.15.1/gemfiles/rails_7_0.gemfile +11 -0
  11. data/jbuilder-2.15.1/gemfiles/rails_7_1.gemfile +10 -0
  12. data/jbuilder-2.15.1/gemfiles/rails_7_2.gemfile +10 -0
  13. data/jbuilder-2.15.1/gemfiles/rails_8_0.gemfile +10 -0
  14. data/jbuilder-2.15.1/gemfiles/rails_8_1.gemfile +10 -0
  15. data/jbuilder-2.15.1/gemfiles/rails_head.gemfile +10 -0
  16. data/jbuilder-2.15.1/jbuilder.gemspec +35 -0
  17. data/jbuilder-2.15.1/lib/generators/rails/jbuilder_generator.rb +65 -0
  18. data/jbuilder-2.15.1/lib/generators/rails/scaffold_controller_generator.rb +24 -0
  19. data/jbuilder-2.15.1/lib/generators/rails/templates/api_controller.rb +69 -0
  20. data/jbuilder-2.15.1/lib/generators/rails/templates/controller.rb +86 -0
  21. data/jbuilder-2.15.1/lib/generators/rails/templates/index.json.jbuilder +1 -0
  22. data/jbuilder-2.15.1/lib/generators/rails/templates/partial.json.jbuilder +16 -0
  23. data/jbuilder-2.15.1/lib/generators/rails/templates/show.json.jbuilder +1 -0
  24. data/jbuilder-2.15.1/lib/jbuilder/blank.rb +13 -0
  25. data/jbuilder-2.15.1/lib/jbuilder/collection_renderer.rb +58 -0
  26. data/jbuilder-2.15.1/lib/jbuilder/errors.rb +26 -0
  27. data/jbuilder-2.15.1/lib/jbuilder/jbuilder.rb +3 -0
  28. data/jbuilder-2.15.1/lib/jbuilder/jbuilder_dependency_tracker.rb +75 -0
  29. data/jbuilder-2.15.1/lib/jbuilder/jbuilder_template.rb +264 -0
  30. data/jbuilder-2.15.1/lib/jbuilder/key_formatter.rb +32 -0
  31. data/jbuilder-2.15.1/lib/jbuilder/railtie.rb +34 -0
  32. data/jbuilder-2.15.1/lib/jbuilder/version.rb +5 -0
  33. data/jbuilder-2.15.1/lib/jbuilder.rb +384 -0
  34. data/jbuilder-2.15.1/test/jbuilder_dependency_tracker_test.rb +71 -0
  35. data/jbuilder-2.15.1/test/jbuilder_generator_test.rb +68 -0
  36. data/jbuilder-2.15.1/test/jbuilder_template_test.rb +469 -0
  37. data/jbuilder-2.15.1/test/jbuilder_test.rb +954 -0
  38. data/jbuilder-2.15.1/test/scaffold_api_controller_generator_test.rb +83 -0
  39. data/jbuilder-2.15.1/test/scaffold_controller_generator_test.rb +116 -0
  40. data/jbuilder-2.15.1/test/test_helper.rb +47 -0
  41. data/micro-lite-hub.gemspec +11 -0
  42. metadata +80 -0
@@ -0,0 +1,954 @@
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
+ delegate :map, :count, to: :@collection
17
+ end
18
+
19
+ class VeryBasicWrapper < BasicObject
20
+ def initialize(thing)
21
+ @thing = thing
22
+ end
23
+
24
+ def method_missing(name, *args, &block)
25
+ @thing.send name, *args, &block
26
+ end
27
+ end
28
+
29
+ # This is not Struct, because structs are Enumerable
30
+ class Person
31
+ attr_reader :name, :age
32
+
33
+ def initialize(name, age)
34
+ @name, @age = name, age
35
+ end
36
+ end
37
+
38
+ class RelationMock
39
+ include Enumerable
40
+
41
+ def each(&block)
42
+ [Person.new('Bob', 30), Person.new('Frank', 50)].each(&block)
43
+ end
44
+
45
+ def empty?
46
+ false
47
+ end
48
+ end
49
+
50
+
51
+ class JbuilderTest < ActiveSupport::TestCase
52
+ teardown do
53
+ Jbuilder.send :class_variable_set, '@@key_formatter', nil
54
+ end
55
+
56
+ test 'single key' do
57
+ result = jbuild do |json|
58
+ json.content 'hello'
59
+ end
60
+
61
+ assert_equal 'hello', result['content']
62
+ end
63
+
64
+ test 'method_missing key' do
65
+ result = jbuild do |json|
66
+ json.method_missing 'hello'
67
+ end
68
+
69
+ assert_equal 'hello', result['method_missing']
70
+ end
71
+
72
+ test 'single key with false value' do
73
+ result = jbuild do |json|
74
+ json.content false
75
+ end
76
+
77
+ assert_equal false, result['content']
78
+ end
79
+
80
+ test 'single key with nil value' do
81
+ result = jbuild do |json|
82
+ json.content nil
83
+ end
84
+
85
+ assert result.has_key?('content')
86
+ assert_nil result['content']
87
+ end
88
+
89
+ test 'multiple keys' do
90
+ result = jbuild do |json|
91
+ json.title 'hello'
92
+ json.content 'world'
93
+ end
94
+
95
+ assert_equal 'hello', result['title']
96
+ assert_equal 'world', result['content']
97
+ end
98
+
99
+ test 'extracting from object' do
100
+ person = Struct.new(:name, :age).new('David', 32)
101
+
102
+ result = jbuild do |json|
103
+ json.extract! person, :name, :age
104
+ end
105
+
106
+ assert_equal 'David', result['name']
107
+ assert_equal 32, result['age']
108
+ end
109
+
110
+ test 'extracting from object using call style' do
111
+ person = Struct.new(:name, :age).new('David', 32)
112
+
113
+ result = jbuild do |json|
114
+ json.(person, :name, :age)
115
+ end
116
+
117
+ assert_equal 'David', result['name']
118
+ assert_equal 32, result['age']
119
+ end
120
+
121
+ test 'extracting from hash' do
122
+ person = {:name => 'Jim', :age => 34}
123
+
124
+ result = jbuild do |json|
125
+ json.extract! person, :name, :age
126
+ end
127
+
128
+ assert_equal 'Jim', result['name']
129
+ assert_equal 34, result['age']
130
+ end
131
+
132
+ test 'nesting single child with block' do
133
+ result = jbuild do |json|
134
+ json.author do
135
+ json.name 'David'
136
+ json.age 32
137
+ end
138
+ end
139
+
140
+ assert_equal 'David', result['author']['name']
141
+ assert_equal 32, result['author']['age']
142
+ end
143
+
144
+ test 'empty block handling' do
145
+ result = jbuild do |json|
146
+ json.foo 'bar'
147
+ json.author do
148
+ end
149
+ end
150
+
151
+ assert_equal 'bar', result['foo']
152
+ assert !result.key?('author')
153
+ end
154
+
155
+ test 'blocks are additive' do
156
+ result = jbuild do |json|
157
+ json.author do
158
+ json.name 'David'
159
+ end
160
+
161
+ json.author do
162
+ json.age 32
163
+ end
164
+ end
165
+
166
+ assert_equal 'David', result['author']['name']
167
+ assert_equal 32, result['author']['age']
168
+ end
169
+
170
+ test 'nested blocks are additive' do
171
+ result = jbuild do |json|
172
+ json.author do
173
+ json.name do
174
+ json.first 'David'
175
+ end
176
+ end
177
+
178
+ json.author do
179
+ json.name do
180
+ json.last 'Heinemeier Hansson'
181
+ end
182
+ end
183
+ end
184
+
185
+ assert_equal 'David', result['author']['name']['first']
186
+ assert_equal 'Heinemeier Hansson', result['author']['name']['last']
187
+ end
188
+
189
+ test 'support merge! method' do
190
+ result = jbuild do |json|
191
+ json.merge! 'foo' => 'bar'
192
+ end
193
+
194
+ assert_equal 'bar', result['foo']
195
+ end
196
+
197
+ test 'support merge! method in a block' do
198
+ result = jbuild do |json|
199
+ json.author do
200
+ json.merge! 'name' => 'Pavel'
201
+ end
202
+ end
203
+
204
+ assert_equal 'Pavel', result['author']['name']
205
+ end
206
+
207
+ test 'support merge! method with Jbuilder instance' do
208
+ obj = jbuild do |json|
209
+ json.foo 'bar'
210
+ end
211
+
212
+ result = jbuild do |json|
213
+ json.merge! obj
214
+ end
215
+
216
+ assert_equal 'bar', result['foo']
217
+ end
218
+
219
+ test 'blocks are additive via extract syntax' do
220
+ person = Person.new('Pavel', 27)
221
+
222
+ result = jbuild do |json|
223
+ json.author person, :age
224
+ json.author person, :name
225
+ end
226
+
227
+ assert_equal 'Pavel', result['author']['name']
228
+ assert_equal 27, result['author']['age']
229
+ end
230
+
231
+ test 'arrays are additive' do
232
+ result = jbuild do |json|
233
+ json.array! %w[foo]
234
+ json.array! %w[bar]
235
+ end
236
+
237
+ assert_equal %w[foo bar], result
238
+ end
239
+
240
+ test 'nesting multiple children with block' do
241
+ result = jbuild do |json|
242
+ json.comments do
243
+ json.child! { json.content 'hello' }
244
+ json.child! { json.content 'world' }
245
+ end
246
+ end
247
+
248
+ assert_equal 'hello', result['comments'].first['content']
249
+ assert_equal 'world', result['comments'].second['content']
250
+ end
251
+
252
+ test 'nesting single child with inline extract' do
253
+ person = Person.new('David', 32)
254
+
255
+ result = jbuild do |json|
256
+ json.author person, :name, :age
257
+ end
258
+
259
+ assert_equal 'David', result['author']['name']
260
+ assert_equal 32, result['author']['age']
261
+ end
262
+
263
+ test 'nesting multiple children from array' do
264
+ comments = [ Comment.new('hello', 1), Comment.new('world', 2) ]
265
+
266
+ result = jbuild do |json|
267
+ json.comments comments, :content
268
+ end
269
+
270
+ assert_equal ['content'], result['comments'].first.keys
271
+ assert_equal 'hello', result['comments'].first['content']
272
+ assert_equal 'world', result['comments'].second['content']
273
+ end
274
+
275
+ test 'nesting multiple children from array when child array is empty' do
276
+ comments = []
277
+
278
+ result = jbuild do |json|
279
+ json.name 'Parent'
280
+ json.comments comments, :content
281
+ end
282
+
283
+ assert_equal 'Parent', result['name']
284
+ assert_equal [], result['comments']
285
+ end
286
+
287
+ test 'nesting multiple children from array with inline loop' do
288
+ comments = [ Comment.new('hello', 1), Comment.new('world', 2) ]
289
+
290
+ result = jbuild do |json|
291
+ json.comments comments do |comment|
292
+ json.content comment.content
293
+ end
294
+ end
295
+
296
+ assert_equal ['content'], result['comments'].first.keys
297
+ assert_equal 'hello', result['comments'].first['content']
298
+ assert_equal 'world', result['comments'].second['content']
299
+ end
300
+
301
+ test 'handles nil-collections as empty arrays' do
302
+ result = jbuild do |json|
303
+ json.comments nil do |comment|
304
+ json.content comment.content
305
+ end
306
+ end
307
+
308
+ assert_equal [], result['comments']
309
+ end
310
+
311
+ test 'nesting multiple children from a non-Enumerable that responds to #map' do
312
+ comments = NonEnumerable.new([ Comment.new('hello', 1), Comment.new('world', 2) ])
313
+
314
+ result = jbuild do |json|
315
+ json.comments comments, :content
316
+ end
317
+
318
+ assert_equal ['content'], result['comments'].first.keys
319
+ assert_equal 'hello', result['comments'].first['content']
320
+ assert_equal 'world', result['comments'].second['content']
321
+ end
322
+
323
+ test 'nesting multiple children from a non-Enumerable that responds to #map with inline loop' do
324
+ comments = NonEnumerable.new([ Comment.new('hello', 1), Comment.new('world', 2) ])
325
+
326
+ result = jbuild do |json|
327
+ json.comments comments do |comment|
328
+ json.content comment.content
329
+ end
330
+ end
331
+
332
+ assert_equal ['content'], result['comments'].first.keys
333
+ assert_equal 'hello', result['comments'].first['content']
334
+ assert_equal 'world', result['comments'].second['content']
335
+ end
336
+
337
+ test 'array! casts array-like objects to array before merging' do
338
+ wrapped_array = VeryBasicWrapper.new(%w[foo bar])
339
+
340
+ result = jbuild do |json|
341
+ json.array! wrapped_array
342
+ end
343
+
344
+ assert_equal %w[foo bar], result
345
+ end
346
+
347
+ test 'nesting multiple children from array with inline loop on root' do
348
+ comments = [ Comment.new('hello', 1), Comment.new('world', 2) ]
349
+
350
+ result = jbuild do |json|
351
+ json.call(comments) do |comment|
352
+ json.content comment.content
353
+ end
354
+ end
355
+
356
+ assert_equal 'hello', result.first['content']
357
+ assert_equal 'world', result.second['content']
358
+ end
359
+
360
+ test 'array nested inside nested hash' do
361
+ result = jbuild do |json|
362
+ json.author do
363
+ json.name 'David'
364
+ json.age 32
365
+
366
+ json.comments do
367
+ json.child! { json.content 'hello' }
368
+ json.child! { json.content 'world' }
369
+ end
370
+ end
371
+ end
372
+
373
+ assert_equal 'hello', result['author']['comments'].first['content']
374
+ assert_equal 'world', result['author']['comments'].second['content']
375
+ end
376
+
377
+ test 'array nested inside array' do
378
+ result = jbuild do |json|
379
+ json.comments do
380
+ json.child! do
381
+ json.authors do
382
+ json.child! do
383
+ json.name 'david'
384
+ end
385
+ end
386
+ end
387
+ end
388
+ end
389
+
390
+ assert_equal 'david', result['comments'].first['authors'].first['name']
391
+ end
392
+
393
+ test 'directly set an array nested in another array' do
394
+ data = [ { :department => 'QA', :not_in_json => 'hello', :names => ['John', 'David'] } ]
395
+
396
+ result = jbuild do |json|
397
+ json.array! data do |object|
398
+ json.department object[:department]
399
+ json.names do
400
+ json.array! object[:names]
401
+ end
402
+ end
403
+ end
404
+
405
+ assert_equal 'David', result[0]['names'].last
406
+ assert !result[0].key?('not_in_json')
407
+ end
408
+
409
+ test 'nested jbuilder objects' do
410
+ to_nest = Jbuilder.new{ |json| json.nested_value 'Nested Test' }
411
+
412
+ result = jbuild do |json|
413
+ json.value 'Test'
414
+ json.nested to_nest
415
+ end
416
+
417
+ expected = {'value' => 'Test', 'nested' => {'nested_value' => 'Nested Test'}}
418
+ assert_equal expected, result
419
+ end
420
+
421
+ test 'nested jbuilder object via set!' do
422
+ to_nest = Jbuilder.new{ |json| json.nested_value 'Nested Test' }
423
+
424
+ result = jbuild do |json|
425
+ json.value 'Test'
426
+ json.set! :nested, to_nest
427
+ end
428
+
429
+ expected = {'value' => 'Test', 'nested' => {'nested_value' => 'Nested Test'}}
430
+ assert_equal expected, result
431
+ end
432
+
433
+ test 'top-level array' do
434
+ comments = [ Comment.new('hello', 1), Comment.new('world', 2) ]
435
+
436
+ result = jbuild do |json|
437
+ json.array! comments do |comment|
438
+ json.content comment.content
439
+ end
440
+ end
441
+
442
+ assert_equal 'hello', result.first['content']
443
+ assert_equal 'world', result.second['content']
444
+ end
445
+
446
+ test 'it allows using next in array block to skip value' do
447
+ comments = [ Comment.new('hello', 1), Comment.new('skip', 2), Comment.new('world', 3) ]
448
+ result = jbuild do |json|
449
+ json.array! comments do |comment|
450
+ next if comment.id == 2
451
+ json.content comment.content
452
+ end
453
+ end
454
+
455
+ assert_equal 2, result.length
456
+ assert_equal 'hello', result.first['content']
457
+ assert_equal 'world', result.second['content']
458
+ end
459
+
460
+ test 'extract attributes directly from array' do
461
+ comments = [ Comment.new('hello', 1), Comment.new('world', 2) ]
462
+
463
+ result = jbuild do |json|
464
+ json.array! comments, :content, :id
465
+ end
466
+
467
+ assert_equal 'hello', result.first['content']
468
+ assert_equal 1, result.first['id']
469
+ assert_equal 'world', result.second['content']
470
+ assert_equal 2, result.second['id']
471
+ end
472
+
473
+ test 'empty top-level array' do
474
+ comments = []
475
+
476
+ result = jbuild do |json|
477
+ json.array! comments do |comment|
478
+ json.content comment.content
479
+ end
480
+ end
481
+
482
+ assert_equal [], result
483
+ end
484
+
485
+ test 'dynamically set a key/value' do
486
+ result = jbuild do |json|
487
+ json.set! :each, 'stuff'
488
+ end
489
+
490
+ assert_equal 'stuff', result['each']
491
+ end
492
+
493
+ test 'dynamically set a key/nested child with block' do
494
+ result = jbuild do |json|
495
+ json.set! :author do
496
+ json.name 'David'
497
+ json.age 32
498
+ end
499
+ end
500
+
501
+ assert_equal 'David', result['author']['name']
502
+ assert_equal 32, result['author']['age']
503
+ end
504
+
505
+ test 'dynamically sets a collection' do
506
+ comments = [ Comment.new('hello', 1), Comment.new('world', 2) ]
507
+
508
+ result = jbuild do |json|
509
+ json.set! :comments, comments, :content
510
+ end
511
+
512
+ assert_equal ['content'], result['comments'].first.keys
513
+ assert_equal 'hello', result['comments'].first['content']
514
+ assert_equal 'world', result['comments'].second['content']
515
+ end
516
+
517
+ test 'query like object' do
518
+ result = jbuild do |json|
519
+ json.relations RelationMock.new, :name, :age
520
+ end
521
+
522
+ assert_equal 2, result['relations'].length
523
+ assert_equal 'Bob', result['relations'][0]['name']
524
+ assert_equal 50, result['relations'][1]['age']
525
+ end
526
+
527
+ test 'initialize via options hash' do
528
+ jbuilder = Jbuilder.new(key_formatter: 1, ignore_nil: 2)
529
+ assert_equal 1, jbuilder.instance_eval{ @key_formatter }
530
+ assert_equal 2, jbuilder.instance_eval{ @ignore_nil }
531
+ end
532
+
533
+ test 'key_format! with parameter' do
534
+ result = jbuild do |json|
535
+ json.key_format! camelize: [:lower]
536
+ json.camel_style 'for JS'
537
+ end
538
+
539
+ assert_equal ['camelStyle'], result.keys
540
+ end
541
+
542
+ test 'key_format! with parameter not as an array' do
543
+ result = jbuild do |json|
544
+ json.key_format! :camelize => :lower
545
+ json.camel_style 'for JS'
546
+ end
547
+
548
+ assert_equal ['camelStyle'], result.keys
549
+ end
550
+
551
+ test 'key_format! propagates to child elements' do
552
+ result = jbuild do |json|
553
+ json.key_format! :upcase
554
+ json.level1 'one'
555
+ json.level2 do
556
+ json.value 'two'
557
+ end
558
+ end
559
+
560
+ assert_equal 'one', result['LEVEL1']
561
+ assert_equal 'two', result['LEVEL2']['VALUE']
562
+ end
563
+
564
+ test 'key_format! resets after child element' do
565
+ result = jbuild do |json|
566
+ json.level2 do
567
+ json.key_format! :upcase
568
+ json.value 'two'
569
+ end
570
+ json.level1 'one'
571
+ end
572
+
573
+ assert_equal 'two', result['level2']['VALUE']
574
+ assert_equal 'one', result['level1']
575
+ end
576
+
577
+ test 'key_format! can be changed in child elements' do
578
+ result = jbuild do |json|
579
+ json.key_format! camelize: :lower
580
+
581
+ json.level_one do
582
+ json.key_format! :upcase
583
+ json.value 'two'
584
+ end
585
+ end
586
+
587
+ assert_equal ['levelOne'], result.keys
588
+ assert_equal ['VALUE'], result['levelOne'].keys
589
+ end
590
+
591
+ test 'key_format! can be changed in array!' do
592
+ result = jbuild do |json|
593
+ json.key_format! camelize: :lower
594
+
595
+ json.level_one do
596
+ json.array! [{value: 'two'}] do |object|
597
+ json.key_format! :upcase
598
+ json.value object[:value]
599
+ end
600
+ end
601
+ end
602
+
603
+ assert_equal ['levelOne'], result.keys
604
+ assert_equal ['VALUE'], result['levelOne'][0].keys
605
+ end
606
+
607
+ test 'key_format! with no parameter' do
608
+ result = jbuild do |json|
609
+ json.key_format! :upcase
610
+ json.lower 'Value'
611
+ end
612
+
613
+ assert_equal ['LOWER'], result.keys
614
+ end
615
+
616
+ test 'key_format! with multiple steps' do
617
+ result = jbuild do |json|
618
+ json.key_format! :upcase, :pluralize
619
+ json.pill 'foo'
620
+ end
621
+
622
+ assert_equal ['PILLs'], result.keys
623
+ end
624
+
625
+ test 'key_format! with lambda/proc' do
626
+ result = jbuild do |json|
627
+ json.key_format! ->(key){ key + ' and friends' }
628
+ json.oats 'foo'
629
+ end
630
+
631
+ assert_equal ['oats and friends'], result.keys
632
+ end
633
+
634
+ test 'key_format! is not applied deeply by default' do
635
+ names = { first_name: 'camel', last_name: 'case' }
636
+ result = jbuild do |json|
637
+ json.key_format! camelize: :lower
638
+ json.set! :all_names, names
639
+ end
640
+
641
+ assert_equal %i[first_name last_name], result['allNames'].keys
642
+ end
643
+
644
+ test 'applying key_format! deeply can be enabled per scope' do
645
+ names = { first_name: 'camel', last_name: 'case' }
646
+ result = jbuild do |json|
647
+ json.key_format! camelize: :lower
648
+ json.scope do
649
+ json.deep_format_keys!
650
+ json.set! :all_names, names
651
+ end
652
+ json.set! :all_names, names
653
+ end
654
+
655
+ assert_equal %w[firstName lastName], result['scope']['allNames'].keys
656
+ assert_equal %i[first_name last_name], result['allNames'].keys
657
+ end
658
+
659
+ test 'applying key_format! deeply can be disabled per scope' do
660
+ names = { first_name: 'camel', last_name: 'case' }
661
+ result = jbuild do |json|
662
+ json.key_format! camelize: :lower
663
+ json.deep_format_keys!
664
+ json.set! :all_names, names
665
+ json.scope do
666
+ json.deep_format_keys! false
667
+ json.set! :all_names, names
668
+ end
669
+ end
670
+
671
+ assert_equal %w[firstName lastName], result['allNames'].keys
672
+ assert_equal %i[first_name last_name], result['scope']['allNames'].keys
673
+ end
674
+
675
+ test 'applying key_format! deeply can be enabled globally' do
676
+ names = { first_name: 'camel', last_name: 'case' }
677
+
678
+ Jbuilder.deep_format_keys true
679
+ result = jbuild do |json|
680
+ json.key_format! camelize: :lower
681
+ json.set! :all_names, names
682
+ end
683
+
684
+ assert_equal %w[firstName lastName], result['allNames'].keys
685
+ Jbuilder.send(:class_variable_set, '@@deep_format_keys', false)
686
+ end
687
+
688
+ test 'deep key_format! with merge!' do
689
+ hash = { camel_style: 'for JS' }
690
+ result = jbuild do |json|
691
+ json.key_format! camelize: :lower
692
+ json.deep_format_keys!
693
+ json.merge! hash
694
+ end
695
+
696
+ assert_equal ['camelStyle'], result.keys
697
+ end
698
+
699
+ test 'deep key_format! with merge! deep' do
700
+ hash = { camel_style: { sub_attr: 'for JS' } }
701
+ result = jbuild do |json|
702
+ json.key_format! camelize: :lower
703
+ json.deep_format_keys!
704
+ json.merge! hash
705
+ end
706
+
707
+ assert_equal ['subAttr'], result['camelStyle'].keys
708
+ end
709
+
710
+ test 'deep key_format! with set! array of hashes' do
711
+ names = [{ first_name: 'camel', last_name: 'case' }]
712
+ result = jbuild do |json|
713
+ json.key_format! camelize: :lower
714
+ json.deep_format_keys!
715
+ json.set! :names, names
716
+ end
717
+
718
+ assert_equal %w[firstName lastName], result['names'][0].keys
719
+ end
720
+
721
+ test 'deep key_format! with set! extracting hash from object' do
722
+ comment = Struct.new(:author).new({ first_name: 'camel', last_name: 'case' })
723
+ result = jbuild do |json|
724
+ json.key_format! camelize: :lower
725
+ json.deep_format_keys!
726
+ json.set! :comment, comment, :author
727
+ end
728
+
729
+ assert_equal %w[firstName lastName], result['comment']['author'].keys
730
+ end
731
+
732
+ test 'deep key_format! with array! of hashes' do
733
+ names = [{ first_name: 'camel', last_name: 'case' }]
734
+ result = jbuild do |json|
735
+ json.key_format! camelize: :lower
736
+ json.deep_format_keys!
737
+ json.array! names
738
+ end
739
+
740
+ assert_equal %w[firstName lastName], result[0].keys
741
+ end
742
+
743
+ test 'deep key_format! with merge! array of hashes' do
744
+ names = [{ first_name: 'camel', last_name: 'case' }]
745
+ new_names = [{ first_name: 'snake', last_name: 'case' }]
746
+ result = jbuild do |json|
747
+ json.key_format! camelize: :lower
748
+ json.deep_format_keys!
749
+ json.array! names
750
+ json.merge! new_names
751
+ end
752
+
753
+ assert_equal %w[firstName lastName], result[1].keys
754
+ end
755
+
756
+ test 'deep key_format! is applied to hash extracted from object' do
757
+ comment = Struct.new(:author).new({ first_name: 'camel', last_name: 'case' })
758
+ result = jbuild do |json|
759
+ json.key_format! camelize: :lower
760
+ json.deep_format_keys!
761
+ json.extract! comment, :author
762
+ end
763
+
764
+ assert_equal %w[firstName lastName], result['author'].keys
765
+ end
766
+
767
+ test 'deep key_format! is applied to hash extracted from hash' do
768
+ comment = {author: { first_name: 'camel', last_name: 'case' }}
769
+ result = jbuild do |json|
770
+ json.key_format! camelize: :lower
771
+ json.deep_format_keys!
772
+ json.extract! comment, :author
773
+ end
774
+
775
+ assert_equal %w[firstName lastName], result['author'].keys
776
+ end
777
+
778
+ test 'deep key_format! is applied to hash extracted directly from array' do
779
+ comments = [Struct.new(:author).new({ first_name: 'camel', last_name: 'case' })]
780
+ result = jbuild do |json|
781
+ json.key_format! camelize: :lower
782
+ json.deep_format_keys!
783
+ json.array! comments, :author
784
+ end
785
+
786
+ assert_equal %w[firstName lastName], result[0]['author'].keys
787
+ end
788
+
789
+ test 'default key_format!' do
790
+ Jbuilder.key_format camelize: :lower
791
+ result = jbuild{ |json| json.camel_style 'for JS' }
792
+ assert_equal ['camelStyle'], result.keys
793
+ end
794
+
795
+ test 'use default key formatter when configured' do
796
+ Jbuilder.key_format
797
+ jbuild{ |json| json.key 'value' }
798
+ formatter = Jbuilder.send(:class_variable_get, '@@key_formatter')
799
+ cache = formatter.instance_variable_get('@cache')
800
+ assert_includes cache, :key
801
+ end
802
+
803
+ test 'ignore_nil! without a parameter' do
804
+ result = jbuild do |json|
805
+ json.ignore_nil!
806
+ json.test nil
807
+ end
808
+
809
+ assert_empty result.keys
810
+ end
811
+
812
+ test 'ignore_nil! with parameter' do
813
+ result = jbuild do |json|
814
+ json.ignore_nil! true
815
+ json.name 'Bob'
816
+ json.dne nil
817
+ end
818
+
819
+ assert_equal ['name'], result.keys
820
+
821
+ result = jbuild do |json|
822
+ json.ignore_nil! false
823
+ json.name 'Bob'
824
+ json.dne nil
825
+ end
826
+
827
+ assert_equal ['name', 'dne'], result.keys
828
+ end
829
+
830
+ test 'default ignore_nil!' do
831
+ Jbuilder.ignore_nil
832
+
833
+ result = jbuild do |json|
834
+ json.name 'Bob'
835
+ json.dne nil
836
+ end
837
+
838
+ assert_equal ['name'], result.keys
839
+ Jbuilder.send(:class_variable_set, '@@ignore_nil', false)
840
+ end
841
+
842
+ test 'nil!' do
843
+ result = jbuild do |json|
844
+ json.key 'value'
845
+ json.nil!
846
+ end
847
+
848
+ assert_nil result
849
+ end
850
+
851
+ test 'null!' do
852
+ result = jbuild do |json|
853
+ json.key 'value'
854
+ json.null!
855
+ end
856
+
857
+ assert_nil result
858
+ end
859
+
860
+ test 'null! in a block' do
861
+ result = jbuild do |json|
862
+ json.author do
863
+ json.name 'David'
864
+ end
865
+
866
+ json.author do
867
+ json.null!
868
+ end
869
+ end
870
+
871
+ assert result.key?('author')
872
+ assert_nil result['author']
873
+ end
874
+
875
+ test 'empty attributes respond to empty?' do
876
+ attributes = Jbuilder.new.attributes!
877
+ assert attributes.empty?
878
+ assert attributes.blank?
879
+ assert !attributes.present?
880
+ end
881
+
882
+ test 'throws ArrayError when trying to add a key to an array' do
883
+ assert_raise Jbuilder::ArrayError do
884
+ jbuild do |json|
885
+ json.array! %w[foo bar]
886
+ json.fizz "buzz"
887
+ end
888
+ end
889
+ end
890
+
891
+ test 'throws NullError when trying to add properties to null' do
892
+ assert_raise Jbuilder::NullError do
893
+ jbuild do |json|
894
+ json.null!
895
+ json.foo 'bar'
896
+ end
897
+ end
898
+ end
899
+
900
+ test 'throws NullError when trying to add properties to null using block syntax' do
901
+ assert_raise Jbuilder::NullError do
902
+ jbuild do |json|
903
+ json.author do
904
+ json.null!
905
+ end
906
+
907
+ json.author do
908
+ json.name "Pavel"
909
+ end
910
+ end
911
+ end
912
+ end
913
+
914
+ test "throws MergeError when trying to merge array with non-empty hash" do
915
+ assert_raise Jbuilder::MergeError do
916
+ jbuild do |json|
917
+ json.name "Daniel"
918
+ json.merge! []
919
+ end
920
+ end
921
+ end
922
+
923
+ test "throws MergeError when trying to merge hash with array" do
924
+ assert_raise Jbuilder::MergeError do
925
+ jbuild do |json|
926
+ json.array!
927
+ json.merge!({})
928
+ end
929
+ end
930
+ end
931
+
932
+ test "throws MergeError when trying to merge invalid objects" do
933
+ assert_raise Jbuilder::MergeError do
934
+ jbuild do |json|
935
+ json.name "Daniel"
936
+ json.merge! "Nope"
937
+ end
938
+ end
939
+ end
940
+
941
+ test "respects JSON encoding customizations" do
942
+ # Active Support overrides Time#as_json for custom formatting.
943
+ # Ensure we call #to_json on the final attributes instead of JSON.dump.
944
+ result = JSON.load(Jbuilder.encode { |json| json.time Time.parse("2018-05-13 11:51:00.485 -0400") })
945
+ assert_equal "2018-05-13T11:51:00.485-04:00", result["time"]
946
+ end
947
+
948
+ test "encode forwards options to new" do
949
+ Jbuilder.encode(key_formatter: 1, ignore_nil: 2) do |json|
950
+ assert_equal 1, json.instance_eval{ @key_formatter }
951
+ assert_equal 2, json.instance_eval{ @ignore_nil }
952
+ end
953
+ end
954
+ end