EOS
) do |arguments|
- if (arguments.size != 0) then
+ if (arguments.size != 1) then
raise(Puppet::ParseError, "sort(): Wrong number of arguments "+
- "given #{arguments.size} for 0")
+ "given #{arguments.size} for 1")
end
+ arguments[0].sort
+
end
end
EOS
) do |arguments|
- if (arguments.size != 2) then
+ if ((arguments.size != 2) and (arguments.size != 1)) then
raise(Puppet::ParseError, "squeeze(): Wrong number of arguments "+
- "given #{arguments.size} for 2")
+ "given #{arguments.size} for 2 or 1")
+ end
+
+ item = arguments[0]
+ squeezeval = arguments[1]
+
+ if item.is_a?(Array) then
+ if squeezeval then
+ item.collect { |i| i.squeeze(squeezeval) }
+ else
+ item.collect { |i| i.squeeze }
+ end
+ else
+ if squeezeval then
+ item.squeeze(squeezeval)
+ else
+ item.squeeze
+ end
end
end
lambda { @scope.function_is_valid_domain_name([]) }.should( raise_error(Puppet::ParseError))
end
+ it "should return true if a valid domain name" do
+ result = @scope.function_is_valid_domain_name("foo.bar.com")
+ result.should(eq(true))
+ end
+
+ it "should return false if not a valid domain name" do
+ result = @scope.function_is_valid_domain_name("not valid")
+ result.should(eq(false))
+ end
+
end
lambda { @scope.function_is_valid_ip_address([]) }.should( raise_error(Puppet::ParseError))
end
+ it "should return true if an IP address" do
+ result = @scope.function_is_valid_ip_address("1.2.3.4")
+ result.should(eq(true))
+ end
+
+ it "should return false if not valid" do
+ result = @scope.function_is_valid_ip_address("asdf")
+ result.should(eq(false))
+ end
+
end
lambda { @scope.function_is_valid_mac_address([]) }.should( raise_error(Puppet::ParseError))
end
+ it "should return true if a valid mac address" do
+ result = @scope.function_is_valid_mac_address("00:a0:1f:12:7f:a0")
+ result.should(eq(true))
+ end
+
+ it "should return false if not valid" do
+ result = @scope.function_is_valid_mac_address("not valid")
+ result.should(eq(false))
+ end
+
end
lambda { @scope.function_join([]) }.should( raise_error(Puppet::ParseError))
end
+ it "should join an array into a string" do
+ result = @scope.function_join([["a","b","c"], ":"])
+ result.should(eq("a:b:c"))
+ end
+
end
lambda { @scope.function_join_with_prefix([]) }.should( raise_error(Puppet::ParseError))
end
+ it "should join an array into a string" do
+ result = @scope.function_join_with_prefix([["a","b","c"], ":", "p"])
+ result.should(eq("pa:pb:pc"))
+ end
+
end
lambda { @scope.function_keys([]) }.should( raise_error(Puppet::ParseError))
end
+ it "should return an array of keys when given a hash" do
+ result = @scope.function_keys([{'a'=>1, 'b' => 2}])
+ result.should(eq(['a','b']))
+ end
+
end
lambda { @scope.function_lstrip([]) }.should( raise_error(Puppet::ParseError))
end
+ it "should lstrip a string" do
+ result = @scope.function_lstrip([" asdf"])
+ result.should(eq('asdf'))
+ end
+
end
lambda { @scope.function_member([]) }.should( raise_error(Puppet::ParseError))
end
+ it "should return true if a member is in an array" do
+ result = @scope.function_member([["a","b","c"], "a"])
+ result.should(eq(true))
+ end
+
+ it "should return false if a member is not in an array" do
+ result = @scope.function_member([["a","b","c"], "d"])
+ result.should(eq(false))
+ end
+
end
lambda { @scope.function_num2bool([]) }.should( raise_error(Puppet::ParseError))
end
+ it "should return true if 1" do
+ result = @scope.function_num2bool(["1"])
+ result.should(eq(true))
+ end
+
end
lambda { @scope.function_prefix([]) }.should( raise_error(Puppet::ParseError))
end
+ it "should return a prefixed array" do
+ result = @scope.function_prefix([['a','b','c'], 'p'])
+ result.should(eq(['pa','pb','pc']))
+ end
+
end
lambda { @scope.function_range([]) }.should( raise_error(Puppet::ParseError))
end
+ it "should return a letter range" do
+ result = @scope.function_range(["a","d"])
+ result.should(eq(['a','b','c','d']))
+ end
+
+ it "should return a number range" do
+ result = @scope.function_range(["1","4"])
+ result.should(eq([1,2,3,4]))
+ end
+
end
lambda { @scope.function_reverse([]) }.should( raise_error(Puppet::ParseError))
end
+ it "should reverse a string" do
+ result = @scope.function_reverse(["asdfghijkl"])
+ result.should(eq('lkjihgfdsa'))
+ end
+
end
lambda { @scope.function_rstrip([]) }.should( raise_error(Puppet::ParseError))
end
+ it "should rstrip a string" do
+ result = @scope.function_rstrip(["asdf "])
+ result.should(eq('asdf'))
+ end
+
+ it "should rstrip each element in an array" do
+ result = @scope.function_rstrip([["a ","b ", "c "]])
+ result.should(eq(['a','b','c']))
+ end
+
end
lambda { @scope.function_size([]) }.should( raise_error(Puppet::ParseError))
end
+ it "should return the size of a string" do
+ result = @scope.function_size(["asdf"])
+ result.should(eq(4))
+ end
+
+ it "should return the size of an array" do
+ result = @scope.function_size([["a","b","c"]])
+ result.should(eq(3))
+ end
+
end
Puppet::Parser::Functions.function("sort").should == "function_sort"
end
- it "should raise a ParseError if there is not 0 arguments" do
- lambda { @scope.function_sort(['']) }.should( raise_error(Puppet::ParseError))
+ it "should raise a ParseError if there is not 1 arguments" do
+ lambda { @scope.function_sort(['','']) }.should( raise_error(Puppet::ParseError))
+ end
+
+ it "should sort an array" do
+ result = @scope.function_sort([["a","c","b"]])
+ result.should(eq(['a','b','c']))
end
end
lambda { @scope.function_squeeze([]) }.should( raise_error(Puppet::ParseError))
end
+ it "should squeeze a string" do
+ result = @scope.function_squeeze(["aaabbbbcccc"])
+ result.should(eq('abc'))
+ end
+
+ it "should squeeze all elements in an array" do
+ result = @scope.function_squeeze([["aaabbbbcccc","dddfff"]])
+ result.should(eq(['abc','df']))
+ end
+
end
lambda { @scope.function_str2bool([]) }.should( raise_error(Puppet::ParseError))
end
+ it "should convert string 'true' to true" do
+ result = @scope.function_str2bool(["true"])
+ result.should(eq(true))
+ end
+
+ it "should convert string 'undef' to false" do
+ result = @scope.function_str2bool(["undef"])
+ result.should(eq(false))
+ end
+
end
lambda { @scope.function_strip([]) }.should( raise_error(Puppet::ParseError))
end
+ it "should strip a string" do
+ result = @scope.function_strip([" ab cd "])
+ result.should(eq('ab cd'))
+ end
+
end
lambda { @scope.function_swapcase([]) }.should( raise_error(Puppet::ParseError))
end
+ it "should swapcase a string" do
+ result = @scope.function_swapcase(["aaBBccDD"])
+ result.should(eq('AAbbCCdd'))
+ end
+
end
lambda { @scope.function_type([]) }.should( raise_error(Puppet::ParseError))
end
+ it "should return a type when given a string" do
+ result = @scope.function_type(["aaabbbbcccc"])
+ result.should(eq('String'))
+ end
+
end
lambda { @scope.function_unique([]) }.should( raise_error(Puppet::ParseError))
end
+ it "should remove duplicate elements in a string" do
+ result = @scope.function_squeeze([["aabbc"]])
+ result.should(eq(['abc']))
+ end
+
end
lambda { @scope.function_upcase([]) }.should( raise_error(Puppet::ParseError))
end
+ it "should upcase a string" do
+ result = @scope.function_upcase(["abc"])
+ result.should(eq('ABC'))
+ end
+
end
lambda { @scope.function_values_at([]) }.should( raise_error(Puppet::ParseError))
end
+ it "should return a value at from an array" do
+ result = @scope.function_values_at([['a','b','c'],"1"])
+ result.should(eq(['b']))
+ end
+
end
lambda { @scope.function_values([]) }.should( raise_error(Puppet::ParseError))
end
+ it "should return values from a hash" do
+ result = @scope.function_values([{'a'=>'1','b'=>'2','c'=>'3'}])
+ result.should(eq(['1','2','3']))
+ end
+
end
lambda { @scope.function_zip([]) }.should( raise_error(Puppet::ParseError))
end
+ it "should be able to zip an array" do
+ result = @scope.function_zip([['1','2','3'],['4','5','6']])
+ result.should(eq([["1", "4"], ["2", "5"], ["3", "6"]]))
+ end
+
end