Skip to content

Commit b2165df

Browse files
committed
Merge pull request #601 from petems/MODULES-1439-any2bool_added
(MODULES-1439) Adds any2bool function
2 parents f46c9fd + 420f76d commit b2165df

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#
2+
# any2bool.rb
3+
#
4+
5+
module Puppet::Parser::Functions
6+
newfunction(:any2bool, :type => :rvalue, :doc => <<-EOS
7+
This converts 'anything' to a boolean. In practise it does the following:
8+
9+
* Strings such as Y,y,1,T,t,TRUE,yes,'true' will return true
10+
* Strings such as 0,F,f,N,n,FALSE,no,'false' will return false
11+
* Booleans will just return their original value
12+
* Number (or a string representation of a number) > 0 will return true, otherwise false
13+
* undef will return false
14+
* Anything else will return true
15+
EOS
16+
) do |arguments|
17+
18+
raise(Puppet::ParseError, "any2bool(): Wrong number of arguments " +
19+
"given (#{arguments.size} for 1)") if arguments.size < 1
20+
21+
# If argument is already Boolean, return it
22+
if !!arguments[0] == arguments[0]
23+
return arguments[0]
24+
end
25+
26+
arg = arguments[0]
27+
28+
if arg == nil
29+
return false
30+
end
31+
32+
if arg == :undef
33+
return false
34+
end
35+
36+
valid_float = !!Float(arg) rescue false
37+
38+
if arg.is_a?(Numeric)
39+
return function_num2bool( [ arguments[0] ] )
40+
end
41+
42+
if arg.is_a?(String)
43+
if valid_float
44+
return function_num2bool( [ arguments[0] ] )
45+
else
46+
return function_str2bool( [ arguments[0] ] )
47+
end
48+
end
49+
50+
return true
51+
52+
end
53+
end
54+
55+
# vim: set ts=2 sw=2 et :

spec/functions/any2bool_spec.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
require 'spec_helper'
2+
3+
describe 'any2bool' do
4+
it { is_expected.not_to eq(nil) }
5+
it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
6+
7+
it { is_expected.to run.with_params(true).and_return(true) }
8+
it { is_expected.to run.with_params(false).and_return(false) }
9+
10+
it { is_expected.to run.with_params('1.5').and_return(true) }
11+
12+
describe 'when testing stringy values that mean "true"' do
13+
[ 'TRUE','1', 't', 'y', 'true', 'yes'].each do |value|
14+
it { is_expected.to run.with_params(value).and_return(true) }
15+
end
16+
end
17+
18+
describe 'when testing stringy values that mean "false"' do
19+
[ 'FALSE','', '0', 'f', 'n', 'false', 'no', 'undef', 'undefined', nil, :undef ].each do |value|
20+
it { is_expected.to run.with_params(value).and_return(false) }
21+
end
22+
end
23+
24+
describe 'when testing numeric values that mean "true"' do
25+
[ 1,'1',1.5, '1.5'].each do |value|
26+
it { is_expected.to run.with_params(value).and_return(true) }
27+
end
28+
end
29+
30+
describe 'when testing numeric that mean "false"' do
31+
[ -1, '-1', -1.5, '-1.5', '0', 0 ].each do |value|
32+
it { is_expected.to run.with_params(value).and_return(false) }
33+
end
34+
end
35+
36+
describe 'everything else returns true' do
37+
[ [], {}, ['1'], [1], {:one => 1} ].each do |value|
38+
it { is_expected.to run.with_params(value).and_return(true) }
39+
end
40+
end
41+
42+
end

0 commit comments

Comments
 (0)