summaryrefslogtreecommitdiffstats
path: root/spec/unit
diff options
context:
space:
mode:
authorBrice Figureau <brice-puppet@daysofwonder.com>2008-09-26 23:03:39 +0200
committerJames Turnbull <james@lovedthanlost.net>2008-10-01 01:35:03 +1000
commit4cf9710bd27fdb5f0720f4d8478ef940e7c4ba59 (patch)
treee2db8d91c8e1d45bb9413899ff6566a712264b0a /spec/unit
parentcfa230a2d7b0c5e57cc0379785bd2025520f1c35 (diff)
downloadpuppet-4cf9710bd27fdb5f0720f4d8478ef940e7c4ba59.tar.gz
puppet-4cf9710bd27fdb5f0720f4d8478ef940e7c4ba59.tar.xz
puppet-4cf9710bd27fdb5f0720f4d8478ef940e7c4ba59.zip
Add parser for arbitrary expressions
The expressions can be used in if 'test' and in the right side of assignements. The expressions can contain any number of sub-expressions combined by either arithmetic operators, comparison operators, or boolean operators. Random Usage Examples: $result = ((( $two + 2) / $one) + 4 * 5.45) - (6 << 7) + (0x800 + -9) or if ($a < 10) and ($a + 10 != 200) { ... }
Diffstat (limited to 'spec/unit')
-rwxr-xr-xspec/unit/parser/lexer.rb36
-rwxr-xr-xspec/unit/parser/parser.rb48
2 files changed, 80 insertions, 4 deletions
diff --git a/spec/unit/parser/lexer.rb b/spec/unit/parser/lexer.rb
index fed1ade7d..c35a81a97 100755
--- a/spec/unit/parser/lexer.rb
+++ b/spec/unit/parser/lexer.rb
@@ -136,7 +136,13 @@ describe Puppet::Parser::Lexer::TOKENS do
:BACKSLASH => '\\',
:FARROW => '=>',
:PARROW => '+>',
- :APPENDS => '+='
+ :APPENDS => '+=',
+ :PLUS => '+',
+ :MINUS => '-',
+ :DIV => '/',
+ :TIMES => '*',
+ :LSHIFT => '<<',
+ :RSHIFT => '>>',
}.each do |name, string|
it "should have a token named #{name.to_s}" do
Puppet::Parser::Lexer::TOKENS[name].should_not be_nil
@@ -213,10 +219,34 @@ describe Puppet::Parser::Lexer::TOKENS[:NAME] do
end
describe Puppet::Parser::Lexer::TOKENS[:NUMBER] do
- before { @token = Puppet::Parser::Lexer::TOKENS[:NUMBER] }
+ before do
+ @token = Puppet::Parser::Lexer::TOKENS[:NUMBER]
+# @regex = Regexp.new('^'+@token.regex.source+'$')
+ @regex = @token.regex
+ end
it "should match against numeric terms" do
- @token.regex.should =~ "2982383139"
+ @regex.should =~ "2982383139"
+ end
+
+ it "should match against float terms" do
+ @regex.should =~ "29823.235"
+ end
+
+ it "should match against hexadecimal terms" do
+ @regex.should =~ "0xBEEF0023"
+ end
+
+ it "should match against float with exponent terms" do
+ @regex.should =~ "10e23"
+ end
+
+ it "should match against float terms with negative exponents" do
+ @regex.should =~ "10e-23"
+ end
+
+ it "should match against float terms with fractional parts and exponent" do
+ @regex.should =~ "1.234e23"
end
it "should return the NAME token and the value" do
diff --git a/spec/unit/parser/parser.rb b/spec/unit/parser/parser.rb
index 94b19be40..17e80bb6a 100755
--- a/spec/unit/parser/parser.rb
+++ b/spec/unit/parser/parser.rb
@@ -8,6 +8,7 @@ describe Puppet::Parser do
before :each do
@parser = Puppet::Parser::Parser.new :environment => "development"
+ @true_ast = AST::Boolean.new :value => true
end
describe "when parsing append operator" do
@@ -31,4 +32,49 @@ describe Puppet::Parser do
end
end
-end
+
+ describe Puppet::Parser, "when parsing 'if'" do
+ it "not, it should create the correct ast objects" do
+ AST::Not.expects(:new).with { |h| h[:value].is_a?(AST::Boolean) }
+ @parser.parse("if ! true { $var = 1 }")
+
+ end
+
+ it "boolean operation, it should create the correct ast objects" do
+ AST::BooleanOperator.expects(:new).with {
+ |h| h[:rval].is_a?(AST::Boolean) and h[:lval].is_a?(AST::Boolean) and h[:operator]=="or"
+ }
+ @parser.parse("if true or true { $var = 1 }")
+
+ end
+
+ it "comparison operation, it should create the correct ast objects" do
+ AST::ComparisonOperator.expects(:new).with {
+ |h| h[:lval].is_a?(AST::Name) and h[:rval].is_a?(AST::Name) and h[:operator]=="<"
+ }
+ @parser.parse("if 1 < 2 { $var = 1 }")
+
+ end
+
+ end
+
+ describe Puppet::Parser, "when parsing if complex expressions" do
+ it "should create a correct ast tree" do
+ AST::ComparisonOperator.expects(:new).with {
+ |h| h[:rval].is_a?(AST::Name) and h[:lval].is_a?(AST::Name) and h[:operator]==">"
+ }.returns("whatever")
+ AST::ComparisonOperator.expects(:new).with {
+ |h| h[:rval].is_a?(AST::Name) and h[:lval].is_a?(AST::Name) and h[:operator]=="=="
+ }.returns("whatever")
+ AST::BooleanOperator.expects(:new).with {
+ |h| h[:rval]=="whatever" and h[:lval]=="whatever" and h[:operator]=="and"
+ }
+ @parser.parse("if (1 > 2) and (1 == 2) { $var = 1 }")
+ end
+
+ it "should raise an error on incorrect expression" do
+ lambda { @parser.parse("if (1 > 2 > ) or (1 == 2) { $var = 1 }") }.should raise_error
+ end
+
+ end
+ end