Luke Kanies wrote: > I'd prefer this be squashed into the other commit - I like having code > and tests in the same commit if possible.
OK, squashed patch below. Sorry for the delay; I've been out of office a while and haven't had time to respond until now. From 729f66af9cc46f483afa57422d3bfaa256474f80 Mon Sep 17 00:00:00 2001 From: Thomas Bellman <[email protected]> Date: Tue, 19 May 2009 16:56:53 +0200 Subject: [PATCH] Improved error handling of split() function. Check for wrong number of arguments. Also added unit tests for split(). Signed-off-by: Thomas Bellman <[email protected]> --- lib/puppet/parser/functions/split.rb | 8 ++++++- spec/unit/parser/functions/split.rb | 35 ++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletions(-) create mode 100755 spec/unit/parser/functions/split.rb diff --git a/lib/puppet/parser/functions/split.rb b/lib/puppet/parser/functions/split.rb index cfb3ab5..afd81f4 100644 --- a/lib/puppet/parser/functions/split.rb +++ b/lib/puppet/parser/functions/split.rb @@ -1,5 +1,5 @@ module Puppet::Parser::Functions - newfunction(:split, :type => :rvalue, + newfunction(:split, :type => :rvalue, :doc => "Split a string variable into an array using the specified split character. Usage:: @@ -8,6 +8,12 @@ Usage:: $array_var = split($string, ',') $array_var holds the result ['value1', 'value2']") do |args| + + if args.length != 2 + raise Puppet::ParseError, ("split(): wrong number of arguments" + + " (#{args.length}; must be 2)") + end + return args[0].split(args[1]) end end diff --git a/spec/unit/parser/functions/split.rb b/spec/unit/parser/functions/split.rb new file mode 100755 index 0000000..7f96d5d --- /dev/null +++ b/spec/unit/parser/functions/split.rb @@ -0,0 +1,35 @@ +#! /usr/bin/env ruby + +require File.dirname(__FILE__) + '/../../../spec_helper' + +describe "the split function" do + + before :each do + @scope = Puppet::Parser::Scope.new() + end + + it "should exist" do + Puppet::Parser::Functions.function("split").should == "function_split" + end + + it "should raise a ParseError if there is less than 2 arguments" do + lambda { @scope.function_split(["foo"]) }.should( + raise_error(Puppet::ParseError)) + end + + it "should raise a ParseError if there is more than 2 arguments" do + lambda { @scope.function_split(["foo", "bar", "gazonk"]) }.should( + raise_error(Puppet::ParseError)) + end + + it "should handle a simple string" do + result = @scope.function_split([ "130;236;254;10", ";"]) + result.should(eql(["130", "236", "254", "10"])) + end + + it "should not interpret the second argument as a regexp" do + result = @scope.function_split([ "130.236;254[.;]10", "[.;]"]) + result.should(eql(["130.236;254", "10"])) + end + +end -- 1.6.0.6 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Puppet Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/puppet-dev?hl=en -~----------~----~----~----~------~----~------~--~---
