summaryrefslogtreecommitdiffstats
path: root/spec/unit/parser
diff options
context:
space:
mode:
Diffstat (limited to 'spec/unit/parser')
-rwxr-xr-xspec/unit/parser/ast/ifstatement.rb75
-rwxr-xr-xspec/unit/parser/ast/match_operator.rb50
2 files changed, 125 insertions, 0 deletions
diff --git a/spec/unit/parser/ast/ifstatement.rb b/spec/unit/parser/ast/ifstatement.rb
new file mode 100755
index 000000000..10d877a4b
--- /dev/null
+++ b/spec/unit/parser/ast/ifstatement.rb
@@ -0,0 +1,75 @@
+#!/usr/bin/env ruby
+
+require File.dirname(__FILE__) + '/../../../spec_helper'
+
+describe Puppet::Parser::AST::IfStatement do
+ before :each do
+ @scope = Puppet::Parser::Scope.new()
+ end
+
+ describe "when evaluating" do
+
+ before :each do
+ @test = stub 'test'
+ @test.stubs(:safeevaluate).with(@scope)
+
+ @stmt = stub 'stmt'
+ @stmt.stubs(:safeevaluate).with(@scope)
+
+ @else = stub 'else'
+ @else.stubs(:safeevaluate).with(@scope)
+
+ @ifstmt = Puppet::Parser::AST::IfStatement.new :test => @test, :statements => @stmt
+ @ifelsestmt = Puppet::Parser::AST::IfStatement.new :test => @test, :statements => @stmt, :else => @else
+ end
+
+ it "should evaluate test" do
+ Puppet::Parser::Scope.stubs(:true?).returns(false)
+
+ @test.expects(:safeevaluate).with(@scope)
+
+ @ifstmt.evaluate(@scope)
+ end
+
+ it "should evaluate if statements if test is true" do
+ Puppet::Parser::Scope.stubs(:true?).returns(true)
+
+ @stmt.expects(:safeevaluate).with(@scope)
+
+ @ifstmt.evaluate(@scope)
+ end
+
+ it "should not evaluate if statements if test is false" do
+ Puppet::Parser::Scope.stubs(:true?).returns(false)
+
+ @stmt.expects(:safeevaluate).with(@scope).never
+
+ @ifstmt.evaluate(@scope)
+ end
+
+ it "should evaluate the else branch if test is false" do
+ Puppet::Parser::Scope.stubs(:true?).returns(false)
+
+ @else.expects(:safeevaluate).with(@scope)
+
+ @ifelsestmt.evaluate(@scope)
+ end
+
+ it "should not evaluate the else branch if test is true" do
+ Puppet::Parser::Scope.stubs(:true?).returns(true)
+
+ @else.expects(:safeevaluate).with(@scope).never
+
+ @ifelsestmt.evaluate(@scope)
+ end
+
+ it "should reset ephemeral statements after evaluation" do
+ Puppet::Parser::Scope.stubs(:true?).returns(true)
+
+ @stmt.expects(:safeevaluate).with(@scope)
+ @scope.expects(:unset_ephemeral_var)
+
+ @ifstmt.evaluate(@scope)
+ end
+ end
+end
diff --git a/spec/unit/parser/ast/match_operator.rb b/spec/unit/parser/ast/match_operator.rb
new file mode 100755
index 000000000..985cf60d5
--- /dev/null
+++ b/spec/unit/parser/ast/match_operator.rb
@@ -0,0 +1,50 @@
+#!/usr/bin/env ruby
+
+require File.dirname(__FILE__) + '/../../../spec_helper'
+
+describe Puppet::Parser::AST::MatchOperator do
+ before :each do
+ @scope = Puppet::Parser::Scope.new()
+
+ @lval = stub 'lval'
+ @lval.stubs(:safeevaluate).with(@scope).returns("this is a string")
+
+ @rval = stub 'rval'
+ @rval.stubs(:evaluate_match)
+
+ @operator = Puppet::Parser::AST::MatchOperator.new :lval => @lval, :rval => @rval, :operator => "=~"
+ end
+
+ it "should evaluate the left operand" do
+ @lval.expects(:safeevaluate).with(@scope)
+
+ @operator.evaluate(@scope)
+ end
+
+ it "should fail for an unknown operator" do
+ lambda { operator = Puppet::Parser::AST::MatchOperator.new :lval => @lval, :operator => "unknown", :rval => @rval }.should raise_error
+ end
+
+ it "should evaluate_match the left operand" do
+ @rval.expects(:evaluate_match).with("this is a string", @scope).returns(:match)
+
+ @operator.evaluate(@scope)
+ end
+
+ { "=~" => true, "!~" => false }.each do |op, res|
+ it "should return #{res} if the regexp matches with #{op}" do
+ match = stub 'match'
+ @rval.stubs(:evaluate_match).with("this is a string", @scope).returns(match)
+
+ operator = Puppet::Parser::AST::MatchOperator.new :lval => @lval, :rval => @rval, :operator => op
+ operator.evaluate(@scope).should == res
+ end
+
+ it "should return #{!res} if the regexp doesn't match" do
+ @rval.stubs(:evaluate_match).with("this is a string", @scope).returns(nil)
+
+ operator = Puppet::Parser::AST::MatchOperator.new :lval => @lval, :rval => @rval, :operator => op
+ operator.evaluate(@scope).should == !res
+ end
+ end
+end