File tree Expand file tree Collapse file tree
lib/puppet/parser/functions
spec/unit/puppet/parser/functions Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -489,6 +489,12 @@ Returns true if the variable passed to this function is an array.
489489
490490- * Type* : rvalue
491491
492+ is_bool
493+ --------
494+ Returns true if the variable passed to this function is a boolean.
495+
496+ - * Type* : rvalue
497+
492498is_domain_name
493499--------------
494500Returns true if the string passed to this function is a syntactically correct domain name.
Original file line number Diff line number Diff line change 1+ #
2+ # is_bool.rb
3+ #
4+
5+ module Puppet ::Parser ::Functions
6+ newfunction ( :is_bool , :type => :rvalue , :doc => <<-EOS
7+ Returns true if the variable passed to this function is a boolean.
8+ EOS
9+ ) do |arguments |
10+
11+ raise ( Puppet ::ParseError , "is_bool(): Wrong number of arguments " +
12+ "given (#{ arguments . size } for 1)" ) if arguments . size != 1
13+
14+ type = arguments [ 0 ]
15+
16+ result = type . is_a? ( TrueClass ) || type . is_a? ( FalseClass )
17+
18+ return result
19+ end
20+ end
21+
22+ # vim: set ts=2 sw=2 et :
Original file line number Diff line number Diff line change @@ -24,7 +24,7 @@ module Puppet::Parser::Functions
2424 end
2525
2626 args . each do |arg |
27- unless ( arg . is_a? ( TrueClass ) || arg . is_a? ( FalseClass ) )
27+ unless function_is_bool ( [ arg ] )
2828 raise Puppet ::ParseError , ( "#{ arg . inspect } is not a boolean. It looks to be a #{ arg . class } " )
2929 end
3030 end
Original file line number Diff line number Diff line change 1+ #! /usr/bin/env ruby -S rspec
2+ require 'spec_helper'
3+
4+ describe "the is_bool function" do
5+ let ( :scope ) { PuppetlabsSpec ::PuppetInternals . scope }
6+
7+ it "should exist" do
8+ Puppet ::Parser ::Functions . function ( "is_bool" ) . should == "function_is_bool"
9+ end
10+
11+ it "should raise a ParseError if there is less than 1 arguments" do
12+ lambda { scope . function_is_bool ( [ ] ) } . should ( raise_error ( Puppet ::ParseError ) )
13+ end
14+
15+ it "should return true if passed a TrueClass" do
16+ result = scope . function_is_bool ( [ true ] )
17+ result . should ( eq ( true ) )
18+ end
19+
20+ it "should return true if passed a FalseClass" do
21+ result = scope . function_is_bool ( [ false ] )
22+ result . should ( eq ( true ) )
23+ end
24+
25+ it "should return false if passed the string 'true'" do
26+ result = scope . function_is_bool ( [ 'true' ] )
27+ result . should ( eq ( false ) )
28+ end
29+
30+ it "should return false if passed the string 'false'" do
31+ result = scope . function_is_bool ( [ 'false' ] )
32+ result . should ( eq ( false ) )
33+ end
34+
35+ it "should return false if passed an array" do
36+ result = scope . function_is_bool ( [ [ "a" , "b" ] ] )
37+ result . should ( eq ( false ) )
38+ end
39+
40+ it "should return false if passed a hash" do
41+ result = scope . function_is_bool ( [ { "a" => "b" } ] )
42+ result . should ( eq ( false ) )
43+ end
44+ end
You can’t perform that action at this time.
0 commit comments