From e61f402283774883d7b7c7d0f04dec7c457c968c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Dal=C3=A9n?= Date: Wed, 12 Nov 2014 15:52:33 +0100 Subject: [PATCH 1/3] (maint) Fix indentation of range function --- lib/puppet/parser/functions/range.rb | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/puppet/parser/functions/range.rb b/lib/puppet/parser/functions/range.rb index ffbdf8463..06d75d408 100644 --- a/lib/puppet/parser/functions/range.rb +++ b/lib/puppet/parser/functions/range.rb @@ -65,21 +65,21 @@ module Puppet::Parser::Functions end end - # Check whether we have integer value if so then make it so ... - if start.match(/^\d+$/) - start = start.to_i - stop = stop.to_i - else - start = start.to_s - stop = stop.to_s - end + # Check whether we have integer value if so then make it so ... + if start.match(/^\d+$/) + start = start.to_i + stop = stop.to_i + else + start = start.to_s + stop = stop.to_s + end - range = case type - when /^(\.\.|\-)$/ then (start .. stop) - when /^(\.\.\.)$/ then (start ... stop) # Exclusive of last element ... - end + range = case type + when /^(\.\.|\-)$/ then (start .. stop) + when /^(\.\.\.)$/ then (start ... stop) # Exclusive of last element ... + end - result = range.step(step).collect { |i| i } # Get them all ... Pokemon ... + result = range.step(step).collect { |i| i } # Get them all ... Pokemon ... return result end From ce995e15d5c266fd6d7fa781284771a5a5d5b00e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Dal=C3=A9n?= Date: Wed, 12 Nov 2014 16:02:05 +0100 Subject: [PATCH 2/3] Make the range function work with integers This is needed for the future parser which actually treats numbers as numbers and strings as strings. With this patch you can use range(1,5) instead of having to quote them like range('1','5'). --- lib/puppet/parser/functions/range.rb | 2 +- spec/functions/range_spec.rb | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/puppet/parser/functions/range.rb b/lib/puppet/parser/functions/range.rb index 06d75d408..49fba21c8 100644 --- a/lib/puppet/parser/functions/range.rb +++ b/lib/puppet/parser/functions/range.rb @@ -66,7 +66,7 @@ module Puppet::Parser::Functions end # Check whether we have integer value if so then make it so ... - if start.match(/^\d+$/) + if start.to_s.match(/^\d+$/) start = start.to_i stop = stop.to_i else diff --git a/spec/functions/range_spec.rb b/spec/functions/range_spec.rb index 9b9ece024..1446b245c 100755 --- a/spec/functions/range_spec.rb +++ b/spec/functions/range_spec.rb @@ -67,4 +67,11 @@ expect(scope.function_range(["00", "10"])).to eq expected end end + + describe 'with a numeric range' do + it "returns a range of numbers" do + expected = (1..10).to_a + expect(scope.function_range([1,10])).to eq expected + end + end end From af0a2779cb63b09a07f675ede3ae0b959c7442f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Dal=C3=A9n?= Date: Wed, 12 Nov 2014 16:52:36 +0100 Subject: [PATCH 3/3] Add range tests for numeric with step and mixed arguments --- spec/functions/range_spec.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/spec/functions/range_spec.rb b/spec/functions/range_spec.rb index 1446b245c..ef86f9719 100755 --- a/spec/functions/range_spec.rb +++ b/spec/functions/range_spec.rb @@ -73,5 +73,14 @@ expected = (1..10).to_a expect(scope.function_range([1,10])).to eq expected end + it "returns a range of numbers with step parameter" do + expected = (1..10).step(2).to_a + expect(scope.function_range([1,10,2])).to eq expected + end + it "works with mixed numeric like strings and numeric arguments" do + expected = (1..10).to_a + expect(scope.function_range(['1',10])).to eq expected + expect(scope.function_range([1,'10'])).to eq expected + end end end