Skip to content

Commit

Permalink
Merge pull request #3849 from thallgren/issue/pup-4438/add_required_r…
Browse files Browse the repository at this point in the history
…epeated_param

(PUP-4438) Add required_repeated_param to 4.x function API
  • Loading branch information
hlindberg committed Apr 20, 2015
2 parents 039d4ed + c7419c1 commit c55b69a
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 2 deletions.
17 changes: 17 additions & 0 deletions lib/puppet/functions.rb
Expand Up @@ -331,6 +331,23 @@ def repeated_param(type, name)
internal_param(type, name)
@max = :default
end
alias optional_repeated_param repeated_param

# Defines a repeated positional parameter with _type_ and _name_ that may occur 1 to "infinite" number of times.
# It may only appear last or just before a block parameter.
#
# @param type [String] The type specification for the parameter.
# @param name [Symbol] The name of the parameter. This is primarily used
# for error message output and does not have to match an implementation
# method parameter.
# @return [Void]
#
# @api public
def required_repeated_param(type, name)
internal_param(type, name)
@min += 1
@max = :default
end

# Defines one required block parameter that may appear last. If type and name is missing the
# default type is "Callable", and the name is "block". If only one
Expand Down
64 changes: 62 additions & 2 deletions spec/unit/functions4_spec.rb
Expand Up @@ -177,7 +177,7 @@ def min(x,y)
end

it 'an error includes optional indicators and count for last element when defined via dispatch' do
f = create_function_with_optionals_and_varargs_via_dispatch()
f = create_function_with_optionals_and_repeated_via_dispatch()
# TODO: Bogus parameters, not yet used
func = f.new(:closure_scope, :loader)
expect(func.is_a?(Puppet::Functions::Function)).to be_true
Expand All @@ -191,6 +191,33 @@ def min(x,y)
min(Integer) - arg count {1}")
end

it 'can create optional repeated parameter' do
f = create_function_with_repeated
func = f.new(:closure_scope, :loader)
expect(func.call({})).to eql(0)
expect(func.call({}, 1)).to eql(1)
expect(func.call({}, 1, 2)).to eql(2)

f = create_function_with_optional_repeated
func = f.new(:closure_scope, :loader)
expect(func.call({})).to eql(0)
expect(func.call({}, 1)).to eql(1)
expect(func.call({}, 1, 2)).to eql(2)
end

it 'can create required repeated parameter' do
f = create_function_with_required_repeated
func = f.new(:closure_scope, :loader)
expect(func.call({}, 1)).to eql(1)
expect(func.call({}, 1, 2)).to eql(2)
expect { func.call({}) }.to raise_error(ArgumentError,
"function 'count_args' called with mis-matched arguments
expected:
count_args(Any c{1,}) - arg count {1,}
actual:
count_args(Undef{0}) - arg count {0}")
end

it 'a function can use inexact argument mapping' do
f = create_function_with_inexact_dispatch
func = f.new(:closure_scope, :loader)
Expand Down Expand Up @@ -563,7 +590,7 @@ def min(x,y,a=1, b=1, *c)
end
end

def create_function_with_optionals_and_varargs_via_dispatch
def create_function_with_optionals_and_repeated_via_dispatch
f = Puppet::Functions.create_function('min') do
dispatch :min do
param 'Numeric', :x
Expand All @@ -578,6 +605,39 @@ def min(x,y,a=1, b=1, *c)
end
end

def create_function_with_repeated
f = Puppet::Functions.create_function('count_args') do
dispatch :count_args do
repeated_param 'Any', :c
end
def count_args(*c)
c.size
end
end
end

def create_function_with_optional_repeated
f = Puppet::Functions.create_function('count_args') do
dispatch :count_args do
optional_repeated_param 'Any', :c
end
def count_args(*c)
c.size
end
end
end

def create_function_with_required_repeated
f = Puppet::Functions.create_function('count_args') do
dispatch :count_args do
required_repeated_param 'Any', :c
end
def count_args(*c)
c.size
end
end
end

def create_function_with_inexact_dispatch
f = Puppet::Functions.create_function('t1') do
dispatch :t1 do
Expand Down

0 comments on commit c55b69a

Please sign in to comment.