Sha256: 0ce0a62d9251f5575ea65580fb318a3da2f1641592355d07ff67b0346297500e

Contents?: true

Size: 1.98 KB

Versions: 5

Compression:

Stored size: 1.98 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

5 entries across 5 versions & 3 rubygems

Version Path
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/representable-3.2.0/test/is_representable_test.rb
fluent-plugin-google-cloud-logging-on-prem-0.1.0 vendor/ruby/3.1.0/gems/representable-3.2.0/test/is_representable_test.rb
representable-3.2.0 test/is_representable_test.rb
representable-3.1.1 test/is_representable_test.rb
representable-3.1.0 test/is_representable_test.rb