[Pkg-puppet-devel] [SCM] Puppet packaging for Debian branch, master, updated. debian/0.24.6-1-356-g5718585

Brice Figureau brice at daysofwonder.com
Fri Jan 23 14:21:03 UTC 2009


The following commit has been merged in the master branch:
commit 8372dc4ca80d95e62c407708a48e51ac09ad2f55
Author: Brice Figureau <brice-puppet at daysofwonder.com>
Date:   Fri Sep 26 22:56:17 2008 +0200

    Add boolean operators to AST

diff --git a/lib/puppet/parser/ast.rb b/lib/puppet/parser/ast.rb
index c9bd7c9..6674452 100644
--- a/lib/puppet/parser/ast.rb
+++ b/lib/puppet/parser/ast.rb
@@ -76,6 +76,7 @@ end
 # And include all of the AST subclasses.
 require 'puppet/parser/ast/astarray'
 require 'puppet/parser/ast/branch'
+require 'puppet/parser/ast/boolean_operator'
 require 'puppet/parser/ast/caseopt'
 require 'puppet/parser/ast/casestatement'
 require 'puppet/parser/ast/collection'
diff --git a/lib/puppet/parser/ast/boolean_operator.rb b/lib/puppet/parser/ast/boolean_operator.rb
new file mode 100644
index 0000000..c3b5c7d
--- /dev/null
+++ b/lib/puppet/parser/ast/boolean_operator.rb
@@ -0,0 +1,48 @@
+require 'puppet'
+require 'puppet/parser/ast/branch'
+
+class Puppet::Parser::AST
+    class BooleanOperator < AST::Branch
+
+        attr_accessor :operator, :lval, :rval
+
+        # Iterate across all of our children.
+        def each
+            [@lval, at rval, at operator].each { |child| yield child }
+        end
+
+        # Returns a boolean which is the result of the boolean operation
+        # of lval and rval operands
+        def evaluate(scope)
+            # evaluate the first operand, should return a boolean value
+            lval = @lval.safeevaluate(scope)
+            
+            # return result
+            # lazy evaluate right operand
+            case @operator
+            when "and";
+                if Puppet::Parser::Scope.true?(lval)
+                    rval = @rval.safeevaluate(scope)
+                    Puppet::Parser::Scope.true?(rval)
+                else # false and false == false
+                    false
+                end
+            when "or"; 
+                if Puppet::Parser::Scope.true?(lval) 
+                    true
+                else
+                    rval = @rval.safeevaluate(scope)
+                    Puppet::Parser::Scope.true?(rval)
+                end
+            end
+        end
+
+        def initialize(hash)
+            super
+
+            unless %w{and or}.include?(@operator)
+                raise ArgumentError, "Invalid boolean operator %s" % @operator
+            end
+        end
+    end
+end
diff --git a/spec/unit/parser/ast/boolean_operator.rb b/spec/unit/parser/ast/boolean_operator.rb
new file mode 100755
index 0000000..7304e2a
--- /dev/null
+++ b/spec/unit/parser/ast/boolean_operator.rb
@@ -0,0 +1,53 @@
+#!/usr/bin/env ruby
+
+require File.dirname(__FILE__) + '/../../../spec_helper'
+
+describe Puppet::Parser::AST::BooleanOperator do
+
+		AST = Puppet::Parser::AST
+
+    before :each do
+        @scope = Puppet::Parser::Scope.new()
+        @true_ast = AST::Boolean.new( :value => true)
+        @false_ast = AST::Boolean.new( :value => false)
+    end
+
+    it "should evaluate left operand inconditionally" do
+        lval = stub "lval"
+        lval.expects(:safeevaluate).with(@scope).returns("true")
+        rval = stub "rval", :safeevaluate => false
+				rval.expects(:safeevaluate).never
+        
+        operator = AST::BooleanOperator.new :rval => rval, :operator => "or", :lval => lval
+        operator.evaluate(@scope)
+    end
+
+    it "should evaluate right 'and' operand only if left operand is true" do
+        lval = stub "lval", :safeevaluate => true
+        rval = stub "rval", :safeevaluate => false
+        rval.expects(:safeevaluate).with(@scope).returns(false)
+        operator = AST::BooleanOperator.new :rval => rval, :operator => "and", :lval => lval
+        operator.evaluate(@scope)
+    end
+
+    it "should evaluate right 'or' operand only if left operand is false" do
+        lval = stub "lval", :safeevaluate => false
+        rval = stub "rval", :safeevaluate => false
+        rval.expects(:safeevaluate).with(@scope).returns(false)
+        operator = AST::BooleanOperator.new :rval => rval, :operator => "or", :lval => lval
+        operator.evaluate(@scope)
+    end
+
+    it "should return true for false OR true" do
+        AST::BooleanOperator.new(:rval => @true_ast, :operator => "or", :lval => @false_ast).evaluate(@scope).should be_true
+    end
+
+    it "should return false for true AND false" do
+        AST::BooleanOperator.new(:rval => @true_ast, :operator => "and", :lval => @false_ast ).evaluate(@scope).should be_false
+    end
+
+    it "should return true for true AND true" do
+        AST::BooleanOperator.new(:rval => @true_ast, :operator => "and", :lval => @true_ast ).evaluate(@scope).should be_true
+    end
+
+end

-- 
Puppet packaging for Debian



More information about the Pkg-puppet-devel mailing list