test/xml_test.rb in representable-3.0.3 vs test/xml_test.rb in representable-3.0.4

- old
+ new

@@ -8,18 +8,18 @@ include Representable::XML property :id property :name end - let (:data) { %{<data><id>1</id><name>Rancid</name></data>} } + 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") } + 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 @@ -143,18 +143,16 @@ describe "DCI" do module SongRepresenter include Representable::XML property :name - representation_wrap = :song end module AlbumRepresenter include Representable::XML property :best_song, :class => Song, :extend => SongRepresenter collection :songs, :class => Song, :as => :song, :extend => SongRepresenter - representation_wrap = :album end it "allows adding the representer by using #extend" do module BandRepresenter @@ -177,15 +175,15 @@ 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) - assert_xml_equal "<album> + @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>", @album.to_xml +</album>" end it "extends contained models when deserializing" do @album = Album.new @album.extend(AlbumRepresenter) @@ -314,50 +312,71 @@ end end end end +# TODO: add parsing tests. +class XMLPropertyTest < Minitest::Spec + Band = Struct.new(:name, :genre) + Manager = Struct.new(:managed) -class CollectionTest < MiniTest::Spec - class Band + #--- + #- :as with scalar + class BandRepresenter < Representable::Decorator include Representable::XML - property :name - attr_accessor :name + property :name, as: :theyCallUs + property :genre, attribute: true + end - def initialize(name=nil) - name and self.name = name - 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 Compilation + class CompilationRepresenter < Representable::Decorator include Representable::XML - collection :bands, :class => Band, :as => :band - attr_accessor :bands + collection :bands, class: Band, as: :group, decorator: BandRepresenter end describe "#from_xml" do it "pushes collection items to array" do - cd = Compilation.new.from_xml(%{ + cd = CompilationRepresenter.new(Compilation.new).from_xml(%{ <compilation> - <band><name>Diesel Boy</name></band> - <band><name>Cobra Skulls</name></band> + <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 - cd.bands = [Band.new("Diesel Boy"), Band.new("Bad Religion")] + cd = Compilation.new([Band.new("Diesel Boy"), Band.new("Bad Religion")]) - assert_xml_equal %{<compilation> - <band><name>Diesel Boy</name></band> - <band><name>Bad Religion</name></band> - </compilation>}, cd.to_xml + 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 @@ -379,12 +398,12 @@ end end describe ":wrap" do - let (:album) { Album.new.extend(xml_doc) } - let (:xml_doc) { + let(:album) { Album.new.extend(xml_doc) } + let(:xml_doc) { Module.new do include Representable::XML collection :songs, :as => :song, :wrap => :songs end } @@ -426,21 +445,21 @@ include Representable::XML property :name self.representation_wrap = :song end - let (:decorator) { rpr = representer; Class.new(Representable::Decorator) { include Representable::XML; include rpr } } + 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>" } + 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 @@ -461,12 +480,12 @@ 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\" />" } + 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 @@ -510,11 +529,11 @@ 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>" } + 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"}) } @@ -525,10 +544,10 @@ hash :songs, class: OpenStruct do property :title end end - let (:doc) { "<open_struct> + let(:doc) { "<open_struct> <open_struct> <title>The Gargoyle</title> </open_struct> <open_struct> <title>Bronx</title>