Sha256: e92b9325bb07f45aace18a2f4e7f82f7e0903dae0cfc0f7fac62567bce277c26

Contents?: true

Size: 1.71 KB

Versions: 1

Compression:

Stored size: 1.71 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 { |options| options[:binding][:arbitrary].new(options[:input]) },
        :arbitrary     => PreparerClass,
        :extend => 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 { |options| options[:binding][:arbitrary].new(options[:input]) },
        :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

1 entries across 1 versions & 1 rubygems

Version Path
representable-3.0.4 test/prepare_test.rb