Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the range function work with integers #365

Merged
merged 3 commits into from Nov 13, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 13 additions & 13 deletions lib/puppet/parser/functions/range.rb
Expand Up @@ -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.to_s.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
Expand Down
16 changes: 16 additions & 0 deletions spec/functions/range_spec.rb
Expand Up @@ -67,4 +67,20 @@
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
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