1323b2d00472e42aebf6f6ce13b1daf708329cb5
[p5sagit/p5-mst-13.2.git] / t / op / subst.t
1 #!./perl
2
3
4 BEGIN {
5     chdir 't' if -d 't';
6     @INC = '../lib' if -d '../lib';
7 }
8
9 print "1..70\n";
10
11 $x = 'foo';
12 $_ = "x";
13 s/x/\$x/;
14 print "#1\t:$_: eq :\$x:\n";
15 if ($_ eq '$x') {print "ok 1\n";} else {print "not ok 1\n";}
16
17 $_ = "x";
18 s/x/$x/;
19 print "#2\t:$_: eq :foo:\n";
20 if ($_ eq 'foo') {print "ok 2\n";} else {print "not ok 2\n";}
21
22 $_ = "x";
23 s/x/\$x $x/;
24 print "#3\t:$_: eq :\$x foo:\n";
25 if ($_ eq '$x foo') {print "ok 3\n";} else {print "not ok 3\n";}
26
27 $b = 'cd';
28 ($a = 'abcdef') =~ s<(b${b}e)>'\n$1';
29 print "#4\t:$1: eq :bcde:\n";
30 print "#4\t:$a: eq :a\\n\$1f:\n";
31 if ($1 eq 'bcde' && $a eq 'a\n$1f') {print "ok 4\n";} else {print "not ok 4\n";}
32
33 $a = 'abacada';
34 if (($a =~ s/a/x/g) == 4 && $a eq 'xbxcxdx')
35     {print "ok 5\n";} else {print "not ok 5\n";}
36
37 if (($a =~ s/a/y/g) == 0 && $a eq 'xbxcxdx')
38     {print "ok 6\n";} else {print "not ok 6 $a\n";}
39
40 if (($a =~ s/b/y/g) == 1 && $a eq 'xyxcxdx')
41     {print "ok 7\n";} else {print "not ok 7 $a\n";}
42
43 $_ = 'ABACADA';
44 if (/a/i && s///gi && $_ eq 'BCD') {print "ok 8\n";} else {print "not ok 8 $_\n";}
45
46 $_ = '\\' x 4;
47 if (length($_) == 4) {print "ok 9\n";} else {print "not ok 9\n";}
48 s/\\/\\\\/g;
49 if ($_ eq '\\' x 8) {print "ok 10\n";} else {print "not ok 10 $_\n";}
50
51 $_ = '\/' x 4;
52 if (length($_) == 8) {print "ok 11\n";} else {print "not ok 11\n";}
53 s/\//\/\//g;
54 if ($_ eq '\\//' x 4) {print "ok 12\n";} else {print "not ok 12\n";}
55 if (length($_) == 12) {print "ok 13\n";} else {print "not ok 13\n";}
56
57 $_ = 'aaaXXXXbbb';
58 s/^a//;
59 print $_ eq 'aaXXXXbbb' ? "ok 14\n" : "not ok 14\n";
60
61 $_ = 'aaaXXXXbbb';
62 s/a//;
63 print $_ eq 'aaXXXXbbb' ? "ok 15\n" : "not ok 15\n";
64
65 $_ = 'aaaXXXXbbb';
66 s/^a/b/;
67 print $_ eq 'baaXXXXbbb' ? "ok 16\n" : "not ok 16\n";
68
69 $_ = 'aaaXXXXbbb';
70 s/a/b/;
71 print $_ eq 'baaXXXXbbb' ? "ok 17\n" : "not ok 17\n";
72
73 $_ = 'aaaXXXXbbb';
74 s/aa//;
75 print $_ eq 'aXXXXbbb' ? "ok 18\n" : "not ok 18\n";
76
77 $_ = 'aaaXXXXbbb';
78 s/aa/b/;
79 print $_ eq 'baXXXXbbb' ? "ok 19\n" : "not ok 19\n";
80
81 $_ = 'aaaXXXXbbb';
82 s/b$//;
83 print $_ eq 'aaaXXXXbb' ? "ok 20\n" : "not ok 20\n";
84
85 $_ = 'aaaXXXXbbb';
86 s/b//;
87 print $_ eq 'aaaXXXXbb' ? "ok 21\n" : "not ok 21\n";
88
89 $_ = 'aaaXXXXbbb';
90 s/bb//;
91 print $_ eq 'aaaXXXXb' ? "ok 22\n" : "not ok 22\n";
92
93 $_ = 'aaaXXXXbbb';
94 s/aX/y/;
95 print $_ eq 'aayXXXbbb' ? "ok 23\n" : "not ok 23\n";
96
97 $_ = 'aaaXXXXbbb';
98 s/Xb/z/;
99 print $_ eq 'aaaXXXzbb' ? "ok 24\n" : "not ok 24\n";
100
101 $_ = 'aaaXXXXbbb';
102 s/aaX.*Xbb//;
103 print $_ eq 'ab' ? "ok 25\n" : "not ok 25\n";
104
105 $_ = 'aaaXXXXbbb';
106 s/bb/x/;
107 print $_ eq 'aaaXXXXxb' ? "ok 26\n" : "not ok 26\n";
108
109 # now for some unoptimized versions of the same.
110
111 $_ = 'aaaXXXXbbb';
112 $x ne $x || s/^a//;
113 print $_ eq 'aaXXXXbbb' ? "ok 27\n" : "not ok 27\n";
114
115 $_ = 'aaaXXXXbbb';
116 $x ne $x || s/a//;
117 print $_ eq 'aaXXXXbbb' ? "ok 28\n" : "not ok 28\n";
118
119 $_ = 'aaaXXXXbbb';
120 $x ne $x || s/^a/b/;
121 print $_ eq 'baaXXXXbbb' ? "ok 29\n" : "not ok 29\n";
122
123 $_ = 'aaaXXXXbbb';
124 $x ne $x || s/a/b/;
125 print $_ eq 'baaXXXXbbb' ? "ok 30\n" : "not ok 30\n";
126
127 $_ = 'aaaXXXXbbb';
128 $x ne $x || s/aa//;
129 print $_ eq 'aXXXXbbb' ? "ok 31\n" : "not ok 31\n";
130
131 $_ = 'aaaXXXXbbb';
132 $x ne $x || s/aa/b/;
133 print $_ eq 'baXXXXbbb' ? "ok 32\n" : "not ok 32\n";
134
135 $_ = 'aaaXXXXbbb';
136 $x ne $x || s/b$//;
137 print $_ eq 'aaaXXXXbb' ? "ok 33\n" : "not ok 33\n";
138
139 $_ = 'aaaXXXXbbb';
140 $x ne $x || s/b//;
141 print $_ eq 'aaaXXXXbb' ? "ok 34\n" : "not ok 34\n";
142
143 $_ = 'aaaXXXXbbb';
144 $x ne $x || s/bb//;
145 print $_ eq 'aaaXXXXb' ? "ok 35\n" : "not ok 35\n";
146
147 $_ = 'aaaXXXXbbb';
148 $x ne $x || s/aX/y/;
149 print $_ eq 'aayXXXbbb' ? "ok 36\n" : "not ok 36\n";
150
151 $_ = 'aaaXXXXbbb';
152 $x ne $x || s/Xb/z/;
153 print $_ eq 'aaaXXXzbb' ? "ok 37\n" : "not ok 37\n";
154
155 $_ = 'aaaXXXXbbb';
156 $x ne $x || s/aaX.*Xbb//;
157 print $_ eq 'ab' ? "ok 38\n" : "not ok 38\n";
158
159 $_ = 'aaaXXXXbbb';
160 $x ne $x || s/bb/x/;
161 print $_ eq 'aaaXXXXxb' ? "ok 39\n" : "not ok 39\n";
162
163 $_ = 'abc123xyz';
164 s/(\d+)/$1*2/e;              # yields 'abc246xyz'
165 print $_ eq 'abc246xyz' ? "ok 40\n" : "not ok 40\n";
166 s/(\d+)/sprintf("%5d",$1)/e; # yields 'abc  246xyz'
167 print $_ eq 'abc  246xyz' ? "ok 41\n" : "not ok 41\n";
168 s/(\w)/$1 x 2/eg;            # yields 'aabbcc  224466xxyyzz'
169 print $_ eq 'aabbcc  224466xxyyzz' ? "ok 42\n" : "not ok 42\n";
170
171 $_ = "aaaaa";
172 print y/a/b/ == 5 ? "ok 43\n" : "not ok 43\n";
173 print y/a/b/ == 0 ? "ok 44\n" : "not ok 44\n";
174 print y/b// == 5 ? "ok 45\n" : "not ok 45\n";
175 print y/b/c/s == 5 ? "ok 46\n" : "not ok 46\n";
176 print y/c// == 1 ? "ok 47\n" : "not ok 47\n";
177 print y/c//d == 1 ? "ok 48\n" : "not ok 48\n";
178 print $_ eq "" ? "ok 49\n" : "not ok 49\n";
179
180 $_ = "Now is the %#*! time for all good men...";
181 print (($x=(y/a-zA-Z //cd)) == 7 ? "ok 50\n" : "not ok 50\n");
182 print y/ / /s == 8 ? "ok 51\n" : "not ok 51\n";
183
184 $_ = 'abcdefghijklmnopqrstuvwxyz0123456789';
185 tr/a-z/A-Z/;
186
187 print $_ eq 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' ? "ok 52\n" : "not ok 52\n";
188
189 # same as tr/A-Z/a-z/;
190 y[\101-\132][\141-\172];
191
192 print $_ eq 'abcdefghijklmnopqrstuvwxyz0123456789' ? "ok 53\n" : "not ok 53\n";
193
194 $_ = '+,-';
195 tr/+--/a-c/;
196 print $_ eq 'abc' ? "ok 54\n" : "not ok 54\n";
197
198 $_ = '+,-';
199 tr/+\--/a\/c/;
200 print $_ eq 'a,/' ? "ok 55\n" : "not ok 55\n";
201
202 $_ = '+,-';
203 tr/-+,/ab\-/;
204 print $_ eq 'b-a' ? "ok 56\n" : "not ok 56\n";
205
206
207 # test recursive substitutions
208 # code based on the recursive expansion of makefile variables
209
210 my %MK = (
211     AAAAA => '$(B)', B=>'$(C)', C => 'D',                       # long->short
212     E     => '$(F)', F=>'p $(G) q', G => 'HHHHH',       # short->long
213     DIR => '$(UNDEFINEDNAME)/xxx',
214 );
215 sub var { 
216     my($var,$level) = @_;
217     return "\$($var)" unless exists $MK{$var};
218     return exp_vars($MK{$var}, $level+1); # can recurse
219 }
220 sub exp_vars { 
221     my($str,$level) = @_;
222     $str =~ s/\$\((\w+)\)/var($1, $level+1)/ge; # can recurse
223     #warn "exp_vars $level = '$str'\n";
224     $str;
225 }
226
227 print exp_vars('$(AAAAA)',0)           eq 'D'
228         ? "ok 57\n" : "not ok 57\n";
229 print exp_vars('$(E)',0)               eq 'p HHHHH q'
230         ? "ok 58\n" : "not ok 58\n";
231 print exp_vars('$(DIR)',0)             eq '$(UNDEFINEDNAME)/xxx'
232         ? "ok 59\n" : "not ok 59\n";
233 print exp_vars('foo $(DIR)/yyy bar',0) eq 'foo $(UNDEFINEDNAME)/xxx/yyy bar'
234         ? "ok 60\n" : "not ok 60\n";
235
236 # a match nested in the RHS of a substitution:
237
238 $_ = "abcd";
239 s/(..)/$x = $1, m#.#/eg;
240 print $x eq "cd" ? "ok 61\n" : "not ok 61\n";
241
242 # Subst and lookbehind
243
244 $_="ccccc";
245 s/(?<!x)c/x/g;
246 print $_ eq "xxxxx" ? "ok 62\n" : "not ok 62 # `$_' ne `xxxxx'\n";
247
248 $_="ccccc";
249 s/(?<!x)(c)/x/g;
250 print $_ eq "xxxxx" ? "ok 63\n" : "not ok 63 # `$_' ne `xxxxx'\n";
251
252 $_="foobbarfoobbar";
253 s/(?<!r)foobbar/foobar/g;
254 print $_ eq "foobarfoobbar" ? "ok 64\n" : "not ok 64 # `$_' ne `foobarfoobbar'\n";
255
256 $_="foobbarfoobbar";
257 s/(?<!ar)(foobbar)/foobar/g;
258 print $_ eq "foobarfoobbar" ? "ok 65\n" : "not ok 65 # `$_' ne `foobarfoobbar'\n";
259
260 $_="foobbarfoobbar";
261 s/(?<!ar)foobbar/foobar/g;
262 print $_ eq "foobarfoobbar" ? "ok 66\n" : "not ok 66 # `$_' ne `foobarfoobbar'\n";
263
264 # check parsing of split subst with comment
265 eval 's{foo} # this is a comment, not a delimiter
266        {bar};';
267 print @? ? "not ok 67\n" : "ok 67\n";
268
269 # check if squashing works at the end of string
270 $_="baacbaa";
271 tr/a/b/s;
272 print $_ eq "bbcbb" ? "ok 68\n" : "not ok 68 # `$_' ne `bbcbb'\n";
273
274 # XXX TODO: Most tests above don't test return values of the ops. They should.
275 $_ = "ab";
276 print (s/a/b/ == 1 ? "ok 69\n" : "not ok 69\n");
277
278 $_ = <<'EOL';
279      $url = new URI::URL "http://www/";   die if $url eq "xXx";
280 EOL
281 $^R = 'junk';
282
283 use re 'eval';
284 $foo = ' $@%#lowercase $@%# lowercase UPPERCASE$@%#UPPERCASE' .
285   ' $@%#lowercase$@%#lowercase$@%# lowercase lowercase $@%#lowercase' .
286   ' lowercase $@%#MiXeD$@%# ';
287
288 s{  \d+          \b [,.;]? (?{ 'digits' })
289    |
290     [a-z]+       \b [,.;]? (?{ 'lowercase' })
291    |
292     [A-Z]+       \b [,.;]? (?{ 'UPPERCASE' })
293    |
294     [A-Z] [a-z]+ \b [,.;]? (?{ 'Capitalized' })
295    |
296     [A-Za-z]+    \b [,.;]? (?{ 'MiXeD' })
297    |
298     [A-Za-z0-9]+ \b [,.;]? (?{ 'alphanumeric' })
299    |
300     \s+                    (?{ ' ' })
301    |
302     [^A-Za-z0-9\s]+          (?{ '$@%#' })
303 }{$^R}xg;
304 print ($_ eq $foo ? "ok 70\n" : "not ok 70\n#'$_'\n#'$foo'\n");
305