Skip to content

Commit f7a1818

Browse files
author
Jeff McCune
committed
Merge pull request #130 from jhoblitt/has_element
(#19272) Add has_element() function
2 parents 36a7b29 + 95cf3fe commit f7a1818

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module Puppet::Parser::Functions
2+
3+
newfunction(:has_element, :type => :rvalue, :doc => <<-'ENDHEREDOC') do |args|
4+
Determine if an array has an element with a matching value.
5+
6+
Example:
7+
8+
$my_array = ['key_one']
9+
if has_element($my_array, 'key_two') {
10+
notice('we will not reach here')
11+
}
12+
if has_element($my_array, 'key_one') {
13+
notice('this will be printed')
14+
}
15+
16+
ENDHEREDOC
17+
18+
unless args.length == 2
19+
raise Puppet::ParseError, ("has_element(): wrong number of arguments (#{args.length}; must be 2)")
20+
end
21+
unless args[0].is_a?(Array)
22+
raise Puppet::ParseError, "has_element(): expects the first argument to be an array, got #{args[0].inspect} which is of type #{args[0].class}"
23+
end
24+
args[0].include?(args[1])
25+
26+
end
27+
28+
end
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#! /usr/bin/env ruby -S rspec
2+
require 'spec_helper'
3+
4+
describe Puppet::Parser::Functions.function(:has_element) do
5+
let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
6+
7+
describe 'when calling has_element from puppet' do
8+
it "should not compile when no arguments are passed" do
9+
pending("Fails on 2.6.x, see bug #15912") if Puppet.version =~ /^2\.6\./
10+
Puppet[:code] = '$x = has_element()'
11+
expect {
12+
scope.compiler.compile
13+
}.to raise_error(Puppet::ParseError, /wrong number of arguments/)
14+
end
15+
16+
it "should not compile when 1 argument is passed" do
17+
pending("Fails on 2.6.x, see bug #15912") if Puppet.version =~ /^2\.6\./
18+
Puppet[:code] = "$x = has_element('foo')"
19+
expect {
20+
scope.compiler.compile
21+
}.to raise_error(Puppet::ParseError, /wrong number of arguments/)
22+
end
23+
24+
it "should require the first value to be an Array" do
25+
pending("Fails on 2.6.x, see bug #15912") if Puppet.version =~ /^2\.6\./
26+
Puppet[:code] = "$x = has_element('foo', 'bar')"
27+
expect {
28+
scope.compiler.compile
29+
}.to raise_error(Puppet::ParseError, /expects the first argument to be an array/)
30+
end
31+
end
32+
33+
describe 'when calling the function has_element from a scope instance' do
34+
it 'should detect existing elements' do
35+
scope.function_has_element([['one'], 'one']).should be_true
36+
end
37+
38+
it 'should detect existing elements' do
39+
scope.function_has_element([['one'], 'two']).should be_false
40+
end
41+
end
42+
end

tests/has_element.pp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
include stdlib
2+
3+
$my_array = ['key_one']
4+
if has_element($my_array, 'key_two') {
5+
notice('we will not reach here')
6+
}
7+
if has_element($my_array, 'key_one') {
8+
notice('this will be printed')
9+
}

0 commit comments

Comments
 (0)