Skip to content

Commit 5989922

Browse files
committed
Add startswith function
This is helpful in places where you need to use variables in the string which needs to be compare e.g. something like this: ```puppet $test.startswith("part${string}01") ```
1 parent ade894a commit 5989922

File tree

4 files changed

+42
-8
lines changed

4 files changed

+42
-8
lines changed

lib/puppet/functions/start_with.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# @summary
2+
# Returns true if str starts with one of the prefixes given. Each of the prefixes should be a String.
3+
#
4+
# @example
5+
# 'foobar'.start_with('foo') => true
6+
# 'foobar'.start_with('bar') => false
7+
# 'foObar'.start_with(['bar', 'baz']) => false
8+
Puppet::Functions.create_function(:start_with) do
9+
# @param test_string The string to check
10+
# @param prefixes The prefixes to check.
11+
#
12+
# @return [Boolean] True or False
13+
dispatch :start_with do
14+
param 'String[1]', :test_string
15+
param 'Variant[String[1],Array[String[1], 1]]', :prefixes
16+
return_type 'Boolean'
17+
end
18+
19+
def start_with(test_string, prefixes)
20+
test_string.start_with?(*prefixes)
21+
end
22+
end
Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
# @summary
2-
# Return true if test_string ends with suffux
2+
# Returns true if str ends with one of the prefixes given. Each of the prefixes should be a String.
33
#
44
# @example
55
# 'foobar'.stdlib::end_with('bar') => true
66
# 'foobar'.stdlib::end_with('foo') => false
7+
# 'foobar'.stdlib::end_with(['foo', 'baz']) => false
78
Puppet::Functions.create_function(:'stdlib::end_with') do
8-
# @param test_string the string to check
9-
# @param suffix the suffix to check
9+
# @param test_string The string to check
10+
# @param suffixes The suffixes to check
1011
#
1112
# @return [Boolean] True or False
1213
dispatch :end_with do
1314
param 'String[1]', :test_string
14-
param 'String[1]', :suffix
15+
param 'Variant[String[1],Array[String[1], 1]]', :suffixes
1516
return_type 'Boolean'
1617
end
1718

18-
def end_with(test_string, suffix)
19-
test_string.end_with?(suffix)
19+
def end_with(test_string, suffixes)
20+
test_string.end_with?(*suffixes)
2021
end
2122
end

spec/functions/end_with_spec.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
describe 'stdlib::end_with' do
44
it { is_expected.to run.with_params('foobar', 'bar').and_return(true) }
55
it { is_expected.to run.with_params('foobar', 'foo').and_return(false) }
6+
it { is_expected.to run.with_params('foobar', ['foo', 'baz']).and_return(false) }
67
it do
78
is_expected.to run.with_params('', 'foo').and_raise_error(
8-
ArgumentError, %r{'stdlib::end_with' parameter 'test_string' expects a String\[1\]}
9+
ArgumentError, %r{'stdlib::end_with' parameter 'test_string' expects a String\[1\] value}
910
)
1011
end
1112
it do
1213
is_expected.to run.with_params('foobar', '').and_raise_error(
13-
ArgumentError, %r{'stdlib::end_with' parameter 'suffix' expects a String\[1\]}
14+
ArgumentError, %r{'stdlib::end_with' parameter 'suffixes' expects a value of type String\[1\] or Array\[String\[1\], 1\]}
1415
)
1516
end
1617
end

spec/functions/startswith_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
require 'spec_helper'
2+
3+
describe 'start_with' do
4+
it { is_expected.not_to eq(nil) }
5+
it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{expects 2 arguments, got none}i) }
6+
it { is_expected.to run.with_params('').and_raise_error(ArgumentError, %r{expects 2 arguments, got 1}) }
7+
8+
it { is_expected.to run.with_params('foobar', 'foo').and_return(true) }
9+
it { is_expected.to run.with_params('foObar', ['bar', 'baz']).and_return(false) }
10+
end

0 commit comments

Comments
 (0)