1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
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
|