Sha256: ecf6b46226d6cc937dbabea60714c205d494f981020f1c3591ba047ddf1a0eb6

Contents?: true

Size: 1.72 KB

Versions: 33

Compression:

Stored size: 1.72 KB

Contents

require 'test_helper'

class PrepareTest < BaseTest
  class PreparerClass
    def initialize(object)
      @object = object
    end

    def ==(b)
      return unless b.instance_of?(PreparerClass)

      object == b.object
    end

    attr_reader :object
  end

  describe "#to_hash" do # TODO: introduce :representable option?
    representer! do
      property :song,
        :prepare       => lambda { |obj, args| args.binding[:arbitrary].new(obj) },
        :arbitrary     => PreparerClass,
        :extend => true,
        :pass_options  => true,
        :representable => false # don't call #to_hash.
    end

    let (:hit) { Struct.new(:song).new(song).extend(representer) }

    it "calls prepare:, nothing else" do
      # render(hit).must_equal_document(output)
      hit.to_hash.must_equal({"song" => PreparerClass.new(song)})
    end


    # it "calls #from_hash on the existing song instance, nothing else" do
    #   song_id = hit.song.object_id

    #   parse(hit, input)

    #   hit.song.title.must_equal "Suffer"
    #   hit.song.object_id.must_equal song_id
    # end
  end


  describe "#from_hash" do
    representer! do
      property :song,
        :prepare       => lambda { |obj, args| args.binding[:arbitrary].new(obj) },
        :arbitrary     => PreparerClass,
        #:extend => true, # TODO: typed: true would be better.
        :instance => String.new, # pass_fragment
        :pass_options  => true,
        :representable => false # don't call #to_hash.
    end

    let (:hit) { Struct.new(:song).new.extend(representer) }

    it "calls prepare:, nothing else" do
      # render(hit).must_equal_document(output)
      hit.from_hash("song" => {})

      hit.song.must_equal(PreparerClass.new(String.new))
    end
  end
end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
representable-2.4.1 test-with-deprecations/prepare_test.rb
representable-2.4.0 test-with-deprecations/prepare_test.rb
representable-2.4.0.rc5 test-with-deprecations/prepare_test.rb
representable-2.4.0.rc4 test-with-deprecations/prepare_test.rb
representable-2.4.0.rc3 test/prepare_test.rb
representable-2.4.0.rc2 test/prepare_test.rb
representable-2.4.0.rc1 test/prepare_test.rb
representable-2.3.0 test/prepare_test.rb
representable-2.2.3 test/prepare_test.rb
representable-2.2.2 test/prepare_test.rb
representable-2.2.1 test/prepare_test.rb
representable-2.2.0 test/prepare_test.rb
representable-2.1.8 test/prepare_test.rb
representable-2.1.7 test/prepare_test.rb
representable-2.1.6 test/prepare_test.rb
representable-2.1.5 test/prepare_test.rb
representable-2.1.4 test/prepare_test.rb
representable-2.1.3 test/prepare_test.rb
representable-2.1.1 test/prepare_test.rb
representable-2.1.0 test/prepare_test.rb