File: value_collection_spec.rb

package info (click to toggle)
puppet 5.5.22-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 21,316 kB
  • sloc: ruby: 254,925; sh: 1,608; xml: 219; makefile: 153; sql: 103
file content (160 lines) | stat: -rw-r--r-- 5,299 bytes parent folder | download | duplicates (3)
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
require 'spec_helper'

require 'puppet/parameter'

describe Puppet::Parameter::ValueCollection do
  before do
    @collection = Puppet::Parameter::ValueCollection.new
  end

  it "should have a method for defining new values" do
    expect(@collection).to respond_to(:newvalues)
  end

  it "should have a method for adding individual values" do
    expect(@collection).to respond_to(:newvalue)
  end

  it "should be able to retrieve individual values" do
    value = @collection.newvalue(:foo)
    expect(@collection.value(:foo)).to equal(value)
  end

  it "should be able to add an individual value with a block" do
    @collection.newvalue(:foo) { raise "testing" }
    expect(@collection.value(:foo).block).to be_instance_of(Proc)
  end

  it "should be able to add values that are empty strings" do
    expect { @collection.newvalue('') }.to_not raise_error
  end

  it "should be able to add values that are empty strings" do
    value = @collection.newvalue('')
    expect(@collection.match?('')).to equal(value)
  end

  describe "when adding a value with a block" do
    it "should set the method name to 'set_' plus the value name" do
      value = @collection.newvalue(:myval) { raise "testing" }
      expect(value.method).to eq("set_myval")
    end
  end

  it "should be able to add an individual value with options" do
    value = @collection.newvalue(:foo, :method => 'set_myval')
    expect(value.method).to eq('set_myval')
  end

  it "should have a method for validating a value" do
    expect(@collection).to respond_to(:validate)
  end

  it "should have a method for munging a value" do
    expect(@collection).to respond_to(:munge)
  end

  it "should be able to generate documentation when it has both values and regexes" do
    @collection.newvalues :foo, "bar", %r{test}
    expect(@collection.doc).to be_instance_of(String)
  end

  it "should correctly generate documentation for values" do
    @collection.newvalues :foo
    expect(@collection.doc).to be_include("Valid values are `foo`")
  end

  it "should correctly generate documentation for regexes" do
    @collection.newvalues %r{\w+}
    expect(@collection.doc).to be_include("Values can match `/\\w+/`")
  end

  it "should be able to find the first matching value" do
    @collection.newvalues :foo, :bar
    expect(@collection.match?("foo")).to be_instance_of(Puppet::Parameter::Value)
  end

  it "should be able to match symbols" do
    @collection.newvalues :foo, :bar
    expect(@collection.match?(:foo)).to be_instance_of(Puppet::Parameter::Value)
  end

  it "should be able to match symbols when a regex is provided" do
    @collection.newvalues %r{.}
    expect(@collection.match?(:foo)).to be_instance_of(Puppet::Parameter::Value)
  end

  it "should be able to match values using regexes" do
    @collection.newvalues %r{.}
    expect(@collection.match?("foo")).not_to be_nil
  end

  it "should prefer value matches to regex matches" do
    @collection.newvalues %r{.}, :foo
    expect(@collection.match?("foo").name).to eq(:foo)
  end

  describe "when validating values" do
    it "should do nothing if no values or regexes have been defined" do
      @collection.validate("foo")
    end

    it "should fail if the value is not a defined value or alias and does not match a regex" do
      @collection.newvalues :foo
      expect { @collection.validate("bar") }.to raise_error(ArgumentError)
    end

    it "should succeed if the value is one of the defined values" do
      @collection.newvalues :foo
      expect { @collection.validate(:foo) }.to_not raise_error
    end

    it "should succeed if the value is one of the defined values even if the definition uses a symbol and the validation uses a string" do
      @collection.newvalues :foo
      expect { @collection.validate("foo") }.to_not raise_error
    end

    it "should succeed if the value is one of the defined values even if the definition uses a string and the validation uses a symbol" do
      @collection.newvalues "foo"
      expect { @collection.validate(:foo) }.to_not raise_error
    end

    it "should succeed if the value is one of the defined aliases" do
      @collection.newvalues :foo
      @collection.aliasvalue :bar, :foo
      expect { @collection.validate("bar") }.to_not raise_error
    end

    it "should succeed if the value matches one of the regexes" do
      @collection.newvalues %r{\d}
      expect { @collection.validate("10") }.to_not raise_error
    end
  end

  describe "when munging values" do
    it "should do nothing if no values or regexes have been defined" do
      expect(@collection.munge("foo")).to eq("foo")
    end

    it "should return return any matching defined values" do
      @collection.newvalues :foo, :bar
      expect(@collection.munge("foo")).to eq(:foo)
    end

    it "should return any matching aliases" do
      @collection.newvalues :foo
      @collection.aliasvalue :bar, :foo
      expect(@collection.munge("bar")).to eq(:foo)
    end

    it "should return the value if it matches a regex" do
      @collection.newvalues %r{\w}
      expect(@collection.munge("bar")).to eq("bar")
    end

    it "should return the value if no other option is matched" do
      @collection.newvalues :foo
      expect(@collection.munge("bar")).to eq("bar")
    end
  end
end