Created
November 7, 2014 08:13
-
-
Save rdark/93ae34b3617b71bbe16d to your computer and use it in GitHub Desktop.
function (three arguments)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Rake spec: | |
$ rake spec | |
function_name | |
example at ./spec/functions/function_name_spec.rb:40 (FAILED - 1) | |
Failures: | |
1) function_name | |
Failure/Error: should run.with_params(:good_array, 'prefix').and_return | |
ArgumentError: | |
wrong number of arguments (0 for 1) | |
# ./spec/functions/function_name_spec.rb:41:in `block (2 levels) in <top (required)>' | |
Finished in 0.0127 seconds | |
1 example, 1 failure | |
Failed examples: | |
rspec ./spec/functions/function_name_spec.rb:40 # function_name | |
Total resources: 0 | |
Touched resources: 0 | |
Resource coverage: NaN% | |
Untouched resources: | |
# | |
# function_name.rb | |
# | |
# === Author | |
# | |
# Richard Clark | |
# | |
module Puppet::Parser::Functions | |
newfunction(:function_name, :type => :rvalue, :doc => <<-EOS | |
DOCS HERE | |
EOS | |
) do |arguments| | |
if (arguments.size < 2) or (arguments.size > 3) then | |
raise Puppet::ParseError, "function_name(): Wrong number of arguments" + | |
"given #{arguments.size} for 2 or 3" | |
end | |
src_array = arguments[0] | |
prefix = arguments[1] | |
unless src_array.is_a? Array | |
raise Puppet::ParseError, "First argument must be an array" | |
end | |
unless prefix.is_a? String | |
raise Puppet::ParseError, "Second argument must be a string" | |
end | |
dest_hash = Hash.new | |
postfix = 1 | |
# verify all sub-data structures are hashes | |
src_array.each do |element| | |
unless element.is_a? Hash | |
raise Puppet::ParseError, "All array elements must be hashes" | |
end | |
# just simple prefix + integer | |
if arguments.size == 2 | |
dest_hash["#{prefix}#{postfix}"] = src_array[element] | |
postfix += 1 | |
end | |
end | |
dest_hash | |
end | |
end | |
# vim: set ts=2 sw=2 et : | |
function_name_spec | |
require 'spec_helper' | |
require 'rspec-puppet' | |
describe 'function_name' do | |
let(:good_array) { | |
[ | |
{ | |
'port' => '443', | |
'data' => 'foo', | |
}, | |
{ | |
'port' => '80', | |
'data' => 'bar', | |
}, | |
{ | |
'port' => '8080', | |
'data' => 'baz', | |
}, | |
] | |
} | |
let(:expect_good_array_iter) { | |
{ | |
'prefix_1' => { | |
'port' => '443', | |
'data' => 'foo', | |
}, | |
'prefix_2' => { | |
'port' => '80', | |
'data' => 'bar', | |
}, | |
'prefix_3' => { | |
'port' => '8080', | |
'data' => 'baz', | |
}, | |
} | |
} | |
describe 'Make a nested hash with prefixed integer iterator' do | |
it { | |
should run.with_params(:good_array, 'prefix').and_return | |
(:expect_good_array_iter) | |
} | |
end | |
# vim: set ts=2 sw=2 et : |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment