]> gitweb.fluxo.info Git - puppet-stdlib.git/commitdiff
Add some more functional tests.
authorKen Barber <ken@bob.sh>
Wed, 29 Jun 2011 22:37:37 +0000 (23:37 +0100)
committerKen Barber <ken@bob.sh>
Wed, 29 Jun 2011 22:37:37 +0000 (23:37 +0100)
16 files changed:
lib/puppet/parser/functions/date.rb
lib/puppet/parser/functions/delete.rb
lib/puppet/parser/functions/fact.rb [deleted file]
lib/puppet/parser/functions/grep.rb
spec/unit/parser/functions/delete_at_spec.rb
spec/unit/parser/functions/delete_spec.rb
spec/unit/parser/functions/downcase_spec.rb
spec/unit/parser/functions/empty_spec.rb
spec/unit/parser/functions/fact_spec.rb [deleted file]
spec/unit/parser/functions/flatten_spec.rb
spec/unit/parser/functions/grep_spec.rb
spec/unit/parser/functions/hash_spec.rb
spec/unit/parser/functions/is_array_spec.rb
spec/unit/parser/functions/is_float_spec.rb
spec/unit/parser/functions/is_hash_spec.rb
spec/unit/parser/functions/is_integer_spec.rb

index 4d0543eee14811c8e1c8847797e76b58026fa039..bc62e60c503cee79899a0a738a4bb4e0d6a37031 100644 (file)
@@ -12,6 +12,8 @@ module Puppet::Parser::Functions
         "given #{arguments.size} for 1")
     end
 
+    # TODO: stubbed
+
   end
 end
 
index 88f3448e5759bc55bd493ac4bf35308d7819c6e6..0d208b5c903c6dd8b65af8fcc80a9c7b6ef0a441 100644 (file)
@@ -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 (file)
index 27b7bb2..0000000
+++ /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 :
index 8549218972f60288d6ac350d3461a8e5bbd804d1..2caaa6f7294a0984cca23e8ba80a5dd67e443362 100644 (file)
@@ -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
 
index a0b5b06b095810f2bd8490f31bda25b9d5592c7d..27db0c8773ebac812db02217c49ded9115f77c10 100755 (executable)
@@ -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
index b0729ab7f6ca271008cd2964fcfbef403443fc2c..fab3230e330686f3f77cb5649c982920cbc88e00 100755 (executable)
@@ -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
index 162291c694bd9c81cadb07f5dcffcd9daac68a60..0d53220d6c8ea8e9f200d0c01560ea17357e263f 100755 (executable)
@@ -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
index beaf45c7419925550f414e5e1360939a992c8d18..6c0fe695594d5ed91f7f0610a2de6de03de9608d 100755 (executable)
@@ -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 (executable)
index c013ae0..0000000
+++ /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
index 7af23c1c26b7668c71acb78648ec55fca64e74f2..91cf4ef7ae5808f8a8250443376d22d66419ea23 100755 (executable)
@@ -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
index 1c949da4d95fe90fb484d65058e1106fef127c43..b1f647cd3fed60fd91211ff3c6db075f54267bfc 100755 (executable)
@@ -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
index 09b0d50a11e27dde2a54b616e30bd59fd54ab07b..6d3d48c7293cdd8461cd2830ebd1629594fb7f96 100644 (file)
@@ -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
index b2843b061c59e657ca7fd769b2f4c408810fa0a6..15b563c9e1a27cdee2273a32d6a332ac8cfa894c 100644 (file)
@@ -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
index e3dc8fccd4894abf58e24f84cd09720af806bc56..3d5801755a522fd5140c9be9eb49c6a37c2bf0ae 100644 (file)
@@ -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
index 66dfdebbb71d44287bd7e59c2e35f0a5b19841ab..94364f5e4bcd35e3d80ccac56eda9c3a3c415e32 100644 (file)
@@ -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
index 131251c4d3a83b78024737ee1ac14c8c88a31067..596bd33082ced834113240ade711530005fa6445 100644 (file)
@@ -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