diff options
author | Brice Figureau <brice-puppet@daysofwonder.com> | 2008-11-26 23:18:45 +0100 |
---|---|---|
committer | Brice Figureau <brice-puppet@daysofwonder.com> | 2008-11-29 12:00:16 +0100 |
commit | d8c741f9d3b07b11f11af0765d740d9e78889794 (patch) | |
tree | 90147b4efdda84b8dbb92975cfc8881c2b9ee025 | |
parent | 3c4efa7ec2043043d72d325e67fe5bd6098e0413 (diff) | |
download | puppet-d8c741f9d3b07b11f11af0765d740d9e78889794.tar.gz puppet-d8c741f9d3b07b11f11af0765d740d9e78889794.tar.xz puppet-d8c741f9d3b07b11f11af0765d740d9e78889794.zip |
Fix #1741 - Puppet::Parser::Functions rmfunctions and unit test
-rw-r--r-- | lib/puppet/parser/functions.rb | 14 | ||||
-rw-r--r-- | spec/unit/parser/functions.rb | 83 |
2 files changed, 97 insertions, 0 deletions
diff --git a/lib/puppet/parser/functions.rb b/lib/puppet/parser/functions.rb index 5fb0439da..b1cd0d083 100644 --- a/lib/puppet/parser/functions.rb +++ b/lib/puppet/parser/functions.rb @@ -54,6 +54,20 @@ module Functions end end + # Remove a function added by newfunction + def self.rmfunction(name) + name = symbolize(name) + + unless @functions.include? name + raise Puppet::DevError, "Function %s is not defined" % name + end + + @functions.delete(name) + + fname = "function_" + name.to_s + Puppet::Parser::Scope.send(:remove_method, fname) + end + # Determine if a given name is a function def self.function(name) name = symbolize(name) diff --git a/spec/unit/parser/functions.rb b/spec/unit/parser/functions.rb new file mode 100644 index 000000000..8d0492d13 --- /dev/null +++ b/spec/unit/parser/functions.rb @@ -0,0 +1,83 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../../spec_helper' + +describe Puppet::Parser::Functions do + + before(:each) do + end + + after(:each) do + # Rationale: + # our various tests will almost all register to Pupet::Parser::Functions + # a new function called "name". All tests are required to stub Puppet::Parser::Scope + # so that +no+ new real ruby method are defined. + # After each test, we want to leave the whole Puppet::Parser::Functions environment + # as it was before we were called, hence we call rmfunction (which might not succeed + # if the function hasn't been registered in the test). It is also important in this + # section to stub +remove_method+ here so that we don't pollute the scope. + Puppet::Parser::Scope.stubs(:remove_method) + begin + Puppet::Parser::Functions.rmfunction("name") + rescue + end + end + + describe "when calling newfunction" do + it "should create the function in the scope class" do + Puppet::Parser::Scope.expects(:define_method).with("function_name", nil) + + Puppet::Parser::Functions.newfunction("name", :type => :rvalue) + end + + it "should raise an error if the function already exists" do + Puppet::Parser::Scope.expects(:define_method).with("function_name", nil).once + Puppet::Parser::Functions.newfunction("name", :type => :rvalue) + + lambda { Puppet::Parser::Functions.newfunction("name", :type => :rvalue) }.should raise_error + end + + it "should raise an error if the function type is not correct" do + Puppet::Parser::Scope.expects(:define_method).with("function_name", nil).never + + lambda { Puppet::Parser::Functions.newfunction("name", :type => :unknown) }.should raise_error + end + end + + describe "when calling rmfunction" do + it "should remove the function in the scope class" do + Puppet::Parser::Scope.stubs(:define_method).with("function_name", nil) + Puppet::Parser::Functions.newfunction("name", :type => :rvalue) + + Puppet::Parser::Scope.expects(:remove_method).with("function_name").once + + Puppet::Parser::Functions.rmfunction("name") + end + + it "should raise an error if the function doesn't exists" do + lambda { Puppet::Parser::Functions.rmfunction("name") }.should raise_error + end + end + + describe "when calling function to test function existance" do + + it "should return false if the function doesn't exist" do + Puppet::Parser::Functions.autoloader.stubs(:load) + + Puppet::Parser::Functions.function("name").should be_false + end + + it "should return it's name if the function exists" do + Puppet::Parser::Scope.stubs(:define_method).with("function_name", nil) + Puppet::Parser::Functions.newfunction("name", :type => :rvalue) + + Puppet::Parser::Functions.function("name").should == "function_name" + end + + it "should try to autoload the function if it doesn't exist yet" do + Puppet::Parser::Functions.autoloader.expects(:load) + + Puppet::Parser::Functions.function("name") + end + end +end |