From 4732676548f91a43bc67ea0b70abdd34cae093c1 Mon Sep 17 00:00:00 2001 From: Steve Huff Date: Tue, 3 Apr 2012 11:46:20 -0400 Subject: add a "step" argument to range() This patch adds an optional "step" argument to the stdlib range() function. There is no change to the default behavior of the function; however, passing a numeric "step" argument invokes the Ruby Range#step method, e.g. range("0", "9", "2") returns [0,2,4,6,8] --- lib/puppet/parser/functions/range.rb | 10 ++++++++- spec/unit/puppet/parser/functions/range_spec.rb | 30 +++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/lib/puppet/parser/functions/range.rb b/lib/puppet/parser/functions/range.rb index 6e85422..03ab9e9 100644 --- a/lib/puppet/parser/functions/range.rb +++ b/lib/puppet/parser/functions/range.rb @@ -18,6 +18,13 @@ Will return: [0,1,2,3,4,5,6,7,8,9] range("a", "c") Will return: ["a","b","c"] + +Passing a third argument will cause the generated range to step by that +interval, e.g. + + range("0", "9", "2") + +Will return: [0,2,4,6,8] EOS ) do |arguments| @@ -28,6 +35,7 @@ Will return: ["a","b","c"] if arguments.size > 1 start = arguments[0] stop = arguments[1] + step = arguments[2].nil? ? 1 : arguments[2].to_i.abs type = '..' # We select simplest type for Range available in Ruby ... @@ -62,7 +70,7 @@ Will return: ["a","b","c"] when /^(\.\.\.)$/ then (start ... stop) # Exclusive of last element ... end - result = range.collect { |i| i } # Get them all ... Pokemon ... + result = range.step(step).collect { |i| i } # Get them all ... Pokemon ... return result end diff --git a/spec/unit/puppet/parser/functions/range_spec.rb b/spec/unit/puppet/parser/functions/range_spec.rb index 8c2446a..24cc391 100644 --- a/spec/unit/puppet/parser/functions/range_spec.rb +++ b/spec/unit/puppet/parser/functions/range_spec.rb @@ -23,9 +23,39 @@ describe "the range function" do result.should(eq(['a','b','c','d'])) end + it "should return a letter range given a step of 1" do + result = @scope.function_range(["a","d","1"]) + result.should(eq(['a','b','c','d'])) + end + + it "should return a stepped letter range" do + result = @scope.function_range(["a","d","2"]) + result.should(eq(['a','c'])) + end + + it "should return a stepped letter range given a negative step" do + result = @scope.function_range(["1","4","-2"]) + result.should(eq(['a','c'])) + end + it "should return a number range" do result = @scope.function_range(["1","4"]) result.should(eq([1,2,3,4])) end + it "should return a number range given a step of 1" do + result = @scope.function_range(["1","4","1"]) + result.should(eq([1,2,3,4])) + end + + it "should return a stepped number range" do + result = @scope.function_range(["1","4","2"]) + result.should(eq([1,3])) + end + + it "should return a stepped number range given a negative step" do + result = @scope.function_range(["1","4","-2"]) + result.should(eq([1,3])) + end + end -- cgit v1.2.3 From 1fcb854f155cd0d63f88a9c792e60555ddffcc16 Mon Sep 17 00:00:00 2001 From: Adrien Thebo Date: Tue, 28 May 2013 11:11:50 -0700 Subject: (maint) split up range spec with describe blocks --- spec/unit/puppet/parser/functions/range_spec.rb | 81 +++++++++++++------------ 1 file changed, 43 insertions(+), 38 deletions(-) diff --git a/spec/unit/puppet/parser/functions/range_spec.rb b/spec/unit/puppet/parser/functions/range_spec.rb index 5eb290f..c7dab2e 100644 --- a/spec/unit/puppet/parser/functions/range_spec.rb +++ b/spec/unit/puppet/parser/functions/range_spec.rb @@ -12,54 +12,59 @@ describe "the range function" do lambda { scope.function_range([]) }.should( raise_error(Puppet::ParseError)) end - it "should return a letter range" do - result = scope.function_range(["a","d"]) - result.should(eq(['a','b','c','d'])) - end + describe 'with a letter range' do + it "should return a letter range" do + result = scope.function_range(["a","d"]) + result.should(eq(['a','b','c','d'])) + end - it "should return a letter range given a step of 1" do - result = scope.function_range(["a","d","1"]) - result.should(eq(['a','b','c','d'])) - end + it "should return a letter range given a step of 1" do + result = scope.function_range(["a","d","1"]) + result.should(eq(['a','b','c','d'])) + end - it "should return a stepped letter range" do - result = scope.function_range(["a","d","2"]) - result.should(eq(['a','c'])) - end + it "should return a stepped letter range" do + result = scope.function_range(["a","d","2"]) + result.should(eq(['a','c'])) + end - it "should return a stepped letter range given a negative step" do - result = scope.function_range(["a","d","-2"]) - result.should(eq(['a','c'])) + it "should return a stepped letter range given a negative step" do + result = scope.function_range(["a","d","-2"]) + result.should(eq(['a','c'])) + end end - it "should return a number range" do - result = scope.function_range(["1","4"]) - result.should(eq([1,2,3,4])) - end + describe 'with a number range' do + it "should return a number range" do + result = scope.function_range(["1","4"]) + result.should(eq([1,2,3,4])) + end - it "should work with padded hostname like strings" do - expected = ("host01".."host10").to_a - scope.function_range(["host01","host10"]).should eq expected - end + it "should return a number range given a step of 1" do + result = scope.function_range(["1","4","1"]) + result.should(eq([1,2,3,4])) + end - it "should coerce zero padded digits to integers" do - expected = (0..10).to_a - scope.function_range(["00", "10"]).should eq expected - end + it "should return a stepped number range" do + result = scope.function_range(["1","4","2"]) + result.should(eq([1,3])) + end - it "should return a number range given a step of 1" do - result = scope.function_range(["1","4","1"]) - result.should(eq([1,2,3,4])) + it "should return a stepped number range given a negative step" do + result = scope.function_range(["1","4","-2"]) + result.should(eq([1,3])) + end end - it "should return a stepped number range" do - result = scope.function_range(["1","4","2"]) - result.should(eq([1,3])) - end + describe 'with a numeric-like string range' do + it "should work with padded hostname like strings" do + expected = ("host01".."host10").to_a + scope.function_range(["host01","host10"]).should eq expected + end - it "should return a stepped number range given a negative step" do - result = scope.function_range(["1","4","-2"]) - result.should(eq([1,3])) + it "should coerce zero padded digits to integers" do + expected = (0..10).to_a + scope.function_range(["00", "10"]).should eq expected + end end - end -- cgit v1.2.3 From dd0a4220d591f778e828f98e73ac67ed9e3b97c4 Mon Sep 17 00:00:00 2001 From: Adrien Thebo Date: Tue, 28 May 2013 11:13:20 -0700 Subject: (maint) Use present tense in range_spec messages --- spec/unit/puppet/parser/functions/range_spec.rb | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/spec/unit/puppet/parser/functions/range_spec.rb b/spec/unit/puppet/parser/functions/range_spec.rb index c7dab2e..07b6f11 100644 --- a/spec/unit/puppet/parser/functions/range_spec.rb +++ b/spec/unit/puppet/parser/functions/range_spec.rb @@ -4,65 +4,65 @@ require 'spec_helper' describe "the range function" do let(:scope) { PuppetlabsSpec::PuppetInternals.scope } - it "should exist" do + it "exists" do Puppet::Parser::Functions.function("range").should == "function_range" end - it "should raise a ParseError if there is less than 1 arguments" do + it "raises a ParseError if there is less than 1 arguments" do lambda { scope.function_range([]) }.should( raise_error(Puppet::ParseError)) end describe 'with a letter range' do - it "should return a letter range" do + it "returns a letter range" do result = scope.function_range(["a","d"]) result.should(eq(['a','b','c','d'])) end - it "should return a letter range given a step of 1" do + it "returns a letter range given a step of 1" do result = scope.function_range(["a","d","1"]) result.should(eq(['a','b','c','d'])) end - it "should return a stepped letter range" do + it "returns a stepped letter range" do result = scope.function_range(["a","d","2"]) result.should(eq(['a','c'])) end - it "should return a stepped letter range given a negative step" do + it "returns a stepped letter range given a negative step" do result = scope.function_range(["a","d","-2"]) result.should(eq(['a','c'])) end end describe 'with a number range' do - it "should return a number range" do + it "returns a number range" do result = scope.function_range(["1","4"]) result.should(eq([1,2,3,4])) end - it "should return a number range given a step of 1" do + it "returns a number range given a step of 1" do result = scope.function_range(["1","4","1"]) result.should(eq([1,2,3,4])) end - it "should return a stepped number range" do + it "returns a stepped number range" do result = scope.function_range(["1","4","2"]) result.should(eq([1,3])) end - it "should return a stepped number range given a negative step" do + it "returns a stepped number range given a negative step" do result = scope.function_range(["1","4","-2"]) result.should(eq([1,3])) end end describe 'with a numeric-like string range' do - it "should work with padded hostname like strings" do + it "works with padded hostname like strings" do expected = ("host01".."host10").to_a scope.function_range(["host01","host10"]).should eq expected end - it "should coerce zero padded digits to integers" do + it "coerces zero padded digits to integers" do expected = (0..10).to_a scope.function_range(["00", "10"]).should eq expected end -- cgit v1.2.3 From 65380bcdfbe91f85013d8a925cdd719826209ce4 Mon Sep 17 00:00:00 2001 From: Adrien Thebo Date: Tue, 28 May 2013 11:14:28 -0700 Subject: (maint) Clean up range_spec error expectation Replace `lambda` with `expect` for making an error expectation Add an explicit error message in expectation --- spec/unit/puppet/parser/functions/range_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/unit/puppet/parser/functions/range_spec.rb b/spec/unit/puppet/parser/functions/range_spec.rb index 07b6f11..426903c 100644 --- a/spec/unit/puppet/parser/functions/range_spec.rb +++ b/spec/unit/puppet/parser/functions/range_spec.rb @@ -9,7 +9,7 @@ describe "the range function" do end it "raises a ParseError if there is less than 1 arguments" do - lambda { scope.function_range([]) }.should( raise_error(Puppet::ParseError)) + expect { scope.function_range([]) }.to raise_error Puppet::ParseError, /Wrong number of arguments.*0 for 1/ end describe 'with a letter range' do -- cgit v1.2.3 From 77768e5d8df7fa37707b75934fc47be9bb4e3989 Mon Sep 17 00:00:00 2001 From: Adrien Thebo Date: Tue, 28 May 2013 11:17:00 -0700 Subject: (maint) Remove syntax decoration from range_spec --- spec/unit/puppet/parser/functions/range_spec.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/spec/unit/puppet/parser/functions/range_spec.rb b/spec/unit/puppet/parser/functions/range_spec.rb index 426903c..0e1ad37 100644 --- a/spec/unit/puppet/parser/functions/range_spec.rb +++ b/spec/unit/puppet/parser/functions/range_spec.rb @@ -15,44 +15,44 @@ describe "the range function" do describe 'with a letter range' do it "returns a letter range" do result = scope.function_range(["a","d"]) - result.should(eq(['a','b','c','d'])) + result.should eq ['a','b','c','d'] end it "returns a letter range given a step of 1" do result = scope.function_range(["a","d","1"]) - result.should(eq(['a','b','c','d'])) + result.should eq ['a','b','c','d'] end it "returns a stepped letter range" do result = scope.function_range(["a","d","2"]) - result.should(eq(['a','c'])) + result.should eq ['a','c'] end it "returns a stepped letter range given a negative step" do result = scope.function_range(["a","d","-2"]) - result.should(eq(['a','c'])) + result.should eq ['a','c'] end end describe 'with a number range' do it "returns a number range" do result = scope.function_range(["1","4"]) - result.should(eq([1,2,3,4])) + result.should eq [1,2,3,4] end it "returns a number range given a step of 1" do result = scope.function_range(["1","4","1"]) - result.should(eq([1,2,3,4])) + result.should eq [1,2,3,4] end it "returns a stepped number range" do result = scope.function_range(["1","4","2"]) - result.should(eq([1,3])) + result.should eq [1,3] end it "returns a stepped number range given a negative step" do result = scope.function_range(["1","4","-2"]) - result.should(eq([1,3])) + result.should eq [1,3] end end -- cgit v1.2.3