summaryrefslogtreecommitdiffstats
path: root/spec
diff options
context:
space:
mode:
authorThomas Bellman <bellman@nsc.liu.se>2009-07-31 18:13:44 +0200
committerJames Turnbull <james@lovedthanlost.net>2009-09-15 06:49:55 +1000
commit630407d527905a9c874ae4b32a62849fdf6864b7 (patch)
tree8591dbea6c654b952edbb5e83f72cf335879e18d /spec
parenta45c4354b9ed8deaeb3173a495f06602472faebe (diff)
downloadpuppet-630407d527905a9c874ae4b32a62849fdf6864b7.tar.gz
puppet-630407d527905a9c874ae4b32a62849fdf6864b7.tar.xz
puppet-630407d527905a9c874ae4b32a62849fdf6864b7.zip
Make regsubst() function operate on arrays (feature #2491).
Allow the first argument to the regsubst() function be an array, and perform regexp replacement on each element of the array in that case. This patch also adds more error checking to give better error messages to the user when given bad parameters. Signed-off-by: Thomas Bellman <bellman@nsc.liu.se>
Diffstat (limited to 'spec')
-rwxr-xr-xspec/unit/parser/functions/regsubst.rb80
1 files changed, 80 insertions, 0 deletions
diff --git a/spec/unit/parser/functions/regsubst.rb b/spec/unit/parser/functions/regsubst.rb
index 0e80ec798..5a533efb1 100755
--- a/spec/unit/parser/functions/regsubst.rb
+++ b/spec/unit/parser/functions/regsubst.rb
@@ -28,6 +28,26 @@ describe "the regsubst function" do
raise_error(Puppet::ParseError))
end
+ it "should raise a ParseError for non-string and non-array target" do
+ lambda { @scope.function_regsubst([4711, "bar", "gazonk"]) }.should(
+ raise_error(Puppet::ParseError))
+ end
+
+ it "should raise a ParseError for array target with non-string element" do
+ lambda { @scope.function_regsubst([["x", ["y"], "z"], "bar", "gazonk"]) }.should(
+ raise_error(Puppet::ParseError))
+ end
+
+ it "should raise a ParseError for a bad regular expression" do
+ lambda { @scope.function_regsubst(["foo", "(bar", "gazonk"]) }.should(
+ raise_error(Puppet::ParseError))
+ end
+
+ it "should raise a ParseError for a non-string regular expression" do
+ lambda { @scope.function_regsubst(["foo", ["bar"], "gazonk"]) }.should(
+ raise_error(Puppet::ParseError))
+ end
+
it "should handle groups" do
result = @scope.function_regsubst(
[ '130.236.254.10',
@@ -85,4 +105,64 @@ describe "the regsubst function" do
result.should(eql('<130>.<236>.<254>.<10>'))
end
+ it "should apply on all elements of an array" do
+ data = ['130.236.254.10', 'foo.example.com', 'coconut', '10.20.30.40']
+ result = @scope.function_regsubst([ data, '[.]', '-'])
+ result.should(eql(
+ ['130-236.254.10', 'foo-example.com', 'coconut', '10-20.30.40']))
+ end
+
+ it "should apply global substitutions on all elements of an array" do
+ data = ['130.236.254.10', 'foo.example.com', 'coconut', '10.20.30.40']
+ result = @scope.function_regsubst([ data, '[.]', '-', 'G'])
+ result.should(eql(
+ ['130-236-254-10', 'foo-example-com', 'coconut', '10-20-30-40']))
+ end
+
+ it "should handle groups on all elements of an array" do
+ data = ['130.236.254.10', 'foo.example.com', 'coconut', '10.20.30.40']
+ result = @scope.function_regsubst(
+ [ data,
+ '^([0-9]+)[.]([0-9]+)[.]([0-9]+)[.]([0-9]+)$',
+ '\4-\3-\2-\1'
+ ])
+ result.should(eql(
+ ['10-254-236-130', 'foo.example.com', 'coconut', '40-30-20-10']))
+ end
+
+ it "should handle global substitutions with groups on all elements of an array" do
+ data = ['130.236.254.10', 'foo.example.com', 'coconut', '10.20.30.40']
+ result = @scope.function_regsubst(
+ [ data,
+ '([^.]+)',
+ '<\1>',
+ 'G'
+ ])
+ result.should(eql(
+ ['<130>.<236>.<254>.<10>', '<foo>.<example>.<com>',
+ '<coconut>', '<10>.<20>.<30>.<40>']))
+ end
+
+ it "should return an array (not a string) for a single element array parameter" do
+ data = ['130.236.254.10']
+ result = @scope.function_regsubst(
+ [ data,
+ '([^.]+)',
+ '<\1>',
+ 'G'
+ ])
+ result.should(eql(['<130>.<236>.<254>.<10>']))
+ end
+
+ it "should return a string (not a one element array) for a simple string parameter" do
+ data = '130.236.254.10'
+ result = @scope.function_regsubst(
+ [ data,
+ '([^.]+)',
+ '<\1>',
+ 'G'
+ ])
+ result.should(eql('<130>.<236>.<254>.<10>'))
+ end
+
end