summaryrefslogtreecommitdiffstats
path: root/spec/unit/parser
diff options
context:
space:
mode:
authorBrice Figureau <brice-puppet@daysofwonder.com>2008-09-26 22:54:42 +0200
committerBrice Figureau <brice@daysofwonder.com>2008-09-30 17:22:07 +0200
commitcfa230a2d7b0c5e57cc0379785bd2025520f1c35 (patch)
tree046bc7c55a6a1a8ed9b36c5371a3901a89a779c4 /spec/unit/parser
parent850e0baf0fbe321f14d4b9d913ce7dea39c9aa27 (diff)
Add arithmetic operators to AST
This changeset adds +,-,/,*,<< and >> computation and AST parse nodes.
Diffstat (limited to 'spec/unit/parser')
-rwxr-xr-xspec/unit/parser/ast/arithmetic_operator.rb73
-rwxr-xr-xspec/unit/parser/ast/minus.rb36
-rwxr-xr-xspec/unit/parser/scope.rb50
3 files changed, 159 insertions, 0 deletions
diff --git a/spec/unit/parser/ast/arithmetic_operator.rb b/spec/unit/parser/ast/arithmetic_operator.rb
new file mode 100755
index 000000000..24d6ad47d
--- /dev/null
+++ b/spec/unit/parser/ast/arithmetic_operator.rb
@@ -0,0 +1,73 @@
+#!/usr/bin/env ruby
+
+require File.dirname(__FILE__) + '/../../../spec_helper'
+
+describe Puppet::Parser::AST::ArithmeticOperator do
+
+ AST = Puppet::Parser::AST
+
+ before :each do
+ @scope = Puppet::Parser::Scope.new()
+ @one = stub 'lval', :safeevaluate => 1
+ @two = stub 'rval', :safeevaluate => 2
+ end
+
+ it "should evaluate both branches" do
+ lval = stub "lval"
+ lval.expects(:safeevaluate).with(@scope).returns(1)
+ rval = stub "rval"
+ rval.expects(:safeevaluate).with(@scope).returns(2)
+
+ operator = AST::ArithmeticOperator.new :rval => rval, :operator => "+", :lval => lval
+ operator.evaluate(@scope)
+ end
+
+ it "should fail for an unknown operator" do
+ lambda { operator = AST::ArithmeticOperator.new :lval => @one, :operator => "%", :rval => @two }.should raise_error
+ end
+
+ it "should call Puppet::Parser::Scope.number?" do
+ Puppet::Parser::Scope.expects(:number?).with(1).returns(1)
+ Puppet::Parser::Scope.expects(:number?).with(2).returns(2)
+
+ AST::ArithmeticOperator.new(:lval => @one, :operator => "+", :rval => @two).evaluate(@scope)
+ end
+
+
+ %w{ + - * / << >>}.each do |op|
+ it "should call ruby Numeric '#{op}'" do
+ one = stub 'one'
+ two = stub 'two'
+ operator = AST::ArithmeticOperator.new :lval => @one, :operator => op, :rval => @two
+ Puppet::Parser::Scope.stubs(:number?).with(1).returns(one)
+ Puppet::Parser::Scope.stubs(:number?).with(2).returns(two)
+ one.expects(:send).with(op,two)
+ operator.evaluate(@scope)
+ end
+ end
+
+ it "should work even with numbers embedded in strings" do
+ two = stub 'two', :safeevaluate => "2"
+ one = stub 'one', :safeevaluate => "1"
+ operator = AST::ArithmeticOperator.new :lval => two, :operator => "+", :rval => one
+ operator.evaluate(@scope).should == 3
+ end
+
+ it "should work even with floats" do
+ two = stub 'two', :safeevaluate => 2.53
+ one = stub 'one', :safeevaluate => 1.80
+ operator = AST::ArithmeticOperator.new :lval => two, :operator => "+", :rval => one
+ operator.evaluate(@scope).should == 4.33
+ end
+
+ it "should work for variables too" do
+ @scope.expects(:lookupvar).with("one").returns(1)
+ @scope.expects(:lookupvar).with("two").returns(2)
+ one = AST::Variable.new( :value => "one" )
+ two = AST::Variable.new( :value => "two" )
+
+ operator = AST::ArithmeticOperator.new :lval => one, :operator => "+", :rval => two
+ operator.evaluate(@scope).should == 3
+ end
+
+end
diff --git a/spec/unit/parser/ast/minus.rb b/spec/unit/parser/ast/minus.rb
new file mode 100755
index 000000000..83bd92d0d
--- /dev/null
+++ b/spec/unit/parser/ast/minus.rb
@@ -0,0 +1,36 @@
+#!/usr/bin/env ruby
+
+require File.dirname(__FILE__) + '/../../../spec_helper'
+
+describe Puppet::Parser::AST::Minus do
+ before :each do
+ @scope = Puppet::Parser::Scope.new()
+ end
+
+ it "should evaluate its argument" do
+ value = stub "value"
+ value.expects(:safeevaluate).with(@scope).returns(123)
+
+ operator = Puppet::Parser::AST::Minus.new :value => value
+ operator.evaluate(@scope)
+ end
+
+ it "should fail if argument is not a string or integer" do
+ array_ast = stub 'array_ast', :safeevaluate => [2]
+ operator = Puppet::Parser::AST::Minus.new :value => array_ast
+ lambda { operator.evaluate(@scope) }.should raise_error
+ end
+
+ it "should work with integer as string" do
+ string = stub 'string', :safeevaluate => "123"
+ operator = Puppet::Parser::AST::Minus.new :value => string
+ operator.evaluate(@scope).should == -123
+ end
+
+ it "should work with integers" do
+ int = stub 'int', :safeevaluate => 123
+ operator = Puppet::Parser::AST::Minus.new :value => int
+ operator.evaluate(@scope).should == -123
+ end
+
+end
diff --git a/spec/unit/parser/scope.rb b/spec/unit/parser/scope.rb
index ec8ab6d7d..fa76c4ff2 100755
--- a/spec/unit/parser/scope.rb
+++ b/spec/unit/parser/scope.rb
@@ -34,4 +34,54 @@ describe Puppet::Parser::Scope do
end
end
+
+ describe Puppet::Parser::Scope, "when calling number?" do
+
+ it "should return nil if called with anything not a number" do
+ Puppet::Parser::Scope.number?([2]).should be_nil
+ end
+
+ it "should return a Fixnum for a Fixnum" do
+ Puppet::Parser::Scope.number?(2).should be_an_instance_of(Fixnum)
+ end
+
+ it "should return a Float for a Float" do
+ Puppet::Parser::Scope.number?(2.34).should be_an_instance_of(Float)
+ end
+
+ it "should return 234 for '234'" do
+ Puppet::Parser::Scope.number?("234").should == 234
+ end
+
+ it "should return nil for 'not a number'" do
+ Puppet::Parser::Scope.number?("not a number").should be_nil
+ end
+
+ it "should return 23.4 for '23.4'" do
+ Puppet::Parser::Scope.number?("23.4").should == 23.4
+ end
+
+ it "should return 23.4e13 for '23.4e13'" do
+ Puppet::Parser::Scope.number?("23.4e13").should == 23.4e13
+ end
+
+ it "should understand negative numbers" do
+ Puppet::Parser::Scope.number?("-234").should == -234
+ end
+
+ it "should know how to convert exponential float numbers ala '23e13'" do
+ Puppet::Parser::Scope.number?("23e13").should == 23e13
+ end
+
+ it "should understand hexadecimal numbers" do
+ Puppet::Parser::Scope.number?("0x234").should == 0x234
+ end
+
+ it "should understand octal numbers" do
+ Puppet::Parser::Scope.number?("0755").should == 0755
+ end
+
+
+ end
+
end