Skip to content

Commit

Permalink
Add start_with function
Browse files Browse the repository at this point in the history
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.start_with("part${string}01")
```
  • Loading branch information
baurmatt committed Feb 12, 2020
1 parent ade894a commit 7091a9d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 8 deletions.
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

0 comments on commit 7091a9d

Please sign in to comment.