Sha256: 077c8d51a075e6246b1a36f6446ae57f065c55e537af74b9c8c2f08b8143dc42

Contents?: true

Size: 1.72 KB

Versions: 5

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 { |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

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/prepare_test.rb
fluent-plugin-google-cloud-logging-on-prem-0.1.0 vendor/ruby/3.1.0/gems/representable-3.2.0/test/prepare_test.rb
representable-3.2.0 test/prepare_test.rb
representable-3.1.1 test/prepare_test.rb
representable-3.1.0 test/prepare_test.rb