Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add start_with function #1086

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions lib/puppet/functions/start_with.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# @summary
# Returns true if str starts with one of the prefixes given. Each of the prefixes should be a String.
#
# @example
# 'foobar'.start_with('foo') => true
# 'foobar'.start_with('bar') => false
# 'foObar'.start_with(['bar', 'baz']) => false
Puppet::Functions.create_function(:start_with) do
# @param test_string The string to check
# @param prefixes The prefixes to check.
#
# @return [Boolean] True or False
dispatch :start_with do
param 'String[1]', :test_string
param 'Variant[String[1],Array[String[1], 1]]', :prefixes
return_type 'Boolean'
end

def start_with(test_string, prefixes)
test_string.start_with?(*prefixes)
end
end
13 changes: 7 additions & 6 deletions lib/puppet/functions/stdlib/end_with.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
# @summary
# Return true if test_string ends with suffux
# Returns true if str ends with one of the prefixes given. Each of the prefixes should be a String.
#
# @example
# 'foobar'.stdlib::end_with('bar') => true
# 'foobar'.stdlib::end_with('foo') => false
# 'foobar'.stdlib::end_with(['foo', 'baz']) => false
Puppet::Functions.create_function(:'stdlib::end_with') do
# @param test_string the string to check
# @param suffix the suffix to check
# @param test_string The string to check
# @param suffixes The suffixes to check
#
# @return [Boolean] True or False
dispatch :end_with do
param 'String[1]', :test_string
param 'String[1]', :suffix
param 'Variant[String[1],Array[String[1], 1]]', :suffixes
return_type 'Boolean'
end

def end_with(test_string, suffix)
test_string.end_with?(suffix)
def end_with(test_string, suffixes)
test_string.end_with?(*suffixes)
end
end
5 changes: 3 additions & 2 deletions spec/functions/end_with_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
describe 'stdlib::end_with' do
it { is_expected.to run.with_params('foobar', 'bar').and_return(true) }
it { is_expected.to run.with_params('foobar', 'foo').and_return(false) }
it { is_expected.to run.with_params('foobar', ['foo', 'baz']).and_return(false) }
it do
is_expected.to run.with_params('', 'foo').and_raise_error(
ArgumentError, %r{'stdlib::end_with' parameter 'test_string' expects a String\[1\]}
ArgumentError, %r{'stdlib::end_with' parameter 'test_string' expects a String\[1\] value}
)
end
it do
is_expected.to run.with_params('foobar', '').and_raise_error(
ArgumentError, %r{'stdlib::end_with' parameter 'suffix' expects a String\[1\]}
ArgumentError, %r{'stdlib::end_with' parameter 'suffixes' expects a value of type String\[1\] or Array\[String\[1\], 1\]}
)
end
end
10 changes: 10 additions & 0 deletions spec/functions/startswith_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require 'spec_helper'

describe 'start_with' do
it { is_expected.not_to eq(nil) }
it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{expects 2 arguments, got none}i) }
it { is_expected.to run.with_params('').and_raise_error(ArgumentError, %r{expects 2 arguments, got 1}) }

it { is_expected.to run.with_params('foobar', 'foo').and_return(true) }
it { is_expected.to run.with_params('foObar', ['bar', 'baz']).and_return(false) }
end