test/inline_test.rb in representable-1.7.6 vs test/inline_test.rb in representable-1.7.7

- old
+ new

@@ -55,32 +55,59 @@ end it { request.to_hash.must_equal({"song"=>{"name"=>"Alive"}}) } end - describe "decorator" do - let (:request) { representer.prepare(OpenStruct.new(:song => song, :who => "Josephine")) } - let (:representer) do - Class.new(Representable::Decorator) do - include Representable::Hash + for_formats( + :hash => [Representable::Hash, {}], + # :xml => [Representable::XML, "<open_struct>\n <song>\n <name>Alive</name>\n </song>\n</open_struct>", "<open_struct><song><name>You've Taken Everything</name></song>/open_struct>"], + # :yaml => [Representable::YAML, "---\nsong:\n name: Alive\n", "---\nsong:\n name: You've Taken Everything\n"], + ) do |format, mod, input| - property :who + describe "parsing [#{format}] where nested property missing" do + representer!(:module => mod) do + property :song do + property :name + end + end + it "doesn't change represented object" do + request.send("from_#{format}", input).song.must_equal song + end + end + end + + + describe "inheriting from outer representer" do + let (:request) { Struct.new(:song, :requester).new(song, "Josephine") } + + [false, true].each do |is_decorator| # test for module and decorator. + representer!(:decorator => is_decorator) do + property :requester + property :song, :class => Song do property :name end + end - self + let (:decorator) { representer.prepare(request) } + + it { decorator.to_hash.must_equal({"requester"=>"Josephine", "song"=>{"name"=>"Alive"}}) } + it { decorator.from_hash({"song"=>{"name"=>"You've Taken Everything"}}).song.name.must_equal "You've Taken Everything"} + end + end + + describe "object pollution" do + representer!(:decorator => true) do + property :song do + property :name end end - it { request.to_hash.must_equal({"who"=>"Josephine", "song"=>{"name"=>"Alive"}}) } - it { request.from_hash({"song"=>{"name"=>"You've Taken Everything"}}).song.name.must_equal "You've Taken Everything"} - - it "uses an inline decorator" do - request.to_hash + it "uses an inline decorator and doesn't alter represented" do + representer.prepare(Struct.new(:song).new(song)).to_hash song.wont_be_kind_of Representable end end # TODO: should be in extend:/decorator: test. @@ -136,9 +163,43 @@ let (:album) { OpenStruct.new(:label => "Epitaph").extend(representer) } it "renders nested Album-properties in separate section" do render(album).must_equal_document output + end + end + end + + + # test helper methods within inline representer + for_formats({ + :hash => [Representable::Hash, {"song"=>{"name"=>"ALIVE"}}], + :xml => [Representable::XML, "<request>\n <song>\n <name>ALIVE</name>\n </song>\n</request>"], + :yaml => [Representable::YAML, "---\nsong:\n name: ALIVE\n"], + }) do |format, mod, output| + + describe "helper method within inline representer [#{format}]" do + let (:format) { format } + + representer!(:module => mod, :decorator => true) do + self.representation_wrap = :request if format == :xml + + property :requester + property :song do + property :name, :decorator_scope => true + + define_method :name do + represented.name.upcase + end + + self.representation_wrap = :song if format == :xml + end + end + + let (:request) { representer.prepare(OpenStruct.new(:song => Song.new("Alive"))) } + + it do + render(request).must_equal_document output end end end end