This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Get t/uni/cache.t working under minitest
[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   SKIP: {
80     skip 'No locale testing without d_setlocale', 8 if(!$Config{d_setlocale});
81     use locale;
82
83     my $char = ":";
84     is(quotemeta($char), "\\$char", "quotemeta '$char' locale");
85     is(length(quotemeta($char)), 2, "quotemeta '$char' locale");
86
87     $char = "M";
88     utf8::upgrade($char);
89     is(quotemeta($char), "$char", "quotemeta '$char' locale");
90     is(length(quotemeta($char)), 1, "quotemeta '$char' locale");
91
92     my $char = "\x{D7}";
93     is(quotemeta($char), "\\$char", "quotemeta '\\x{D7}' locale");
94     is(length(quotemeta($char)), 2, "quotemeta '\\x{D7}' locale length");
95
96     $char = "\x{D8}";  # Every non-ASCII Latin1 is quoted in locale.
97     is(quotemeta($char), "\\$char", "quotemeta '\\x{D8}' locale");
98     is(length(quotemeta($char)), 2, "quotemeta '\\x{D8}' locale length");
99     }
100 }
101 {
102     use feature 'unicode_strings';
103     is(quotemeta("\x{d7}"), "\\\x{d7}", "quotemeta Latin1 unicode_strings quoted");
104     is(length(quotemeta("\x{d7}")), 2, "quotemeta Latin1 unicode_strings quoted length");
105     is(quotemeta("\x{d8}"), "\x{d8}", "quotemeta Latin1 unicode_strings nonquoted");
106     is(length(quotemeta("\x{d8}")), 1, "quotemeta Latin1 unicode_strings nonquoted length");
107
108   SKIP: {
109     skip 'No locale testing without d_setlocale', 12 if(!$Config{d_setlocale});
110     use locale;
111
112     my $char = ":";
113     utf8::upgrade($char);
114     is(quotemeta($char), "\\$char", "quotemeta '$char' locale in UTF-8");
115     is(length(quotemeta($char)), 2, "quotemeta '$char' locale  in UTF-8 length");
116
117     $char = "M";
118     utf8::upgrade($char);
119     is(quotemeta($char), "$char", "quotemeta '$char' locale in UTF-8");
120     is(length(quotemeta($char)), 1, "quotemeta '$char' locale in UTF-8 length");
121
122     my $char = "\N{U+D7}";
123     utf8::upgrade($char);
124     is(quotemeta($char), "\\$char", "quotemeta '\\N{U+D7}' locale in UTF-8");
125     is(length(quotemeta($char)), 2, "quotemeta '\\N{U+D7}' locale in UTF-8 length");
126
127     $char = "\N{U+D8}";  # Every non-ASCII Latin1 is quoted in locale.
128     utf8::upgrade($char);
129     is(quotemeta($char), "\\$char", "quotemeta '\\N{U+D8}' locale in UTF-8");
130     is(length(quotemeta($char)), 2, "quotemeta '\\N{U+D8}' locale in UTF-8 length");
131
132     is(quotemeta("\x{263a}"), "\\\x{263a}", "quotemeta locale Unicode quoted");
133     is(length(quotemeta("\x{263a}")), 2, "quotemeta locale Unicode quoted length");
134     is(quotemeta("\x{100}"), "\x{100}", "quotemeta locale Unicode nonquoted");
135     is(length(quotemeta("\x{100}")), 1, "quotemeta locale Unicode nonquoted length");
136   }
137 }
138
139 $a = "foo|bar";
140 is("a\Q\Ec$a", "acfoo|bar", '\Q\E');
141 is("a\L\Ec$a", "acfoo|bar", '\L\E');
142 is("a\l\Ec$a", "acfoo|bar", '\l\E');
143 is("a\U\Ec$a", "acfoo|bar", '\U\E');
144 is("a\u\Ec$a", "acfoo|bar", '\u\E');