XXX: catch invalid rangematch() input (return undef, tests)
authorMischa POSLAWSKY <perl@shiar.org>
Wed, 11 Nov 2009 15:58:39 +0000 (16:58 +0100)
committerMischa POSLAWSKY <perl@shiar.org>
Wed, 11 Nov 2009 15:58:39 +0000 (16:58 +0100)
lib/List/Index.pm
t/20-links.t

index 23ab261e92dd3198bfa41e6a17baef3e6e6d6bfc..c42db44eadb5be52310a3ec76b54df640eb06209 100644 (file)
@@ -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) {
index f8f462da980cd10ad329326a31de19d7263ef427..d9ab338c50cc7a9776424da9b049a43aa152538b 100644 (file)
@@ -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');
+}