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 67 68 69 70 71 72 73 74 75 76 77
|
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
|