]> git.vpit.fr Git - perl/modules/Regexp-Wildcards.git/blob - t/10-jokers.t
Importing Regexp-Wildcards-0.06.tar.gz
[perl/modules/Regexp-Wildcards.git] / t / 10-jokers.t
1 #!perl -T
2
3 use Test::More tests => 4 * (4 + 2 + 7 + 9 + 2) * 3;
4
5 use Regexp::Wildcards qw/wc2re/;
6
7 sub try {
8  my ($t, $s, $x, $y) = @_;
9  $y = $x unless defined $y;
10  ok(wc2re('ab' . $x,      $t) eq 'ab' . $y,      $s . ' (begin) ['.$t.']');
11  ok(wc2re('a' . $x . 'b', $t) eq 'a' . $y . 'b', $s . ' (middle) ['.$t.']');
12  ok(wc2re($x . 'ab',      $t) eq $y . 'ab',      $s . ' (end) ['.$t.']');
13 }
14
15 sub alltests {
16  my ($t, $one, $any) = @_;
17
18  # Simple
19
20  try $t, "simple $any", $any, '.*';
21  try $t, "simple $one", $one, '.';
22
23  ok(wc2re($one.$any.'ab', $t)    eq '..*ab',
24     "simple $one and $any (begin) [$t]");
25  ok(wc2re($one.'a'.$any.'b', $t) eq '.a.*b',
26     "simple $one and $any (middle) [$t]");
27  ok(wc2re($one.'ab'.$any, $t)    eq '.ab.*',
28     "simple $one and $any (end) [$t]");
29
30  ok(wc2re($any.'ab'.$one, $t)    eq '.*ab.',
31     "simple $any and $one (begin) [$t]");
32  ok(wc2re('a'.$any.'b'.$one, $t) eq 'a.*b.',
33     "simple $any and $one (middle) [$t]");
34  ok(wc2re('ab'.$any.$one, $t)    eq 'ab.*.',
35     "simple $any and $one (end) [$t]");
36
37  # Multiple
38
39  try $t, "multiple $any", $any x 2, '.*';
40  try $t, "multiple $one", $one x 2, '..';
41
42  # Variables
43
44  {
45   local $Regexp::Wildcards::CaptureSingle = 1;
46   try $t, "multiple capturing $one", $one.$one.'\\'.$one.$one,
47                                      '(.)(.)\\'.$one.'(.)';
48
49   local $Regexp::Wildcards::CaptureAny = 1;
50   try $t, "multiple capturing $any (greedy)", $any.$any.'\\'.$any.$any,
51                                               '(.*)\\'.$any.'(.*)';
52   my $wc = $any.$any.$one.$one.'\\'.$one.$one.'\\'.$any.$any;
53   try $t, "multiple capturing $any (greedy) and capturing $one",
54           $wc, '(.*)(.)(.)\\'.$one.'(.)\\'.$any.'(.*)';
55
56   $Regexp::Wildcards::CaptureSingle = 0;
57   try $t, "multiple capturing $any (greedy) and non-capturing $one",
58           $wc, '(.*)..\\'.$one.'.\\'.$any.'(.*)';
59
60   $Regexp::Wildcards::CaptureAny = -1;
61   try $t, "multiple capturing $any (non-greedy)", $any.$any.'\\'.$any.$any,
62                                                   '(.*?)\\'.$any.'(.*?)';
63   try $t, "multiple capturing $any (non-greedy) and non-capturing $one",
64           $wc, '(.*?)..\\'.$one.'.\\'.$any.'(.*?)';
65
66   $Regexp::Wildcards::CaptureSingle = 1;
67   try $t, "multiple capturing $any (non-greedy) and capturing $one",
68           $wc, '(.*?)(.)(.)\\'.$one.'(.)\\'.$any.'(.*?)';
69  }
70
71  # Escaping
72
73  try $t, "escaping $any", '\\'.$any;
74  try $t, "escaping $one", '\\'.$one;
75  try $t, "escaping \\\\\\$any", '\\\\\\'.$any;
76  try $t, "escaping \\\\\\$one", '\\\\\\'.$one;
77
78  try $t, "not escaping \\\\$any", '\\\\'.$any, '\\\\.*';
79  try $t, "not escaping \\\\$one", '\\\\'.$one, '\\\\.';
80
81  try $t, 'escaping \\', '\\', '\\\\';
82  try $t, 'escaping regex characters', '[]', '\\[\\]';
83  try $t, 'not escaping escaped regex characters', '\\\\\\[\\]';
84
85  # Mixed
86
87  try $t, "mixed $any and \\$any", $any.'\\'.$any.$any, '.*\\'.$any.'.*';
88  try $t, "mixed $one and \\$one", $one.'\\'.$one.$one, '.\\'.$one.'.';
89 }
90
91 alltests $_,    '?', '*' for qw/jokers unix win32/;
92 alltests 'sql', '_', '%';