From: Matthew Horsfall Date: Wed, 2 Mar 2011 18:42:52 +0000 (-0500) Subject: Int type constraint allowed for a newline character due to the /^..$/ syntax. Use... X-Git-Tag: 1.9904~4 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=f55fba5ca6833056e1f8788fc9fde94c14907699;p=gitmo%2FMoose.git Int type constraint allowed for a newline character due to the /^..$/ syntax. Use /\A..\z/ instead. --- diff --git a/lib/Moose/Util/TypeConstraints/OptimizedConstraints.pm b/lib/Moose/Util/TypeConstraints/OptimizedConstraints.pm index 23a7deb..9365a8c 100644 --- a/lib/Moose/Util/TypeConstraints/OptimizedConstraints.pm +++ b/lib/Moose/Util/TypeConstraints/OptimizedConstraints.pm @@ -25,7 +25,7 @@ sub Num { !ref($_[0]) && looks_like_number($_[0]) } # and that confuses some things (like JSON.pm) sub Int { my $value = $_[0]; - defined($value) && !ref($value) && $value =~ /^-?[0-9]+$/ + defined($value) && !ref($value) && $value =~ /\A-?[0-9]+\z/ } sub ScalarRef { ref($_[0]) eq 'SCALAR' || ref($_[0]) eq 'REF' } diff --git a/t/040_type_constraints/003_util_std_type_constraints.t b/t/040_type_constraints/003_util_std_type_constraints.t index 20c0f54..2065e40 100644 --- a/t/040_type_constraints/003_util_std_type_constraints.t +++ b/t/040_type_constraints/003_util_std_type_constraints.t @@ -134,8 +134,9 @@ ok(!defined Ref(undef), '... Ref rejects anything which is not a Ref') ok(defined Int(0), '... Int accepts anything which is an Int'); ok(defined Int(100), '... Int accepts anything which is an Int'); -ok(!defined Int(0.5), '... Int accepts anything which is not an Int'); -ok(!defined Int(100.01), '... Int accepts anything which is not an Int'); +ok(defined Int(-5), '... Int accepts anything which is an Int'); +ok(!defined Int(0.5), '... Int rejects anything which is not an Int'); +ok(!defined Int(100.01), '... Int rejects anything which is not an Int'); ok(!defined Int(''), '... Int rejects anything which is not an Int'); ok(!defined Int('Foo'), '... Int rejects anything which is not an Int'); ok(!defined Int([]), '... Int rejects anything which is not an Int'); @@ -148,6 +149,8 @@ ok(!defined Int($fh), '... Int rejects anything which is not an Int ok(!defined Int(qr/../), '... Int rejects anything which is not an Int'); ok(!defined Int(bless {}, 'Foo'), '... Int rejects anything which is not an Int'); ok(!defined Int(undef), '... Int rejects anything which is not an Int'); +ok(!defined Int("1\n"), '... Int rejects anything which is not an Int'); +ok(!defined Int("\n1"), '... Int rejects anything which is not an Int'); ok(defined Num(0), '... Num accepts anything which is an Num'); ok(defined Num(100), '... Num accepts anything which is an Num');