summaryrefslogtreecommitdiff
path: root/spec/unit/pops/adaptable_spec.rb
blob: 544a6c54f432312dd3027e1b018fe37c4f403b2a (plain)
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
140
141
#! /usr/bin/env ruby
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)
    a.class.should == 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)
    b.value.should == 10
  end

  it "should return the correct adapter if there are several" do
    d = Duck.new
    other = OtherAdapter.adapt(d)
    a = ValueAdapter.adapt(d)
    a.value = 10
    b = ValueAdapter.adapt(d)
    b.value.should == 10
  end

  it "should allow specialization to override creating" do
    d = Duck.new
    a = OtherAdapter.adapt(d)
    a.value.should == "I am calling you Daffy."
  end

  it "should create a new adapter overriding existing" do
    d = Duck.new
    a = OtherAdapter.adapt(d)
    a.value.should == "I am calling you Daffy."
    a.value = "Something different"
    a.value.should == "Something different"
    b = OtherAdapter.adapt(d)
    b.value.should == "Something different"
    b = OtherAdapter.adapt_new(d)
    b.value.should == "I am calling you Daffy."
  end

  it "should not create adapter on get" do
    d = Duck.new
    a = OtherAdapter.get(d)
    a.should == nil
  end

  it "should return same adapter from get after adapt" do
    d = Duck.new
    a = OtherAdapter.get(d)
    a.should == nil
    a = OtherAdapter.adapt(d)
    a.value.should == "I am calling you Daffy."
    b = OtherAdapter.get(d)
    b.value.should == "I am calling you Daffy."
    a.should == b
  end

  it "should handle adapters in nested namespaces" do
    d = Duck.new
    a = Farm::FarmAdapter.get(d)
    a.should == nil
    a = Farm::FarmAdapter.adapt(d)
    a.value = 10
    b = Farm::FarmAdapter.get(d)
    b.value.should == 10
  end

  it "should be able to clear the adapter" do
    d = Duck.new
    a = OtherAdapter.adapt(d)
    a.value.should == "I am calling you Daffy."
    # The adapter cleared should be returned
    OtherAdapter.clear(d).value.should == "I am calling you Daffy."
    OtherAdapter.get(d).should == 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
      a.value.should == "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
      a.value.should == "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
      a.value.should == "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
      a.value.should == "Donald, the Duck"
    end
  end
end