Skip to content

Commit ade894a

Browse files
author
carabasdaniel
authored
Merge pull request #1084 from b4ldr/master
stdlib::end_with: create String.end_with function
2 parents 59e3ef8 + 44d32d2 commit ade894a

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# @summary
2+
# Return true if test_string ends with suffux
3+
#
4+
# @example
5+
# 'foobar'.stdlib::end_with('bar') => true
6+
# 'foobar'.stdlib::end_with('foo') => false
7+
Puppet::Functions.create_function(:'stdlib::end_with') do
8+
# @param test_string the string to check
9+
# @param suffix the suffix to check
10+
#
11+
# @return [Boolean] True or False
12+
dispatch :end_with do
13+
param 'String[1]', :test_string
14+
param 'String[1]', :suffix
15+
return_type 'Boolean'
16+
end
17+
18+
def end_with(test_string, suffix)
19+
test_string.end_with?(suffix)
20+
end
21+
end

spec/functions/end_with_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
require 'spec_helper'
2+
3+
describe 'stdlib::end_with' do
4+
it { is_expected.to run.with_params('foobar', 'bar').and_return(true) }
5+
it { is_expected.to run.with_params('foobar', 'foo').and_return(false) }
6+
it do
7+
is_expected.to run.with_params('', 'foo').and_raise_error(
8+
ArgumentError, %r{'stdlib::end_with' parameter 'test_string' expects a String\[1\]}
9+
)
10+
end
11+
it do
12+
is_expected.to run.with_params('foobar', '').and_raise_error(
13+
ArgumentError, %r{'stdlib::end_with' parameter 'suffix' expects a String\[1\]}
14+
)
15+
end
16+
end

0 commit comments

Comments
 (0)