Skip to content

Commit 928c131

Browse files
committed
Merge branch 'pull-56'
2 parents 9c8c827 + 77768e5 commit 928c131

File tree

2 files changed

+61
-17
lines changed

2 files changed

+61
-17
lines changed

lib/puppet/parser/functions/range.rb

+9-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ module Puppet::Parser::Functions
2727
range("host01", "host10")
2828
2929
Will return: ["host01", "host02", ..., "host09", "host10"]
30+
31+
Passing a third argument will cause the generated range to step by that
32+
interval, e.g.
33+
34+
range("0", "9", "2")
35+
36+
Will return: [0,2,4,6,8]
3037
EOS
3138
) do |arguments|
3239

@@ -37,6 +44,7 @@ module Puppet::Parser::Functions
3744
if arguments.size > 1
3845
start = arguments[0]
3946
stop = arguments[1]
47+
step = arguments[2].nil? ? 1 : arguments[2].to_i.abs
4048

4149
type = '..' # We select simplest type for Range available in Ruby ...
4250

@@ -71,7 +79,7 @@ module Puppet::Parser::Functions
7179
when /^(\.\.\.)$/ then (start ... stop) # Exclusive of last element ...
7280
end
7381

74-
result = range.collect { |i| i } # Get them all ... Pokemon ...
82+
result = range.step(step).collect { |i| i } # Get them all ... Pokemon ...
7583

7684
return result
7785
end

spec/unit/puppet/parser/functions/range_spec.rb

+52-16
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,67 @@
44
describe "the range function" do
55
let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
66

7-
it "should exist" do
7+
it "exists" do
88
Puppet::Parser::Functions.function("range").should == "function_range"
99
end
1010

11-
it "should raise a ParseError if there is less than 1 arguments" do
12-
lambda { scope.function_range([]) }.should( raise_error(Puppet::ParseError))
11+
it "raises a ParseError if there is less than 1 arguments" do
12+
expect { scope.function_range([]) }.to raise_error Puppet::ParseError, /Wrong number of arguments.*0 for 1/
1313
end
1414

15-
it "should return a letter range" do
16-
result = scope.function_range(["a","d"])
17-
result.should(eq(['a','b','c','d']))
18-
end
15+
describe 'with a letter range' do
16+
it "returns a letter range" do
17+
result = scope.function_range(["a","d"])
18+
result.should eq ['a','b','c','d']
19+
end
20+
21+
it "returns a letter range given a step of 1" do
22+
result = scope.function_range(["a","d","1"])
23+
result.should eq ['a','b','c','d']
24+
end
1925

20-
it "should return a number range" do
21-
result = scope.function_range(["1","4"])
22-
result.should(eq([1,2,3,4]))
26+
it "returns a stepped letter range" do
27+
result = scope.function_range(["a","d","2"])
28+
result.should eq ['a','c']
29+
end
30+
31+
it "returns a stepped letter range given a negative step" do
32+
result = scope.function_range(["a","d","-2"])
33+
result.should eq ['a','c']
34+
end
2335
end
2436

25-
it "should work with padded hostname like strings" do
26-
expected = ("host01".."host10").to_a
27-
scope.function_range(["host01","host10"]).should eq expected
37+
describe 'with a number range' do
38+
it "returns a number range" do
39+
result = scope.function_range(["1","4"])
40+
result.should eq [1,2,3,4]
41+
end
42+
43+
it "returns a number range given a step of 1" do
44+
result = scope.function_range(["1","4","1"])
45+
result.should eq [1,2,3,4]
46+
end
47+
48+
it "returns a stepped number range" do
49+
result = scope.function_range(["1","4","2"])
50+
result.should eq [1,3]
51+
end
52+
53+
it "returns a stepped number range given a negative step" do
54+
result = scope.function_range(["1","4","-2"])
55+
result.should eq [1,3]
56+
end
2857
end
2958

30-
it "should coerce zero padded digits to integers" do
31-
expected = (0..10).to_a
32-
scope.function_range(["00", "10"]).should eq expected
59+
describe 'with a numeric-like string range' do
60+
it "works with padded hostname like strings" do
61+
expected = ("host01".."host10").to_a
62+
scope.function_range(["host01","host10"]).should eq expected
63+
end
64+
65+
it "coerces zero padded digits to integers" do
66+
expected = (0..10).to_a
67+
scope.function_range(["00", "10"]).should eq expected
68+
end
3369
end
3470
end

0 commit comments

Comments
 (0)