Created
December 6, 2013 00:20
-
-
Save glarizza/7816594 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| # <modulepath>/custom_functions/lib/puppet/parser/functions/longest.rb | |
| Puppet::Parser::Functions.newfunction(:longest, :type => :rvalue, :doc => "Longest Function") do |args| | |
| raise Puppet::Error, "#longest: accepts only one argument, you passed #{args.length}" if args.length != 1 | |
| raise Puppet::Error, "#longest: accepts a single Array, you passed: #{args[0].class}" if args[0].class != Array | |
| @longest = '' | |
| args[0].each do |item| | |
| @longest = item if item.length > @longest.length | |
| end | |
| @longest | |
| end | |
| [root@gary modules]# cat longest/spec/unit/puppet/parser/functions/longest_spec.rb | |
| # <modulepath>/longest/spec/unit/puppet/parser/functions/longest_spec.rb | |
| require 'puppet' | |
| require 'rspec-puppet' | |
| describe Puppet::Parser::Functions.function(:longest) do | |
| let :scope do | |
| PuppetlabsSpec::PuppetInternals.scope | |
| end | |
| it "should return the longest string in an array of strings" do | |
| scope.function_longest([['foo','bar','bazzle']]).should == 'bazzle' | |
| end | |
| it "should raise an error if you pass more or less than a single argument" do | |
| expect { scope.function_longest(['foo', 'bar']) }.to raise_error Puppet::Error, /#longest: accepts only one argumen/ | |
| end | |
| it "should raise an error if you don't pass an Array" do | |
| expect { scope.function_longest(['foo']) }.to raise_error Puppet::Error, /#longest: accepts a single Array/ | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment