20-jokers.t 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!perl -T
  2. use strict;
  3. use warnings;
  4. use Test::More tests => 3 * (4 + 2 + 7 + 8 + 6 + 2) * 3;
  5. use Regexp::Wildcards;
  6. sub try {
  7. my ($rw, $s, $x, $y) = @_;
  8. $y = $x unless defined $y;
  9. my $d = $rw->{do};
  10. $d = join ' ', keys %$d if ref($d) eq 'HASH';
  11. is($rw->convert('ab' . $x), 'ab' . $y, $s . " (begin) [$d]");
  12. is($rw->convert('a' . $x . 'b'), 'a' . $y . 'b', $s . " (middle) [$d]");
  13. is($rw->convert($x . 'ab'), $y . 'ab', $s . " (end) [$d]");
  14. }
  15. sub alltests {
  16. my ($d, $one, $any) = @_;
  17. my $rw = Regexp::Wildcards->new;
  18. $rw->do(set => $d);
  19. $d = join ' ', keys %$d if ref($d) eq 'HASH';
  20. # Simple
  21. try $rw, "simple $any", $any, '.*';
  22. try $rw, "simple $one", $one, '.';
  23. is($rw->convert($one.$any.'ab'), '..*ab',
  24. "simple $one and $any (begin) [$d]");
  25. is($rw->convert($one.'a'.$any.'b'), '.a.*b',
  26. "simple $one and $any (middle) [$d]");
  27. is($rw->convert($one.'ab'.$any), '.ab.*',
  28. "simple $one and $any (end) [$d]");
  29. is($rw->convert($any.'ab'.$one), '.*ab.',
  30. "simple $any and $one (begin) [$d]");
  31. is($rw->convert('a'.$any.'b'.$one), 'a.*b.',
  32. "simple $any and $one (middle) [$d]");
  33. is($rw->convert('ab'.$any.$one), 'ab.*.',
  34. "simple $any and $one (end) [$d]");
  35. # Multiple
  36. try $rw, "multiple $any", $any x 2, '.*';
  37. try $rw, "multiple $one", $one x 2, '..';
  38. # Captures
  39. $rw->capture('single');
  40. try $rw, "multiple capturing $one", $one.$one.'\\'.$one.$one,
  41. '(.)(.)\\'.$one.'(.)';
  42. $rw->capture(add => [ qw<any greedy> ]);
  43. try $rw, "multiple capturing $any (greedy)", $any.$any.'\\'.$any.$any,
  44. '(.*)\\'.$any.'(.*)';
  45. my $wc = $any.$any.$one.$one.'\\'.$one.$one.'\\'.$any.$any;
  46. try $rw, "multiple capturing $any (greedy) and capturing $one",
  47. $wc, '(.*)(.)(.)\\'.$one.'(.)\\'.$any.'(.*)';
  48. $rw->capture(set => [ qw<any greedy> ]);
  49. try $rw, "multiple capturing $any (greedy) and non-capturing $one",
  50. $wc, '(.*)..\\'.$one.'.\\'.$any.'(.*)';
  51. $rw->capture(rem => 'greedy');
  52. try $rw, "multiple capturing $any (non-greedy)", $any.$any.'\\'.$any.$any,
  53. '(.*?)\\'.$any.'(.*?)';
  54. try $rw, "multiple capturing $any (non-greedy) and non-capturing $one",
  55. $wc, '(.*?)..\\'.$one.'.\\'.$any.'(.*?)';
  56. $rw->capture({ single => 1, any => 1 });
  57. try $rw, "multiple capturing $any (non-greedy) and capturing $one",
  58. $wc, '(.*?)(.)(.)\\'.$one.'(.)\\'.$any.'(.*?)';
  59. $rw->capture();
  60. # Escaping
  61. try $rw, "escaping $any", '\\'.$any;
  62. try $rw, "escaping $any before intermediate newline", '\\'.$any ."\n\\".$any;
  63. try $rw, "escaping $one", '\\'.$one;
  64. try $rw, "escaping $one before intermediate newline", '\\'.$one ."\n\\".$one;
  65. try $rw, "escaping \\\\\\$any", '\\\\\\'.$any;
  66. try $rw, "escaping \\\\\\$one", '\\\\\\'.$one;
  67. try $rw, "not escaping \\\\$any", '\\\\'.$any, '\\\\.*';
  68. try $rw, "not escaping \\\\$one", '\\\\'.$one, '\\\\.';
  69. # Escaping escapes
  70. try $rw, 'escaping \\', '\\', '\\\\';
  71. try $rw, 'not escaping \\', '\\\\', '\\\\';
  72. try $rw, 'escaping \\ before intermediate newline', "\\\n\\", "\\\\\n\\\\";
  73. try $rw, 'not escaping \\ before intermediate newline', "\\\\\n\\\\", "\\\\\n\\\\";
  74. try $rw, 'escaping regex characters', '[]', '\\[\\]';
  75. try $rw, 'not escaping escaped regex characters', '\\\\\\[\\]';
  76. # Mixed
  77. try $rw, "mixed $any and \\$any", $any.'\\'.$any.$any, '.*\\'.$any.'.*';
  78. try $rw, "mixed $one and \\$one", $one.'\\'.$one.$one, '.\\'.$one.'.';
  79. }
  80. alltests 'jokers', '?', '*';
  81. alltests 'sql', '_', '%';
  82. alltests [ qw<jokers sql> ], '_', '*';