Skip to content

Commit 5c0b3ea

Browse files
authored
Merge pull request puppetlabs#5229 from thallgren/issue/pup-6647/zero-parameters-dispatch
(PUP-6647) Fix problem with dispatch that declares zero parameters
2 parents b1ac4d5 + 21615ce commit 5c0b3ea

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

lib/puppet/functions.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ def create_callable(types, block_type, from, to)
521521
@type_parser.parse(t, loader)
522522
end
523523

524-
if from != to
524+
if from == 0 || from != to
525525
# :optional and/or :repeated parameters are present.
526526
mapped_types << from
527527
mapped_types << to

spec/unit/functions4_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,13 @@ def test(x)
113113
end.to raise_error(ArgumentError, "'min' expects 2 arguments, got 3")
114114
end
115115

116+
it 'correct dispatch is chosen when zero parameter dispatch exists' do
117+
f = create_function_with_no_parameter_dispatch
118+
func = f.new(:closure_scope, :loader)
119+
expect(func.is_a?(Puppet::Functions::Function)).to be_truthy
120+
expect(func.call({}, 1)).to eql(1)
121+
end
122+
116123
it 'an error is raised if simple function-name and method are not matched' do
117124
expect do
118125
f = create_badly_named_method_function_class()
@@ -917,6 +924,22 @@ def test(x)
917924
end
918925
end
919926

927+
def create_function_with_no_parameter_dispatch
928+
f = Puppet::Functions.create_function('test') do
929+
dispatch :test_no_args do
930+
end
931+
dispatch :test_one_arg do
932+
param 'Integer', :x
933+
end
934+
def test_no_args
935+
0
936+
end
937+
def test_one_arg(x)
938+
x
939+
end
940+
end
941+
end
942+
920943
def type_alias_t(name, type_string)
921944
type_expr = Puppet::Pops::Parser::EvaluatingParser.new.parse_string(type_string).current
922945
Puppet::Pops::Types::TypeFactory.type_alias(name, type_expr)

0 commit comments

Comments
 (0)