From fde64f37c98291f4a5b9ee1fcf634288e42b730a Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Sun, 24 Jul 2011 00:39:17 +0100 Subject: (#1) - fleshed out some more tests. --- lib/puppet/parser/functions/sort.rb | 6 ++++-- lib/puppet/parser/functions/squeeze.rb | 21 +++++++++++++++++++-- .../parser/functions/is_valid_domain_name_spec.rb | 10 ++++++++++ .../parser/functions/is_valid_ip_address_spec.rb | 10 ++++++++++ .../parser/functions/is_valid_mac_address_spec.rb | 10 ++++++++++ spec/unit/parser/functions/join_spec.rb | 5 +++++ spec/unit/parser/functions/join_with_prefix_spec.rb | 5 +++++ spec/unit/parser/functions/keys_spec.rb | 5 +++++ spec/unit/parser/functions/lstrip_spec.rb | 5 +++++ spec/unit/parser/functions/member_spec.rb | 10 ++++++++++ spec/unit/parser/functions/num2bool_spec.rb | 5 +++++ spec/unit/parser/functions/prefix_spec.rb | 5 +++++ spec/unit/parser/functions/range_spec.rb | 10 ++++++++++ spec/unit/parser/functions/reverse_spec.rb | 5 +++++ spec/unit/parser/functions/rstrip_spec.rb | 10 ++++++++++ spec/unit/parser/functions/size_spec.rb | 10 ++++++++++ spec/unit/parser/functions/sort_spec.rb | 9 +++++++-- spec/unit/parser/functions/squeeze_spec.rb | 10 ++++++++++ spec/unit/parser/functions/str2bool_spec.rb | 10 ++++++++++ spec/unit/parser/functions/strip_spec.rb | 5 +++++ spec/unit/parser/functions/swapcase_spec.rb | 5 +++++ spec/unit/parser/functions/type_spec.rb | 5 +++++ spec/unit/parser/functions/unique_spec.rb | 5 +++++ spec/unit/parser/functions/upcase_spec.rb | 5 +++++ spec/unit/parser/functions/values_at_spec.rb | 5 +++++ spec/unit/parser/functions/values_spec.rb | 5 +++++ spec/unit/parser/functions/zip_spec.rb | 5 +++++ 27 files changed, 195 insertions(+), 6 deletions(-) diff --git a/lib/puppet/parser/functions/sort.rb b/lib/puppet/parser/functions/sort.rb index 974141c..54a4018 100644 --- a/lib/puppet/parser/functions/sort.rb +++ b/lib/puppet/parser/functions/sort.rb @@ -7,11 +7,13 @@ module Puppet::Parser::Functions 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 diff --git a/lib/puppet/parser/functions/squeeze.rb b/lib/puppet/parser/functions/squeeze.rb index 02eb00c..9a1dd20 100644 --- a/lib/puppet/parser/functions/squeeze.rb +++ b/lib/puppet/parser/functions/squeeze.rb @@ -7,9 +7,26 @@ module Puppet::Parser::Functions 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 diff --git a/spec/unit/parser/functions/is_valid_domain_name_spec.rb b/spec/unit/parser/functions/is_valid_domain_name_spec.rb index 5cea285..f03f6e8 100644 --- a/spec/unit/parser/functions/is_valid_domain_name_spec.rb +++ b/spec/unit/parser/functions/is_valid_domain_name_spec.rb @@ -18,4 +18,14 @@ describe "the is_valid_domain_name function" do 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 diff --git a/spec/unit/parser/functions/is_valid_ip_address_spec.rb b/spec/unit/parser/functions/is_valid_ip_address_spec.rb index fa803dd..ee53ee1 100644 --- a/spec/unit/parser/functions/is_valid_ip_address_spec.rb +++ b/spec/unit/parser/functions/is_valid_ip_address_spec.rb @@ -18,4 +18,14 @@ describe "the is_valid_ip_address function" do 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 diff --git a/spec/unit/parser/functions/is_valid_mac_address_spec.rb b/spec/unit/parser/functions/is_valid_mac_address_spec.rb index f2a8389..c4eb468 100644 --- a/spec/unit/parser/functions/is_valid_mac_address_spec.rb +++ b/spec/unit/parser/functions/is_valid_mac_address_spec.rb @@ -18,4 +18,14 @@ describe "the is_valid_mac_address function" do 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 diff --git a/spec/unit/parser/functions/join_spec.rb b/spec/unit/parser/functions/join_spec.rb index a7dc0e5..1b3dec8 100644 --- a/spec/unit/parser/functions/join_spec.rb +++ b/spec/unit/parser/functions/join_spec.rb @@ -18,4 +18,9 @@ describe "the join function" do 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 diff --git a/spec/unit/parser/functions/join_with_prefix_spec.rb b/spec/unit/parser/functions/join_with_prefix_spec.rb index 0182d8c..70e6965 100644 --- a/spec/unit/parser/functions/join_with_prefix_spec.rb +++ b/spec/unit/parser/functions/join_with_prefix_spec.rb @@ -18,4 +18,9 @@ describe "the join_with_prefix function" do 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 diff --git a/spec/unit/parser/functions/keys_spec.rb b/spec/unit/parser/functions/keys_spec.rb index 13dc260..927be96 100644 --- a/spec/unit/parser/functions/keys_spec.rb +++ b/spec/unit/parser/functions/keys_spec.rb @@ -18,4 +18,9 @@ describe "the keys function" do 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 diff --git a/spec/unit/parser/functions/lstrip_spec.rb b/spec/unit/parser/functions/lstrip_spec.rb index 9726675..ac331fa 100644 --- a/spec/unit/parser/functions/lstrip_spec.rb +++ b/spec/unit/parser/functions/lstrip_spec.rb @@ -18,4 +18,9 @@ describe "the lstrip function" do 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 diff --git a/spec/unit/parser/functions/member_spec.rb b/spec/unit/parser/functions/member_spec.rb index 39b684f..2cebc0d 100644 --- a/spec/unit/parser/functions/member_spec.rb +++ b/spec/unit/parser/functions/member_spec.rb @@ -18,4 +18,14 @@ describe "the member function" do 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 diff --git a/spec/unit/parser/functions/num2bool_spec.rb b/spec/unit/parser/functions/num2bool_spec.rb index fbd25c8..2f6435d 100644 --- a/spec/unit/parser/functions/num2bool_spec.rb +++ b/spec/unit/parser/functions/num2bool_spec.rb @@ -18,4 +18,9 @@ describe "the num2bool function" do 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 diff --git a/spec/unit/parser/functions/prefix_spec.rb b/spec/unit/parser/functions/prefix_spec.rb index 9ede439..a0cbcab 100644 --- a/spec/unit/parser/functions/prefix_spec.rb +++ b/spec/unit/parser/functions/prefix_spec.rb @@ -18,4 +18,9 @@ describe "the prefix function" do 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 diff --git a/spec/unit/parser/functions/range_spec.rb b/spec/unit/parser/functions/range_spec.rb index 23310bc..8c2446a 100644 --- a/spec/unit/parser/functions/range_spec.rb +++ b/spec/unit/parser/functions/range_spec.rb @@ -18,4 +18,14 @@ describe "the range function" do 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 diff --git a/spec/unit/parser/functions/reverse_spec.rb b/spec/unit/parser/functions/reverse_spec.rb index 27aa2cf..4fa50e4 100644 --- a/spec/unit/parser/functions/reverse_spec.rb +++ b/spec/unit/parser/functions/reverse_spec.rb @@ -18,4 +18,9 @@ describe "the reverse function" do 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 diff --git a/spec/unit/parser/functions/rstrip_spec.rb b/spec/unit/parser/functions/rstrip_spec.rb index a6e73ec..af8cc12 100644 --- a/spec/unit/parser/functions/rstrip_spec.rb +++ b/spec/unit/parser/functions/rstrip_spec.rb @@ -18,4 +18,14 @@ describe "the rstrip function" do 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 diff --git a/spec/unit/parser/functions/size_spec.rb b/spec/unit/parser/functions/size_spec.rb index 0d1f0c4..ccaa335 100644 --- a/spec/unit/parser/functions/size_spec.rb +++ b/spec/unit/parser/functions/size_spec.rb @@ -18,4 +18,14 @@ describe "the size function" do 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 diff --git a/spec/unit/parser/functions/sort_spec.rb b/spec/unit/parser/functions/sort_spec.rb index ae62d5f..da5db7a 100644 --- a/spec/unit/parser/functions/sort_spec.rb +++ b/spec/unit/parser/functions/sort_spec.rb @@ -14,8 +14,13 @@ describe "the sort function" do 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 diff --git a/spec/unit/parser/functions/squeeze_spec.rb b/spec/unit/parser/functions/squeeze_spec.rb index da8965c..9355ad2 100644 --- a/spec/unit/parser/functions/squeeze_spec.rb +++ b/spec/unit/parser/functions/squeeze_spec.rb @@ -18,4 +18,14 @@ describe "the squeeze function" do 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 diff --git a/spec/unit/parser/functions/str2bool_spec.rb b/spec/unit/parser/functions/str2bool_spec.rb index 6a1ac95..d7f0ac9 100644 --- a/spec/unit/parser/functions/str2bool_spec.rb +++ b/spec/unit/parser/functions/str2bool_spec.rb @@ -18,4 +18,14 @@ describe "the str2bool function" do 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 diff --git a/spec/unit/parser/functions/strip_spec.rb b/spec/unit/parser/functions/strip_spec.rb index ca06845..48a52dd 100644 --- a/spec/unit/parser/functions/strip_spec.rb +++ b/spec/unit/parser/functions/strip_spec.rb @@ -18,4 +18,9 @@ describe "the strip function" do 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 diff --git a/spec/unit/parser/functions/swapcase_spec.rb b/spec/unit/parser/functions/swapcase_spec.rb index 7c5ff30..2686054 100644 --- a/spec/unit/parser/functions/swapcase_spec.rb +++ b/spec/unit/parser/functions/swapcase_spec.rb @@ -18,4 +18,9 @@ describe "the swapcase function" do 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 diff --git a/spec/unit/parser/functions/type_spec.rb b/spec/unit/parser/functions/type_spec.rb index 4109da1..fddb87d 100644 --- a/spec/unit/parser/functions/type_spec.rb +++ b/spec/unit/parser/functions/type_spec.rb @@ -18,4 +18,9 @@ describe "the type function" do 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 diff --git a/spec/unit/parser/functions/unique_spec.rb b/spec/unit/parser/functions/unique_spec.rb index fe7b3ca..7b8b08d 100644 --- a/spec/unit/parser/functions/unique_spec.rb +++ b/spec/unit/parser/functions/unique_spec.rb @@ -18,4 +18,9 @@ describe "the unique function" do 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 diff --git a/spec/unit/parser/functions/upcase_spec.rb b/spec/unit/parser/functions/upcase_spec.rb index 39884eb..10e4c8a 100644 --- a/spec/unit/parser/functions/upcase_spec.rb +++ b/spec/unit/parser/functions/upcase_spec.rb @@ -18,4 +18,9 @@ describe "the upcase function" do 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 diff --git a/spec/unit/parser/functions/values_at_spec.rb b/spec/unit/parser/functions/values_at_spec.rb index af5dc25..ec8730b 100644 --- a/spec/unit/parser/functions/values_at_spec.rb +++ b/spec/unit/parser/functions/values_at_spec.rb @@ -18,4 +18,9 @@ describe "the values_at function" do 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 diff --git a/spec/unit/parser/functions/values_spec.rb b/spec/unit/parser/functions/values_spec.rb index 2c313a0..92f1311 100644 --- a/spec/unit/parser/functions/values_spec.rb +++ b/spec/unit/parser/functions/values_spec.rb @@ -18,4 +18,9 @@ describe "the values function" do 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 diff --git a/spec/unit/parser/functions/zip_spec.rb b/spec/unit/parser/functions/zip_spec.rb index 3d0392a..074f4df 100644 --- a/spec/unit/parser/functions/zip_spec.rb +++ b/spec/unit/parser/functions/zip_spec.rb @@ -18,4 +18,9 @@ describe "the zip function" do 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 -- cgit v1.2.3