Skip to content

Commit 0ea0e40

Browse files
committed
Merge pull request #523 from DavidS/modules-2516-is_a
(MODULES-2561) add is_a function
2 parents c0df819 + 00c881d commit 0ea0e40

File tree

5 files changed

+111
-1
lines changed

5 files changed

+111
-1
lines changed

README.markdown

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,29 @@ Converts an array into a hash. For example, `hash(['a',1,'b',2,'c',3])` returns
403403

404404
Returns an array an intersection of two. For example, `intersection(["a","b","c"],["b","c","d"])` returns ["b","c"]. *Type*: rvalue.
405405

406+
#### `is_a`
407+
408+
Boolean check to determine whether a variable is of a given data type. This is equivalent to the `=~` type checks.
409+
410+
~~~
411+
foo = 3
412+
$bar = [1,2,3]
413+
$baz = 'A string!'
414+
415+
if $foo.is_a(Integer) {
416+
notify { 'foo!': }
417+
}
418+
if $bar.is_a(Array) {
419+
notify { 'bar!': }
420+
}
421+
if $baz.is_a(String) {
422+
notify { 'baz!': }
423+
}
424+
~~~
425+
426+
See the documentation for "The Puppet Type System" for more information about types.
427+
See the `assert_type()` function for flexible ways to assert the type of a value.
428+
406429
#### `is_array`
407430

408431
Returns 'true' if the variable passed to this function is an array. *Type*: rvalue.

lib/puppet/functions/is_a.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Boolean check to determine whether a variable is of a given data type. This is equivalent to the `=~` type checks.
2+
#
3+
# @example how to check a data type
4+
# # check a data type
5+
# foo = 3
6+
# $bar = [1,2,3]
7+
# $baz = 'A string!'
8+
#
9+
# if $foo.is_a(Integer) {
10+
# notify { 'foo!': }
11+
# }
12+
# if $bar.is_a(Array) {
13+
# notify { 'bar!': }
14+
# }
15+
# if $baz.is_a(String) {
16+
# notify { 'baz!': }
17+
# }
18+
#
19+
# See the documentation for "The Puppet Type System" for more information about types.
20+
# See the `assert_type()` function for flexible ways to assert the type of a value.
21+
#
22+
Puppet::Functions.create_function(:is_a) do
23+
dispatch :is_a do
24+
param 'Any', :value
25+
param 'Type', :type
26+
end
27+
28+
def is_a(value, type)
29+
# See puppet's lib/puppet/pops/evaluator/evaluator_impl.rb eval_MatchExpression
30+
Puppet::Pops::Types::TypeCalculator.instance?(type, value)
31+
end
32+
end

spec/acceptance/is_a_spec.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#! /usr/bin/env ruby -S rspec
2+
require 'spec_helper_acceptance'
3+
4+
describe 'is_a function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('operatingsystem')) do
5+
it 'should match a string' do
6+
pp = <<-EOS
7+
if 'hello world'.is_a(String) {
8+
notify { 'output correct': }
9+
}
10+
EOS
11+
12+
apply_manifest(pp, :catch_failures => true) do |r|
13+
expect(r.stdout).to match(/Notice: output correct/)
14+
end
15+
end
16+
17+
it 'should not match a integer as string' do
18+
pp = <<-EOS
19+
if 5.is_a(String) {
20+
notify { 'output wrong': }
21+
}
22+
EOS
23+
24+
apply_manifest(pp, :catch_failures => true) do |r|
25+
expect(r.stdout).not_to match(/Notice: output wrong/)
26+
end
27+
end
28+
end

spec/functions/is_a_spec.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
require 'spec_helper'
2+
3+
if ENV["FUTURE_PARSER"] == 'yes'
4+
describe 'type_of' do
5+
pending 'teach rspec-puppet to load future-only functions under 3.7.5' do
6+
it { is_expected.not_to eq(nil) }
7+
end
8+
end
9+
end
10+
11+
if Puppet.version.to_f >= 4.0
12+
describe 'is_a' do
13+
it { is_expected.not_to eq(nil) }
14+
it { is_expected.to run.with_params().and_raise_error(ArgumentError) }
15+
it { is_expected.to run.with_params('', '').and_raise_error(ArgumentError) }
16+
17+
it 'succeeds when comparing a string and a string' do
18+
is_expected.to run.with_params('hello world', String).and_return(true)
19+
end
20+
21+
it 'fails when comparing an integer and a string' do
22+
is_expected.to run.with_params(5, String).and_return(false)
23+
end
24+
end
25+
end

spec/functions/type_of_spec.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
if ENV["FUTURE_PARSER"] == 'yes'
44
describe 'type_of' do
5-
pending 'teach rspec-puppet to load future-only functions under 3.7.5'
5+
pending 'teach rspec-puppet to load future-only functions under 3.7.5' do
6+
it { is_expected.not_to eq(nil) }
7+
end
68
end
79
end
810

0 commit comments

Comments
 (0)