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
|
require "test_helper"
class WrapTest < Minitest::Spec
class HardcoreBand
include Representable::Hash
end
class SoftcoreBand < HardcoreBand
end
let(:band) { HardcoreBand.new }
it "returns false per default" do
assert_nil SoftcoreBand.new.send(:representation_wrap)
end
it "infers a printable class name if set to true" do
HardcoreBand.representation_wrap = true
assert_equal "hardcore_band", band.send(:representation_wrap)
end
it "can be set explicitely" do
HardcoreBand.representation_wrap = "breach"
assert_equal "breach", band.send(:representation_wrap)
end
for_formats(
:hash => [Representable::Hash, {"Blink182"=>{"genre"=>"Pop"}}, {"Blink182"=>{"genre"=>"Poppunk"}}],
:json => [Representable::JSON, "{\"Blink182\":{\"genre\":\"Pop\"}}", "{\"Blink182\":{\"genre\":\"Poppunk\"}}"],
:xml => [Representable::XML, "<Blink182><genre>Pop</genre></Blink182>", "<Blink182><genre>Poppunk</genre></Blink182>"],
# :yaml => [Representable::YAML, "---\nBlink182:\n"], # TODO: fix YAML.
) do |format, mod, output, input|
describe "[#{format}] dynamic wrap" do
let(:band) { representer.prepare(Struct.new(:name, :genre).new("Blink", "Pop")) }
let(:format) { format }
representer!(:module => mod) do
self.representation_wrap = lambda { |args| "#{name}#{args[:number]}" }
property :genre
end
it { render(band, {:number => 182}).must_equal_document(output) }
it { parse(band, input, {:number => 182}).genre.must_equal "Poppunk" } # TODO: better test. also, xml parses _any_ wrap.
end
end
end
class HashDisableWrapTest < Minitest::Spec
Band = Struct.new(:name, :label)
Album = Struct.new(:band)
Label = Struct.new(:name)
class BandDecorator < Representable::Decorator
include Representable::Hash
self.representation_wrap = :bands
property :name
property :label do # this should have a wrap!
self.representation_wrap = :important
property :name
end
end
let(:band) { BandDecorator.prepare(Band.new("Social Distortion")) }
# direct, local api.
it do
band.to_hash.must_equal({"bands" => {"name"=>"Social Distortion"}})
band.to_hash(wrap: false).must_equal({"name"=>"Social Distortion"})
band.to_hash(wrap: :band).must_equal(:band=>{"name"=>"Social Distortion"})
end
it do
band.from_hash({"bands" => {"name"=>"Social Distortion"}}).name.must_equal "Social Distortion"
band.from_hash({"name"=>"Social Distortion"}, wrap: false).name.must_equal "Social Distortion"
band.from_hash({band: {"name"=>"Social Distortion"}}, wrap: :band).name.must_equal "Social Distortion"
end
class AlbumDecorator < Representable::Decorator
include Representable::Hash
self.representation_wrap = :albums
property :band, decorator: BandDecorator, wrap: false, class: Band
end
let(:album) { AlbumDecorator.prepare(Album.new(Band.new("Social Distortion", Label.new("Epitaph")))) }
# band has wrap turned off per property definition, however, label still has wrap.
it "renders" do
album.to_hash.must_equal({"albums" => {"band" => {"name"=>"Social Distortion", "label"=>{"important"=>{"name"=>"Epitaph"}}}}})
end
it "parses" do
album.from_hash({"albums" => {"band" => {"name"=>"Rvivr"}}}).band.name.must_equal "Rvivr"
end
end
class XMLDisableWrapTest < Minitest::Spec
Band = Struct.new(:name, :label)
Album = Struct.new(:band)
Label = Struct.new(:name)
class BandDecorator < Representable::Decorator
include Representable::XML
self.representation_wrap = :bands # when nested, it uses this.
property :name
# property :label do # this should have a wrap!
# self.representation_wrap = :important
# property :name
# end
end
let(:band) { BandDecorator.prepare(Band.new("Social Distortion")) }
it do
band.to_xml.must_equal_xml "<bands><name>Social Distortion</name></bands>"
band.to_xml(wrap: "combo").must_equal_xml "<combo><name>Social Distortion</name></combo>"
end
class AlbumDecorator < Representable::Decorator
include Representable::XML
self.representation_wrap = :albums
property :band, decorator: BandDecorator, wrap: "po", class: Band
end
let(:album) { AlbumDecorator.prepare(Album.new(Band.new("Social Distortion", Label.new("Epitaph")))) }
# band has wrap turned of per property definition, however, label still has wrap.
it "rendersxx" do
skip
album.to_xml.must_equal({"albums" => {"band" => {"name"=>"Social Distortion", "label"=>{"important"=>{"name"=>"Epitaph"}}}}})
end
# it "parses" do
# album.from_hash({"albums" => {"band" => {"name"=>"Rvivr"}}}).band.name.must_equal "Rvivr"
# end
end
|