From 464fb1f41b9c7197fcaade6831b80d6390829ec7 Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Wed, 29 Jun 2011 23:37:37 +0100 Subject: Add some more functional tests. --- lib/puppet/parser/functions/date.rb | 2 ++ lib/puppet/parser/functions/delete.rb | 6 +++++ lib/puppet/parser/functions/fact.rb | 36 --------------------------- lib/puppet/parser/functions/grep.rb | 5 ++++ spec/unit/parser/functions/delete_at_spec.rb | 5 ++++ spec/unit/parser/functions/delete_spec.rb | 5 ++++ spec/unit/parser/functions/downcase_spec.rb | 5 ++++ spec/unit/parser/functions/empty_spec.rb | 5 ++++ spec/unit/parser/functions/fact_spec.rb | 21 ---------------- spec/unit/parser/functions/flatten_spec.rb | 5 ++++ spec/unit/parser/functions/grep_spec.rb | 5 ++++ spec/unit/parser/functions/hash_spec.rb | 5 ++++ spec/unit/parser/functions/is_array_spec.rb | 10 ++++++++ spec/unit/parser/functions/is_float_spec.rb | 15 +++++++++++ spec/unit/parser/functions/is_hash_spec.rb | 15 +++++++++++ spec/unit/parser/functions/is_integer_spec.rb | 15 +++++++++++ 16 files changed, 103 insertions(+), 57 deletions(-) delete mode 100644 lib/puppet/parser/functions/fact.rb delete mode 100755 spec/unit/parser/functions/fact_spec.rb diff --git a/lib/puppet/parser/functions/date.rb b/lib/puppet/parser/functions/date.rb index 4d0543e..bc62e60 100644 --- a/lib/puppet/parser/functions/date.rb +++ b/lib/puppet/parser/functions/date.rb @@ -12,6 +12,8 @@ module Puppet::Parser::Functions "given #{arguments.size} for 1") end + # TODO: stubbed + end end diff --git a/lib/puppet/parser/functions/delete.rb b/lib/puppet/parser/functions/delete.rb index 88f3448..0d208b5 100644 --- a/lib/puppet/parser/functions/delete.rb +++ b/lib/puppet/parser/functions/delete.rb @@ -15,6 +15,12 @@ module Puppet::Parser::Functions "given #{arguments.size} for 2") end + a = arguments[0] + item = arguments[1] + + a.delete(item) + a + end end diff --git a/lib/puppet/parser/functions/fact.rb b/lib/puppet/parser/functions/fact.rb deleted file mode 100644 index 27b7bb2..0000000 --- a/lib/puppet/parser/functions/fact.rb +++ /dev/null @@ -1,36 +0,0 @@ -# -# fact.rb -# - -module Puppet::Parser::Functions - newfunction(:fact, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - raise(Puppet::ParseError, "fact(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 - - fact = arguments[0] - - unless fact.is_a?(String) - raise(Puppet::ParseError, 'fact(): Requires fact name to be a string') - end - - raise(Puppet::ParseError, 'fact(): You must provide ' + - 'fact name') if fact.empty? - - result = lookupvar(fact) # Get the value of interest from Facter ... - - # - # Now this is a funny one ... Puppet does not have a concept of - # returning neither undef nor nil back for use within the Puppet DSL - # and empty string is as closest to actual undef as you we can get - # at this point in time ... - # - result = result.empty? ? '' : result - - return result - end -end - -# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/grep.rb b/lib/puppet/parser/functions/grep.rb index 8549218..2caaa6f 100644 --- a/lib/puppet/parser/functions/grep.rb +++ b/lib/puppet/parser/functions/grep.rb @@ -12,6 +12,11 @@ module Puppet::Parser::Functions "given #{arguments.size} for 2") end + a = arguments[0] + pattern = Regexp.new(arguments[1]) + + a.grep(pattern) + end end diff --git a/spec/unit/parser/functions/delete_at_spec.rb b/spec/unit/parser/functions/delete_at_spec.rb index a0b5b06..27db0c8 100755 --- a/spec/unit/parser/functions/delete_at_spec.rb +++ b/spec/unit/parser/functions/delete_at_spec.rb @@ -18,4 +18,9 @@ describe "the delete_at function" do lambda { @scope.function_delete_at([]) }.should( raise_error(Puppet::ParseError)) end + it "should delete an item at specified location from an array" do + result = @scope.function_delete_at([['a','b','c'],1]) + result.should(eq(['a','c'])) + end + end diff --git a/spec/unit/parser/functions/delete_spec.rb b/spec/unit/parser/functions/delete_spec.rb index b0729ab..fab3230 100755 --- a/spec/unit/parser/functions/delete_spec.rb +++ b/spec/unit/parser/functions/delete_spec.rb @@ -18,4 +18,9 @@ describe "the delete function" do lambda { @scope.function_delete([]) }.should( raise_error(Puppet::ParseError)) end + it "should delete an item from an array" do + result = @scope.function_delete([['a','b','c'],'b']) + result.should(eq(['a','c'])) + end + end diff --git a/spec/unit/parser/functions/downcase_spec.rb b/spec/unit/parser/functions/downcase_spec.rb index 162291c..0d53220 100755 --- a/spec/unit/parser/functions/downcase_spec.rb +++ b/spec/unit/parser/functions/downcase_spec.rb @@ -18,4 +18,9 @@ describe "the downcase function" do lambda { @scope.function_downcase([]) }.should( raise_error(Puppet::ParseError)) end + it "should downcase a string" do + result = @scope.function_downcase(["ASFD"]) + result.should(eq("asfd")) + end + end diff --git a/spec/unit/parser/functions/empty_spec.rb b/spec/unit/parser/functions/empty_spec.rb index beaf45c..6c0fe69 100755 --- a/spec/unit/parser/functions/empty_spec.rb +++ b/spec/unit/parser/functions/empty_spec.rb @@ -18,4 +18,9 @@ describe "the empty function" do lambda { @scope.function_empty([]) }.should( raise_error(Puppet::ParseError)) end + it "should return a true for an empty string" do + result = @scope.function_empty(['']) + result.should(eq(true)) + end + end diff --git a/spec/unit/parser/functions/fact_spec.rb b/spec/unit/parser/functions/fact_spec.rb deleted file mode 100755 index c013ae0..0000000 --- a/spec/unit/parser/functions/fact_spec.rb +++ /dev/null @@ -1,21 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the fact function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("fact").should == "function_fact" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_fact([]) }.should( raise_error(Puppet::ParseError)) - end - -end diff --git a/spec/unit/parser/functions/flatten_spec.rb b/spec/unit/parser/functions/flatten_spec.rb index 7af23c1..91cf4ef 100755 --- a/spec/unit/parser/functions/flatten_spec.rb +++ b/spec/unit/parser/functions/flatten_spec.rb @@ -18,4 +18,9 @@ describe "the flatten function" do lambda { @scope.function_flatten([]) }.should( raise_error(Puppet::ParseError)) end + it "should flatten a complex data structure" do + result = @scope.function_flatten([["a","b",["c",["d","e"],"f","g"]]]) + result.should(eq(["a","b","c","d","e","f","g"])) + end + end diff --git a/spec/unit/parser/functions/grep_spec.rb b/spec/unit/parser/functions/grep_spec.rb index 1c949da..b1f647c 100755 --- a/spec/unit/parser/functions/grep_spec.rb +++ b/spec/unit/parser/functions/grep_spec.rb @@ -18,4 +18,9 @@ describe "the grep function" do lambda { @scope.function_grep([]) }.should( raise_error(Puppet::ParseError)) end + it "should grep contents from an array" do + result = @scope.function_grep([["aaabbb","bbbccc","dddeee"], "bbb"]) + result.should(eq(["aaabbb","bbbccc"])) + end + end diff --git a/spec/unit/parser/functions/hash_spec.rb b/spec/unit/parser/functions/hash_spec.rb index 09b0d50..6d3d48c 100644 --- a/spec/unit/parser/functions/hash_spec.rb +++ b/spec/unit/parser/functions/hash_spec.rb @@ -18,4 +18,9 @@ describe "the hash function" do lambda { @scope.function_hash([]) }.should( raise_error(Puppet::ParseError)) end + it "should convert an array to a hash" do + result = @scope.function_hash([['a',1,'b',2,'c',3]]) + result.should(eq({'a'=>1,'b'=>2,'c'=>3})) + end + end diff --git a/spec/unit/parser/functions/is_array_spec.rb b/spec/unit/parser/functions/is_array_spec.rb index b2843b0..15b563c 100644 --- a/spec/unit/parser/functions/is_array_spec.rb +++ b/spec/unit/parser/functions/is_array_spec.rb @@ -18,4 +18,14 @@ describe "the is_array function" do lambda { @scope.function_is_array([]) }.should( raise_error(Puppet::ParseError)) end + it "should return true if passed an array" do + result = @scope.function_is_array([[1,2,3]]) + result.should(eq(true)) + end + + it "should return false if passed a hash" do + result = @scope.function_is_array([{'a'=>1}]) + result.should(eq(false)) + end + end diff --git a/spec/unit/parser/functions/is_float_spec.rb b/spec/unit/parser/functions/is_float_spec.rb index e3dc8fc..3d58017 100644 --- a/spec/unit/parser/functions/is_float_spec.rb +++ b/spec/unit/parser/functions/is_float_spec.rb @@ -18,4 +18,19 @@ describe "the is_float function" do lambda { @scope.function_is_float([]) }.should( raise_error(Puppet::ParseError)) end + it "should return true if a float" do + result = @scope.function_is_float([0.12]) + result.should(eq(true)) + end + + it "should return false if a string" do + result = @scope.function_is_float(["asdf"]) + result.should(eq(false)) + end + + it "should return false if not an integer" do + result = @scope.function_is_float([3]) + result.should(eq(false)) + end + end diff --git a/spec/unit/parser/functions/is_hash_spec.rb b/spec/unit/parser/functions/is_hash_spec.rb index 66dfdeb..94364f5 100644 --- a/spec/unit/parser/functions/is_hash_spec.rb +++ b/spec/unit/parser/functions/is_hash_spec.rb @@ -18,4 +18,19 @@ describe "the is_hash function" do lambda { @scope.function_is_hash([]) }.should( raise_error(Puppet::ParseError)) end + it "should return true if passed a hash" do + result = @scope.function_is_hash([{"a"=>1,"b"=>2}]) + result.should(eq(true)) + end + + it "should return false if passed an array" do + result = @scope.function_is_hash([["a","b"]]) + result.should(eq(false)) + end + + it "should return false if passed a string" do + result = @scope.function_is_hash(["asdf"]) + result.should(eq(false)) + end + end diff --git a/spec/unit/parser/functions/is_integer_spec.rb b/spec/unit/parser/functions/is_integer_spec.rb index 131251c..596bd33 100644 --- a/spec/unit/parser/functions/is_integer_spec.rb +++ b/spec/unit/parser/functions/is_integer_spec.rb @@ -18,4 +18,19 @@ describe "the is_integer function" do lambda { @scope.function_is_integer([]) }.should( raise_error(Puppet::ParseError)) end + it "should return true if an integer" do + result = @scope.function_is_integer([3]) + result.should(eq(true)) + end + + it "should return false if a float" do + result = @scope.function_is_integer([3.2]) + result.should(eq(false)) + end + + it "should return false if a string" do + result = @scope.function_is_integer(["asdf"]) + result.should(eq(false)) + end + end -- cgit v1.2.3