Skip to content

Commit d34b227

Browse files
committed
Accept UnaryMinusExpression as class parameter type
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
1 parent 57a00cc commit d34b227

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

lib/puppet/pops/evaluator/literal_evaluator.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ def literal_AccessExpression(o)
7676
o.keys.map { |v| literal(v) }
7777
end
7878

79+
def literal_UnaryMinusExpression(o)
80+
-literal(o.expr)
81+
end
82+
7983
def literal_ConcatenatedString(o)
8084
# use double quoted string value if there is no interpolation
8185
throw :not_literal unless o.segments.size == 1 && o.segments[0].is_a?(Model::LiteralString)

spec/unit/pops/evaluator/literal_evaluator_spec.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
'a' => 'a',
1818
'a::b' => 'a::b',
1919
'Integer[1]' => [1],
20+
'Integer[-1]' => [-1],
2021
'File' => "file",
2122

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

40-
['1+1', '[1,2, 1+2]', '{a=>1+1}', '"x$y"', '"x${y}z"', 'Integer[1-3]', 'Optional[[String]]'].each do |source|
41+
[ '',
42+
'1+1',
43+
'[1,2, 1+2]',
44+
'{a=>1+1}',
45+
'"x$y"',
46+
'"x${y}z"',
47+
'Integer[1-3]',
48+
'Integer[-1-3]',
49+
'Optional[[String]]'
50+
].each do |source|
4151
it "throws :not_literal for non literal expression '#{source}'" do
4252
expect{leval.literal(parser.parse_string(source))}.to throw_symbol(:not_literal)
4353
end

0 commit comments

Comments
 (0)