representable 3.3.1 → 3.4.0

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 (67) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGES.md +6 -0
  3. data/Gemfile +0 -1
  4. data/README.md +1 -7
  5. data/lib/representable/json.rb +11 -14
  6. data/lib/representable/version.rb +1 -1
  7. data/representable.gemspec +1 -2
  8. metadata +1 -60
  9. data/test/as_test.rb +0 -60
  10. data/test/benchmarking.rb +0 -78
  11. data/test/binding_test.rb +0 -54
  12. data/test/cached_test.rb +0 -177
  13. data/test/class_test.rb +0 -132
  14. data/test/coercion_test.rb +0 -73
  15. data/test/config/inherit_test.rb +0 -130
  16. data/test/config_test.rb +0 -120
  17. data/test/decorator_scope_test.rb +0 -30
  18. data/test/decorator_test.rb +0 -109
  19. data/test/default_test.rb +0 -34
  20. data/test/defaults_options_test.rb +0 -121
  21. data/test/definition_test.rb +0 -253
  22. data/test/examples/example.rb +0 -314
  23. data/test/examples/object.rb +0 -28
  24. data/test/exec_context_test.rb +0 -88
  25. data/test/features_test.rb +0 -89
  26. data/test/filter_test.rb +0 -66
  27. data/test/for_collection_test.rb +0 -71
  28. data/test/generic_test.rb +0 -109
  29. data/test/getter_setter_test.rb +0 -26
  30. data/test/hash_bindings_test.rb +0 -96
  31. data/test/hash_test.rb +0 -197
  32. data/test/heritage_test.rb +0 -64
  33. data/test/if_test.rb +0 -83
  34. data/test/include_exclude_test.rb +0 -96
  35. data/test/inherit_test.rb +0 -246
  36. data/test/inline_test.rb +0 -281
  37. data/test/instance_test.rb +0 -289
  38. data/test/is_representable_test.rb +0 -79
  39. data/test/json_test.rb +0 -379
  40. data/test/lonely_test.rb +0 -241
  41. data/test/nested_test.rb +0 -112
  42. data/test/object_test.rb +0 -60
  43. data/test/option_test.rb +0 -38
  44. data/test/parse_pipeline_test.rb +0 -68
  45. data/test/pipeline_test.rb +0 -309
  46. data/test/populator_test.rb +0 -108
  47. data/test/prepare_test.rb +0 -64
  48. data/test/private_options_test.rb +0 -19
  49. data/test/reader_writer_test.rb +0 -19
  50. data/test/realistic_benchmark.rb +0 -96
  51. data/test/render_nil_test.rb +0 -21
  52. data/test/represent_test.rb +0 -84
  53. data/test/representable_test.rb +0 -526
  54. data/test/schema_test.rb +0 -175
  55. data/test/serialize_deserialize_test.rb +0 -33
  56. data/test/skip_test.rb +0 -92
  57. data/test/stringify_hash_test.rb +0 -39
  58. data/test/test_helper.rb +0 -153
  59. data/test/test_helper_test.rb +0 -25
  60. data/test/uncategorized_test.rb +0 -72
  61. data/test/user_options_test.rb +0 -24
  62. data/test/wrap_test.rb +0 -162
  63. data/test/xml_assertions.rb +0 -29
  64. data/test/xml_bindings_test.rb +0 -55
  65. data/test/xml_namespace_test.rb +0 -193
  66. data/test/xml_test.rb +0 -584
  67. data/test/yaml_test.rb +0 -209
data/test/xml_test.rb DELETED
@@ -1,584 +0,0 @@
1
- require "test_helper"
2
-
3
- class XmlPublicMethodsTest < Minitest::Spec
4
- #---
5
- # from_hash
6
- class BandRepresenter < Representable::Decorator
7
- include Representable::XML
8
- property :id
9
- property :name
10
- end
11
-
12
- let(:data) { %{<band><id>1</id><name>Rancid</name></band>} }
13
-
14
- it { _(BandRepresenter.new(Band.new).from_xml(data)[:id, :name]).must_equal %w[1 Rancid] }
15
- it { _(BandRepresenter.new(Band.new).parse(data)[:id, :name]).must_equal %w[1 Rancid] }
16
-
17
- #---
18
- # to_hash
19
- let(:band) { Band.new("1", "Rancid") }
20
-
21
- it { assert_xml_equal(BandRepresenter.new(band).to_xml, data) }
22
- it { assert_xml_equal(BandRepresenter.new(band).render, data) }
23
- end
24
-
25
- class XmlTest < Minitest::Spec
26
- class Band
27
- include Representable::XML
28
- property :name
29
- attr_accessor :name
30
-
31
- def initialize(name = nil)
32
- name and self.name = name
33
- end
34
- end
35
-
36
- XML = Representable::XML
37
- Def = Representable::Definition
38
-
39
- describe "Xml module" do
40
- before do
41
- @Band = Class.new do
42
- include Representable::XML
43
- self.representation_wrap = :band
44
- property :name
45
- property :label
46
- attr_accessor :name, :label
47
- end
48
-
49
- @band = @Band.new
50
- end
51
-
52
- describe "#from_xml" do
53
- before do
54
- @band = @Band.new
55
- @xml = %{<band><name>Nofx</name><label>NOFX</label></band>}
56
- end
57
-
58
- it "parses XML and assigns properties" do
59
- @band.from_xml(@xml)
60
- assert_equal %w[Nofx NOFX], [@band.name, @band.label]
61
- end
62
- end
63
-
64
- describe "#from_xml with remove_namespaces! and xmlns present" do
65
- before do
66
- @Band.remove_namespaces!
67
- @band = @Band.new
68
- @xml = %{<band xmlns="exists"><name>Nofx</name><label>NOFX</label></band>}
69
- end
70
-
71
- it "parses with xmlns present" do
72
- @band.from_xml(@xml)
73
- assert_equal %w[Nofx NOFX], [@band.name, @band.label]
74
- end
75
- end
76
-
77
- describe "#from_node" do
78
- before do
79
- @band = @Band.new
80
- @xml = Nokogiri::XML(%{<band><name>Nofx</name><label>NOFX</label></band>}).root
81
- end
82
-
83
- it "receives Nokogiri node and assigns properties" do
84
- @band.from_node(@xml)
85
- assert_equal %w[Nofx NOFX], [@band.name, @band.label]
86
- end
87
- end
88
-
89
- describe "#to_xml" do
90
- it "delegates to #to_node and returns string" do
91
- assert_xml_equal(Band.new("Rise Against").to_xml, "<band><name>Rise Against</name></band>")
92
- end
93
- end
94
-
95
- describe "#to_node" do
96
- it "returns Nokogiri node" do
97
- node = Band.new("Rise Against").to_node
98
- assert_kind_of Nokogiri::XML::Element, node
99
- end
100
-
101
- it "wraps with infered class name per default" do
102
- node = Band.new("Rise Against").to_node
103
- assert_xml_equal(node.to_s, "<band><name>Rise Against</name></band>")
104
- end
105
-
106
- it "respects #representation_wrap=" do
107
- klass = Class.new(Band) do
108
- include Representable
109
- property :name
110
- end
111
-
112
- klass.representation_wrap = :group
113
- assert_xml_equal(klass.new("Rise Against").to_node.to_s, "<group><name>Rise Against</name></group>")
114
- end
115
- end
116
-
117
- describe "XML::Binding#build_for" do
118
- it "returns AttributeBinding" do
119
- assert_kind_of XML::Binding::Attribute, XML::Binding.build_for(Def.new(:band, :as => "band", :attribute => true))
120
- end
121
-
122
- it "returns Binding" do
123
- assert_kind_of XML::Binding, XML::Binding.build_for(Def.new(:band, :class => Hash))
124
- assert_kind_of XML::Binding, XML::Binding.build_for(Def.new(:band, :as => :content))
125
- end
126
-
127
- it "returns CollectionBinding" do
128
- assert_kind_of XML::Binding::Collection, XML::Binding.build_for(Def.new(:band, :collection => true))
129
- end
130
-
131
- it "returns HashBinding" do
132
- assert_kind_of XML::Binding::Hash, XML::Binding.build_for(Def.new(:band, :hash => true))
133
- end
134
- end
135
-
136
- describe "DCI" do
137
- module SongRepresenter
138
- include Representable::XML
139
- property :name
140
- end
141
-
142
- module AlbumRepresenter
143
- include Representable::XML
144
- property :best_song, :class => Song, :extend => SongRepresenter
145
- collection :songs, :class => Song, :as => :song, :extend => SongRepresenter
146
- end
147
-
148
- it "allows adding the representer by using #extend" do
149
- module BandRepresenter
150
- include Representable::XML
151
- property :name
152
- end
153
-
154
- civ = Object.new
155
- civ.instance_eval do
156
- def name; "CIV"; end
157
-
158
- def name=(v)
159
- @name = v
160
- end
161
- end
162
-
163
- civ.extend(BandRepresenter)
164
- assert_xml_equal(civ.to_xml, "<object><name>CIV</name></object>")
165
- end
166
-
167
- it "extends contained models when serializing" do
168
- @album = Album.new(
169
- [Song.new("I Hate My Brain"), mr = Song.new("Mr. Charisma")], mr
170
- )
171
- @album.extend(AlbumRepresenter)
172
-
173
- assert_xml_equal(@album.to_xml, "<album>
174
- <song><name>Mr. Charisma</name></song>
175
- <song><name>I Hate My Brain</name></song>
176
- <song><name>Mr. Charisma</name></song>
177
- </album>")
178
- end
179
-
180
- it "extends contained models when deserializing" do
181
- @album = Album.new
182
- @album.extend(AlbumRepresenter)
183
-
184
- @album.from_xml("<album><best_song><name>Mr. Charisma</name></best_song><song><name>I Hate My Brain</name></song><song><name>Mr. Charisma</name></song></album>")
185
- assert_equal "Mr. Charisma", @album.best_song.name
186
- end
187
- end
188
- end
189
- end
190
-
191
- class AttributesTest < Minitest::Spec
192
- describe ":as => rel, :attribute => true" do
193
- class Link
194
- include Representable::XML
195
- property :href, :as => "href", :attribute => true
196
- property :title, :as => "title", :attribute => true
197
- attr_accessor :href, :title
198
- end
199
-
200
- it "#from_xml creates correct accessors" do
201
- link = Link.new.from_xml(
202
- %{
203
- <a href="http://apotomo.de" title="Home, sweet home" />
204
- }
205
- )
206
- assert_equal "http://apotomo.de", link.href
207
- assert_equal "Home, sweet home", link.title
208
- end
209
-
210
- it "#to_xml serializes correctly" do
211
- link = Link.new
212
- link.href = "http://apotomo.de/"
213
-
214
- assert_xml_equal(link.to_xml, %{<link href="http://apotomo.de/">})
215
- end
216
- end
217
- end
218
-
219
- class CDataBand
220
- class CData < Representable::XML::Binding
221
- def serialize_node(parent, _value)
222
- parent << Nokogiri::XML::CDATA.new(parent, represented.name)
223
- end
224
- end
225
-
226
- include Representable::XML
227
- property :name, :binding => ->(*args) {
228
- CData.new(*args)
229
- } # getter: lambda { |opt| Nokogiri::XML::CDATA.new(opt[:doc], name) }
230
- attr_accessor :name
231
-
232
- def initialize(name = nil)
233
- name and self.name = name
234
- end
235
- end
236
-
237
- class TypedPropertyTest < Minitest::Spec
238
- class Band
239
- include Representable::XML
240
- property :name
241
- attr_accessor :name
242
-
243
- def initialize(name = nil)
244
- name and self.name = name
245
- end
246
- end
247
-
248
- module AlbumRepresenter
249
- include Representable::XML
250
- property :band, :class => Band
251
- end
252
-
253
- class Album
254
- attr_accessor :band
255
-
256
- def initialize(band = nil)
257
- @band = band
258
- end
259
- end
260
-
261
- # TODO: property :group, :class => Band
262
- # :class
263
- # where to mixin DCI?
264
- describe ":class => Item" do
265
- it "#from_xml creates one Item instance" do
266
- album = Album.new.extend(AlbumRepresenter).from_xml(
267
- %{
268
- <album>
269
- <band><name>Bad Religion</name></band>
270
- </album>
271
- }
272
- )
273
- assert_equal "Bad Religion", album.band.name
274
- end
275
-
276
- describe "#to_xml" do
277
- it "doesn't escape xml from Band#to_xml" do
278
- band = Band.new("Bad Religion")
279
- album = Album.new(band).extend(AlbumRepresenter)
280
-
281
- assert_xml_equal(album.to_xml, %{<album>
282
- <band>
283
- <name>Bad Religion</name>
284
- </band>
285
- </album>})
286
- end
287
-
288
- it "doesn't escape and wrap string from Band#to_node" do
289
- band = Band.new("Bad Religion")
290
- band.instance_eval do
291
- def to_node(*)
292
- "<band>Baaaad Religion</band>"
293
- end
294
- end
295
-
296
- assert_xml_equal(Album.new(band).extend(AlbumRepresenter).to_xml, %{<album><band>Baaaad Religion</band></album>})
297
- end
298
- end
299
-
300
- describe "#to_xml with CDATA" do
301
- it "wraps Band name in CDATA#to_xml" do
302
- band = CDataBand.new("Bad Religion")
303
- album = Album.new(band).extend(AlbumRepresenter)
304
-
305
- assert_xml_equal(album.to_xml, %{<album>
306
- <c_data_band>
307
- <name><![CDATA[Bad Religion]]></name>
308
- </c_data_band>
309
- </album>})
310
- end
311
- end
312
- end
313
- end
314
-
315
- # TODO: add parsing tests.
316
- class XMLPropertyTest < Minitest::Spec
317
- Band = Struct.new(:name, :genre)
318
- Manager = Struct.new(:managed)
319
-
320
- #---
321
- #- :as with scalar
322
- class BandRepresenter < Representable::Decorator
323
- include Representable::XML
324
- property :name, as: :theyCallUs
325
- property :genre, attribute: true
326
- end
327
-
328
- it { assert_xml_equal(BandRepresenter.new(Band.new("Mute")).to_xml, %{<band><theyCallUs>Mute</theyCallUs></band>}) }
329
-
330
- class ManagerRepresenter < Representable::Decorator
331
- include Representable::XML
332
- property :managed, as: :band, decorator: BandRepresenter
333
- end
334
-
335
- #- :as with nested property
336
- it {
337
- assert_xml_equal(ManagerRepresenter.new(
338
- Manager.new(
339
- Band.new(
340
- "Mute",
341
- "Punkrock"
342
- )
343
- )
344
- ).to_xml, %{<manager><band genre="Punkrock"><theyCallUs>Mute</theyCallUs></band></manager>})
345
- }
346
- end
347
-
348
- class XMLCollectionTest < Minitest::Spec
349
- Band = Struct.new(:name)
350
- Compilation = Struct.new(:bands)
351
-
352
- class BandRepresenter < Representable::Decorator
353
- include Representable::XML
354
- property :name
355
- end
356
-
357
- #---
358
- #- :as, :decorator, :class
359
- describe ":class => Band, :as => :band, :collection => true" do
360
- class CompilationRepresenter < Representable::Decorator
361
- include Representable::XML
362
- collection :bands, class: Band, as: :group, decorator: BandRepresenter
363
- end
364
-
365
- describe "#from_xml" do
366
- it "pushes collection items to array" do
367
- cd = CompilationRepresenter.new(Compilation.new).from_xml(
368
- %{
369
- <compilation>
370
- <group><name>Diesel Boy</name></group>
371
- <group><name>Cobra Skulls</name></group>
372
- </compilation>
373
- }
374
- )
375
- assert_equal ["Cobra Skulls", "Diesel Boy"], cd.bands.map(&:name).sort
376
- end
377
- end
378
-
379
- it "responds to #to_xml" do
380
- cd = Compilation.new([Band.new("Diesel Boy"), Band.new("Bad Religion")])
381
-
382
- assert_xml_equal(CompilationRepresenter.new(cd).to_xml, %{<compilation>
383
- <group><name>Diesel Boy</name></group>
384
- <group><name>Bad Religion</name></group>
385
- </compilation>})
386
- end
387
- end
388
-
389
- describe ":as" do
390
- let(:xml_doc) do
391
- Module.new do
392
- include Representable::XML
393
- collection :songs, :as => :song
394
- end
395
- end
396
-
397
- it "collects untyped items" do
398
- album = Album.new.extend(xml_doc).from_xml(
399
- %{
400
- <album>
401
- <song>Two Kevins</song>
402
- <song>Wright and Rong</song>
403
- <song>Laundry Basket</song>
404
- </album>
405
- }
406
- )
407
- assert_equal ["Laundry Basket", "Two Kevins", "Wright and Rong"].sort, album.songs.sort
408
- end
409
- end
410
-
411
- describe ":wrap" do
412
- let(:album) { Album.new.extend(xml_doc) }
413
- let(:xml_doc) do
414
- Module.new do
415
- include Representable::XML
416
- collection :songs, :as => :song, :wrap => :songs
417
- end
418
- end
419
-
420
- describe "#from_xml" do
421
- it "finds items in wrapped collection" do
422
- album.from_xml(
423
- %{
424
- <album>
425
- <songs>
426
- <song>Two Kevins</song>
427
- <song>Wright and Rong</song>
428
- <song>Laundry Basket</song>
429
- </songs>
430
- </album>
431
- }
432
- )
433
- assert_equal ["Laundry Basket", "Two Kevins", "Wright and Rong"].sort, album.songs.sort
434
- end
435
- end
436
-
437
- describe "#to_xml" do
438
- it "wraps items" do
439
- album.songs = ["Laundry Basket", "Two Kevins", "Wright and Rong"]
440
- assert_xml_equal(album.to_xml, %{
441
- <album>
442
- <songs>
443
- <song>Laundry Basket</song>
444
- <song>Two Kevins</song>
445
- <song>Wright and Rong</song>
446
- </songs>
447
- </album>
448
- })
449
- end
450
- end
451
- end
452
-
453
- require "representable/xml/hash"
454
- class LonelyRepresenterTest < Minitest::Spec
455
- # TODO: where is the XML::Hash test?
456
- module SongRepresenter
457
- include Representable::XML
458
- property :name
459
- self.representation_wrap = :song
460
- end
461
-
462
- let(:decorator) { rpr = representer; Class.new(Representable::Decorator) { include Representable::XML; include rpr } }
463
-
464
- describe "XML::Collection" do
465
- describe "with contained objects" do
466
- representer!(:module => Representable::XML::Collection) do
467
- items :class => Song, :extend => SongRepresenter
468
- self.representation_wrap = :songs
469
- end
470
-
471
- let(:songs) { [Song.new("Days Go By"), Song.new("Can't Take Them All")] }
472
- let(:xml_doc) { "<songs><song><name>Days Go By</name></song><song><name>Can't Take Them All</name></song></songs>" }
473
-
474
- it "renders array" do
475
- assert_xml_equal(songs.extend(representer).to_xml, xml_doc)
476
- end
477
-
478
- it "renders array with decorator" do
479
- assert_xml_equal(decorator.new(songs).to_xml, xml_doc)
480
- end
481
-
482
- it "parses array" do
483
- _([].extend(representer).from_xml(xml_doc)).must_equal songs
484
- end
485
-
486
- it "parses array with decorator" do
487
- _(decorator.new([]).from_xml(xml_doc)).must_equal songs
488
- end
489
- end
490
- end
491
-
492
- describe "XML::AttributeHash" do # TODO: move to HashTest.
493
- representer!(:module => Representable::XML::AttributeHash) do
494
- self.representation_wrap = :songs
495
- end
496
-
497
- let(:songs) { {"one" => "Graveyards", "two" => "Can't Take Them All"} }
498
- let(:xml_doc) { "<songs one=\"Graveyards\" two=\"Can't Take Them All\" />" }
499
-
500
- describe "#to_xml" do
501
- it "renders hash" do
502
- assert_xml_equal(songs.extend(representer).to_xml, xml_doc)
503
- end
504
-
505
- it "respects :exclude" do
506
- assert_xml_equal(songs.extend(representer).to_xml(:exclude => [:one]), "<songs two=\"Can't Take Them All\" />")
507
- end
508
-
509
- it "respects :include" do
510
- assert_xml_equal(songs.extend(representer).to_xml(:include => [:two]), "<songs two=\"Can't Take Them All\" />")
511
- end
512
-
513
- it "renders hash with decorator" do
514
- assert_xml_equal(decorator.new(songs).to_xml, xml_doc)
515
- end
516
- end
517
-
518
- describe "#from_json" do
519
- it "returns hash" do
520
- _({}.extend(representer).from_xml(xml_doc)).must_equal songs
521
- end
522
-
523
- it "respects :exclude" do
524
- assert_equal({"two" => "Can't Take Them All"}, {}.extend(representer).from_xml(xml_doc, :exclude => [:one]))
525
- end
526
-
527
- it "respects :include" do
528
- assert_equal({"one" => "Graveyards"}, {}.extend(representer).from_xml(xml_doc, :include => [:one]))
529
- end
530
-
531
- it "parses hash with decorator" do
532
- _(decorator.new({}).from_xml(xml_doc)).must_equal songs
533
- end
534
- end
535
- end
536
- end
537
- end
538
-
539
- class XmlHashTest < Minitest::Spec
540
- # scalar, no object
541
- describe "plain text" do
542
- representer!(module: Representable::XML) do
543
- hash :songs
544
- end
545
-
546
- let(:doc) { "<open_struct><first>The Gargoyle</first><second>Bronx</second></open_struct>" }
547
-
548
- # to_xml
549
- it { assert_xml_equal(OpenStruct.new(songs: {"first" => "The Gargoyle", "second" => "Bronx"}).extend(representer).to_xml, doc) }
550
- # FIXME: this NEVER worked!
551
- # it { OpenStruct.new.extend(representer).from_xml(doc).songs.must_equal({"first" => "The Gargoyle", "second" => "Bronx"}) }
552
- end
553
-
554
- describe "with objects" do
555
- representer!(module: Representable::XML) do
556
- hash :songs, class: OpenStruct do
557
- property :title
558
- end
559
- end
560
-
561
- let(:doc) do
562
- "<open_struct>
563
- <open_struct>
564
- <title>The Gargoyle</title>
565
- </open_struct>
566
- <open_struct>
567
- <title>Bronx</title>
568
- </open_struct>
569
- </open_struct>"
570
- end
571
-
572
- # to_xml
573
- it {
574
- assert_xml_equal(OpenStruct.new(
575
- songs: {
576
- "first" => OpenStruct.new(title: "The Gargoyle"),
577
- "second" => OpenStruct.new(title: "Bronx")
578
- }
579
- ).extend(representer).to_xml, doc)
580
- }
581
- # FIXME: this NEVER worked!
582
- # it { OpenStruct.new.extend(representer).from_xml(doc).songs.must_equal({"first" => "The Gargoyle", "second" => "Bronx"}) }
583
- end
584
- end