diff options
| author | Brice Figureau <brice-puppet@daysofwonder.com> | 2010-06-06 17:22:03 +0200 |
|---|---|---|
| committer | test branch <puppet-dev@googlegroups.com> | 2010-02-17 06:50:53 -0800 |
| commit | 9592dd896c6aab9a3e9b0181c943e7c9ba3d2d6d (patch) | |
| tree | b2823dc0ed736e3c4cb34f4a0dc5756c7a4061cd /spec/unit/parser | |
| parent | 3696d951f70f5b94b49619dfbc57138d5241bbb8 (diff) | |
| download | puppet-9592dd896c6aab9a3e9b0181c943e7c9ba3d2d6d.tar.gz puppet-9592dd896c6aab9a3e9b0181c943e7c9ba3d2d6d.tar.xz puppet-9592dd896c6aab9a3e9b0181c943e7c9ba3d2d6d.zip | |
Fix #3871 - Add the 'in' operator
This operator allows to find if the left operand is in the right one.
The left operand must be resort to a string, but the right operand can be:
* a string
* an array
* a hash (the search is done on the keys)
This syntax can be used in any place where an expression is supported.
Syntax:
$eatme = 'eat'
if $eatme in ['ate', 'eat'] {
...
}
$value = 'beat generation'
if 'eat' in $value {
notice("on the road")
}
Signed-off-by: Brice Figureau <brice-puppet@daysofwonder.com>
Diffstat (limited to 'spec/unit/parser')
| -rw-r--r-- | spec/unit/parser/ast/in_operator.rb | 60 | ||||
| -rwxr-xr-x | spec/unit/parser/lexer.rb | 3 |
2 files changed, 62 insertions, 1 deletions
diff --git a/spec/unit/parser/ast/in_operator.rb b/spec/unit/parser/ast/in_operator.rb new file mode 100644 index 000000000..df73645a2 --- /dev/null +++ b/spec/unit/parser/ast/in_operator.rb @@ -0,0 +1,60 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../../../spec_helper' + +require 'puppet/parser/ast/in_operator' + +describe Puppet::Parser::AST::InOperator do + before :each do + @scope = Puppet::Parser::Scope.new() + + @lval = stub 'lval' + @lval.stubs(:safeevaluate).with(@scope).returns("left") + + @rval = stub 'rval' + @rval.stubs(:safeevaluate).with(@scope).returns("right") + + @operator = Puppet::Parser::AST::InOperator.new :lval => @lval, :rval => @rval + end + + it "should evaluate the left operand" do + @lval.expects(:safeevaluate).with(@scope).returns("string") + + @operator.evaluate(@scope) + end + + it "should evaluate the right operand" do + @rval.expects(:safeevaluate).with(@scope).returns("string") + + @operator.evaluate(@scope) + end + + it "should raise an argument error if lval is not a string" do + @lval.expects(:safeevaluate).with(@scope).returns([12,13]) + + lambda { @operator.evaluate(@scope) }.should raise_error + end + + it "should raise an argument error if rval doesn't support the include? method" do + @rval.expects(:safeevaluate).with(@scope).returns(stub 'value') + + lambda { @operator.evaluate(@scope) }.should raise_error + end + + it "should not raise an argument error if rval supports the include? method" do + @rval.expects(:safeevaluate).with(@scope).returns(stub 'value', :include? => true) + + lambda { @operator.evaluate(@scope) }.should_not raise_error + end + + it "should return rval.include?(lval)" do + lval = stub 'lvalue', :is_a? => true + @lval.stubs(:safeevaluate).with(@scope).returns(lval) + + rval = stub 'rvalue' + @rval.stubs(:safeevaluate).with(@scope).returns(rval) + rval.expects(:include?).with(lval).returns(:result) + + @operator.evaluate(@scope).should == :result + end +end diff --git a/spec/unit/parser/lexer.rb b/spec/unit/parser/lexer.rb index 480a416e0..c437034a4 100755 --- a/spec/unit/parser/lexer.rb +++ b/spec/unit/parser/lexer.rb @@ -203,7 +203,8 @@ describe Puppet::Parser::Lexer::TOKENS do "or" => :OR, "undef" => :UNDEF, "false" => :FALSE, - "true" => :TRUE + "true" => :TRUE, + "in" => :IN, }.each do |string, name| it "should have a keyword named #{name.to_s}" do Puppet::Parser::Lexer::KEYWORDS[name].should_not be_nil |
