File: xml_test.rb

package info (click to toggle)
ruby-representable 3.0.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 896 kB
  • sloc: ruby: 6,432; makefile: 3
file content (563 lines) | stat: -rw-r--r-- 16,189 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
require 'test_helper'


class XmlPublicMethodsTest < Minitest::Spec
  #---
  # from_hash
  class BandRepresenter < Representable::Decorator
    include Representable::XML
    property :id
    property :name
  end

  let(:data) { %{<data><id>1</id><name>Rancid</name></data>} }

  it { BandRepresenter.new(Band.new).from_xml(data)[:id, :name].must_equal ["1", "Rancid"] }
  it { BandRepresenter.new(Band.new).parse(data)[:id, :name].must_equal ["1", "Rancid"] }

  #---
  # to_hash
  let(:band) { Band.new("1", "Rancid") }

  it { BandRepresenter.new(band).to_xml.must_equal_xml data }
  it { BandRepresenter.new(band).render.must_equal_xml data }
end

class XmlTest < Minitest::Spec

  class Band
    include Representable::XML
    property :name
    attr_accessor :name

    def initialize(name=nil)
      name and self.name = name
    end
  end


  XML = Representable::XML
  Def = Representable::Definition

  describe "Xml module" do
    before do
      @Band = Class.new do
        include Representable::XML
        self.representation_wrap = :band
        property :name
        property :label
        attr_accessor :name, :label
      end

      @band = @Band.new
    end


    describe "#from_xml" do
      before do
        @band = @Band.new
        @xml  = %{<band><name>Nofx</name><label>NOFX</label></band>}
      end

      it "parses XML and assigns properties" do
        @band.from_xml(@xml)
        assert_equal ["Nofx", "NOFX"], [@band.name, @band.label]
      end
    end

    describe "#from_xml with remove_namespaces! and xmlns present" do
      before do
        @Band.remove_namespaces!
        @band = @Band.new
        @xml = %{<band xmlns="exists"><name>Nofx</name><label>NOFX</label></band>}
      end

      it "parses with xmlns present" do
        @band.from_xml(@xml)
        assert_equal ["Nofx", "NOFX"], [@band.name, @band.label]
      end
    end

    describe "#from_node" do
      before do
        @band = @Band.new
        @xml  = Nokogiri::XML(%{<band><name>Nofx</name><label>NOFX</label></band>}).root
      end

      it "receives Nokogiri node and assigns properties" do
        @band.from_node(@xml)
        assert_equal ["Nofx", "NOFX"], [@band.name, @band.label]
      end
    end


    describe "#to_xml" do
      it "delegates to #to_node and returns string" do
        assert_xml_equal "<band><name>Rise Against</name></band>", Band.new("Rise Against").to_xml
      end
    end


    describe "#to_node" do
      it "returns Nokogiri node" do
        node = Band.new("Rise Against").to_node
        assert_kind_of Nokogiri::XML::Element, node
      end

      it "wraps with infered class name per default" do
        node = Band.new("Rise Against").to_node
        assert_xml_equal "<band><name>Rise Against</name></band>", node.to_s
      end

      it "respects #representation_wrap=" do
        klass = Class.new(Band) do
          include Representable
          property :name
        end

        klass.representation_wrap = :group
        assert_xml_equal "<group><name>Rise Against</name></group>", klass.new("Rise Against").to_node.to_s
      end
    end


    describe "XML::Binding#build_for" do
      it "returns AttributeBinding" do
        assert_kind_of XML::Binding::Attribute, XML::Binding.build_for(Def.new(:band, :as => "band", :attribute => true))
      end

      it "returns Binding" do
        assert_kind_of XML::Binding, XML::Binding.build_for(Def.new(:band, :class => Hash))
        assert_kind_of XML::Binding, XML::Binding.build_for(Def.new(:band, :as => :content))
      end

      it "returns CollectionBinding" do
        assert_kind_of XML::Binding::Collection, XML::Binding.build_for(Def.new(:band, :collection => :true))
      end

      it "returns HashBinding" do
        assert_kind_of XML::Binding::Hash, XML::Binding.build_for(Def.new(:band, :hash => :true))
      end
    end


    describe "DCI" do
      module SongRepresenter
        include Representable::XML
        property :name
      end

      module AlbumRepresenter
        include Representable::XML
        property :best_song, :class => Song, :extend => SongRepresenter
        collection :songs, :class => Song, :as => :song, :extend => SongRepresenter
      end


      it "allows adding the representer by using #extend" do
        module BandRepresenter
          include Representable::XML
          property :name
        end

        civ = Object.new
        civ.instance_eval do
          def name; "CIV"; end
          def name=(v)
            @name = v
          end
        end

        civ.extend(BandRepresenter)
        assert_xml_equal "<object><name>CIV</name></object>", civ.to_xml
      end

      it "extends contained models when serializing" do
        @album = Album.new(
          [Song.new("I Hate My Brain"), mr=Song.new("Mr. Charisma")], mr)
        @album.extend(AlbumRepresenter)

        @album.to_xml.must_equal_xml "<album>
  <song><name>Mr. Charisma</name></song>
  <song><name>I Hate My Brain</name></song>
  <song><name>Mr. Charisma</name></song>
</album>"
      end

      it "extends contained models when deserializing" do
        @album = Album.new
        @album.extend(AlbumRepresenter)

        @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>")
        assert_equal "Mr. Charisma", @album.best_song.name
      end
    end
  end
end


class AttributesTest < Minitest::Spec
  describe ":as => rel, :attribute => true" do
    class Link
      include Representable::XML
      property :href,   :as => "href",  :attribute => true
      property :title,  :as => "title", :attribute => true
      attr_accessor :href, :title
    end

    it "#from_xml creates correct accessors" do
      link = Link.new.from_xml(%{
        <a href="http://apotomo.de" title="Home, sweet home" />
      })
      assert_equal "http://apotomo.de", link.href
      assert_equal "Home, sweet home",  link.title
    end

    it "#to_xml serializes correctly" do
      link = Link.new
      link.href = "http://apotomo.de/"

      assert_xml_equal %{<link href="http://apotomo.de/">}, link.to_xml
    end
  end
end


class CDataBand
  class CData < Representable::XML::Binding
    def serialize_node(parent, value)
      parent << Nokogiri::XML::CDATA.new(parent, represented.name)
    end
  end

  include Representable::XML
  property :name, :binding => lambda { |*args| CData.new(*args) }#getter: lambda { |opt| Nokogiri::XML::CDATA.new(opt[:doc], name) }
  attr_accessor :name

  def initialize(name=nil)
    name and self.name = name
  end
end

class TypedPropertyTest < Minitest::Spec
  class Band
    include Representable::XML
    property :name
    attr_accessor :name

    def initialize(name=nil)
      name and self.name = name
    end
  end

  module AlbumRepresenter
    include Representable::XML
    property :band, :class => Band
  end

  class Album
    attr_accessor :band
    def initialize(band=nil)
      @band = band
    end
  end

  # TODO:property :group, :class => Band
  # :class
  # where to mixin DCI?
  describe ":class => Item" do
    it "#from_xml creates one Item instance" do
      album = Album.new.extend(AlbumRepresenter).from_xml(%{
        <album>
          <band><name>Bad Religion</name></band>
        </album>
      })
      assert_equal "Bad Religion", album.band.name
    end

    describe "#to_xml" do
      it "doesn't escape xml from Band#to_xml" do
        band = Band.new("Bad Religion")
        album = Album.new(band).extend(AlbumRepresenter)

        assert_xml_equal %{<album>
         <band>
           <name>Bad Religion</name>
         </band>
       </album>}, album.to_xml
      end

      it "doesn't escape and wrap string from Band#to_node" do
        band = Band.new("Bad Religion")
        band.instance_eval do
          def to_node(*)
            "<band>Baaaad Religion</band>"
          end
        end

        assert_xml_equal %{<album><band>Baaaad Religion</band></album>}, Album.new(band).extend(AlbumRepresenter).to_xml
      end
    end

    describe "#to_xml with CDATA" do
      it "wraps Band name in CDATA#to_xml" do
        band = CDataBand.new("Bad Religion")
        album = Album.new(band).extend(AlbumRepresenter)

        assert_xml_equal %{<album>
         <c_data_band>
           <name><![CDATA[Bad Religion]]></name>
         </c_data_band>
       </album>}, album.to_xml
      end
    end
  end
end

# TODO: add parsing tests.
class XMLPropertyTest < Minitest::Spec
  Band = Struct.new(:name, :genre)
  Manager = Struct.new(:managed)

  #---
  #- :as with scalar
  class BandRepresenter < Representable::Decorator
    include Representable::XML
    property :name, as: :theyCallUs
    property :genre, attribute: true
  end

  it { BandRepresenter.new(Band.new("Mute")).to_xml.must_equal_xml %{<band><theyCallUs>Mute</theyCallUs></band>} }

  class ManagerRepresenter < Representable::Decorator
    include Representable::XML
    property :managed, as: :band, decorator: BandRepresenter
  end

  #- :as with nested property
  it { ManagerRepresenter.new(Manager.new(Band.new("Mute", "Punkrock"))).to_xml.must_equal_xml %{<manager><band genre="Punkrock"><theyCallUs>Mute</theyCallUs></band></manager>} }
end


class XMLCollectionTest < Minitest::Spec
  Band        = Struct.new(:name)
  Compilation = Struct.new(:bands)

  class BandRepresenter < Representable::Decorator
    include Representable::XML
    property :name
  end

  #---
  #- :as, :decorator, :class
  describe ":class => Band, :as => :band, :collection => true" do
    class CompilationRepresenter < Representable::Decorator
      include Representable::XML
      collection :bands, class: Band, as: :group, decorator: BandRepresenter
    end

    describe "#from_xml" do
      it "pushes collection items to array" do
        cd = CompilationRepresenter.new(Compilation.new).from_xml(%{
          <compilation>
            <group><name>Diesel Boy</name></group>
            <group><name>Cobra Skulls</name></group>
          </compilation>
        })
        assert_equal ["Cobra Skulls", "Diesel Boy"], cd.bands.map(&:name).sort
      end
    end

    it "responds to #to_xml" do
      cd = Compilation.new([Band.new("Diesel Boy"), Band.new("Bad Religion")])

      CompilationRepresenter.new(cd).to_xml.must_equal_xml %{<compilation>
        <group><name>Diesel Boy</name></group>
        <group><name>Bad Religion</name></group>
      </compilation>}
    end
  end


  describe ":as" do
    let(:xml_doc) {
      Module.new do
        include Representable::XML
        collection :songs, :as => :song
      end }

    it "collects untyped items" do
      album = Album.new.extend(xml_doc).from_xml(%{
        <album>
          <song>Two Kevins</song>
          <song>Wright and Rong</song>
          <song>Laundry Basket</song>
        </album>
      })
      assert_equal ["Laundry Basket", "Two Kevins", "Wright and Rong"].sort, album.songs.sort
    end
  end


  describe ":wrap" do
    let(:album) { Album.new.extend(xml_doc) }
    let(:xml_doc) {
      Module.new do
        include Representable::XML
        collection :songs, :as => :song, :wrap => :songs
      end }

    describe "#from_xml" do
      it "finds items in wrapped collection" do
        album.from_xml(%{
          <album>
            <songs>
              <song>Two Kevins</song>
              <song>Wright and Rong</song>
              <song>Laundry Basket</song>
            </songs>
          </album>
        })
        assert_equal ["Laundry Basket", "Two Kevins", "Wright and Rong"].sort, album.songs.sort
      end
    end

    describe "#to_xml" do
      it "wraps items" do
        album.songs = ["Laundry Basket", "Two Kevins", "Wright and Rong"]
        assert_xml_equal %{
          <album>
            <songs>
              <song>Laundry Basket</song>
              <song>Two Kevins</song>
              <song>Wright and Rong</song>
            </songs>
          </album>
        }, album.to_xml
      end
    end
  end

  require 'representable/xml/hash'
  class LonelyRepresenterTest < Minitest::Spec
    # TODO: where is the XML::Hash test?
    module SongRepresenter
      include Representable::XML
      property :name
      self.representation_wrap = :song
    end

    let(:decorator) { rpr = representer; Class.new(Representable::Decorator) { include Representable::XML; include rpr } }

    describe "XML::Collection" do
      describe "with contained objects" do
        representer!(:module => Representable::XML::Collection)  do
          items :class => Song, :extend => SongRepresenter
          self.representation_wrap= :songs
        end

        let(:songs) { [Song.new("Days Go By"), Song.new("Can't Take Them All")] }
        let(:xml_doc)   { "<songs><song><name>Days Go By</name></song><song><name>Can't Take Them All</name></song></songs>" }

        it "renders array" do
          songs.extend(representer).to_xml.must_equal_xml xml_doc
        end

        it "renders array with decorator" do
          decorator.new(songs).to_xml.must_equal_xml xml_doc
        end

        it "parses array" do
          [].extend(representer).from_xml(xml_doc).must_equal songs
        end

        it "parses array with decorator" do
          decorator.new([]).from_xml(xml_doc).must_equal songs
        end
      end
    end

    describe "XML::AttributeHash" do  # TODO: move to HashTest.
      representer!(:module => Representable::XML::AttributeHash) do
        self.representation_wrap= :songs
      end

      let(:songs) { {"one" => "Graveyards", "two" => "Can't Take Them All"} }
      let(:xml_doc)   { "<favs one=\"Graveyards\" two=\"Can't Take Them All\" />" }

      describe "#to_xml" do
        it "renders hash" do
          songs.extend(representer).to_xml.must_equal_xml xml_doc
        end

        it "respects :exclude" do
          assert_xml_equal "<favs two=\"Can't Take Them All\" />", songs.extend(representer).to_xml(:exclude => [:one])
        end

        it "respects :include" do
          assert_xml_equal "<favs two=\"Can't Take Them All\" />", songs.extend(representer).to_xml(:include => [:two])
        end

        it "renders hash with decorator" do
          decorator.new(songs).to_xml.must_equal_xml xml_doc
        end
      end

      describe "#from_json" do
        it "returns hash" do
          {}.extend(representer).from_xml(xml_doc).must_equal songs
        end

        it "respects :exclude" do
          assert_equal({"two" => "Can't Take Them All"}, {}.extend(representer).from_xml(xml_doc, :exclude => [:one]))
        end

        it "respects :include" do
          assert_equal({"one" => "Graveyards"}, {}.extend(representer).from_xml(xml_doc, :include => [:one]))
        end

        it "parses hash with decorator" do
          decorator.new({}).from_xml(xml_doc).must_equal songs
        end
      end
    end
  end
end

class XmlHashTest < Minitest::Spec
  # scalar, no object
  describe "plain text" do
    representer!(module: Representable::XML) do
      hash :songs
    end

    let(:doc) { "<open_struct><first>The Gargoyle</first><second>Bronx</second></open_struct>" }

    # to_xml
    it { OpenStruct.new(songs: {"first" => "The Gargoyle", "second" => "Bronx"}).extend(representer).to_xml.must_equal_xml(doc) }
    # FIXME: this NEVER worked!
    # it { OpenStruct.new.extend(representer).from_xml(doc).songs.must_equal({"first" => "The Gargoyle", "second" => "Bronx"}) }
  end

  describe "with objects" do
    representer!(module: Representable::XML) do
      hash :songs, class: OpenStruct do
        property :title
      end
    end

    let(:doc) { "<open_struct>
  <open_struct>
    <title>The Gargoyle</title>
  </open_struct>
  <open_struct>
    <title>Bronx</title>
  </open_struct>
</open_struct>" }

    # to_xml
    it { OpenStruct.new(songs: {"first" => OpenStruct.new(title: "The Gargoyle"), "second" => OpenStruct.new(title: "Bronx")}).extend(representer).to_xml.must_equal_xml(doc) }
    # FIXME: this NEVER worked!
    # it { OpenStruct.new.extend(representer).from_xml(doc).songs.must_equal({"first" => "The Gargoyle", "second" => "Bronx"}) }
  end
end