[Pkg-puppet-devel] [SCM] Puppet packaging for Debian branch, upstream, updated. 0.25.5-639-g8f94f35
test branch
puppet-dev at googlegroups.com
Wed Jul 14 10:30:32 UTC 2010
The following commit has been merged in the upstream branch:
commit 3c86666f68d50eb1a45bf1e1b81b8ffad0f3b9c1
Author: Luke Kanies <luke at madstop.com>
Date: Thu Oct 29 09:12:26 2009 -0700
Moving Parameter utility classes into separate files
Signed-off-by: Luke Kanies <luke at madstop.com>
diff --git a/lib/puppet/parameter.rb b/lib/puppet/parameter.rb
index 58a9147..0e4071c 100644
--- a/lib/puppet/parameter.rb
+++ b/lib/puppet/parameter.rb
@@ -12,216 +12,7 @@ class Puppet::Parameter
include Puppet::Util::MethodHelper
include Puppet::Util::Cacher
- # A collection of values and regexes, used for specifying
- # what values are allowed in a given parameter.
- class ValueCollection
- class Value
- attr_reader :name, :options, :event
- attr_accessor :block, :call, :method, :required_features
-
- # Add an alias for this value.
- def alias(name)
- @aliases << convert(name)
- end
-
- # Return all aliases.
- def aliases
- @aliases.dup
- end
-
- # Store the event that our value generates, if it does so.
- def event=(value)
- @event = convert(value)
- end
-
- def initialize(name)
- if name.is_a?(Regexp)
- @name = name
- else
- # Convert to a string and then a symbol, so things like true/false
- # still show up as symbols.
- @name = convert(name)
- end
-
- @aliases = []
-
- @call = :instead
- end
-
- # Does a provided value match our value?
- def match?(value)
- if regex?
- return true if name =~ value.to_s
- else
- return true if name == convert(value)
- return @aliases.include?(convert(value))
- end
- end
-
- # Is our value a regex?
- def regex?
- @name.is_a?(Regexp)
- end
-
- private
-
- # A standard way of converting all of our values, so we're always
- # comparing apples to apples.
- def convert(value)
- if value == ''
- # We can't intern an empty string, yay.
- value
- else
- value.to_s.to_sym
- end
- end
- end
-
- def aliasvalue(name, other)
- other = other.to_sym
- unless value = match?(other)
- raise Puppet::DevError, "Cannot alias nonexistent value %s" % other
- end
-
- value.alias(name)
- end
-
- # Return a doc string for all of the values in this parameter/property.
- def doc
- unless defined?(@doc)
- @doc = ""
- unless values.empty?
- @doc += " Valid values are "
- @doc += @strings.collect do |value|
- if aliases = value.aliases and ! aliases.empty?
- "``%s`` (also called ``%s``)" % [value.name, aliases.join(", ")]
- else
- "``%s``" % value.name
- end
- end.join(", ") + "."
- end
-
- unless regexes.empty?
- @doc += " Values can match ``" + regexes.join("``, ``") + "``."
- end
- end
-
- @doc
- end
-
- # Does this collection contain any value definitions?
- def empty?
- @values.empty?
- end
-
- def initialize
- # We often look values up by name, so a hash makes more sense.
- @values = {}
-
- # However, we want to retain the ability to match values in order,
- # but we always prefer directly equality (i.e., strings) over regex matches.
- @regexes = []
- @strings = []
- end
-
- # Can we match a given value?
- def match?(test_value)
- # First look for normal values
- if value = @strings.find { |v| v.match?(test_value) }
- return value
- end
-
- # Then look for a regex match
- @regexes.find { |v| v.match?(test_value) }
- end
-
- # If the specified value is allowed, then munge appropriately.
- def munge(value)
- return value if empty?
-
- if instance = match?(value)
- if instance.regex?
- return value
- else
- return instance.name
- end
- else
- return value
- end
- end
-
- # Define a new valid value for a property. You must provide the value itself,
- # usually as a symbol, or a regex to match the value.
- #
- # The first argument to the method is either the value itself or a regex.
- # The second argument is an option hash; valid options are:
- # * <tt>:event</tt>: The event that should be returned when this value is set.
- # * <tt>:call</tt>: When to call any associated block. The default value
- # is ``instead``, which means to call the value instead of calling the
- # provider. You can also specify ``before`` or ``after``, which will
- # call both the block and the provider, according to the order you specify
- # (the ``first`` refers to when the block is called, not the provider).
- def newvalue(name, options = {}, &block)
- value = Value.new(name)
- @values[value.name] = value
- if value.regex?
- @regexes << value
- else
- @strings << value
- end
-
- options.each { |opt, arg| value.send(opt.to_s + "=", arg) }
- if block_given?
- value.block = block
- else
- value.call = options[:call] || :none
- end
-
- if block_given? and ! value.regex?
- value.method ||= "set_" + value.name.to_s
- end
-
- value
- end
-
- # Define one or more new values for our parameter.
- def newvalues(*names)
- names.each { |name| newvalue(name) }
- end
-
- def regexes
- @regexes.collect { |r| r.name.inspect }
- end
-
- # Verify that the passed value is valid.
- def validate(value)
- return if empty?
-
- unless @values.detect { |name, v| v.match?(value) }
- str = "Invalid value %s. " % [value.inspect]
-
- unless values.empty?
- str += "Valid values are %s. " % values.join(", ")
- end
-
- unless regexes.empty?
- str += "Valid values match %s." % regexes.join(", ")
- end
-
- raise ArgumentError, str
- end
- end
-
- # Return a single value instance.
- def value(name)
- @values[name]
- end
-
- # Return the list of valid values.
- def values
- @strings.collect { |s| s.name }
- end
- end
+ require 'puppet/parameter/value_collection'
class << self
include Puppet::Util
diff --git a/lib/puppet/parameter/value.rb b/lib/puppet/parameter/value.rb
new file mode 100644
index 0000000..5171f25
--- /dev/null
+++ b/lib/puppet/parameter/value.rb
@@ -0,0 +1,64 @@
+require 'puppet/parameter/value_collection'
+
+# An individual Value class.
+class Puppet::Parameter::Value
+ attr_reader :name, :options, :event
+ attr_accessor :block, :call, :method, :required_features
+
+ # Add an alias for this value.
+ def alias(name)
+ @aliases << convert(name)
+ end
+
+ # Return all aliases.
+ def aliases
+ @aliases.dup
+ end
+
+ # Store the event that our value generates, if it does so.
+ def event=(value)
+ @event = convert(value)
+ end
+
+ def initialize(name)
+ if name.is_a?(Regexp)
+ @name = name
+ else
+ # Convert to a string and then a symbol, so things like true/false
+ # still show up as symbols.
+ @name = convert(name)
+ end
+
+ @aliases = []
+
+ @call = :instead
+ end
+
+ # Does a provided value match our value?
+ def match?(value)
+ if regex?
+ return true if name =~ value.to_s
+ else
+ return true if name == convert(value)
+ return @aliases.include?(convert(value))
+ end
+ end
+
+ # Is our value a regex?
+ def regex?
+ @name.is_a?(Regexp)
+ end
+
+ private
+
+ # A standard way of converting all of our values, so we're always
+ # comparing apples to apples.
+ def convert(value)
+ if value == ''
+ # We can't intern an empty string, yay.
+ value
+ else
+ value.to_s.to_sym
+ end
+ end
+end
diff --git a/lib/puppet/parameter/value_collection.rb b/lib/puppet/parameter/value_collection.rb
new file mode 100644
index 0000000..436226e
--- /dev/null
+++ b/lib/puppet/parameter/value_collection.rb
@@ -0,0 +1,151 @@
+require 'puppet/parameter/value'
+
+# A collection of values and regexes, used for specifying
+# what values are allowed in a given parameter.
+class Puppet::Parameter::ValueCollection
+
+ def aliasvalue(name, other)
+ other = other.to_sym
+ unless value = match?(other)
+ raise Puppet::DevError, "Cannot alias nonexistent value %s" % other
+ end
+
+ value.alias(name)
+ end
+
+ # Return a doc string for all of the values in this parameter/property.
+ def doc
+ unless defined?(@doc)
+ @doc = ""
+ unless values.empty?
+ @doc += " Valid values are "
+ @doc += @strings.collect do |value|
+ if aliases = value.aliases and ! aliases.empty?
+ "``%s`` (also called ``%s``)" % [value.name, aliases.join(", ")]
+ else
+ "``%s``" % value.name
+ end
+ end.join(", ") + "."
+ end
+
+ unless regexes.empty?
+ @doc += " Values can match ``" + regexes.join("``, ``") + "``."
+ end
+ end
+
+ @doc
+ end
+
+ # Does this collection contain any value definitions?
+ def empty?
+ @values.empty?
+ end
+
+ def initialize
+ # We often look values up by name, so a hash makes more sense.
+ @values = {}
+
+ # However, we want to retain the ability to match values in order,
+ # but we always prefer directly equality (i.e., strings) over regex matches.
+ @regexes = []
+ @strings = []
+ end
+
+ # Can we match a given value?
+ def match?(test_value)
+ # First look for normal values
+ if value = @strings.find { |v| v.match?(test_value) }
+ return value
+ end
+
+ # Then look for a regex match
+ @regexes.find { |v| v.match?(test_value) }
+ end
+
+ # If the specified value is allowed, then munge appropriately.
+ def munge(value)
+ return value if empty?
+
+ if instance = match?(value)
+ if instance.regex?
+ return value
+ else
+ return instance.name
+ end
+ else
+ return value
+ end
+ end
+
+ # Define a new valid value for a property. You must provide the value itself,
+ # usually as a symbol, or a regex to match the value.
+ #
+ # The first argument to the method is either the value itself or a regex.
+ # The second argument is an option hash; valid options are:
+ # * <tt>:event</tt>: The event that should be returned when this value is set.
+ # * <tt>:call</tt>: When to call any associated block. The default value
+ # is ``instead``, which means to call the value instead of calling the
+ # provider. You can also specify ``before`` or ``after``, which will
+ # call both the block and the provider, according to the order you specify
+ # (the ``first`` refers to when the block is called, not the provider).
+ def newvalue(name, options = {}, &block)
+ value = Puppet::Parameter::Value.new(name)
+ @values[value.name] = value
+ if value.regex?
+ @regexes << value
+ else
+ @strings << value
+ end
+
+ options.each { |opt, arg| value.send(opt.to_s + "=", arg) }
+ if block_given?
+ value.block = block
+ else
+ value.call = options[:call] || :none
+ end
+
+ if block_given? and ! value.regex?
+ value.method ||= "set_" + value.name.to_s
+ end
+
+ value
+ end
+
+ # Define one or more new values for our parameter.
+ def newvalues(*names)
+ names.each { |name| newvalue(name) }
+ end
+
+ def regexes
+ @regexes.collect { |r| r.name.inspect }
+ end
+
+ # Verify that the passed value is valid.
+ def validate(value)
+ return if empty?
+
+ unless @values.detect { |name, v| v.match?(value) }
+ str = "Invalid value %s. " % [value.inspect]
+
+ unless values.empty?
+ str += "Valid values are %s. " % values.join(", ")
+ end
+
+ unless regexes.empty?
+ str += "Valid values match %s." % regexes.join(", ")
+ end
+
+ raise ArgumentError, str
+ end
+ end
+
+ # Return a single value instance.
+ def value(name)
+ @values[name]
+ end
+
+ # Return the list of valid values.
+ def values
+ @strings.collect { |s| s.name }
+ end
+end
diff --git a/spec/unit/parameter.rb b/spec/unit/parameter.rb
index e3eaca6..817b5c6 100755
--- a/spec/unit/parameter.rb
+++ b/spec/unit/parameter.rb
@@ -159,247 +159,3 @@ describe Puppet::Parameter do
end
end
-describe Puppet::Parameter::ValueCollection do
- before do
- @collection = Puppet::Parameter::ValueCollection.new
- end
-
- it "should have a method for defining new values" do
- @collection.should respond_to(:newvalues)
- end
-
- it "should have a method for adding individual values" do
- @collection.should respond_to(:newvalue)
- end
-
- it "should be able to retrieve individual values" do
- value = @collection.newvalue(:foo)
- @collection.value(:foo).should equal(value)
- end
-
- it "should be able to add an individual value with a block" do
- @collection.newvalue(:foo) { raise "testing" }
- @collection.value(:foo).block.should be_instance_of(Proc)
- end
-
- it "should be able to add values that are empty strings" do
- lambda { @collection.newvalue('') }.should_not raise_error
- end
-
- it "should be able to add values that are empty strings" do
- value = @collection.newvalue('')
- @collection.match?('').should equal(value)
- end
-
- it "should set :call to :none when adding a value with no block" do
- value = @collection.newvalue(:foo)
- value.call.should == :none
- 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" }
- value.method.should == "set_myval"
- end
- end
-
- it "should be able to add an individual value with options" do
- value = @collection.newvalue(:foo, :call => :bar)
- value.call.should == :bar
- end
-
- it "should have a method for validating a value" do
- @collection.should respond_to(:validate)
- end
-
- it "should have a method for munging a value" do
- @collection.should 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}
- @collection.doc.should be_instance_of(String)
- end
-
- it "should correctly generate documentation for values" do
- @collection.newvalues :foo
- @collection.doc.should be_include("Valid values are ``foo``")
- end
-
- it "should correctly generate documentation for regexes" do
- @collection.newvalues %r{\w+}
- @collection.doc.should be_include("Values can match ``/\\w+/``")
- end
-
- it "should be able to find the first matching value" do
- @collection.newvalues :foo, :bar
- @collection.match?("foo").should be_instance_of(Puppet::Parameter::ValueCollection::Value)
- end
-
- it "should be able to match symbols" do
- @collection.newvalues :foo, :bar
- @collection.match?(:foo).should be_instance_of(Puppet::Parameter::ValueCollection::Value)
- end
-
- it "should be able to match symbols when a regex is provided" do
- @collection.newvalues %r{.}
- @collection.match?(:foo).should be_instance_of(Puppet::Parameter::ValueCollection::Value)
- end
-
- it "should be able to match values using regexes" do
- @collection.newvalues %r{.}
- @collection.match?("foo").should_not be_nil
- end
-
- it "should prefer value matches to regex matches" do
- @collection.newvalues %r{.}, :foo
- @collection.match?("foo").name.should == :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
- lambda { @collection.validate("bar") }.should raise_error(ArgumentError)
- end
-
- it "should succeed if the value is one of the defined values" do
- @collection.newvalues :foo
- lambda { @collection.validate(:foo) }.should_not raise_error(ArgumentError)
- 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
- lambda { @collection.validate("foo") }.should_not raise_error(ArgumentError)
- 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"
- lambda { @collection.validate(:foo) }.should_not raise_error(ArgumentError)
- end
-
- it "should succeed if the value is one of the defined aliases" do
- @collection.newvalues :foo
- @collection.aliasvalue :bar, :foo
- lambda { @collection.validate("bar") }.should_not raise_error(ArgumentError)
- end
-
- it "should succeed if the value matches one of the regexes" do
- @collection.newvalues %r{\d}
- lambda { @collection.validate("10") }.should_not raise_error(ArgumentError)
- end
- end
-
- describe "when munging values" do
- it "should do nothing if no values or regexes have been defined" do
- @collection.munge("foo").should == "foo"
- end
-
- it "should return return any matching defined values" do
- @collection.newvalues :foo, :bar
- @collection.munge("foo").should == :foo
- end
-
- it "should return any matching aliases" do
- @collection.newvalues :foo
- @collection.aliasvalue :bar, :foo
- @collection.munge("bar").should == :foo
- end
-
- it "should return the value if it matches a regex" do
- @collection.newvalues %r{\w}
- @collection.munge("bar").should == "bar"
- end
-
- it "should return the value if no other option is matched" do
- @collection.newvalues :foo
- @collection.munge("bar").should == "bar"
- end
- end
-end
-
-describe Puppet::Parameter::ValueCollection::Value do
- it "should require a name" do
- lambda { Puppet::Parameter::ValueCollection::Value.new }.should raise_error(ArgumentError)
- end
-
- it "should set its name" do
- Puppet::Parameter::ValueCollection::Value.new(:foo).name.should == :foo
- end
-
- it "should support regexes as names" do
- lambda { Puppet::Parameter::ValueCollection::Value.new(%r{foo}) }.should_not raise_error
- end
-
- it "should mark itself as a regex if its name is a regex" do
- Puppet::Parameter::ValueCollection::Value.new(%r{foo}).should be_regex
- end
-
- it "should always convert its name to a symbol if it is not a regex" do
- Puppet::Parameter::ValueCollection::Value.new("foo").name.should == :foo
- Puppet::Parameter::ValueCollection::Value.new(true).name.should == :true
- end
-
- it "should support adding aliases" do
- Puppet::Parameter::ValueCollection::Value.new("foo").should respond_to(:alias)
- end
-
- it "should be able to return its aliases" do
- value = Puppet::Parameter::ValueCollection::Value.new("foo")
- value.alias("bar")
- value.alias("baz")
- value.aliases.should == [:bar, :baz]
- end
-
- [:block, :call, :method, :event, :required_features].each do |attr|
- it "should support a #{attr} attribute" do
- value = Puppet::Parameter::ValueCollection::Value.new("foo")
- value.should respond_to(attr.to_s + "=")
- value.should respond_to(attr)
- end
- end
-
- it "should default to :instead for :call if a block is provided" do
- Puppet::Parameter::ValueCollection::Value.new("foo").call.should == :instead
- end
-
- it "should always return events as symbols" do
- value = Puppet::Parameter::ValueCollection::Value.new("foo")
- value.event = "foo_test"
- value.event.should == :foo_test
- end
-
- describe "when matching" do
- describe "a regex" do
- it "should return true if the regex matches the value" do
- Puppet::Parameter::ValueCollection::Value.new(/\w/).should be_match("foo")
- end
-
- it "should return false if the regex does not match the value" do
- Puppet::Parameter::ValueCollection::Value.new(/\d/).should_not be_match("foo")
- end
- end
-
- describe "a non-regex" do
- it "should return true if the value, converted to a symbol, matches the name" do
- Puppet::Parameter::ValueCollection::Value.new("foo").should be_match("foo")
- Puppet::Parameter::ValueCollection::Value.new(:foo).should be_match(:foo)
- Puppet::Parameter::ValueCollection::Value.new(:foo).should be_match("foo")
- Puppet::Parameter::ValueCollection::Value.new("foo").should be_match(:foo)
- end
-
- it "should return false if the value, converted to a symbol, does not match the name" do
- Puppet::Parameter::ValueCollection::Value.new(:foo).should_not be_match(:bar)
- end
-
- it "should return true if any of its aliases match" do
- value = Puppet::Parameter::ValueCollection::Value.new("foo")
- value.alias("bar")
- value.should be_match("bar")
- end
- end
- end
-end
diff --git a/spec/unit/parameter/value.rb b/spec/unit/parameter/value.rb
new file mode 100755
index 0000000..f6def01
--- /dev/null
+++ b/spec/unit/parameter/value.rb
@@ -0,0 +1,88 @@
+#!/usr/bin/env ruby
+
+require File.dirname(__FILE__) + '/../../spec_helper'
+
+require 'puppet/parameter'
+
+describe Puppet::Parameter::Value do
+ it "should require a name" do
+ lambda { Puppet::Parameter::Value.new }.should raise_error(ArgumentError)
+ end
+
+ it "should set its name" do
+ Puppet::Parameter::Value.new(:foo).name.should == :foo
+ end
+
+ it "should support regexes as names" do
+ lambda { Puppet::Parameter::Value.new(%r{foo}) }.should_not raise_error
+ end
+
+ it "should mark itself as a regex if its name is a regex" do
+ Puppet::Parameter::Value.new(%r{foo}).should be_regex
+ end
+
+ it "should always convert its name to a symbol if it is not a regex" do
+ Puppet::Parameter::Value.new("foo").name.should == :foo
+ Puppet::Parameter::Value.new(true).name.should == :true
+ end
+
+ it "should support adding aliases" do
+ Puppet::Parameter::Value.new("foo").should respond_to(:alias)
+ end
+
+ it "should be able to return its aliases" do
+ value = Puppet::Parameter::Value.new("foo")
+ value.alias("bar")
+ value.alias("baz")
+ value.aliases.should == [:bar, :baz]
+ end
+
+ [:block, :call, :method, :event, :required_features].each do |attr|
+ it "should support a #{attr} attribute" do
+ value = Puppet::Parameter::Value.new("foo")
+ value.should respond_to(attr.to_s + "=")
+ value.should respond_to(attr)
+ end
+ end
+
+ it "should default to :instead for :call if a block is provided" do
+ Puppet::Parameter::Value.new("foo").call.should == :instead
+ end
+
+ it "should always return events as symbols" do
+ value = Puppet::Parameter::Value.new("foo")
+ value.event = "foo_test"
+ value.event.should == :foo_test
+ end
+
+ describe "when matching" do
+ describe "a regex" do
+ it "should return true if the regex matches the value" do
+ Puppet::Parameter::Value.new(/\w/).should be_match("foo")
+ end
+
+ it "should return false if the regex does not match the value" do
+ Puppet::Parameter::Value.new(/\d/).should_not be_match("foo")
+ end
+ end
+
+ describe "a non-regex" do
+ it "should return true if the value, converted to a symbol, matches the name" do
+ Puppet::Parameter::Value.new("foo").should be_match("foo")
+ Puppet::Parameter::Value.new(:foo).should be_match(:foo)
+ Puppet::Parameter::Value.new(:foo).should be_match("foo")
+ Puppet::Parameter::Value.new("foo").should be_match(:foo)
+ end
+
+ it "should return false if the value, converted to a symbol, does not match the name" do
+ Puppet::Parameter::Value.new(:foo).should_not be_match(:bar)
+ end
+
+ it "should return true if any of its aliases match" do
+ value = Puppet::Parameter::Value.new("foo")
+ value.alias("bar")
+ value.should be_match("bar")
+ end
+ end
+ end
+end
diff --git a/spec/unit/parameter/value_collection.rb b/spec/unit/parameter/value_collection.rb
new file mode 100755
index 0000000..421e5a2
--- /dev/null
+++ b/spec/unit/parameter/value_collection.rb
@@ -0,0 +1,167 @@
+#!/usr/bin/env ruby
+
+require File.dirname(__FILE__) + '/../../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
+ @collection.should respond_to(:newvalues)
+ end
+
+ it "should have a method for adding individual values" do
+ @collection.should respond_to(:newvalue)
+ end
+
+ it "should be able to retrieve individual values" do
+ value = @collection.newvalue(:foo)
+ @collection.value(:foo).should equal(value)
+ end
+
+ it "should be able to add an individual value with a block" do
+ @collection.newvalue(:foo) { raise "testing" }
+ @collection.value(:foo).block.should be_instance_of(Proc)
+ end
+
+ it "should be able to add values that are empty strings" do
+ lambda { @collection.newvalue('') }.should_not raise_error
+ end
+
+ it "should be able to add values that are empty strings" do
+ value = @collection.newvalue('')
+ @collection.match?('').should equal(value)
+ end
+
+ it "should set :call to :none when adding a value with no block" do
+ value = @collection.newvalue(:foo)
+ value.call.should == :none
+ 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" }
+ value.method.should == "set_myval"
+ end
+ end
+
+ it "should be able to add an individual value with options" do
+ value = @collection.newvalue(:foo, :call => :bar)
+ value.call.should == :bar
+ end
+
+ it "should have a method for validating a value" do
+ @collection.should respond_to(:validate)
+ end
+
+ it "should have a method for munging a value" do
+ @collection.should 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}
+ @collection.doc.should be_instance_of(String)
+ end
+
+ it "should correctly generate documentation for values" do
+ @collection.newvalues :foo
+ @collection.doc.should be_include("Valid values are ``foo``")
+ end
+
+ it "should correctly generate documentation for regexes" do
+ @collection.newvalues %r{\w+}
+ @collection.doc.should be_include("Values can match ``/\\w+/``")
+ end
+
+ it "should be able to find the first matching value" do
+ @collection.newvalues :foo, :bar
+ @collection.match?("foo").should be_instance_of(Puppet::Parameter::Value)
+ end
+
+ it "should be able to match symbols" do
+ @collection.newvalues :foo, :bar
+ @collection.match?(:foo).should be_instance_of(Puppet::Parameter::Value)
+ end
+
+ it "should be able to match symbols when a regex is provided" do
+ @collection.newvalues %r{.}
+ @collection.match?(:foo).should be_instance_of(Puppet::Parameter::Value)
+ end
+
+ it "should be able to match values using regexes" do
+ @collection.newvalues %r{.}
+ @collection.match?("foo").should_not be_nil
+ end
+
+ it "should prefer value matches to regex matches" do
+ @collection.newvalues %r{.}, :foo
+ @collection.match?("foo").name.should == :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
+ lambda { @collection.validate("bar") }.should raise_error(ArgumentError)
+ end
+
+ it "should succeed if the value is one of the defined values" do
+ @collection.newvalues :foo
+ lambda { @collection.validate(:foo) }.should_not raise_error(ArgumentError)
+ 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
+ lambda { @collection.validate("foo") }.should_not raise_error(ArgumentError)
+ 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"
+ lambda { @collection.validate(:foo) }.should_not raise_error(ArgumentError)
+ end
+
+ it "should succeed if the value is one of the defined aliases" do
+ @collection.newvalues :foo
+ @collection.aliasvalue :bar, :foo
+ lambda { @collection.validate("bar") }.should_not raise_error(ArgumentError)
+ end
+
+ it "should succeed if the value matches one of the regexes" do
+ @collection.newvalues %r{\d}
+ lambda { @collection.validate("10") }.should_not raise_error(ArgumentError)
+ end
+ end
+
+ describe "when munging values" do
+ it "should do nothing if no values or regexes have been defined" do
+ @collection.munge("foo").should == "foo"
+ end
+
+ it "should return return any matching defined values" do
+ @collection.newvalues :foo, :bar
+ @collection.munge("foo").should == :foo
+ end
+
+ it "should return any matching aliases" do
+ @collection.newvalues :foo
+ @collection.aliasvalue :bar, :foo
+ @collection.munge("bar").should == :foo
+ end
+
+ it "should return the value if it matches a regex" do
+ @collection.newvalues %r{\w}
+ @collection.munge("bar").should == "bar"
+ end
+
+ it "should return the value if no other option is matched" do
+ @collection.newvalues :foo
+ @collection.munge("bar").should == "bar"
+ end
+ end
+end
--
Puppet packaging for Debian
More information about the Pkg-puppet-devel
mailing list