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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
require 'spec_helper'
require 'puppet/pops'
describe Puppet::Pops::Adaptable::Adapter do
class ValueAdapter < Puppet::Pops::Adaptable::Adapter
attr_accessor :value
end
class OtherAdapter < Puppet::Pops::Adaptable::Adapter
attr_accessor :value
def OtherAdapter.create_adapter(o)
x = new
x.value="I am calling you Daffy."
x
end
end
module Farm
class FarmAdapter < Puppet::Pops::Adaptable::Adapter
attr_accessor :value
end
end
class Duck
include Puppet::Pops::Adaptable
end
it "should create specialized adapter instance on call to adapt" do
d = Duck.new
a = ValueAdapter.adapt(d)
expect(a.class).to eq(ValueAdapter)
end
it "should produce the same instance on multiple adaptations" do
d = Duck.new
a = ValueAdapter.adapt(d)
a.value = 10
b = ValueAdapter.adapt(d)
expect(b.value).to eq(10)
end
it "should return the correct adapter if there are several" do
d = Duck.new
a = ValueAdapter.adapt(d)
a.value = 10
b = ValueAdapter.adapt(d)
expect(b.value).to eq(10)
end
it "should allow specialization to override creating" do
d = Duck.new
a = OtherAdapter.adapt(d)
expect(a.value).to eq("I am calling you Daffy.")
end
it "should create a new adapter overriding existing" do
d = Duck.new
a = OtherAdapter.adapt(d)
expect(a.value).to eq("I am calling you Daffy.")
a.value = "Something different"
expect(a.value).to eq("Something different")
b = OtherAdapter.adapt(d)
expect(b.value).to eq("Something different")
b = OtherAdapter.adapt_new(d)
expect(b.value).to eq("I am calling you Daffy.")
end
it "should not create adapter on get" do
d = Duck.new
a = OtherAdapter.get(d)
expect(a).to eq(nil)
end
it "should return same adapter from get after adapt" do
d = Duck.new
a = OtherAdapter.get(d)
expect(a).to eq(nil)
a = OtherAdapter.adapt(d)
expect(a.value).to eq("I am calling you Daffy.")
b = OtherAdapter.get(d)
expect(b.value).to eq("I am calling you Daffy.")
expect(a).to eq(b)
end
it "should handle adapters in nested namespaces" do
d = Duck.new
a = Farm::FarmAdapter.get(d)
expect(a).to eq(nil)
a = Farm::FarmAdapter.adapt(d)
a.value = 10
b = Farm::FarmAdapter.get(d)
expect(b.value).to eq(10)
end
it "should be able to clear the adapter" do
d = Duck.new
a = OtherAdapter.adapt(d)
expect(a.value).to eq("I am calling you Daffy.")
# The adapter cleared should be returned
expect(OtherAdapter.clear(d).value).to eq("I am calling you Daffy.")
expect(OtherAdapter.get(d)).to eq(nil)
end
context "When adapting with #adapt it" do
it "should be possible to pass a block to configure the adapter" do
d = Duck.new
a = OtherAdapter.adapt(d) do |x|
x.value = "Donald"
end
expect(a.value).to eq("Donald")
end
it "should be possible to pass a block to configure the adapter and get the adapted" do
d = Duck.new
a = OtherAdapter.adapt(d) do |x, o|
x.value = "Donald, the #{o.class.name}"
end
expect(a.value).to eq("Donald, the Duck")
end
end
context "When adapting with #adapt_new it" do
it "should be possible to pass a block to configure the adapter" do
d = Duck.new
a = OtherAdapter.adapt_new(d) do |x|
x.value = "Donald"
end
expect(a.value).to eq("Donald")
end
it "should be possible to pass a block to configure the adapter and get the adapted" do
d = Duck.new
a = OtherAdapter.adapt_new(d) do |x, o|
x.value = "Donald, the #{o.class.name}"
end
expect(a.value).to eq("Donald, the Duck")
end
end
end
|