Skip to content

Commit 44f098d

Browse files
committed
Merge branch 'ticket/master/7659-puppet-docstring'
* ticket/master/7659-puppet-docstring: (#7659) add more tests for possible hash use syntaxes (#7659) add a test for puppetdoc in the presence of hash literals (#7659) Fix commentstack when parsing hashes Closes GH-2056
2 parents ef2f741 + 90e103f commit 44f098d

File tree

3 files changed

+62
-14
lines changed

3 files changed

+62
-14
lines changed

lib/puppet/parser/grammar.ra

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,19 +738,22 @@ regex: REGEX {
738738
}
739739

740740
hash: LBRACE hashpairs RBRACE {
741+
@lexer.commentpop
741742
if val[1].instance_of?(AST::ASTHash)
742743
result = val[1]
743744
else
744745
result = ast AST::ASTHash, { :value => val[1] }
745746
end
746747
}
747748
| LBRACE hashpairs COMMA RBRACE {
749+
@lexer.commentpop
748750
if val[1].instance_of?(AST::ASTHash)
749751
result = val[1]
750752
else
751753
result = ast AST::ASTHash, { :value => val[1] }
752754
end
753755
} | LBRACE RBRACE {
756+
@lexer.commentpop
754757
result = ast AST::ASTHash
755758
}
756759

lib/puppet/parser/parser.rb

Lines changed: 15 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/integration/parser/parser_spec.rb

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,20 +80,62 @@ def parse_with(&block)
8080
# @parser = Puppet::Parser::Parser.new "development"
8181
end
8282
shared_examples_for 'a puppet parser' do
83-
describe "when parsing comments before statement" do
83+
describe "when parsing comments before a statement" do
8484
it "should associate the documentation to the statement AST node" do
8585
if Puppet[:parser] == 'future'
8686
pending "egrammar does not yet process comments"
8787
end
8888
ast = @parser.parse("""
8989
# comment
90-
class test {}
90+
class test {
91+
$foo = {bar => 23}
92+
$bar = [23, 42]
93+
$x = 'argument'
94+
# this comment should not be returned
95+
some_function('with', {a => 'hash'},
96+
['and', 1, 'array', $argument],
97+
) # not?
98+
}
9199
""")
92100

93101
ast.code[0].should be_a(Puppet::Parser::AST::Hostclass)
94102
ast.code[0].name.should == 'test'
95103
ast.code[0].instantiate('')[0].doc.should == "comment\n"
96104
end
105+
106+
{ "an empty hash" => "{}",
107+
"a simple hash" => "{ 'key' => 'value' }",
108+
"a nested hash" => "{ 'first' => $x, 'second' => { a => 1, b => 2 } }"
109+
}.each_pair do |hash_desc, hash_expr|
110+
context "in the presence of #{hash_desc}" do
111+
{ "a parameter default" => "class test($param = #{hash_expr}) { }",
112+
"a parameter value" => "foo { 'bar': options => #{hash_expr} }",
113+
"an plusignment rvalue" => "Foo['bar'] { options +> #{hash_expr} }",
114+
"an assignment rvalue" => "$x = #{hash_expr}",
115+
"an inequality rvalue" => "if $x != #{hash_expr} { }",
116+
"an function argument in parenthesis" => "flatten(#{hash_expr})",
117+
"a second argument" => "merge($x, #{hash_expr})",
118+
}.each_pair do |dsl_desc, dsl_expr|
119+
context "as #{dsl_desc}" do
120+
it "should associate the docstring to the container" do
121+
ast = @parser.parse("# comment\nclass container { #{dsl_expr} }\n")
122+
ast.code[0].instantiate('')[0].doc.should == "comment\n"
123+
end
124+
end
125+
end
126+
# Pending, these syntaxes are not yet supported in 3.x
127+
#
128+
# @todo Merge these into the test above after the migration to the new
129+
# parser is complete.
130+
{ "a selector alternative" => "$opt ? { { 'a' => 1 } => true, default => false }",
131+
"an argument without parenthesis" => "flatten { 'a' => 1 }",
132+
}.each_pair do |dsl_desc, dsl_expr|
133+
context "as #{dsl_desc}" do
134+
it "should associate the docstring to the container"
135+
end
136+
end
137+
end
138+
end
97139
end
98140

99141
describe "when parsing" do

0 commit comments

Comments
 (0)