Hi,
Here is a set of patch implementing complex if test expressions.
I tried to add unit test that make sense to all the 'touched'
classes.
The code is available in my masterzen github account in the
branch: feature/ifexpr
Please review and comment.
Thanks
Signed-off-by: Brice Figureau <[EMAIL PROTECTED]>
---
lib/puppet/parser/ast.rb | 1 +
lib/puppet/parser/ast/boolean_operator.rb | 36 +++++++++++++++++++++++++++++
spec/unit/parser/ast/boolean_operator.rb | 32 +++++++++++++++++++++++++
3 files changed, 69 insertions(+), 0 deletions(-)
create mode 100644 lib/puppet/parser/ast/boolean_operator.rb
create mode 100755 spec/unit/parser/ast/boolean_operator.rb
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..b9b3529
--- /dev/null
+++ b/lib/puppet/parser/ast/boolean_operator.rb
@@ -0,0 +1,36 @@
+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
+ [EMAIL PROTECTED],@rval,@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 operands, should return a boolean value
+ lval = @lval.safeevaluate(scope)
+ rval = @rval.safeevaluate(scope)
+
+ # return result
+ case @operator
+ when "and": Puppet::Parser::Scope.true?(rval) and
Puppet::Parser::Scope.true?(lval)
+ when "or": Puppet::Parser::Scope.true?(rval) or
Puppet::Parser::Scope.true?(lval)
+ 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..3811513
--- /dev/null
+++ b/spec/unit/parser/ast/boolean_operator.rb
@@ -0,0 +1,32 @@
+#!/usr/bin/env ruby
+
+require File.dirname(__FILE__) + '/../../../spec_helper'
+
+describe Puppet::Parser::AST::BooleanOperator do
+ before :each do
+ @scope = Puppet::Parser::Scope.new()
+ @true_ast = Puppet::Parser::AST::Boolean.new( :value => true)
+ @false_ast = Puppet::Parser::AST::Boolean.new( :value => false)
+ end
+
+ it "should evaluate both branches" do
+ lval = stub "lval"
+ lval.expects(:safeevaluate).with(@scope)
+ rval = stub "rval"
+ rval.expects(:safeevaluate).with(@scope)
+
+ operator = Puppet::Parser::AST::BooleanOperator.new :rval => rval,
:operator => "or", :lval => lval
+ operator.evaluate(@scope)
+ end
+
+ it "should return true for true OR false" do
+ operator = Puppet::Parser::AST::BooleanOperator.new :rval =>
@true_ast, :operator => "or", :lval => @false_ast
+ operator.evaluate(@scope).should == true
+ end
+
+ it "should return false for true AND false" do
+ operator = Puppet::Parser::AST::BooleanOperator.new(:rval =>
@true_ast, :operator => "and", :lval => @false_ast )
+ operator.evaluate(@scope).should == false
+ end
+
+end
--
1.5.6.5
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Puppet Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/puppet-dev?hl=en
-~----------~----~----~----~------~----~------~--~---