From: Mischa POSLAWSKY Date: Wed, 11 Nov 2009 15:58:39 +0000 (+0100) Subject: XXX: catch invalid rangematch() input (return undef, tests) X-Git-Url: http://git.shiar.net/perl/list-index.git/commitdiff_plain/d6c30ef4ef400982bc1b08e9b7bf93993ea29be5 XXX: catch invalid rangematch() input (return undef, tests) --- diff --git a/lib/List/Index.pm b/lib/List/Index.pm index 23ab261..c42db44 100644 --- a/lib/List/Index.pm +++ b/lib/List/Index.pm @@ -59,6 +59,10 @@ sub rangematch { my @allow; if (length $s1) { + if (length $s2) { + $s1 le $s2 or $s1 =~ /^\Q$s2/ or return undef; + } + my $prefix = ''; my $char; for my $i (0 .. length($s1) - 1) { diff --git a/t/20-links.t b/t/20-links.t index f8f462d..d9ab338 100644 --- a/t/20-links.t +++ b/t/20-links.t @@ -2,7 +2,7 @@ use strict; use warnings; -use Test::More tests => 22; +use Test::More tests => 28; use Test::NoWarnings; use Data::Dump 'pp'; @@ -11,6 +11,8 @@ BEGIN { use_ok('List::Index' => 'rangematch'); } for ( [ q => 'q'], ['#foo.!$' => '\#foo\.\!\$'], + [ '-' => ''], + [ '' => ''], [ -q => '(?:(?![q-z])|q)'], [ -qqq => '(?:(?![q-z])|q(?![q-z])|qq(?![q-z])|qqq)'], [ 'q-' => '[q-z]'], @@ -31,7 +33,15 @@ for ( ['qqq-q' => '(?:q[r-z]|qq[q-z])'], ) { my ($in, $out) = @$_; - is(eval { rangematch($in) }, "(?i-xsm:^$out)", $in); + is(eval { rangematch($in) }, "(?i-xsm:^$out)", (length $in ? $in : q{''})); diag($@) if $@; } +for my $in ( + 'qqq-qc', + 'x-q', + 'xxx-qqq', + 'xxx-q', +) { + is(eval { rangematch($in) }, undef, (length $in ? $in : q{''}) . ' failure'); +}