Skip to content

(maint) move/rewrite round() as ruby function #798

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

Merged
merged 1 commit into from
Jul 28, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1546,7 +1546,7 @@ For example, `reject(['aaa','bbb','ccc','aaaddd'], 'aaa')` returns ['bbb','ccc']

Reverses the order of a string or array.

#### `stdlib::round`
#### `round`

Rounds a number to the nearest integer

Expand Down
9 changes: 0 additions & 9 deletions functions/round.pp

This file was deleted.

33 changes: 33 additions & 0 deletions lib/puppet/parser/functions/round.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#
# round.rb
#

module Puppet::Parser::Functions
newfunction(:round, :type => :rvalue, :doc => <<-EOS
Rounds a number to the nearest integer

*Examples:*

round(2.9)

returns: 3

round(2.4)

returns: 2

EOS
) do |args|

raise Puppet::ParseError, "round(): Wrong number of arguments given #{args.size} for 1" if args.size != 1
raise Puppet::ParseError, "round(): Expected a Numeric, got #{args[0].class}" unless args[0].is_a? Numeric

value = args[0]

if value >= 0
Integer(value + 0.5)
else
Integer(value - 0.5)
end
end
end
5 changes: 4 additions & 1 deletion spec/functions/round_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
require 'spec_helper'

describe 'stdlib::round' do
describe 'round' do
it { is_expected.not_to eq(nil) }
it { is_expected.to run.with_params(34.3).and_return(34) }
it { is_expected.to run.with_params(-34.3).and_return(-34) }
it { is_expected.to run.with_params(34.5).and_return(35) }
it { is_expected.to run.with_params(-34.5).and_return(-35) }
it { is_expected.to run.with_params(34.7).and_return(35) }
it { is_expected.to run.with_params(-34.7).and_return(-35) }
it { is_expected.to run.with_params("test").and_raise_error Puppet::ParseError }
it { is_expected.to run.with_params("test", "best").and_raise_error Puppet::ParseError }
it { is_expected.to run.with_params(3, 4).and_raise_error Puppet::ParseError }
end