require 'test_helper' require 'representable/xml' class Band include Representable::XML representable_property :name def initialize(name=nil) name and self.name = name end end class Label def to_xml "" end end class XmlTest < MiniTest::Spec XML = Representable::XML Def = Representable::Definition describe "Xml module" do before do @Band = Class.new do include Representable::XML self.representation_name= :band representable_property :name representable_property :label end end class Band include Representable::XML representable_property :href, :from => "@href" representable_property :title, :from => "@title" end it "#definition_class returns Definition class" do assert_equal XML::Definition, Band.definition_class end describe "#binding_for_definition" do it "returns AttributeBinding" do assert_kind_of XML::AttributeBinding, Band.binding_for_definition(Def.new(:band, :from => "@band")) end it "returns ObjectBinding" do assert_kind_of XML::ObjectBinding, Band.binding_for_definition(Def.new(:band, :as => Hash)) end it "returns TextBinding" do assert_kind_of XML::TextBinding, Band.binding_for_definition(Def.new(:band, :from => :content)) end end describe "#from_xml" do before do @band = @Band.new end it "accepts xml string" do @band.from_xml(%{Nofx}) assert_equal ["Nofx", "NOFX"], [@band.name, @band.label] end it "forwards block to #update_properties_from" do @band.from_xml(%{Nofx}) do |binding| binding.definition.name == "name" end assert_equal ["Nofx", nil], [@band.name, @band.label] end end end end class AttributesTest < MiniTest::Spec describe ":from => @rel" do class Link include Representable::XML representable_property :href, :from => "@href" representable_property :title, :from => "@title" end it "#from_xml creates correct accessors" do link = Link.from_xml(%{ }) 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.to_xml.to_s end end end class TypedPropertyTest < MiniTest::Spec describe ":as => Item" do class Album include Representable::XML representable_property :band, :as => Band representable_property :label, :as => Label end it "#from_xml creates one Item instance" do album = Album.from_xml(%{ Bad Religion }) 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 band.name = "Bad Religion" album = Album.new album.band = band assert_xml_equal %{ Bad Religion }, album.to_xml.to_s end it "doesn't escape and wrap string from Label#to_xml" do album = Album.new album.label = Label.new assert_xml_equal %{ }, album.to_xml.to_s end end end end class CollectionTest < MiniTest::Spec describe ":as => [Band], :tag => :band" do class Compilation include Representable::XML representable_collection :bands, :as => Band, :from => :band end describe "#representable_collection" do it "declares a collection" do assert Compilation.representable_attrs.first.array? end end describe "#from_xml" do it "pushes collection items to array" do cd = Compilation.from_xml(%{ Diesel Boy Cobra Skulls }) assert_equal ["Cobra Skulls", "Diesel Boy"], cd.bands.map(&:name).sort end it "collections can be empty" do cd = Compilation.from_xml(%{ }) assert_equal [], cd.bands end end it "responds to #to_xml" do cd = Compilation.new cd.bands = [Band.new("Diesel Boy"), Band.new("Bad Religion")] assert_xml_equal %{ Diesel Boy Bad Religion }, cd.to_xml.to_s end end describe ":as => []" do class Album include Representable::XML representable_collection :songs, :from => :song end it "collects untyped items" do album = Album.from_xml(%{ Two Kevins Wright and Rong Laundry Basket }) assert_equal ["Laundry Basket", "Two Kevins", "Wright and Rong"].sort, album.songs.sort end end end