blob: f68aff6709c2e8d337112e240c3a393e70b3f4b2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
#!/usr/bin/env rspec
require 'spec_helper'
describe Puppet::Parser::Parser do
module ParseMatcher
class ParseAs
def initialize(klass)
@parser = Puppet::Parser::Parser.new "development"
@class = klass
end
def result_instance
@result.code[0]
end
def matches?(string)
@string = string
@result = @parser.parse(string)
result_instance.instance_of?(@class)
end
def description
"parse as a #{@class}"
end
def failure_message
" expected #{@string} to parse as #{@class} but was #{result_instance.class}"
end
def negative_failure_message
" expected #{@string} not to parse as #{@class}"
end
end
def parse_as(klass)
ParseAs.new(klass)
end
class ParseWith
def initialize(block)
@parser = Puppet::Parser::Parser.new "development"
@block = block
end
def result_instance
@result.code[0]
end
def matches?(string)
@string = string
@result = @parser.parse(string)
@block.call(result_instance)
end
def description
"parse with the block evaluating to true"
end
def failure_message
" expected #{@string} to parse with a true result in the block"
end
def negative_failure_message
" expected #{@string} not to parse with a true result in the block"
end
end
def parse_with(&block)
ParseWith.new(block)
end
end
include ParseMatcher
before :each do
@resource_type_collection = Puppet::Resource::TypeCollection.new("env")
@parser = Puppet::Parser::Parser.new "development"
end
describe "when parsing comments before statement" do
it "should associate the documentation to the statement AST node" do
ast = @parser.parse("""
# comment
class test {}
""")
ast.code[0].should be_a(Puppet::Parser::AST::Hostclass)
ast.code[0].name.should == 'test'
ast.code[0].instantiate('')[0].doc.should == "comment\n"
end
end
describe "when parsing" do
it "should be able to parse normal left to right relationships" do
"Notify[foo] -> Notify[bar]".should parse_as(Puppet::Parser::AST::Relationship)
end
it "should be able to parse right to left relationships" do
"Notify[foo] <- Notify[bar]".should parse_as(Puppet::Parser::AST::Relationship)
end
it "should be able to parse normal left to right subscriptions" do
"Notify[foo] ~> Notify[bar]".should parse_as(Puppet::Parser::AST::Relationship)
end
it "should be able to parse right to left subscriptions" do
"Notify[foo] <~ Notify[bar]".should parse_as(Puppet::Parser::AST::Relationship)
end
it "should correctly set the arrow type of a relationship" do
"Notify[foo] <~ Notify[bar]".should parse_with { |rel| rel.arrow == "<~" }
end
it "should be able to parse deep hash access" do
%q{
$hash = { 'a' => { 'b' => { 'c' => 'it works' } } }
$out = $hash['a']['b']['c']
}.should parse_with { |v| v.value.is_a?(Puppet::Parser::AST::ASTHash) }
end
it "should fail if asked to parse '$foo::::bar'" do
expect { @parser.parse("$foo::::bar") }.should raise_error(Puppet::ParseError, /Syntax error at ':'/)
end
describe "function calls" do
it "should be able to pass an array to a function" do
"my_function([1,2,3])".should parse_with { |fun|
fun.is_a?(Puppet::Parser::AST::Function) &&
fun.arguments[0].evaluate(stub 'scope') == ['1','2','3']
}
end
it "should be able to pass a hash to a function" do
"my_function({foo => bar})".should parse_with { |fun|
fun.is_a?(Puppet::Parser::AST::Function) &&
fun.arguments[0].evaluate(stub 'scope') == {'foo' => 'bar'}
}
end
end
describe "collections" do
it "should find resources according to an expression" do
%q{
File <| mode == 0700 + 0050 + 0050 |>
}.should parse_with { |coll|
coll.is_a?(Puppet::Parser::AST::Collection) &&
coll.query.evaluate(stub 'scope').first == "param_values.value = '528' and param_names.name = 'mode'"
}
end
end
end
end
|