This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix and test for [perl #15549 Empty \Q\E not permitted]
[perl5.git] / t / op / quotemeta.t
1 #!./perl
2
3 BEGIN {
4     chdir 't' if -d 't';
5     @INC = qw(../lib .);
6     require Config; import Config;
7     require "test.pl";
8 }
9
10 plan tests => 22;
11
12 if ($Config{ebcdic} eq 'define') {
13     $_ = join "", map chr($_), 129..233;
14
15     # 105 characters - 52 letters = 53 backslashes
16     # 105 characters + 53 backslashes = 158 characters
17     $_ = quotemeta $_;
18     is(length($_), 158, "quotemeta string");
19     # 104 non-backslash characters
20     is(tr/\\//cd, 104, "tr count non-backslashed");
21 } else { # some ASCII descendant, then.
22     $_ = join "", map chr($_), 32..127;
23
24     # 96 characters - 52 letters - 10 digits - 1 underscore = 33 backslashes
25     # 96 characters + 33 backslashes = 129 characters
26     $_ = quotemeta $_;
27     is(length($_), 129, "quotemeta string");
28     # 95 non-backslash characters
29     is(tr/\\//cd, 95, "tr count non-backslashed");
30 }
31
32 is(length(quotemeta ""), 0, "quotemeta empty string");
33
34 is("aA\UbB\LcC\EdD", "aABBccdD", 'aA\UbB\LcC\EdD');
35 is("aA\LbB\UcC\EdD", "aAbbCCdD", 'aA\LbB\UcC\EdD');
36 is("\L\upERL", "Perl", '\L\upERL');
37 is("\u\LpERL", "Perl", '\u\LpERL');
38 is("\U\lPerl", "pERL", '\U\lPerl');
39 is("\l\UPerl", "pERL", '\l\UPerl');
40 is("\u\LpE\Q#X#\ER\EL", "Pe\\#x\\#rL", '\u\LpE\Q#X#\ER\EL');
41 is("\l\UPe\Q!x!\Er\El", "pE\\!X\\!Rl", '\l\UPe\Q!x!\Er\El');
42 is("\Q\u\LpE.X.R\EL\E.", "Pe\\.x\\.rL.", '\Q\u\LpE.X.R\EL\E.');
43 is("\Q\l\UPe*x*r\El\E*", "pE\\*X\\*Rl*", '\Q\l\UPe*x*r\El\E*');
44 is("\U\lPerl\E\E\E\E", "pERL", '\U\lPerl\E\E\E\E');
45 is("\l\UPerl\E\E\E\E", "pERL", '\l\UPerl\E\E\E\E');
46
47 is(quotemeta("\x{263a}"), "\x{263a}", "quotemeta Unicode");
48 is(length(quotemeta("\x{263a}")), 1, "quotemeta Unicode length");
49
50 $a = "foo|bar";
51 is("a\Q\Ec$a", "acfoo|bar", '\Q\E');
52 is("a\L\Ec$a", "acfoo|bar", '\L\E');
53 is("a\l\Ec$a", "acfoo|bar", '\l\E');
54 is("a\U\Ec$a", "acfoo|bar", '\U\E');
55 is("a\u\Ec$a", "acfoo|bar", '\u\E');