This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
skip failing leak test under -Dmad
[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 => 60;
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 quoted");
48 is(length(quotemeta("\x{263a}")), 2, "quotemeta Unicode quoted length");
49 is(quotemeta("\x{100}"), "\x{100}", "quotemeta Unicode nonquoted");
50 is(length(quotemeta("\x{100}")), 1, "quotemeta Unicode nonquoted length");
51
52 my $char = ":";
53 utf8::upgrade($char);
54 is(quotemeta($char), "\\$char", "quotemeta '$char' in UTF-8");
55 is(length(quotemeta($char)), 2, "quotemeta '$char'  in UTF-8 length");
56
57 $char = "M";
58 utf8::upgrade($char);
59 is(quotemeta($char), "$char", "quotemeta '$char' in UTF-8");
60 is(length(quotemeta($char)), 1, "quotemeta '$char'  in UTF-8 length");
61
62 my $char = "\N{U+D7}";
63 utf8::upgrade($char);
64 is(quotemeta($char), "\\$char", "quotemeta '\\N{U+D7}' in UTF-8");
65 is(length(quotemeta($char)), 2, "quotemeta '\\N{U+D7}'  in UTF-8 length");
66
67 $char = "\N{U+D8}";
68 utf8::upgrade($char);
69 is(quotemeta($char), "$char", "quotemeta '\\N{U+D8}' in UTF-8");
70 is(length(quotemeta($char)), 1, "quotemeta '\\N{U+D8}'  in UTF-8 length");
71
72 {
73     no feature 'unicode_strings';
74     is(quotemeta("\x{d7}"), "\\\x{d7}", "quotemeta Latin1 no unicode_strings quoted");
75     is(length(quotemeta("\x{d7}")), 2, "quotemeta Latin1 no unicode_strings quoted length");
76     is(quotemeta("\x{d8}"), "\\\x{d8}", "quotemeta Latin1 no unicode_strings quoted");
77     is(length(quotemeta("\x{d8}")), 2, "quotemeta Latin1 no unicode_strings quoted length");
78
79     use locale;
80
81     my $char = ":";
82     is(quotemeta($char), "\\$char", "quotemeta '$char' locale");
83     is(length(quotemeta($char)), 2, "quotemeta '$char' locale");
84
85     $char = "M";
86     utf8::upgrade($char);
87     is(quotemeta($char), "$char", "quotemeta '$char' locale");
88     is(length(quotemeta($char)), 1, "quotemeta '$char' locale");
89
90     my $char = "\x{D7}";
91     is(quotemeta($char), "\\$char", "quotemeta '\\x{D7}' locale");
92     is(length(quotemeta($char)), 2, "quotemeta '\\x{D7}' locale length");
93
94     $char = "\x{D8}";  # Every non-ASCII Latin1 is quoted in locale.
95     is(quotemeta($char), "\\$char", "quotemeta '\\x{D8}' locale");
96     is(length(quotemeta($char)), 2, "quotemeta '\\x{D8}' locale length");
97 }
98 {
99     use feature 'unicode_strings';
100     is(quotemeta("\x{d7}"), "\\\x{d7}", "quotemeta Latin1 unicode_strings quoted");
101     is(length(quotemeta("\x{d7}")), 2, "quotemeta Latin1 unicode_strings quoted length");
102     is(quotemeta("\x{d8}"), "\x{d8}", "quotemeta Latin1 unicode_strings nonquoted");
103     is(length(quotemeta("\x{d8}")), 1, "quotemeta Latin1 unicode_strings nonquoted length");
104
105     use locale;
106
107     my $char = ":";
108     utf8::upgrade($char);
109     is(quotemeta($char), "\\$char", "quotemeta '$char' locale in UTF-8");
110     is(length(quotemeta($char)), 2, "quotemeta '$char' locale  in UTF-8 length");
111
112     $char = "M";
113     utf8::upgrade($char);
114     is(quotemeta($char), "$char", "quotemeta '$char' locale in UTF-8");
115     is(length(quotemeta($char)), 1, "quotemeta '$char' locale in UTF-8 length");
116
117     my $char = "\N{U+D7}";
118     utf8::upgrade($char);
119     is(quotemeta($char), "\\$char", "quotemeta '\\N{U+D7}' locale in UTF-8");
120     is(length(quotemeta($char)), 2, "quotemeta '\\N{U+D7}' locale in UTF-8 length");
121
122     $char = "\N{U+D8}";  # Every non-ASCII Latin1 is quoted in locale.
123     utf8::upgrade($char);
124     is(quotemeta($char), "\\$char", "quotemeta '\\N{U+D8}' locale in UTF-8");
125     is(length(quotemeta($char)), 2, "quotemeta '\\N{U+D8}' locale in UTF-8 length");
126
127     is(quotemeta("\x{263a}"), "\\\x{263a}", "quotemeta locale Unicode quoted");
128     is(length(quotemeta("\x{263a}")), 2, "quotemeta locale Unicode quoted length");
129     is(quotemeta("\x{100}"), "\x{100}", "quotemeta locale Unicode nonquoted");
130     is(length(quotemeta("\x{100}")), 1, "quotemeta locale Unicode nonquoted length");
131 }
132
133 $a = "foo|bar";
134 is("a\Q\Ec$a", "acfoo|bar", '\Q\E');
135 is("a\L\Ec$a", "acfoo|bar", '\L\E');
136 is("a\l\Ec$a", "acfoo|bar", '\l\E');
137 is("a\U\Ec$a", "acfoo|bar", '\U\E');
138 is("a\u\Ec$a", "acfoo|bar", '\u\E');