Skip to content

Commit e18f042

Browse files
authored
Merge pull request #624 from MiamiOH/master
Added the regexpescape function.
2 parents 468b082 + 4cc5604 commit e18f042

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#
2+
# regexpescape.rb
3+
#
4+
module Puppet::Parser::Functions
5+
newfunction(:regexpescape, :type => :rvalue, :doc => <<-EOS
6+
Regexp escape a string or array of strings.
7+
Requires either a single string or an array as an input.
8+
EOS
9+
) do |arguments| # rubocop:disable Style/ClosingParenthesisIndentation
10+
raise(Puppet::ParseError, 'regexpescape(): Wrong number of arguments ' \
11+
"given (#{arguments.size} for 1)") if arguments.empty?
12+
13+
value = arguments[0]
14+
15+
unless value.is_a?(Array) || value.is_a?(String)
16+
raise(Puppet::ParseError, 'regexpescape(): Requires either ' \
17+
'array or string to work with')
18+
end
19+
20+
result = if value.is_a?(Array)
21+
# Numbers in Puppet are often string-encoded which is troublesome ...
22+
value.collect { |i| i.is_a?(String) ? Regexp.escape(i) : i }
23+
else
24+
Regexp.escape(value)
25+
end
26+
27+
return result
28+
end
29+
end
30+
31+
# vim: set ts=2 sw=2 et :

spec/functions/regexpescape_spec.rb

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
require 'spec_helper'
2+
3+
describe 'regexpescape' do
4+
describe 'signature validation' do
5+
it { is_expected.not_to eq(nil) }
6+
it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
7+
it {
8+
pending("Current implementation ignores parameters after the first.")
9+
is_expected.to run.with_params('', '').and_raise_error(Puppet::ParseError, /wrong number of arguments/i)
10+
}
11+
it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, /Requires either array or string to work/) }
12+
it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, /Requires either array or string to work/) }
13+
it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, /Requires either array or string to work/) }
14+
end
15+
16+
describe 'handling normal strings' do
17+
it 'should call ruby\'s Regexp.escape function' do
18+
Regexp.expects(:escape).with('regexp_string').returns('escaped_regexp_string').once
19+
is_expected.to run.with_params('regexp_string').and_return('escaped_regexp_string')
20+
end
21+
end
22+
23+
describe 'handling classes derived from String' do
24+
it 'should call ruby\'s Regexp.escape function' do
25+
regexp_string = AlsoString.new('regexp_string')
26+
Regexp.expects(:escape).with(regexp_string).returns('escaped_regexp_string').once
27+
is_expected.to run.with_params(regexp_string).and_return("escaped_regexp_string")
28+
end
29+
end
30+
31+
describe 'strings in arrays handling' do
32+
it { is_expected.to run.with_params([]).and_return([]) }
33+
it { is_expected.to run.with_params(['one*', "two"]).and_return(['one\*', "two"]) }
34+
it { is_expected.to run.with_params(['one*', 1, true, {}, "two"]).and_return(['one\*', 1, true, {}, "two"]) }
35+
end
36+
end

0 commit comments

Comments
 (0)