summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrien Thebo <git@somethingsinistral.net>2013-03-27 14:04:12 -0700
committerAdrien Thebo <git@somethingsinistral.net>2013-03-27 14:04:12 -0700
commit0adcf646b85ea3379ac8cb25050d445e577b71a3 (patch)
tree8bc6c111e3a4ae99e01c3954d68b128ee37daf5a
parent2c1b2c07c2d09464377cbde7b21952c99877751c (diff)
parent87c0f0c45418b276942e3de6deb8a2b681566019 (diff)
downloadpuppet-stdlib-0adcf646b85ea3379ac8cb25050d445e577b71a3.tar.gz
puppet-stdlib-0adcf646b85ea3379ac8cb25050d445e577b71a3.tar.bz2
Merge branch '3.x' into 4.x
-rw-r--r--lib/puppet/parser/functions/max.rb10
-rw-r--r--lib/puppet/parser/functions/min.rb10
-rwxr-xr-xspec/unit/puppet/parser/functions/max_spec.rb4
-rwxr-xr-xspec/unit/puppet/parser/functions/min_spec.rb4
4 files changed, 26 insertions, 2 deletions
diff --git a/lib/puppet/parser/functions/max.rb b/lib/puppet/parser/functions/max.rb
index 10b6f74..60fb94a 100644
--- a/lib/puppet/parser/functions/max.rb
+++ b/lib/puppet/parser/functions/max.rb
@@ -8,6 +8,14 @@ module Puppet::Parser::Functions
raise(Puppet::ParseError, "max(): Wrong number of arguments " +
"need at least one") if args.size == 0
- return args.max
+ # Sometimes we get numbers as numerics and sometimes as strings.
+ # We try to compare them as numbers when possible
+ return args.max do |a,b|
+ if a.to_s =~ /\A-?\d+(.\d+)?\z/ and b.to_s =~ /\A-?\d+(.\d+)?\z/ then
+ a.to_f <=> b.to_f
+ else
+ a.to_s <=> b.to_s
+ end
+ end
end
end
diff --git a/lib/puppet/parser/functions/min.rb b/lib/puppet/parser/functions/min.rb
index abf1b62..6bd6ebf 100644
--- a/lib/puppet/parser/functions/min.rb
+++ b/lib/puppet/parser/functions/min.rb
@@ -8,6 +8,14 @@ module Puppet::Parser::Functions
raise(Puppet::ParseError, "min(): Wrong number of arguments " +
"need at least one") if args.size == 0
- return args.min
+ # Sometimes we get numbers as numerics and sometimes as strings.
+ # We try to compare them as numbers when possible
+ return args.min do |a,b|
+ if a.to_s =~ /\A^-?\d+(.\d+)?\z/ and b.to_s =~ /\A-?\d+(.\d+)?\z/ then
+ a.to_f <=> b.to_f
+ else
+ a.to_s <=> b.to_s
+ end
+ end
end
end
diff --git a/spec/unit/puppet/parser/functions/max_spec.rb b/spec/unit/puppet/parser/functions/max_spec.rb
index 604927e..ff6f2b3 100755
--- a/spec/unit/puppet/parser/functions/max_spec.rb
+++ b/spec/unit/puppet/parser/functions/max_spec.rb
@@ -20,4 +20,8 @@ describe "the max function" do
it "should be able to compare numbers" do
scope.function_max([6,8,4]).should(eq(8))
end
+
+ it "should be able to compare a number with a stringified number" do
+ scope.function_max([1,"2"]).should(eq("2"))
+ end
end
diff --git a/spec/unit/puppet/parser/functions/min_spec.rb b/spec/unit/puppet/parser/functions/min_spec.rb
index 781422c..71d593e 100755
--- a/spec/unit/puppet/parser/functions/min_spec.rb
+++ b/spec/unit/puppet/parser/functions/min_spec.rb
@@ -20,4 +20,8 @@ describe "the min function" do
it "should be able to compare numbers" do
scope.function_min([6,8,4]).should(eq(4))
end
+
+ it "should be able to compare a number with a stringified number" do
+ scope.function_min([1,"2"]).should(eq(1))
+ end
end