X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;f=t%2F10-jokers.t;h=27a041a8b7186120a376627e231ccb3fa6804c46;hb=5468cc9fedae639e785117ab38807d857c84bdec;hp=67a1fd8ac2b206f6d89039ea02c564e461857d5e;hpb=d3841a7816c3e170f292ced4a5818ab252574300;p=perl%2Fmodules%2FRegexp-Wildcards.git diff --git a/t/10-jokers.t b/t/10-jokers.t index 67a1fd8..27a041a 100644 --- a/t/10-jokers.t +++ b/t/10-jokers.t @@ -1,45 +1,85 @@ #!perl -T -use Test::More tests => 3 * (4 + 2 + 9 + 2) * 3; +use strict; +use warnings; + +use Test::More tests => 4 * (4 + 2 + 7 + 9 + 2) * 3; use Regexp::Wildcards qw/wc2re/; sub try { my ($t, $s, $x, $y) = @_; $y = $x unless defined $y; - ok(wc2re('ab' . $x, $t) eq 'ab' . $y, $s . ' (beginning)'); - ok(wc2re('a' . $x . 'b', $t) eq 'a' . $y . 'b', $s . ' (middle)'); - ok(wc2re($x . 'ab', $t) eq $y . 'ab', $s . ' (end)'); + ok(wc2re('ab' . $x, $t) eq 'ab' . $y, $s . ' (begin) ['.$t.']'); + ok(wc2re('a' . $x . 'b', $t) eq 'a' . $y . 'b', $s . ' (middle) ['.$t.']'); + ok(wc2re($x . 'ab', $t) eq $y . 'ab', $s . ' (end) ['.$t.']'); } -for my $t (qw/unix win32 jokers/) { +sub alltests { + my ($t, $one, $any) = @_; + # Simple - try $t, 'simple *', '*', '.*'; - try $t, 'simple ?', '?', '.'; + try $t, "simple $any", $any, '.*'; + try $t, "simple $one", $one, '.'; - ok(wc2re('?*ab', $t) eq '..*ab', 'simple ? and * (beginning)'); - ok(wc2re('?a*b', $t) eq '.a.*b', 'simple ? and * (middle)'); - ok(wc2re('?ab*', $t) eq '.ab.*', 'simple ? and * (end)'); + ok(wc2re($one.$any.'ab', $t) eq '..*ab', + "simple $one and $any (begin) [$t]"); + ok(wc2re($one.'a'.$any.'b', $t) eq '.a.*b', + "simple $one and $any (middle) [$t]"); + ok(wc2re($one.'ab'.$any, $t) eq '.ab.*', + "simple $one and $any (end) [$t]"); - ok(wc2re('*ab?', $t) eq '.*ab.', 'simple * and ? (beginning)'); - ok(wc2re('a*b?', $t) eq 'a.*b.', 'simple * and ? (middle)'); - ok(wc2re('ab*?', $t) eq 'ab.*.', 'simple * and ? (end)'); + ok(wc2re($any.'ab'.$one, $t) eq '.*ab.', + "simple $any and $one (begin) [$t]"); + ok(wc2re('a'.$any.'b'.$one, $t) eq 'a.*b.', + "simple $any and $one (middle) [$t]"); + ok(wc2re('ab'.$any.$one, $t) eq 'ab.*.', + "simple $any and $one (end) [$t]"); # Multiple - try $t, 'multiple *', '**', '.*'; - try $t, 'multiple ?', '??', '..'; + try $t, "multiple $any", $any x 2, '.*'; + try $t, "multiple $one", $one x 2, '..'; + + # Variables + + { + local $Regexp::Wildcards::CaptureSingle = 1; + try $t, "multiple capturing $one", $one.$one.'\\'.$one.$one, + '(.)(.)\\'.$one.'(.)'; + + local $Regexp::Wildcards::CaptureAny = 1; + try $t, "multiple capturing $any (greedy)", $any.$any.'\\'.$any.$any, + '(.*)\\'.$any.'(.*)'; + my $wc = $any.$any.$one.$one.'\\'.$one.$one.'\\'.$any.$any; + try $t, "multiple capturing $any (greedy) and capturing $one", + $wc, '(.*)(.)(.)\\'.$one.'(.)\\'.$any.'(.*)'; + + $Regexp::Wildcards::CaptureSingle = 0; + try $t, "multiple capturing $any (greedy) and non-capturing $one", + $wc, '(.*)..\\'.$one.'.\\'.$any.'(.*)'; + + $Regexp::Wildcards::CaptureAny = -1; + try $t, "multiple capturing $any (non-greedy)", $any.$any.'\\'.$any.$any, + '(.*?)\\'.$any.'(.*?)'; + try $t, "multiple capturing $any (non-greedy) and non-capturing $one", + $wc, '(.*?)..\\'.$one.'.\\'.$any.'(.*?)'; + + $Regexp::Wildcards::CaptureSingle = 1; + try $t, "multiple capturing $any (non-greedy) and capturing $one", + $wc, '(.*?)(.)(.)\\'.$one.'(.)\\'.$any.'(.*?)'; + } # Escaping - try $t, 'escaping *', '\\*'; - try $t, 'escaping *', '\\?'; - try $t, 'escaping \\\\\\*', '\\\\\\*'; - try $t, 'escaping \\\\\\?', '\\\\\\?'; + try $t, "escaping $any", '\\'.$any; + try $t, "escaping $one", '\\'.$one; + try $t, "escaping \\\\\\$any", '\\\\\\'.$any; + try $t, "escaping \\\\\\$one", '\\\\\\'.$one; - try $t, 'not escaping \\\\*', '\\\\*', '\\\\.*'; - try $t, 'not escaping \\\\?', '\\\\?', '\\\\.'; + try $t, "not escaping \\\\$any", '\\\\'.$any, '\\\\.*'; + try $t, "not escaping \\\\$one", '\\\\'.$one, '\\\\.'; try $t, 'escaping \\', '\\', '\\\\'; try $t, 'escaping regex characters', '[]', '\\[\\]'; @@ -47,6 +87,9 @@ for my $t (qw/unix win32 jokers/) { # Mixed - try $t, 'mixed * and \\*', '*\\**', '.*\\*.*'; - try $t, 'mixed ? and \\?', '?\\??', '.\\?.'; + try $t, "mixed $any and \\$any", $any.'\\'.$any.$any, '.*\\'.$any.'.*'; + try $t, "mixed $one and \\$one", $one.'\\'.$one.$one, '.\\'.$one.'.'; } + +alltests $_, '?', '*' for qw/jokers unix win32/; +alltests 'sql', '_', '%';