summaryrefslogtreecommitdiffstats
path: root/spec/unit/parser
diff options
context:
space:
mode:
authorBrice Figureau <brice-puppet@daysofwonder.com>2009-07-28 19:13:54 +0200
committerJames Turnbull <james@lovedthanlost.net>2009-08-01 11:15:29 +1000
commitef68967f2b72e609a9d69e53771a61fd9f522149 (patch)
treeb9e8baa5a45d31f03fa4fae83cb0160a71957dd9 /spec/unit/parser
parent17e62b1ec806815abea909291df1e591a825c375 (diff)
downloadpuppet-ef68967f2b72e609a9d69e53771a61fd9f522149.tar.gz
puppet-ef68967f2b72e609a9d69e53771a61fd9f522149.tar.xz
puppet-ef68967f2b72e609a9d69e53771a61fd9f522149.zip
Fix #2033 - Allow regexp in if expression
This changeset introduces regexp in if expression with the use of the =~ (match) and !~ (not match) operator. Usage: if $uname =~ /Linux|Debian/ { ... } Moreover this patch creates ephemeral variables ($0 to $9) in the current scope which contains the regex captures: if $uname =~ /(Linux|Debian)/ { notice("this is a $1 system") } Signed-off-by: Brice Figureau <brice-puppet@daysofwonder.com>
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