diff options
| author | Brice Figureau <brice-puppet@daysofwonder.com> | 2008-09-26 22:59:27 +0200 |
|---|---|---|
| committer | Brice Figureau <brice@daysofwonder.com> | 2008-09-30 17:08:52 +0200 |
| commit | 9cdecfecce84b9e0a88b5ea86b3136a1025ac9d9 (patch) | |
| tree | 1131c7e2c37bf02f0f6c5087ad3d71eb52f6dd2f /spec/unit/parser | |
| parent | 8372dc4ca80d95e62c407708a48e51ac09ad2f55 (diff) | |
| download | puppet-9cdecfecce84b9e0a88b5ea86b3136a1025ac9d9.tar.gz puppet-9cdecfecce84b9e0a88b5ea86b3136a1025ac9d9.tar.xz puppet-9cdecfecce84b9e0a88b5ea86b3136a1025ac9d9.zip | |
Add comparison operators (< > == != <= >=) to AST
Diffstat (limited to 'spec/unit/parser')
| -rwxr-xr-x | spec/unit/parser/ast/comparison_operator.rb | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/spec/unit/parser/ast/comparison_operator.rb b/spec/unit/parser/ast/comparison_operator.rb new file mode 100755 index 000000000..dbea349f2 --- /dev/null +++ b/spec/unit/parser/ast/comparison_operator.rb @@ -0,0 +1,52 @@ +#!/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 + + %w{< > <= >= ==}.each do |oper| + it "should return the result of using '#{oper}' to compare the left and right sides" do + one = stub 'one', :safeevaluate => "1" + two = stub 'two', :safeevaluate => "2" + operator = Puppet::Parser::AST::ComparisonOperator.new :lval => one, :operator => oper, :rval => two + operator.evaluate(@scope).should == 1.send(oper,2) + end + end + + it "should return the result of using '!=' to compare the left and right sides" do + one = stub 'one', :safeevaluate => "1" + two = stub 'two', :safeevaluate => "2" + operator = Puppet::Parser::AST::ComparisonOperator.new :lval => one, :operator => '!=', :rval => two + operator.evaluate(@scope).should == true + 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 |
