Skip to content

Commit

Permalink
Accept UnaryMinusExpression as class parameter type
Browse files Browse the repository at this point in the history
Previously, class parameters of the form `Integer[-1] $param` would fail
compilation, because the value `-1` was lexed as a UnaryMinusExpression
containing a LiteralInteger. And since the LiteralEvaluator didn't implement the
`literal_UnaryMinusExpression` method, the visitor called `literal_XXX` for each
ancestor class, until reaching `literal_Object`, which always raises.

This adds the `literal_UnaryMinusExpression` method and returns -1 times
the expression it wraps. This is similar to how the TypeParser interprets
UnaryMinusExpressions[1].

[1] https://github.com/puppetlabs/puppet/blob/8.5.0/lib/puppet/pops/types/type_parser.rb#L161
  • Loading branch information
joshcooper committed Feb 29, 2024
1 parent dd1bb20 commit 2e53599
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/puppet/pops/evaluator/literal_evaluator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ def literal_AccessExpression(o)
o.keys.map { |v| literal(v) }
end

def literal_UnaryMinusExpression(o)
-literal(o.expr)
end

def literal_ConcatenatedString(o)
# use double quoted string value if there is no interpolation
throw :not_literal unless o.segments.size == 1 && o.segments[0].is_a?(Model::LiteralString)
Expand Down
12 changes: 11 additions & 1 deletion spec/unit/pops/evaluator/literal_evaluator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
'a' => 'a',
'a::b' => 'a::b',
'Integer[1]' => [1],
'Integer[-1]' => [-1],
'File' => "file",

# special values
Expand All @@ -37,7 +38,16 @@
expect(leval.literal(parser.parse_string('undef'))).to be_nil
end

['1+1', '[1,2, 1+2]', '{a=>1+1}', '"x$y"', '"x${y}z"', 'Integer[1-3]', 'Optional[[String]]'].each do |source|
[ '',
'1+1',
'[1,2, 1+2]',
'{a=>1+1}',
'"x$y"',
'"x${y}z"',
'Integer[1-3]',
'Integer[-1-3]',
'Optional[[String]]'
].each do |source|
it "throws :not_literal for non literal expression '#{source}'" do
expect{leval.literal(parser.parse_string(source))}.to throw_symbol(:not_literal)
end
Expand Down

0 comments on commit 2e53599

Please sign in to comment.