Sha256: 90281b5d5d37fc211516bc71b32d2eef700968a1c446945bef732eea1f3769be

Contents?: true

Size: 1.47 KB

Versions: 25

Compression:

Stored size: 1.47 KB

Contents

require 'test_helper'

class AsTest < MiniTest::Spec
  for_formats(
    :hash => [Representable::Hash, {"title" => "Wie Es Geht"}, {"title" => "Revolution"}],
    # :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, output|

    let (:song) { representer.prepare(Song.new("Revolution")) }
    let (:format) { format }


    describe "as: with :symbol" do
      representer!(:module => mod) do
        property :name, :as => :title
      end

      it { render(song).must_equal_document output }
      it { parse(song, input).name.must_equal "Wie Es Geht" }
    end


    describe "as: with lambda" do
      representer!(:module => mod) do
        property :name, :as => lambda { |*| "#{self.class}" }
      end

      it { render(song).must_equal_document({"Song" => "Revolution"}) }
      it { parse(song, {"Song" => "Wie Es Geht"}).name.must_equal "Wie Es Geht" }
    end


    describe "lambda arguments" do
      representer! do
        property :name, :as => lambda { |*args| args.inspect }
      end

      it { render(song, :volume => 1).must_equal_document({"[{:volume=>1}]" => "Revolution"}) }
      it { parse(song, {"[{:volume=>1}]" => "Wie Es Geht"}, :volume => 1).name.must_equal "Wie Es Geht" }
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
representable-2.2.3 test/as_test.rb
representable-2.2.2 test/as_test.rb
representable-2.2.1 test/as_test.rb
representable-2.2.0 test/as_test.rb
representable-2.1.8 test/as_test.rb
representable-2.1.7 test/as_test.rb
representable-2.1.6 test/as_test.rb
representable-2.1.5 test/as_test.rb
representable-2.1.4 test/as_test.rb
representable-2.1.3 test/as_test.rb
representable-2.1.1 test/as_test.rb
representable-2.1.0 test/as_test.rb
representable-2.0.4 test/as_test.rb
representable-2.0.3 test/as_test.rb
representable-2.0.2 test/as_test.rb
representable-2.0.1 test/as_test.rb
representable-2.0.0 test/as_test.rb
representable-2.0.0.rc2 test/as_test.rb
representable-2.0.0.rc1 test/as_test.rb
representable-1.8.5 test/as_test.rb