Skip to content

Commit 84a1ddd

Browse files
authored
Merge pull request #736 from HelenCampbell/lengthfunct
Addition of new length function
2 parents e4ea1c4 + 2d9a94e commit 84a1ddd

File tree

4 files changed

+56
-1
lines changed

4 files changed

+56
-1
lines changed

README.markdown

+5-1
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,10 @@ The return value is an array in which each element is one joined key/value pair.
831831

832832
Returns the keys of a hash as an array. *Type*: rvalue.
833833

834+
#### `length`
835+
836+
Returns the length of a given string, array or hash. To eventually replace the deprecated size() function as can handle the new type functionality introduced in Puppet 4. *Type*: rvalue.
837+
834838
#### `loadyaml`
835839

836840
Loads a YAML file containing an array, string, or hash, and returns the data in the corresponding native data type.
@@ -1083,7 +1087,7 @@ Randomizes the order of a string or array elements. *Type*: rvalue.
10831087

10841088
#### `size`
10851089

1086-
Returns the number of elements in a string, an array or a hash. *Type*: rvalue.
1090+
Returns the number of elements in a string, an array or a hash. May get confused around Puppet 4 type values and as such is to be deprecated in the next release and replaced with the new stdlib length function. *Type*: rvalue.
10871091

10881092
#### `sort`
10891093

lib/puppet/functions/length.rb

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#A function to eventually replace the old size() function for stdlib - The original size function did not handle Puppets new type capabilities, so this function is a Puppet 4 compatible solution.
2+
Puppet::Functions.create_function(:length) do
3+
dispatch :length do
4+
param 'Variant[String,Array,Hash]', :value
5+
end
6+
def length(value)
7+
if value.is_a?(String)
8+
result = value.length
9+
elsif value.is_a?(Array) || value.is_a?(Hash)
10+
result = value.size
11+
end
12+
return result
13+
end
14+
end

lib/puppet/parser/functions/size.rb

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ module Puppet::Parser::Functions
1212

1313
item = arguments[0]
1414

15+
function_deprecation([:size, 'This method is going to be deprecated, please use the stdlib length function.'])
16+
1517
if item.is_a?(String)
1618

1719
begin

spec/functions/length.rb

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
require 'spec_helper'
2+
3+
describe 'length' do
4+
it { is_expected.not_to eq(nil) }
5+
it { is_expected.to run.with_params().and_raise_error(ArgumentError, /'length' expects 1 argument, got none/) }
6+
it { is_expected.to run.with_params([], 'extra').and_raise_error(ArgumentError, /'length' expects 1 argument, got 2/) }
7+
it { is_expected.to run.with_params(1).and_raise_error(ArgumentError, /expects a value of type String, Array, or Hash, got Integer/) }
8+
it { is_expected.to run.with_params(true).and_raise_error(ArgumentError, /expects a value of type String, Array, or Hash, got Boolean/) }
9+
it { is_expected.to run.with_params('1').and_return(1) }
10+
it { is_expected.to run.with_params('1.0').and_return(3) }
11+
it { is_expected.to run.with_params([]).and_return(0) }
12+
it { is_expected.to run.with_params(['a']).and_return(1) }
13+
it { is_expected.to run.with_params(['one', 'two', 'three']).and_return(3) }
14+
it { is_expected.to run.with_params(['one', 'two', 'three', 'four']).and_return(4) }
15+
16+
it { is_expected.to run.with_params({}).and_return(0) }
17+
it { is_expected.to run.with_params({'1' => '2'}).and_return(1) }
18+
it { is_expected.to run.with_params({'1' => '2', '4' => '4'}).and_return(2) }
19+
it { is_expected.to run.with_params({'€' => '@', '竹' => 'ǿňè'}).and_return(2) }
20+
21+
it { is_expected.to run.with_params('').and_return(0) }
22+
it { is_expected.to run.with_params('a').and_return(1) }
23+
it { is_expected.to run.with_params('abc').and_return(3) }
24+
it { is_expected.to run.with_params('abcd').and_return(4) }
25+
it { is_expected.to run.with_params('万').and_return(1) }
26+
it { is_expected.to run.with_params('āβćđ').and_return(4) }
27+
28+
context 'when using a class extending String' do
29+
it 'should call its size method' do
30+
value = AlsoString.new('asdfghjkl')
31+
value.expects(:length).returns('foo')
32+
expect(subject).to run.with_params(value).and_return('foo')
33+
end
34+
end
35+
end

0 commit comments

Comments
 (0)