summaryrefslogtreecommitdiff
path: root/spec/unit/node/attribute_trait/decorator_spec.rb
blob: a87109216fc76ccb6921a7f76fe5010444c524d1 (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

require 'spec_helper'

describe Chef::Node::AttributeTrait::Decorator do
  class Test
    include Chef::Node::AttributeTrait::Decorator
  end

  let(:test) { Test[] }

  context "#to_hash" do
    it "return a real hash" do
      test['foo'] = 'bar'
      expect(test.to_hash).to be_instance_of(Hash)
    end
  end

  context "#each" do
    it "yields decorated objects when nested" do
      test['foo'] = { 'bar' => 'baz' }
      test['bar'] = [ 1, 2 ]
      test.each { |k, v| expect(v).to be_instance_of(Test) }
    end

    it "yields undecorated objects when not nested" do
      test['foo'] = 1
      test['bar'] = "string"
      test.each { |k, v| expect(v).not_to be_instance_of(Test) }
    end
  end

  context "Array#map" do
    it "yields decorated objects when nested" do
      test['array'] =  [ { a: 'a' }, { b: 'b' } ]
      map = test['array'].map { |e| expect(e).to be_instance_of(Test) }
      expect(map[0]).to be_instance_of(Test)
    end

    it "yields undecorated objects when not nested" do
      test['array'] =  [ 1, true, "string" ]
      map = test['array'].map { |e| expect(e).not_to be_instance_of(Test) }
      expect(map[0]).not_to be_instance_of(Test)
    end
  end
end