require 'test_helper'
class ItemRepresenter
include Roar::Representer::XML
self.representation_wrap= :item
property :value
end
class PositionRepresenter
include Roar::Representer::XML
self.representation_wrap= :position
property :id
property :item, :class => ItemRepresenter
end
class XMLRepresenterUnitTest < MiniTest::Spec
describe "XmlRepresenter" do
describe "#link" do
class Rapper
include Roar::Representer::XML
include Roar::Representer::Feature::Hypermedia
link :self
link :next
end
it "creates a LinksDefinition" do
assert_equal 1, Rapper.representable_attrs.size
assert_equal [{:rel=>:self, :block=>nil}, {:rel=>:next, :block=>nil}], Rapper.representable_attrs.first.rel2block
end
end
end
end
class XMLRepresenterFunctionalTest < MiniTest::Spec
class GreedyOrder
include TestModel
accessors :id, :items
def self.model_name
:order
end
end
class TestXmlRepresenter
include Roar::Representer::XML
self.representation_wrap= :order
property :id
end
describe "XMLRepresenter" do
before do
@m = {"id" => "1"}
@o = Order.new(@m)
@r = TestXmlRepresenter.new
@i = ItemRepresenter.new
@i.value = "Beer"
end
describe "#to_attributes" do
it "returns a nested attributes hash" do
@r = PositionRepresenter.from_attributes("id" => 1, "item" => @i)
assert_equal({"id" => 1, "item" => {"value" => "Beer"}}, @r.to_attributes)
end
end
describe "#to_xml" do
it "serializes the model" do
assert_xml_equal "", @r.to_xml
@r.id = 1
assert_xml_equal "1", @r.to_xml
@r.id = 2
assert_xml_equal "2", @r.to_xml(:wrap => :rap)
end
it "is aliased by #serialize" do
assert_equal @r.to_xml, @r.serialize
end
end
describe "#from_xml" do
it "deserializes object" do
@order = Order.new.from_xml("1")
assert_equal "1", @order.id
end
it "is aliased by #deserialize" do
@order = Order.new.deserialize("1")
assert_equal "1", @order.id
end
end
describe "XML.from_xml" do
class Order
include Roar::Representer::XML
property :id
property :pending
end
it "is aliased to #deserialize" do
assert_equal TestXmlRepresenter.from_xml("").to_attributes, TestXmlRepresenter.deserialize("").to_attributes
end
it "accepts :except option" do
order = Order.from_xml(%{11}, :except => [:id])
assert_equal nil, order.id
assert_equal "1", order.pending
end
it "accepts :include option" do
order = Order.from_xml(%{11}, :include => [:id])
assert_equal "1", order.id
assert_equal nil, order.pending
end
end
describe "without options" do
it ".from_xml returns the deserialized model" do
@m = TestXmlRepresenter.from_xml("1")
assert_equal "1", @m.id
end
#it ".from_xml still works with nil" do
# assert TestXmlRepresenter.from_xml(nil)
#end
end
describe "with a typed attribute" do
before do
@r = PositionRepresenter.from_attributes("id" => "1")
end
it "#serialize skips empty :item" do
assert_xml_equal "1", @r.to_xml
end
it "#to_xml delegates to ItemRepresenter#to_xml" do
@r.item = @i
assert_xml_equal "1Beer", @r.to_xml
end
it ".from_xml typecasts :item" do
@m = PositionRepresenter.from_xml("1beer\n")
assert_equal "1", @m.id
assert_equal "beer", @m.item.value
end
end
describe "with a typed list" do
before do
@c = Class.new do
include Roar::Representer::XML
self.representation_wrap= :order
property :id
collection :items, :class => ItemRepresenter, :from => :item
end
@r = @c.from_attributes("id" => 1)
end
it "#serialize_model skips empty :item" do
assert_xml_equal "1", @r.to_xml
end
it "#serialize delegates to ItemXmlRepresenter#to_xml in list" do
@r.items = [ItemRepresenter.from_attributes("value" => "Bier")]
assert_xml_equal "1Bier",
@r.to_xml
end
it ".from_xml typecasts list" do
@m = @c.from_xml("1beer\n")
assert_equal "1", @m.id
assert_equal 1, @m.items.size
assert_equal "beer", @m.items.first.value
end
end
end
end
class XmlHyperlinkRepresenterTest < MiniTest::Spec
describe "API" do
before do
@l = Roar::Representer::XML::Hyperlink.from_xml(%{})
end
it "responds to #to_xml" do
assert_xml_equal %{}, @l.to_xml
end
it "responds to #rel" do
assert_equal "self", @l.rel
end
it "responds to #href" do
assert_equal "http://roar.apotomo.de", @l.href
end
end
end