Skip to content

Commit

Permalink
accept any case of boolean strings
Browse files Browse the repository at this point in the history
  * previously the str2bool function did not accept 'TRUE' as a bool
    type.  This causes the function to now accept TRUE, FALSE strings
    as a boolean type in order to be converted to a proper boolean.
  * This would also cause Y,N, YES, NO to be accepted as boolean types
    as well.
  • Loading branch information
logicminds committed Sep 4, 2015
1 parent 6a1afae commit f2f2db4
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ Returns a new string where runs of the same character that occur in this set are

#### `str2bool`

Converts a string to a boolean. This attempts to convert strings that contain values such as '1', 't', 'y', and 'yes' to 'true' and strings that contain values such as '0', 'f', 'n', and 'no' to 'false'. *Type*: rvalue.
Converts a string to a boolean regardless of case. This attempts to convert strings that contain values such as '1', 't', 'y', 'Y', 'YES','yes', and 'TRUE' to 'true' and strings that contain values such as '0', 'f','F', 'N','n', 'NO','FALSE', and 'no' to 'false'. *Type*: rvalue.

#### `str2saltedsha512`

Expand Down
8 changes: 4 additions & 4 deletions lib/puppet/parser/functions/str2bool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
module Puppet::Parser::Functions
newfunction(:str2bool, :type => :rvalue, :doc => <<-EOS
This converts a string to a boolean. This attempt to convert strings that
contain things like: y, 1, t, true to 'true' and strings that contain things
like: 0, f, n, false, no to 'false'.
contain things like: Y,y, 1, T,t, TRUE,true to 'true' and strings that contain things
like: 0, F,f, N,n, false, FALSE, no to 'false'.
EOS
) do |arguments|

Expand All @@ -32,8 +32,8 @@ module Puppet::Parser::Functions
# We yield false in this case.
#
when /^$/, '' then false # Empty string will be false ...
when /^(1|t|y|true|yes)$/ then true
when /^(0|f|n|false|no)$/ then false
when /^(1|t|y|true|yes)$/i then true
when /^(0|f|n|false|no)$/i then false
when /^(undef|undefined)$/ then false # This is not likely to happen ...
else
raise(Puppet::ParseError, 'str2bool(): Unknown type of boolean given')
Expand Down
4 changes: 2 additions & 2 deletions spec/functions/str2bool_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError, /Unknown type of boolean given/) }

describe 'when testing values that mean "true"' do
[ '1', 't', 'y', 'true', 'yes', true ].each do |value|
[ 'TRUE','1', 't', 'y', 'true', 'yes', true ].each do |value|
it { is_expected.to run.with_params(value).and_return(true) }
end
end

describe 'when testing values that mean "false"' do
[ '', '0', 'f', 'n', 'false', 'no', false, 'undef', 'undefined' ].each do |value|
[ 'FALSE','', '0', 'f', 'n', 'false', 'no', false, 'undef', 'undefined' ].each do |value|
it { is_expected.to run.with_params(value).and_return(false) }
end
end
Expand Down

0 comments on commit f2f2db4

Please sign in to comment.