]> git.donarmstrong.com Git - dsa-puppet.git/blob - 3rdparty/modules/stdlib/spec/functions/range_spec.rb
upgrade to stdlib 4.6.1
[dsa-puppet.git] / 3rdparty / modules / stdlib / spec / functions / range_spec.rb
1 #! /usr/bin/env ruby -S rspec
2 require 'spec_helper'
3
4 describe "the range function" do
5   let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
6
7   it "exists" do
8     expect(Puppet::Parser::Functions.function("range")).to eq("function_range")
9   end
10
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/
13   end
14
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']
19     end
20
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']
24     end
25
26     it "returns a stepped letter range" do
27       result = scope.function_range(["a","d","2"])
28       expect(result).to 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       expect(result).to eq ['a','c']
34     end
35   end
36
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]
41     end
42
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]
46     end
47
48     it "returns a stepped number range" do
49       result = scope.function_range(["1","4","2"])
50       expect(result).to 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       expect(result).to eq [1,3]
56     end
57   end
58
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
63     end
64
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
68     end
69   end
70
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
75     end
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
79     end
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
84     end
85   end
86 end