]> git.vpit.fr Git - perl/modules/Regexp-Wildcards.git/blob - t/10-jokers.t
Importing Regexp-Wildcards-0.04.tar.gz
[perl/modules/Regexp-Wildcards.git] / t / 10-jokers.t
1 #!perl -T
2
3 use Test::More tests => 3 * (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 . ' (beginning)');
11  ok(wc2re('a' . $x . 'b', $t) eq 'a' . $y . 'b', $s . ' (middle)');
12  ok(wc2re($x . 'ab',      $t) eq $y . 'ab',      $s . ' (end)');
13 }
14
15 for my $t (qw/unix win32 jokers/) {
16  # Simple
17
18  try $t, 'simple *', '*', '.*';
19  try $t, 'simple ?', '?', '.';
20
21  ok(wc2re('?*ab', $t) eq '..*ab', 'simple ? and * (beginning)');
22  ok(wc2re('?a*b', $t) eq '.a.*b', 'simple ? and * (middle)');
23  ok(wc2re('?ab*', $t) eq '.ab.*', 'simple ? and * (end)');
24
25  ok(wc2re('*ab?', $t) eq '.*ab.', 'simple * and ? (beginning)');
26  ok(wc2re('a*b?', $t) eq 'a.*b.', 'simple * and ? (middle)');
27  ok(wc2re('ab*?', $t) eq 'ab.*.', 'simple * and ? (end)');
28
29  # Multiple
30
31  try $t, 'multiple *', '**', '.*';
32  try $t, 'multiple ?', '??', '..';
33
34  # Variables
35
36  {
37   local $Regexp::Wildcards::CaptureSingle = 1;
38   try $t, 'multiple capturing ?', '??\\??', '(.)(.)\\?(.)';
39   local $Regexp::Wildcards::CaptureAny = 1;
40   try $t, 'multiple capturing * (greedy)', '**\\**', '(.*)\\*(.*)';
41   try $t, 'multiple capturing * (greedy) and capturing ?',
42           '**??\\??\\**', '(.*)(.)(.)\\?(.)\\*(.*)';
43   $Regexp::Wildcards::CaptureSingle = 0;
44   try $t, 'multiple capturing * (greedy) and non-capturing ?',
45           '**??\\??\\**', '(.*)..\\?.\\*(.*)';
46   $Regexp::Wildcards::CaptureAny = -1;
47   try $t, 'multiple capturing * (non-greedy)', '**\\**', '(.*?)\\*(.*?)';
48   try $t, 'multiple capturing * (non-greedy) and non-capturing ?',
49           '**??\\??\\**', '(.*?)..\\?.\\*(.*?)';
50   $Regexp::Wildcards::CaptureSingle = 1;
51   try $t, 'multiple capturing * (non-greedy) and capturing ?',
52           '**??\\??\\**', '(.*?)(.)(.)\\?(.)\\*(.*?)';
53  }
54
55  # Escaping
56
57  try $t, 'escaping *', '\\*';
58  try $t, 'escaping *', '\\?';
59  try $t, 'escaping \\\\\\*', '\\\\\\*';
60  try $t, 'escaping \\\\\\?', '\\\\\\?';
61
62  try $t, 'not escaping \\\\*', '\\\\*', '\\\\.*';
63  try $t, 'not escaping \\\\?', '\\\\?', '\\\\.';
64
65  try $t, 'escaping \\', '\\', '\\\\';
66  try $t, 'escaping regex characters', '[]', '\\[\\]';
67  try $t, 'not escaping escaped regex characters', '\\\\\\[\\]';
68
69  # Mixed
70
71  try $t, 'mixed * and \\*', '*\\**', '.*\\*.*';
72  try $t, 'mixed ? and \\?', '?\\??', '.\\?.';
73 }