1 #! /usr/bin/env ruby -S rspec
4 describe "the range function" do
5 let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
8 expect(Puppet::Parser::Functions.function("range")).to eq("function_range")
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/
15 describe 'with a letter range' do
16 it "returns a letter range" do
17 result = scope.function_range(["a","d"])
18 expect(result).to eq ['a','b','c','d']
21 it "returns a letter range given a step of 1" do
22 result = scope.function_range(["a","d","1"])
23 expect(result).to eq ['a','b','c','d']
26 it "returns a stepped letter range" do
27 result = scope.function_range(["a","d","2"])
28 expect(result).to eq ['a','c']
31 it "returns a stepped letter range given a negative step" do
32 result = scope.function_range(["a","d","-2"])
33 expect(result).to eq ['a','c']
37 describe 'with a number range' do
38 it "returns a number range" do
39 result = scope.function_range(["1","4"])
40 expect(result).to eq [1,2,3,4]
43 it "returns a number range given a step of 1" do
44 result = scope.function_range(["1","4","1"])
45 expect(result).to eq [1,2,3,4]
48 it "returns a stepped number range" do
49 result = scope.function_range(["1","4","2"])
50 expect(result).to eq [1,3]
53 it "returns a stepped number range given a negative step" do
54 result = scope.function_range(["1","4","-2"])
55 expect(result).to eq [1,3]
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 expect(scope.function_range(["host01","host10"])).to eq expected
65 it "coerces zero padded digits to integers" do
66 expected = (0..10).to_a
67 expect(scope.function_range(["00", "10"])).to eq expected
71 describe 'with a numeric range' do
72 it "returns a range of numbers" do
73 expected = (1..10).to_a
74 expect(scope.function_range([1,10])).to eq expected
76 it "returns a range of numbers with step parameter" do
77 expected = (1..10).step(2).to_a
78 expect(scope.function_range([1,10,2])).to eq expected
80 it "works with mixed numeric like strings and numeric arguments" do
81 expected = (1..10).to_a
82 expect(scope.function_range(['1',10])).to eq expected
83 expect(scope.function_range([1,'10'])).to eq expected