Signed-off-by: Brice Figureau <[EMAIL PROTECTED]>
---
lib/puppet/parser/ast.rb | 1 +
...{boolean_operator.rb => comparison_operator.rb} | 16 +++--
spec/unit/parser/ast/comparison_operator.rb | 66 ++++++++++++++++++++
3 files changed, 77 insertions(+), 6 deletions(-)
copy lib/puppet/parser/ast/{boolean_operator.rb => comparison_operator.rb}
(64%)
create mode 100755 spec/unit/parser/ast/comparison_operator.rb
diff --git a/lib/puppet/parser/ast.rb b/lib/puppet/parser/ast.rb
index 6674452..da82a30 100644
--- a/lib/puppet/parser/ast.rb
+++ b/lib/puppet/parser/ast.rb
@@ -81,6 +81,7 @@ require 'puppet/parser/ast/caseopt'
require 'puppet/parser/ast/casestatement'
require 'puppet/parser/ast/collection'
require 'puppet/parser/ast/collexpr'
+require 'puppet/parser/ast/comparison_operator'
require 'puppet/parser/ast/definition'
require 'puppet/parser/ast/else'
require 'puppet/parser/ast/function'
diff --git a/lib/puppet/parser/ast/boolean_operator.rb
b/lib/puppet/parser/ast/comparison_operator.rb
similarity index 64%
copy from lib/puppet/parser/ast/boolean_operator.rb
copy to lib/puppet/parser/ast/comparison_operator.rb
index b9b3529..078da99 100644
--- a/lib/puppet/parser/ast/boolean_operator.rb
+++ b/lib/puppet/parser/ast/comparison_operator.rb
@@ -2,7 +2,7 @@ require 'puppet'
require 'puppet/parser/ast/branch'
class Puppet::Parser::AST
- class BooleanOperator < AST::Branch
+ class ComparisonOperator < AST::Branch
attr_accessor :operator, :lval, :rval
@@ -17,19 +17,23 @@ class Puppet::Parser::AST
# 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)
+ when "==": lval == rval
+ when "!=": lval != rval
+ when "<": lval < rval
+ when ">": lval > rval
+ when "<=": lval <= rval
+ when ">=": lval >= rval
end
end
def initialize(hash)
super
- unless %w{and or}.include?(@operator)
- raise ArgumentError, "Invalid boolean operator %s" % @operator
+ unless %w{== != < > <= >=}.include?(@operator)
+ raise ArgumentError, "Invalid comparison operator %s" %
@operator
end
end
end
diff --git a/spec/unit/parser/ast/comparison_operator.rb
b/spec/unit/parser/ast/comparison_operator.rb
new file mode 100755
index 0000000..3654f89
--- /dev/null
+++ b/spec/unit/parser/ast/comparison_operator.rb
@@ -0,0 +1,66 @@
+#!/usr/bin/env ruby
+
+require File.dirname(__FILE__) + '/../../../spec_helper'
+
+describe Puppet::Parser::AST::ComparisonOperator do
+ before :each do
+ @scope = Puppet::Parser::Scope.new()
+ @one = Puppet::Parser::AST::FlatString.new( :value => 1 )
+ @two = Puppet::Parser::AST::FlatString.new( :value => 2 )
+ 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::ComparisonOperator.new :lval => lval,
:operator => "==", :rval => rval
+ operator.evaluate(@scope)
+ end
+
+ it "should fail for an unknown operator" do
+ lambda { operator = Puppet::Parser::AST::ComparisonOperator.new :lval
=> @one, :operator => "or", :rval => @two }.should raise_error
+ end
+
+ it "should return true for 1 < 2" do
+ operator = Puppet::Parser::AST::ComparisonOperator.new :lval => @one,
:operator => "<", :rval => @two
+ operator.evaluate(@scope).should == true
+ end
+
+ it "should return false for 1 > 2" do
+ operator = Puppet::Parser::AST::ComparisonOperator.new :lval => @one,
:operator => ">", :rval => @two
+ operator.evaluate(@scope).should == false
+ end
+
+ it "should return true for 1 <= 2" do
+ operator = Puppet::Parser::AST::ComparisonOperator.new :lval => @one,
:operator => "<=", :rval => @two
+ operator.evaluate(@scope).should == true
+ end
+
+ it "should return true for 2 <= 2" do
+ operator = Puppet::Parser::AST::ComparisonOperator.new :lval => @two,
:operator => "<=", :rval => @two
+ operator.evaluate(@scope).should == true
+ end
+
+ it "should return true for 2 == 2" do
+ operator = Puppet::Parser::AST::ComparisonOperator.new :lval => @two,
:operator => "==", :rval => @two
+ operator.evaluate(@scope).should == true
+ end
+
+ it "should return false for 2 != 2" do
+ operator = Puppet::Parser::AST::ComparisonOperator.new :lval => @two,
:operator => "!=", :rval => @two
+ operator.evaluate(@scope).should == false
+ end
+
+ it "should work for variables too" do
+ @scope.expects(:lookupvar).with("one").returns(1)
+ @scope.expects(:lookupvar).with("two").returns(2)
+ one = Puppet::Parser::AST::Variable.new( :value => "one" )
+ two = Puppet::Parser::AST::Variable.new( :value => "two" )
+
+ operator = Puppet::Parser::AST::ComparisonOperator.new :lval => one,
:operator => "<", :rval => two
+ operator.evaluate(@scope).should == true
+ 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
-~----------~----~----~----~------~----~------~--~---