Sha256: c0f96e8fb341f21ba766873716ca715a32d7e9abfd6d5f42887a83806d1366a8

Contents?: true

Size: 1.95 KB

Versions: 42

Compression:

Stored size: 1.95 KB

Contents

require 'test_helper'

class IsRepresentableTest < BaseTest
  describe "representable: false, extend:" do
    representer!(:inject => :song_representer) do
      property :song,
        :representable => false,
        :extend        => song_representer
    end

    it "does extend but doesn't call #to_hash" do
      Struct.new(:song).new(song = Object.new).extend(representer).
        to_hash.must_equal("song" => song)
      song.must_be_kind_of Representable::Hash
    end
  end


  describe "representable: true, no extend:" do
    representer!(:inject => :song_representer) do
      property :song,
        :representable => true
    end

    it "doesn't extend but calls #to_hash" do
      song = Object.new
      song.instance_eval do
        def to_hash(*)
          1
        end
      end

      Struct.new(:song).new(song).extend(representer).
        to_hash.must_equal("song" => 1)
      song.wont_be_kind_of Representable::Hash
    end
  end

  # TODO: TEST implement for from_hash.

  describe "representable: false, with class:" do
    representer!(:inject => :song_representer) do
      property :song,
        :representable => false, :class => OpenStruct, :extend => song_representer
    end

    it "does extend but doesn't call #from_hash" do
      hit = Struct.new(:song).new.extend(representer).
        from_hash("song" => 1)

      hit.song.must_equal OpenStruct.new
      hit.song.must_be_kind_of Representable::Hash
    end
  end


  describe "representable: true, without extend: but class:" do
    SongReader = Class.new do
      def from_hash(*)
        "Piano?"
      end
    end

    representer!(:inject => :song_representer) do
      property :song,
        :representable => true, :class => SongReader
    end

    it "doesn't extend but calls #from_hash" do
      hit = Struct.new(:song).new.extend(representer).
        from_hash("song" => "Sonata No.2")

      hit.song.must_equal "Piano?"
      hit.song.wont_be_kind_of Representable::Hash
    end
  end
end

Version data entries

42 entries across 38 versions & 1 rubygems

Version Path
representable-3.0.4 test/is_representable_test.rb
representable-3.0.3 test/is_representable_test.rb
representable-3.0.2 test/is_representable_test.rb
representable-3.0.1 test/is_representable_test.rb
representable-3.0.0 test/is_representable_test.rb
representable-2.4.1 test-with-deprecations/is_representable_test.rb
representable-2.4.1 test/is_representable_test.rb
representable-2.4.0 test/is_representable_test.rb
representable-2.4.0 test-with-deprecations/is_representable_test.rb
representable-2.4.0.rc5 test-with-deprecations/is_representable_test.rb
representable-2.4.0.rc5 test/is_representable_test.rb
representable-2.4.0.rc4 test/is_representable_test.rb
representable-2.4.0.rc4 test-with-deprecations/is_representable_test.rb
representable-2.4.0.rc3 test/is_representable_test.rb
representable-2.4.0.rc2 test/is_representable_test.rb
representable-2.4.0.rc1 test/is_representable_test.rb
representable-2.3.0 test/is_representable_test.rb
representable-2.2.3 test/is_representable_test.rb
representable-2.2.2 test/is_representable_test.rb
representable-2.2.1 test/is_representable_test.rb