Skip to content

Commit 379ddac

Browse files
Modified regex for using floating point numbers in comparison and also added test cases
1 parent 536a175 commit 379ddac

File tree

3 files changed

+34
-10
lines changed

3 files changed

+34
-10
lines changed

lib/Template/Liquid/Condition.pm

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,9 @@ sub _equal { # XXX - Pray we don't have a recursive data structure...
9191
if (!$ref_l) {
9292
return
9393
!!(grep {defined} $l, $r)
94-
? (grep {m[\D]o} $l, $r)
95-
? $l eq $r
96-
: $l == $r
94+
? (grep {m[^\d+(\.\d+)?$]o} $l, $r)
95+
? $l == $r
96+
: $l eq $r
9797
: !1;
9898
}
9999
elsif ($ref_l eq 'ARRAY') {
@@ -129,9 +129,9 @@ sub gt {
129129
$r = $_r if defined $_r;
130130
return
131131
!!(grep {defined} $l, $r)
132-
? (grep {m[\D]o} $l, $r)
133-
? $l gt $r
134-
: $l > $r
132+
? (grep {m[^\d+(\.\d+)?$]o} $l, $r)
133+
? $l > $r
134+
: $l gt $r
135135
: 0;
136136
}
137137
sub lt { return !$_[0]->gt }

t/0200_tags/02006_if.t

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,36 @@ is(Template::Liquid->parse(<<'INPUT')->render(), <<'EXPECTED', '1 == 1');
1010
INPUT
1111
One equals one
1212
EXPECTED
13+
is(Template::Liquid->parse(<<'INPUT')->render(), <<'EXPECTED', '1.1 == 1.1');
14+
{% if 1.1 == 1.1 %}1.1 equals 1.1{% endif %}
15+
INPUT
16+
1.1 equals 1.1
17+
EXPECTED
1318
is(Template::Liquid->parse(<<'INPUT')->render(), <<'EXPECTED', '1 != 1');
1419
{% if 1 != 1 %}One does not equal one{% endif %}
1520
INPUT
1621

22+
EXPECTED
23+
is(Template::Liquid->parse(<<'INPUT')->render(), <<'EXPECTED', '1.1 != 1.1');
24+
{% if 1.1 != 1.1 %}1.1 does not equal 1.1{% endif %}
25+
INPUT
26+
1727
EXPECTED
1828
is(Template::Liquid->parse(<<'INPUT')->render(), <<'EXPECTED', q[1 < 2]);
1929
{% if 1 < 2 %}Yep.{% endif %}
2030
INPUT
2131
Yep.
2232
EXPECTED
33+
is(Template::Liquid->parse(<<'INPUT')->render(), <<'EXPECTED', q[1.1 < 2.2]);
34+
{% if 1.1 < 2.2 %}Yep.{% endif %}
35+
INPUT
36+
Yep.
37+
EXPECTED
38+
is(Template::Liquid->parse(<<'INPUT')->render(), <<'EXPECTED', q[2.2 > 1.1]);
39+
{% if 2.2 > 1.1 %}Yep.{% endif %}
40+
INPUT
41+
Yep.
42+
EXPECTED
2343
is(Template::Liquid->parse(<<'INPUT')->render(), <<'EXPECTED', q[1 > 2]);
2444
{% if 1 > 2 %}Yep.{% endif %}
2545
INPUT

t/0200_tags/02101_whitespace_control.t

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ use Template::Liquid;
66
$|++;
77
is( Template::Liquid->parse(
88
<<'TEMPLATE')->render(), 'Wow,John G. Chalmers-Smith, you have a long name!', 'whitespace control');
9-
{%- assign username = "John G. Chalmers-Smith" -%}
10-
{%- if username and username.size > 10 -%}
9+
{%- assign username = "John G. Chalmers-Smith" -%}
10+
{%- assign username_size = username | size -%}
11+
{%- if username and username_size > 10 -%}
1112
Wow, {{- username -}} , you have a long name!
1213
{%- else -%}
1314
Hello there!
@@ -16,21 +17,24 @@ TEMPLATE
1617
is( Template::Liquid->parse(
1718
<<'TEMPLATE')->render(), <<'EXPECTED', 'without whitespace control');
1819
{% assign username = "John G. Chalmers-Smith" %}
19-
{% if username and username.size > 10 %}
20+
{% assign username_size = username | size %}
21+
{% if username and username_size > 10 %}
2022
Wow, {{ username }}, you have a long name!
2123
{% else %}
2224
Hello there!
2325
{% endif %}
2426
TEMPLATE
2527

2628

29+
2730
Wow, John G. Chalmers-Smith, you have a long name!
2831

2932
EXPECTED
3033
is( Template::Liquid->parse(
3134
<<'TEMPLATE')->render(), <<'EXPECTED', 'mixed with and without whitespace control');
3235
{%-assign username = "John G. Chalmers-Smith"-%}
33-
{%-if username and username.size > 10 %}
36+
{%-assign username_size = username | size-%}
37+
{%-if username and username_size > 10 %}
3438
Wow, {{- username -}} , you have a long name!
3539
Wow, {{ username -}} , you have a long name!
3640
Wow, {{- username }} , you have a long name!

0 commit comments

Comments
 (0)