-
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.
Merge pull request #949 from cocker-cc/feature/master/add_function_ex…
…tname add Function extname()
- Loading branch information
Showing
3 changed files
with
57 additions
and
0 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
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,26 @@ | ||
# Returns the Extension (the Portion of Filename in Path starting from the | ||
# last Period). | ||
# | ||
# If Path is a Dotfile, or starts with a Period, then the starting Dot is not | ||
# dealt with the Start of the Extension. | ||
# | ||
# An empty String will also be returned, when the Period is the last Character | ||
# in Path. | ||
|
||
Puppet::Functions.create_function(:'stdlib::extname') do | ||
# @param filename The Filename | ||
# @return [String] The Extension starting from the last Period | ||
# @example Determining the Extension of a Filename | ||
# stdlib::extname('test.rb') => '.rb' | ||
# stdlib::extname('a/b/d/test.rb') => '.rb' | ||
# stdlib::extname('test') => '' | ||
# stdlib::extname('.profile') => '' | ||
dispatch :extname do | ||
param 'String', :filename | ||
return_type 'String' | ||
end | ||
|
||
def extname(filename) | ||
File.extname(filename) | ||
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
require 'spec_helper' | ||
|
||
describe 'stdlib::extname' do | ||
it { is_expected.not_to eq(nil) } | ||
it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{'stdlib::extname' expects 1 argument, got none}) } | ||
it { is_expected.to run.with_params('one', 'two').and_raise_error(ArgumentError, %r{'stdlib::extname' expects 1 argument, got 2}) } | ||
it { is_expected.to run.with_params([]).and_raise_error(ArgumentError, %r{'stdlib::extname' parameter 'filename' expects a String value, got Array}) } | ||
it { is_expected.to run.with_params('test.rb').and_return('.rb') } | ||
it { is_expected.to run.with_params('a/b/d/test.rb').and_return('.rb') } | ||
it { is_expected.to run.with_params('test').and_return('') } | ||
it { is_expected.to run.with_params('.profile').and_return('') } | ||
|
||
context 'with UTF8 and double byte characters' do | ||
it { is_expected.to run.with_params('file_√ạĺűē/竹.rb').and_return('.rb') } | ||
end | ||
end |