-
Notifications
You must be signed in to change notification settings - Fork 582
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
4 changed files
with
42 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |