Update mcollective.init according to OSCI-855
[packages/precise/mcollective.git] / spec / unit / matcher / parser_spec.rb
1 #! /usr/bin/env rspec
2
3 require 'spec_helper'
4
5 module MCollective
6   module Matcher
7     describe Parser do
8       before :each do
9         Config.instance.stubs(:color).returns(false)
10       end
11
12       describe '#parse' do
13         it "should parse statements seperated by '='" do
14           parser = Parser.new("foo=bar")
15           parser.execution_stack.should == [{"statement" => "foo=bar"}]
16         end
17
18         it "should parse statements seperated by '<'" do
19           parser = Parser.new("foo<bar")
20           parser.execution_stack.should == [{"statement" => "foo<bar"}]
21         end
22
23         it "should parse statements seperated by '>'" do
24           parser = Parser.new("foo>bar")
25           parser.execution_stack.should == [{"statement" => "foo>bar"}]
26         end
27
28         it "should parse statements seperated by '<='" do
29           parser = Parser.new("foo<=bar")
30           parser.execution_stack.should == [{"statement" => "foo<=bar"}]
31         end
32
33         it "should parse statements seperated by '>='" do
34           parser = Parser.new("foo>=bar")
35           parser.execution_stack.should == [{"statement" => "foo>=bar"}]
36         end
37
38         it "should parse class regex statements" do
39           parser = Parser.new("/foo/")
40           parser.execution_stack.should == [{"statement" => "/foo/"}]
41         end
42
43         it "should parse fact regex statements" do
44           parser = Parser.new("foo=/bar/")
45           parser.execution_stack.should == [{"statement" => "foo=/bar/"}]
46         end
47
48         it "should parse a correct 'and' token" do
49           parser = Parser.new("foo=bar and bar=foo")
50           parser.execution_stack.should == [{"statement" => "foo=bar"}, {"and" => "and"}, {"statement" => "bar=foo"}]
51         end
52
53         it "should not parse an incorrect and token" do
54           expect {
55             parser = Parser.new("and foo=bar")
56           }.to raise_error(RuntimeError, "Parse errors found while parsing -S input and foo=bar")
57         end
58
59         it "should parse a correct 'or' token" do
60           parser = Parser.new("foo=bar or bar=foo")
61           parser.execution_stack.should == [{"statement" => "foo=bar"}, {"or" => "or"}, {"statement" => "bar=foo"}]
62         end
63
64         it "should not parse an incorrect or token" do
65           expect{
66             parser = Parser.new("or foo=bar")
67           }.to raise_error(RuntimeError, "Parse errors found while parsing -S input or foo=bar")
68         end
69
70         it "should parse a correct 'not' token" do
71           parser = Parser.new("! bar=foo")
72           parser.execution_stack.should == [{"not" => "not"}, {"statement" => "bar=foo"}]
73           parser = Parser.new("not bar=foo")
74           parser.execution_stack.should == [{"not" => "not"}, {"statement" => "bar=foo"}]
75         end
76
77         it "should not parse an incorrect 'not' token" do
78           expect{
79             parser = Parser.new("foo=bar !")
80           }.to raise_error(RuntimeError, "Parse errors found while parsing -S input foo=bar !")
81         end
82
83         it "should parse correct parentheses" do
84           parser = Parser.new("(foo=bar)")
85           parser.execution_stack.should == [{"(" => "("}, {"statement" => "foo=bar"}, {")" => ")"}]
86         end
87
88         it "should fail on incorrect parentheses" do
89           expect{
90             parser = Parser.new(")foo=bar(")
91           }.to raise_error(RuntimeError, "Malformed token(s) found while parsing -S input )foo=bar(")
92         end
93
94         it "should fail on missing parentheses" do
95           expect{
96             parser = Parser.new("(foo=bar")
97           }.to raise_error(RuntimeError, "Missing parenthesis found while parsing -S input (foo=bar")
98         end
99
100         it "should parse correctly formatted compound statements" do
101           parser = Parser.new("(foo=bar or foo=rab) and (bar=foo)")
102           parser.execution_stack.should == [{"(" => "("}, {"statement"=>"foo=bar"}, {"or"=>"or"}, {"statement"=>"foo=rab"},
103                                             {")"=>")"}, {"and"=>"and"}, {"("=>"("}, {"statement"=>"bar=foo"},
104                                             {")"=>")"}]
105         end
106
107         it "should parse complex fstatements and statements with operators seperated by whitespaces" do
108           parser = Parser.new("foo('bar').value = 1 and foo=bar or foo  = bar")
109           parser.execution_stack.should == [{"fstatement"=>"foo('bar').value=1"}, {"and"=>"and"}, {"statement"=>"foo=bar"}, {"or"=>"or"}, {"statement"=>"foo=bar"}]
110         end
111
112         it "should parse statements where classes are mixed with fact comparisons and fstatements" do
113           parser = Parser.new("klass and function('param').value = 1 and fact=value")
114           parser.execution_stack.should == [{"statement" => "klass"},
115                                             {"and" => "and"},
116                                             {"fstatement" => "function('param').value=1"},
117                                             {"and" => "and"},
118                                             {"statement" => "fact=value"}]
119         end
120       end
121     end
122   end
123 end