Skip to content

Commit 0adcf64

Browse files
committed
Merge branch '3.x' into 4.x
2 parents 2c1b2c0 + 87c0f0c commit 0adcf64

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed

lib/puppet/parser/functions/max.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ module Puppet::Parser::Functions
88
raise(Puppet::ParseError, "max(): Wrong number of arguments " +
99
"need at least one") if args.size == 0
1010

11-
return args.max
11+
# Sometimes we get numbers as numerics and sometimes as strings.
12+
# We try to compare them as numbers when possible
13+
return args.max do |a,b|
14+
if a.to_s =~ /\A-?\d+(.\d+)?\z/ and b.to_s =~ /\A-?\d+(.\d+)?\z/ then
15+
a.to_f <=> b.to_f
16+
else
17+
a.to_s <=> b.to_s
18+
end
19+
end
1220
end
1321
end

lib/puppet/parser/functions/min.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ module Puppet::Parser::Functions
88
raise(Puppet::ParseError, "min(): Wrong number of arguments " +
99
"need at least one") if args.size == 0
1010

11-
return args.min
11+
# Sometimes we get numbers as numerics and sometimes as strings.
12+
# We try to compare them as numbers when possible
13+
return args.min do |a,b|
14+
if a.to_s =~ /\A^-?\d+(.\d+)?\z/ and b.to_s =~ /\A-?\d+(.\d+)?\z/ then
15+
a.to_f <=> b.to_f
16+
else
17+
a.to_s <=> b.to_s
18+
end
19+
end
1220
end
1321
end

spec/unit/puppet/parser/functions/max_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,8 @@
2020
it "should be able to compare numbers" do
2121
scope.function_max([6,8,4]).should(eq(8))
2222
end
23+
24+
it "should be able to compare a number with a stringified number" do
25+
scope.function_max([1,"2"]).should(eq("2"))
26+
end
2327
end

spec/unit/puppet/parser/functions/min_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,8 @@
2020
it "should be able to compare numbers" do
2121
scope.function_min([6,8,4]).should(eq(4))
2222
end
23+
24+
it "should be able to compare a number with a stringified number" do
25+
scope.function_min([1,"2"]).should(eq(1))
26+
end
2327
end

0 commit comments

Comments
 (0)