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
|
require 'test_helper'
class FeaturesTest < Minitest::Spec
module Title
def title; "Is It A Lie"; end
end
module Length
def length; "2:31"; end
end
definition = lambda {
feature Title
feature Length
# exec_context: :decorator, so the readers are called on the Decorator instance (which gets readers from features!).
property :title, exec_context: :decorator
property :length, exec_context: :decorator
property :details do
property :title, exec_context: :decorator
end
}
let(:song) { OpenStruct.new(:details => Object.new) }
describe "Module" do
representer! do
instance_exec(&definition)
end
it { song.extend(representer).to_hash.must_equal({"title"=>"Is It A Lie", "length"=>"2:31", "details"=>{"title"=>"Is It A Lie"}}) }
end
describe "Decorator" do
representer!(:decorator => true) do
instance_exec(&definition)
end
it { representer.new(song).to_hash.must_equal({"title"=>"Is It A Lie", "length"=>"2:31", "details"=>{"title"=>"Is It A Lie"}}) }
end
end
class FeatureInclusionOrderTest < Minitest::Spec
module Title
def title
"I was first!"
end
end
module OverridingTitle
def title
"I am number two, " + super
end
end
representer!(decorator: true) do
feature Title
feature OverridingTitle
property :title, exec_context: :decorator
property :song do
property :title, exec_context: :decorator
end
end
it do
representer.new(OpenStruct.new(song: Object)).to_hash.must_equal({"title"=>"I am number two, I was first!", "song"=>{"title"=>"I am number two, I was first!"}})
end
end
|