Commit | Line | Data |
---|---|---|
69026470 | 1 | # |
f69d9fdf KW |
2 | # t/test.pl - most of Test::More functionality without the fuss, plus |
3 | # has mappings native_to_latin1 and latin1_to_native so that fewer tests | |
4 | # on non ASCII-ish platforms need to be skipped | |
485f531e DL |
5 | |
6 | ||
7 | # NOTE: | |
8 | # | |
9 | # Increment ($x++) has a certain amount of cleverness for things like | |
10 | # | |
11 | # $x = 'zz'; | |
12 | # $x++; # $x eq 'aaa'; | |
69026470 | 13 | # |
485f531e DL |
14 | # stands more chance of breaking than just a simple |
15 | # | |
16 | # $x = $x + 1 | |
17 | # | |
18 | # In this file, we use the latter "Baby Perl" approach, and increment | |
19 | # will be worked over by t/op/inc.t | |
69026470 | 20 | |
dcc7f481 | 21 | $Level = 1; |
69026470 JH |
22 | my $test = 1; |
23 | my $planned; | |
6137113d | 24 | my $noplan; |
5fe9b82b | 25 | my $Perl; # Safer version of $^X set by which_perl() |
69026470 | 26 | |
ef237063 NC |
27 | # This defines ASCII/UTF-8 vs EBCDIC/UTF-EBCDIC |
28 | $::IS_ASCII = ord 'A' == 65; | |
29 | $::IS_EBCDIC = ord 'A' == 193; | |
30 | ||
7d932aad | 31 | $TODO = 0; |
b6345914 | 32 | $NO_ENDING = 0; |
02455492 | 33 | $Tests_Are_Passing = 1; |
7d932aad | 34 | |
3d66076a MS |
35 | # Use this instead of print to avoid interference while testing globals. |
36 | sub _print { | |
37 | local($\, $", $,) = (undef, ' ', ''); | |
38 | print STDOUT @_; | |
39 | } | |
40 | ||
41 | sub _print_stderr { | |
42 | local($\, $", $,) = (undef, ' ', ''); | |
43 | print STDERR @_; | |
44 | } | |
45 | ||
69026470 JH |
46 | sub plan { |
47 | my $n; | |
48 | if (@_ == 1) { | |
49 | $n = shift; | |
6137113d NC |
50 | if ($n eq 'no_plan') { |
51 | undef $n; | |
52 | $noplan = 1; | |
53 | } | |
69026470 JH |
54 | } else { |
55 | my %plan = @_; | |
8210c8d3 | 56 | $n = $plan{tests}; |
69026470 | 57 | } |
3d66076a | 58 | _print "1..$n\n" unless $noplan; |
69026470 JH |
59 | $planned = $n; |
60 | } | |
61 | ||
c4ef7183 MS |
62 | |
63 | # Set the plan at the end. See Test::More::done_testing. | |
64 | sub done_testing { | |
65 | my $n = $test - 1; | |
66 | $n = shift if @_; | |
67 | ||
68 | _print "1..$n\n"; | |
69 | $planned = $n; | |
70 | } | |
71 | ||
72 | ||
69026470 JH |
73 | END { |
74 | my $ran = $test - 1; | |
6137113d NC |
75 | if (!$NO_ENDING) { |
76 | if (defined $planned && $planned != $ran) { | |
3d66076a | 77 | _print_stderr |
6137113d NC |
78 | "# Looks like you planned $planned tests but ran $ran.\n"; |
79 | } elsif ($noplan) { | |
3d66076a | 80 | _print "1..$ran\n"; |
6137113d | 81 | } |
69026470 JH |
82 | } |
83 | } | |
84 | ||
de522f7a | 85 | sub _diag { |
cf8feb78 | 86 | return unless @_; |
92c9394b | 87 | my @mess = _comment(@_); |
44826442 | 88 | $TODO ? _print(@mess) : _print_stderr(@mess); |
de522f7a MS |
89 | } |
90 | ||
93f09d7b | 91 | # Use this instead of "print STDERR" when outputting failure diagnostic |
92c9394b | 92 | # messages |
485f531e DL |
93 | sub diag { |
94 | _diag(@_); | |
95 | } | |
96 | ||
93f09d7b | 97 | # Use this instead of "print" when outputting informational messages |
92c9394b MS |
98 | sub note { |
99 | return unless @_; | |
100 | _print( _comment(@_) ); | |
101 | } | |
102 | ||
445876fa KW |
103 | sub is_miniperl { |
104 | return !defined &DynaLoader::boot_DynaLoader; | |
105 | } | |
106 | ||
92c9394b MS |
107 | sub _comment { |
108 | return map { /^#/ ? "$_\n" : "# $_\n" } | |
109 | map { split /\n/ } @_; | |
110 | } | |
111 | ||
69026470 JH |
112 | sub skip_all { |
113 | if (@_) { | |
7bb7fa38 | 114 | _print "1..0 # Skip @_\n"; |
69026470 | 115 | } else { |
3d66076a | 116 | _print "1..0\n"; |
69026470 JH |
117 | } |
118 | exit(0); | |
119 | } | |
120 | ||
c82d0e1e | 121 | sub skip_all_if_miniperl { |
445876fa | 122 | skip_all(@_) if is_miniperl(); |
c82d0e1e NC |
123 | } |
124 | ||
273be65c | 125 | sub skip_all_without_dynamic_extension { |
7465bc32 | 126 | my $extension = shift; |
273be65c | 127 | skip_all("no dynamic loading on miniperl, no $extension") if is_miniperl(); |
cb01154c | 128 | unless (eval {require Config; 1}) { |
7465bc32 NC |
129 | warn "test.pl had problems loading Config: $@"; |
130 | return; | |
131 | } | |
2929db82 | 132 | $extension =~ s!::!/!g; |
7465bc32 NC |
133 | return if ($Config::Config{extensions} =~ /\b$extension\b/); |
134 | skip_all("$extension was not built"); | |
135 | } | |
136 | ||
e05e9c3d NC |
137 | sub skip_all_without_perlio { |
138 | skip_all('no PerlIO') unless PerlIO::Layer->find('perlio'); | |
139 | } | |
140 | ||
9c8416b2 | 141 | sub skip_all_without_config { |
cb01154c | 142 | unless (eval {require Config; 1}) { |
9c8416b2 NC |
143 | warn "test.pl had problems loading Config: $@"; |
144 | return; | |
145 | } | |
77ba2250 NC |
146 | foreach (@_) { |
147 | next if $Config::Config{$_}; | |
148 | my $key = $_; # Need to copy, before trying to modify. | |
9c8416b2 NC |
149 | $key =~ s/^use//; |
150 | $key =~ s/^d_//; | |
77ba2250 | 151 | skip_all("no $key"); |
9c8416b2 | 152 | } |
9c8416b2 NC |
153 | } |
154 | ||
69026470 | 155 | sub _ok { |
7d932aad | 156 | my ($pass, $where, $name, @mess) = @_; |
69026470 JH |
157 | # Do not try to microoptimize by factoring out the "not ". |
158 | # VMS will avenge. | |
7d932aad MS |
159 | my $out; |
160 | if ($name) { | |
b734d6c9 MS |
161 | # escape out '#' or it will interfere with '# skip' and such |
162 | $name =~ s/#/\\#/g; | |
7d932aad | 163 | $out = $pass ? "ok $test - $name" : "not ok $test - $name"; |
69026470 | 164 | } else { |
7d932aad | 165 | $out = $pass ? "ok $test" : "not ok $test"; |
69026470 | 166 | } |
7d932aad | 167 | |
02455492 NC |
168 | if ($TODO) { |
169 | $out = $out . " # TODO $TODO"; | |
170 | } else { | |
171 | $Tests_Are_Passing = 0 unless $pass; | |
172 | } | |
173 | ||
3d66076a | 174 | _print "$out\n"; |
7d932aad | 175 | |
9b9ae264 DM |
176 | if ($pass) { |
177 | note @mess; # Ensure that the message is properly escaped. | |
178 | } | |
179 | else { | |
de522f7a | 180 | _diag "# Failed $where\n"; |
9b9ae264 | 181 | _diag @mess; |
69026470 | 182 | } |
7d932aad | 183 | |
485f531e | 184 | $test = $test + 1; # don't use ++ |
1577bb16 MS |
185 | |
186 | return $pass; | |
69026470 JH |
187 | } |
188 | ||
189 | sub _where { | |
dcc7f481 | 190 | my @caller = caller($Level); |
69026470 JH |
191 | return "at $caller[1] line $caller[2]"; |
192 | } | |
193 | ||
1d662fb6 | 194 | # DON'T use this for matches. Use like() instead. |
c3029c66 | 195 | sub ok ($@) { |
7d932aad MS |
196 | my ($pass, $name, @mess) = @_; |
197 | _ok($pass, _where(), $name, @mess); | |
69026470 JH |
198 | } |
199 | ||
b3c72391 JH |
200 | sub _q { |
201 | my $x = shift; | |
202 | return 'undef' unless defined $x; | |
203 | my $q = $x; | |
d279d8f8 NC |
204 | $q =~ s/\\/\\\\/g; |
205 | $q =~ s/'/\\'/g; | |
b3c72391 JH |
206 | return "'$q'"; |
207 | } | |
208 | ||
677fb045 NC |
209 | sub _qq { |
210 | my $x = shift; | |
211 | return defined $x ? '"' . display ($x) . '"' : 'undef'; | |
212 | }; | |
213 | ||
214 | # keys are the codes \n etc map to, values are 2 char strings such as \n | |
215 | my %backslash_escape; | |
216 | foreach my $x (split //, 'nrtfa\\\'"') { | |
217 | $backslash_escape{ord eval "\"\\$x\""} = "\\$x"; | |
218 | } | |
219 | # A way to display scalars containing control characters and Unicode. | |
220 | # Trying to avoid setting $_, or relying on local $_ to work. | |
221 | sub display { | |
222 | my @result; | |
223 | foreach my $x (@_) { | |
224 | if (defined $x and not ref $x) { | |
225 | my $y = ''; | |
226 | foreach my $c (unpack("U*", $x)) { | |
227 | if ($c > 255) { | |
11ea18f2 | 228 | $y = $y . sprintf "\\x{%x}", $c; |
677fb045 | 229 | } elsif ($backslash_escape{$c}) { |
11ea18f2 | 230 | $y = $y . $backslash_escape{$c}; |
677fb045 NC |
231 | } else { |
232 | my $z = chr $c; # Maybe we can get away with a literal... | |
1cfccccd KW |
233 | if ($z =~ /[[:^print:]]/) { |
234 | ||
235 | # Use octal for characters traditionally expressed as | |
236 | # such: the low controls | |
237 | if ($c <= 037) { | |
238 | $z = sprintf "\\%03o", $c; | |
239 | } else { | |
240 | $z = sprintf "\\x{%x}", $c; | |
241 | } | |
242 | } | |
11ea18f2 | 243 | $y = $y . $z; |
677fb045 NC |
244 | } |
245 | } | |
246 | $x = $y; | |
247 | } | |
248 | return $x unless wantarray; | |
249 | push @result, $x; | |
250 | } | |
251 | return @result; | |
252 | } | |
253 | ||
c3029c66 | 254 | sub is ($$@) { |
7d932aad | 255 | my ($got, $expected, $name, @mess) = @_; |
c831d34f MS |
256 | |
257 | my $pass; | |
258 | if( !defined $got || !defined $expected ) { | |
259 | # undef only matches undef | |
260 | $pass = !defined $got && !defined $expected; | |
261 | } | |
262 | else { | |
263 | $pass = $got eq $expected; | |
264 | } | |
265 | ||
69026470 | 266 | unless ($pass) { |
d5f8084a KW |
267 | unshift(@mess, "# got "._qq($got)."\n", |
268 | "# expected "._qq($expected)."\n"); | |
69026470 | 269 | } |
7d932aad | 270 | _ok($pass, _where(), $name, @mess); |
69026470 JH |
271 | } |
272 | ||
c3029c66 | 273 | sub isnt ($$@) { |
3e90d5a3 | 274 | my ($got, $isnt, $name, @mess) = @_; |
c831d34f MS |
275 | |
276 | my $pass; | |
277 | if( !defined $got || !defined $isnt ) { | |
278 | # undef only matches undef | |
279 | $pass = defined $got || defined $isnt; | |
280 | } | |
281 | else { | |
282 | $pass = $got ne $isnt; | |
283 | } | |
284 | ||
3e90d5a3 | 285 | unless( $pass ) { |
d5f8084a | 286 | unshift(@mess, "# it should not be "._qq($got)."\n", |
3e90d5a3 MS |
287 | "# but it is.\n"); |
288 | } | |
289 | _ok($pass, _where(), $name, @mess); | |
290 | } | |
291 | ||
c3029c66 | 292 | sub cmp_ok ($$$@) { |
58d76dfd JH |
293 | my($got, $type, $expected, $name, @mess) = @_; |
294 | ||
295 | my $pass; | |
296 | { | |
297 | local $^W = 0; | |
298 | local($@,$!); # don't interfere with $@ | |
299 | # eval() sometimes resets $! | |
300 | $pass = eval "\$got $type \$expected"; | |
301 | } | |
302 | unless ($pass) { | |
303 | # It seems Irix long doubles can have 2147483648 and 2147483648 | |
93f09d7b | 304 | # that stringify to the same thing but are actually numerically |
58d76dfd JH |
305 | # different. Display the numbers if $type isn't a string operator, |
306 | # and the numbers are stringwise the same. | |
307 | # (all string operators have alphabetic names, so tr/a-z// is true) | |
93f09d7b PA |
308 | # This will also show numbers for some unneeded cases, but will |
309 | # definitely be helpful for things such as == and <= that fail | |
58d76dfd JH |
310 | if ($got eq $expected and $type !~ tr/a-z//) { |
311 | unshift @mess, "# $got - $expected = " . ($got - $expected) . "\n"; | |
312 | } | |
d5f8084a KW |
313 | unshift(@mess, "# got "._qq($got)."\n", |
314 | "# expected $type "._qq($expected)."\n"); | |
58d76dfd JH |
315 | } |
316 | _ok($pass, _where(), $name, @mess); | |
317 | } | |
318 | ||
319 | # Check that $got is within $range of $expected | |
320 | # if $range is 0, then check it's exact | |
321 | # else if $expected is 0, then $range is an absolute value | |
322 | # otherwise $range is a fractional error. | |
323 | # Here $range must be numeric, >= 0 | |
324 | # Non numeric ranges might be a useful future extension. (eg %) | |
c3029c66 | 325 | sub within ($$$@) { |
58d76dfd JH |
326 | my ($got, $expected, $range, $name, @mess) = @_; |
327 | my $pass; | |
328 | if (!defined $got or !defined $expected or !defined $range) { | |
329 | # This is a fail, but doesn't need extra diagnostics | |
330 | } elsif ($got !~ tr/0-9// or $expected !~ tr/0-9// or $range !~ tr/0-9//) { | |
331 | # This is a fail | |
332 | unshift @mess, "# got, expected and range must be numeric\n"; | |
333 | } elsif ($range < 0) { | |
334 | # This is also a fail | |
335 | unshift @mess, "# range must not be negative\n"; | |
336 | } elsif ($range == 0) { | |
337 | # Within 0 is == | |
338 | $pass = $got == $expected; | |
339 | } elsif ($expected == 0) { | |
340 | # If expected is 0, treat range as absolute | |
341 | $pass = ($got <= $range) && ($got >= - $range); | |
342 | } else { | |
343 | my $diff = $got - $expected; | |
344 | $pass = abs ($diff / $expected) < $range; | |
345 | } | |
346 | unless ($pass) { | |
347 | if ($got eq $expected) { | |
348 | unshift @mess, "# $got - $expected = " . ($got - $expected) . "\n"; | |
349 | } | |
d5f8084a KW |
350 | unshift@mess, "# got "._qq($got)."\n", |
351 | "# expected "._qq($expected)." (within "._qq($range).")\n"; | |
58d76dfd JH |
352 | } |
353 | _ok($pass, _where(), $name, @mess); | |
354 | } | |
355 | ||
69026470 | 356 | # Note: this isn't quite as fancy as Test::More::like(). |
724aa791 JC |
357 | |
358 | sub like ($$@) { like_yn (0,@_) }; # 0 for - | |
359 | sub unlike ($$@) { like_yn (1,@_) }; # 1 for un- | |
360 | ||
361 | sub like_yn ($$$@) { | |
0973e8e6 | 362 | my ($flip, undef, $expected, $name, @mess) = @_; |
69026470 | 363 | my $pass; |
0973e8e6 NC |
364 | $pass = $_[1] =~ /$expected/ if !$flip; |
365 | $pass = $_[1] !~ /$expected/ if $flip; | |
724aa791 | 366 | unless ($pass) { |
0973e8e6 | 367 | unshift(@mess, "# got '$_[1]'\n", |
5a4a8c8b NC |
368 | $flip |
369 | ? "# expected !~ /$expected/\n" : "# expected /$expected/\n"); | |
69026470 | 370 | } |
5693d826 | 371 | local $Level = $Level + 1; |
7d932aad | 372 | _ok($pass, _where(), $name, @mess); |
69026470 JH |
373 | } |
374 | ||
375 | sub pass { | |
376 | _ok(1, '', @_); | |
377 | } | |
378 | ||
379 | sub fail { | |
380 | _ok(0, _where(), @_); | |
381 | } | |
382 | ||
ad20d923 | 383 | sub curr_test { |
cf8feb78 | 384 | $test = shift if @_; |
ad20d923 MS |
385 | return $test; |
386 | } | |
387 | ||
3e90d5a3 | 388 | sub next_test { |
178eff92 | 389 | my $retval = $test; |
485f531e | 390 | $test = $test + 1; # don't use ++ |
178eff92 | 391 | $retval; |
3e90d5a3 MS |
392 | } |
393 | ||
69026470 JH |
394 | # Note: can't pass multipart messages since we try to |
395 | # be compatible with Test::More::skip(). | |
396 | sub skip { | |
7d932aad | 397 | my $why = shift; |
982b7cb7 | 398 | my $n = @_ ? shift : 1; |
69026470 | 399 | for (1..$n) { |
7bb7fa38 | 400 | _print "ok $test # skip $why\n"; |
485f531e | 401 | $test = $test + 1; |
69026470 JH |
402 | } |
403 | local $^W = 0; | |
404 | last SKIP; | |
405 | } | |
406 | ||
8c49cd2e | 407 | sub skip_if_miniperl { |
445876fa | 408 | skip(@_) if is_miniperl(); |
8c49cd2e NC |
409 | } |
410 | ||
09f04786 MS |
411 | sub todo_skip { |
412 | my $why = shift; | |
413 | my $n = @_ ? shift : 1; | |
414 | ||
415 | for (1..$n) { | |
7bb7fa38 | 416 | _print "not ok $test # TODO & SKIP $why\n"; |
485f531e | 417 | $test = $test + 1; |
09f04786 MS |
418 | } |
419 | local $^W = 0; | |
420 | last TODO; | |
421 | } | |
422 | ||
69026470 JH |
423 | sub eq_array { |
424 | my ($ra, $rb) = @_; | |
425 | return 0 unless $#$ra == $#$rb; | |
426 | for my $i (0..$#$ra) { | |
8210c8d3 | 427 | next if !defined $ra->[$i] && !defined $rb->[$i]; |
135d199b DM |
428 | return 0 if !defined $ra->[$i]; |
429 | return 0 if !defined $rb->[$i]; | |
69026470 JH |
430 | return 0 unless $ra->[$i] eq $rb->[$i]; |
431 | } | |
432 | return 1; | |
433 | } | |
434 | ||
677fb045 NC |
435 | sub eq_hash { |
436 | my ($orig, $suspect) = @_; | |
437 | my $fail; | |
438 | while (my ($key, $value) = each %$suspect) { | |
439 | # Force a hash recompute if this perl's internals can cache the hash key. | |
440 | $key = "" . $key; | |
441 | if (exists $orig->{$key}) { | |
442 | if ($orig->{$key} ne $value) { | |
3d66076a | 443 | _print "# key ", _qq($key), " was ", _qq($orig->{$key}), |
de522f7a | 444 | " now ", _qq($value), "\n"; |
677fb045 NC |
445 | $fail = 1; |
446 | } | |
447 | } else { | |
3d66076a | 448 | _print "# key ", _qq($key), " is ", _qq($value), |
75385f53 | 449 | ", not in original.\n"; |
677fb045 NC |
450 | $fail = 1; |
451 | } | |
452 | } | |
453 | foreach (keys %$orig) { | |
454 | # Force a hash recompute if this perl's internals can cache the hash key. | |
455 | $_ = "" . $_; | |
456 | next if (exists $suspect->{$_}); | |
3d66076a | 457 | _print "# key ", _qq($_), " was ", _qq($orig->{$_}), " now missing.\n"; |
677fb045 NC |
458 | $fail = 1; |
459 | } | |
460 | !$fail; | |
461 | } | |
462 | ||
d47bdea7 | 463 | # We only provide a subset of the Test::More functionality. |
c3029c66 | 464 | sub require_ok ($) { |
69026470 | 465 | my ($require) = @_; |
d47bdea7 NC |
466 | if ($require =~ tr/[A-Za-z0-9:.]//c) { |
467 | fail("Invalid character in \"$require\", passed to require_ok"); | |
468 | } else { | |
469 | eval <<REQUIRE_OK; | |
69026470 JH |
470 | require $require; |
471 | REQUIRE_OK | |
d47bdea7 NC |
472 | is($@, '', _where(), "require $require"); |
473 | } | |
69026470 JH |
474 | } |
475 | ||
c3029c66 | 476 | sub use_ok ($) { |
69026470 | 477 | my ($use) = @_; |
d47bdea7 NC |
478 | if ($use =~ tr/[A-Za-z0-9:.]//c) { |
479 | fail("Invalid character in \"$use\", passed to use"); | |
480 | } else { | |
481 | eval <<USE_OK; | |
69026470 JH |
482 | use $use; |
483 | USE_OK | |
d47bdea7 NC |
484 | is($@, '', _where(), "use $use"); |
485 | } | |
69026470 JH |
486 | } |
487 | ||
137352a2 RGS |
488 | # runperl - Runs a separate perl interpreter. |
489 | # Arguments : | |
490 | # switches => [ command-line switches ] | |
491 | # nolib => 1 # don't use -I../lib (included by default) | |
3d7a9343 | 492 | # non_portable => Don't warn if a one liner contains quotes |
137352a2 | 493 | # prog => one-liner (avoid quotes) |
d83945bc | 494 | # progs => [ multi-liner (avoid quotes) ] |
137352a2 RGS |
495 | # progfile => perl script |
496 | # stdin => string to feed the stdin | |
497 | # stderr => redirect stderr to stdout | |
498 | # args => [ command-line arguments to the perl program ] | |
cb9c5e20 | 499 | # verbose => print the command line |
137352a2 RGS |
500 | |
501 | my $is_mswin = $^O eq 'MSWin32'; | |
502 | my $is_netware = $^O eq 'NetWare'; | |
137352a2 | 503 | my $is_vms = $^O eq 'VMS'; |
e67ed694 | 504 | my $is_cygwin = $^O eq 'cygwin'; |
137352a2 | 505 | |
cb9c5e20 JH |
506 | sub _quote_args { |
507 | my ($runperl, $args) = @_; | |
508 | ||
509 | foreach (@$args) { | |
510 | # In VMS protect with doublequotes because otherwise | |
511 | # DCL will lowercase -- unless already doublequoted. | |
ea9ac5ad | 512 | $_ = q(").$_.q(") if $is_vms && !/^\"/ && length($_) > 0; |
1cce9906 | 513 | $runperl = $runperl . ' ' . $_; |
cb9c5e20 | 514 | } |
1cce9906 | 515 | return $runperl; |
cb9c5e20 JH |
516 | } |
517 | ||
4cd2bd1f | 518 | sub _create_runperl { # Create the string to qx in runperl(). |
137352a2 | 519 | my %args = @_; |
5fe9b82b JH |
520 | my $runperl = which_perl(); |
521 | if ($runperl =~ m/\s/) { | |
522 | $runperl = qq{"$runperl"}; | |
523 | } | |
6cf707aa RGS |
524 | #- this allows, for example, to set PERL_RUNPERL_DEBUG=/usr/bin/valgrind |
525 | if ($ENV{PERL_RUNPERL_DEBUG}) { | |
526 | $runperl = "$ENV{PERL_RUNPERL_DEBUG} $runperl"; | |
527 | } | |
f93a5f07 | 528 | unless ($args{nolib}) { |
11ea18f2 | 529 | $runperl = $runperl . ' "-I../lib"'; # doublequotes because of VMS |
137352a2 | 530 | } |
d83945bc | 531 | if ($args{switches}) { |
343d4a7b JH |
532 | local $Level = 2; |
533 | die "test.pl:runperl(): 'switches' must be an ARRAYREF " . _where() | |
534 | unless ref $args{switches} eq "ARRAY"; | |
1cce9906 | 535 | $runperl = _quote_args($runperl, $args{switches}); |
d83945bc | 536 | } |
137352a2 | 537 | if (defined $args{prog}) { |
21820af6 JH |
538 | die "test.pl:runperl(): both 'prog' and 'progs' cannot be used " . _where() |
539 | if defined $args{progs}; | |
d83945bc A |
540 | $args{progs} = [$args{prog}] |
541 | } | |
542 | if (defined $args{progs}) { | |
21820af6 JH |
543 | die "test.pl:runperl(): 'progs' must be an ARRAYREF " . _where() |
544 | unless ref $args{progs} eq "ARRAY"; | |
d83945bc | 545 | foreach my $prog (@{$args{progs}}) { |
3d7a9343 NC |
546 | if ($prog =~ tr/'"// && !$args{non_portable}) { |
547 | warn "quotes in prog >>$prog<< are not portable"; | |
548 | } | |
d83945bc | 549 | if ($is_mswin || $is_netware || $is_vms) { |
11ea18f2 | 550 | $runperl = $runperl . qq ( -e "$prog" ); |
d83945bc A |
551 | } |
552 | else { | |
11ea18f2 | 553 | $runperl = $runperl . qq ( -e '$prog' ); |
d83945bc A |
554 | } |
555 | } | |
137352a2 | 556 | } elsif (defined $args{progfile}) { |
11ea18f2 | 557 | $runperl = $runperl . qq( "$args{progfile}"); |
9a731dbd | 558 | } else { |
93f09d7b | 559 | # You probably didn't want to be sucking in from the upstream stdin |
9a731dbd NC |
560 | die "test.pl:runperl(): none of prog, progs, progfile, args, " |
561 | . " switches or stdin specified" | |
562 | unless defined $args{args} or defined $args{switches} | |
563 | or defined $args{stdin}; | |
137352a2 RGS |
564 | } |
565 | if (defined $args{stdin}) { | |
dc459aad JH |
566 | # so we don't try to put literal newlines and crs onto the |
567 | # command line. | |
568 | $args{stdin} =~ s/\n/\\n/g; | |
569 | $args{stdin} =~ s/\r/\\r/g; | |
5ae09a77 | 570 | |
137352a2 | 571 | if ($is_mswin || $is_netware || $is_vms) { |
5fe9b82b | 572 | $runperl = qq{$Perl -e "print qq(} . |
137352a2 RGS |
573 | $args{stdin} . q{)" | } . $runperl; |
574 | } | |
575 | else { | |
5fe9b82b | 576 | $runperl = qq{$Perl -e 'print qq(} . |
137352a2 RGS |
577 | $args{stdin} . q{)' | } . $runperl; |
578 | } | |
579 | } | |
580 | if (defined $args{args}) { | |
1cce9906 | 581 | $runperl = _quote_args($runperl, $args{args}); |
cb9c5e20 | 582 | } |
11ea18f2 | 583 | $runperl = $runperl . ' 2>&1' if $args{stderr}; |
cb9c5e20 JH |
584 | if ($args{verbose}) { |
585 | my $runperldisplay = $runperl; | |
586 | $runperldisplay =~ s/\n/\n\#/g; | |
3d66076a | 587 | _print_stderr "# $runperldisplay\n"; |
137352a2 | 588 | } |
4cd2bd1f JH |
589 | return $runperl; |
590 | } | |
591 | ||
592 | sub runperl { | |
9a731dbd NC |
593 | die "test.pl:runperl() does not take a hashref" |
594 | if ref $_[0] and ref $_[0] eq 'HASH'; | |
4cd2bd1f | 595 | my $runperl = &_create_runperl; |
613de57f NC |
596 | my $result; |
597 | ||
8210c8d3 MB |
598 | my $tainted = ${^TAINT}; |
599 | my %args = @_; | |
485f531e | 600 | exists $args{switches} && grep m/^-T$/, @{$args{switches}} and $tainted = $tainted + 1; |
8210c8d3 MB |
601 | |
602 | if ($tainted) { | |
613de57f NC |
603 | # We will assume that if you're running under -T, you really mean to |
604 | # run a fresh perl, so we'll brute force launder everything for you | |
605 | my $sep; | |
606 | ||
cb01154c | 607 | if (! eval {require Config; 1}) { |
613de57f NC |
608 | warn "test.pl had problems loading Config: $@"; |
609 | $sep = ':'; | |
610 | } else { | |
afe79e7b | 611 | $sep = $Config::Config{path_sep}; |
a70a1627 | 612 | } |
613de57f NC |
613 | |
614 | my @keys = grep {exists $ENV{$_}} qw(CDPATH IFS ENV BASH_ENV); | |
615 | local @ENV{@keys} = (); | |
616 | # Untaint, plus take out . and empty string: | |
02bb3106 | 617 | local $ENV{'DCL$PATH'} = $1 if $is_vms && exists($ENV{'DCL$PATH'}) && ($ENV{'DCL$PATH'} =~ /(.*)/s); |
613de57f | 618 | $ENV{PATH} =~ /(.*)/s; |
8210c8d3 | 619 | local $ENV{PATH} = |
3b6d8381 | 620 | join $sep, grep { $_ ne "" and $_ ne "." and -d $_ and |
326b5008 | 621 | ($is_mswin or $is_vms or !(stat && (stat _)[2]&0022)) } |
8210c8d3 | 622 | split quotemeta ($sep), $1; |
59aae9bd JH |
623 | if ($is_cygwin) { # Must have /bin under Cygwin |
624 | if (length $ENV{PATH}) { | |
625 | $ENV{PATH} = $ENV{PATH} . $sep; | |
626 | } | |
627 | $ENV{PATH} = $ENV{PATH} . '/bin'; | |
628 | } | |
613de57f NC |
629 | $runperl =~ /(.*)/s; |
630 | $runperl = $1; | |
631 | ||
632 | $result = `$runperl`; | |
633 | } else { | |
634 | $result = `$runperl`; | |
a70a1627 | 635 | } |
137352a2 RGS |
636 | $result =~ s/\n\n/\n/ if $is_vms; # XXX pipes sometimes double these |
637 | return $result; | |
638 | } | |
639 | ||
140f5369 MS |
640 | # Nice alias |
641 | *run_perl = *run_perl = \&runperl; # shut up "used only once" warning | |
8799135f | 642 | |
c4fbe247 | 643 | sub DIE { |
3d66076a | 644 | _print_stderr "# @_\n"; |
c4fbe247 | 645 | exit 1; |
8799135f MS |
646 | } |
647 | ||
b5fe401b | 648 | # A somewhat safer version of the sometimes wrong $^X. |
17a740d5 JH |
649 | sub which_perl { |
650 | unless (defined $Perl) { | |
651 | $Perl = $^X; | |
8210c8d3 | 652 | |
73421c4a | 653 | # VMS should have 'perl' aliased properly |
4b0f0df6 | 654 | return $Perl if $is_vms; |
73421c4a | 655 | |
17a740d5 | 656 | my $exe; |
cb01154c | 657 | if (! eval {require Config; 1}) { |
17a740d5 JH |
658 | warn "test.pl had problems loading Config: $@"; |
659 | $exe = ''; | |
85363d30 | 660 | } else { |
afe79e7b | 661 | $exe = $Config::Config{_exe}; |
85363d30 | 662 | } |
da405c16 | 663 | $exe = '' unless defined $exe; |
8210c8d3 | 664 | |
17a740d5 JH |
665 | # This doesn't absolutize the path: beware of future chdirs(). |
666 | # We could do File::Spec->abs2rel() but that does getcwd()s, | |
667 | # which is a bit heavyweight to do here. | |
8210c8d3 | 668 | |
17a740d5 | 669 | if ($Perl =~ /^perl\Q$exe\E$/i) { |
8db06b02 | 670 | my $perl = "perl$exe"; |
cb01154c | 671 | if (! eval {require File::Spec; 1}) { |
17a740d5 | 672 | warn "test.pl had problems loading File::Spec: $@"; |
8db06b02 | 673 | $Perl = "./$perl"; |
17a740d5 | 674 | } else { |
8db06b02 | 675 | $Perl = File::Spec->catfile(File::Spec->curdir(), $perl); |
17a740d5 JH |
676 | } |
677 | } | |
196918b0 PG |
678 | |
679 | # Build up the name of the executable file from the name of | |
680 | # the command. | |
681 | ||
682 | if ($Perl !~ /\Q$exe\E$/i) { | |
11ea18f2 | 683 | $Perl = $Perl . $exe; |
196918b0 | 684 | } |
c880be78 | 685 | |
8db06b02 | 686 | warn "which_perl: cannot find $Perl from $^X" unless -f $Perl; |
8210c8d3 | 687 | |
17a740d5 JH |
688 | # For subcommands to use. |
689 | $ENV{PERLEXE} = $Perl; | |
85363d30 | 690 | } |
17a740d5 | 691 | return $Perl; |
b5fe401b MS |
692 | } |
693 | ||
435e7af6 | 694 | sub unlink_all { |
55b0687d | 695 | my $count = 0; |
435e7af6 NC |
696 | foreach my $file (@_) { |
697 | 1 while unlink $file; | |
55b0687d BG |
698 | if( -f $file ){ |
699 | _print_stderr "# Couldn't unlink '$file': $!\n"; | |
700 | }else{ | |
701 | ++$count; | |
702 | } | |
435e7af6 | 703 | } |
55b0687d | 704 | $count; |
435e7af6 | 705 | } |
eeabcb2d | 706 | |
748a4b20 NC |
707 | my %tmpfiles; |
708 | END { unlink_all keys %tmpfiles } | |
709 | ||
710 | # A regexp that matches the tempfile names | |
711 | $::tempfile_regexp = 'tmp\d+[A-Z][A-Z]?'; | |
c1ddc35c | 712 | |
7a7e4936 NC |
713 | # Avoid ++, avoid ranges, avoid split // |
714 | my @letters = qw(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z); | |
715 | sub tempfile { | |
716 | my $count = 0; | |
717 | do { | |
718 | my $temp = $count; | |
719 | my $try = "tmp$$"; | |
720 | do { | |
11ea18f2 | 721 | $try = $try . $letters[$temp % 26]; |
748a4b20 | 722 | $temp = int ($temp / 26); |
7a7e4936 | 723 | } while $temp; |
748a4b20 NC |
724 | # Need to note all the file names we allocated, as a second request may |
725 | # come before the first is created. | |
726 | if (!-e $try && !$tmpfiles{$try}) { | |
c1ddc35c | 727 | # We have a winner |
11ea18f2 | 728 | $tmpfiles{$try} = 1; |
c1ddc35c NC |
729 | return $try; |
730 | } | |
7a7e4936 NC |
731 | $count = $count + 1; |
732 | } while $count < 26 * 26; | |
733 | die "Can't find temporary file name starting 'tmp$$'"; | |
734 | } | |
735 | ||
c1ddc35c | 736 | # This is the temporary file for _fresh_perl |
7a7e4936 | 737 | my $tmpfile = tempfile(); |
eeabcb2d | 738 | |
f5cda331 | 739 | sub _fresh_perl { |
55280a0d | 740 | my($prog, $action, $expect, $runperl_args, $name) = @_; |
eeabcb2d | 741 | |
11ea18f2 NC |
742 | # Given the choice of the mis-parsable {} |
743 | # (we want an anon hash, but a borked lexer might think that it's a block) | |
744 | # or relying on taking a reference to a lexical | |
745 | # (\ might be mis-parsed, and the reference counting on the pad may go | |
746 | # awry) | |
747 | # it feels like the least-worse thing is to assume that auto-vivification | |
748 | # works. At least, this is only going to be a run-time failure, so won't | |
749 | # affect tests using this file but not this function. | |
eeabcb2d MS |
750 | $runperl_args->{progfile} = $tmpfile; |
751 | $runperl_args->{stderr} = 1; | |
752 | ||
753 | open TEST, ">$tmpfile" or die "Cannot open $tmpfile: $!"; | |
754 | ||
755 | # VMS adjustments | |
4b0f0df6 | 756 | if( $is_vms ) { |
eeabcb2d MS |
757 | $prog =~ s#/dev/null#NL:#; |
758 | ||
8210c8d3 | 759 | # VMS file locking |
eeabcb2d MS |
760 | $prog =~ s{if \(-e _ and -f _ and -r _\)} |
761 | {if (-e _ and -f _)} | |
762 | } | |
763 | ||
0d65d7d5 | 764 | print TEST $prog; |
eeabcb2d MS |
765 | close TEST or die "Cannot close $tmpfile: $!"; |
766 | ||
767 | my $results = runperl(%$runperl_args); | |
768 | my $status = $?; | |
769 | ||
770 | # Clean up the results into something a bit more predictable. | |
50f17f89 | 771 | $results =~ s/\n+$//; |
748a4b20 NC |
772 | $results =~ s/at\s+$::tempfile_regexp\s+line/at - line/g; |
773 | $results =~ s/of\s+$::tempfile_regexp\s+aborted/of - aborted/g; | |
eeabcb2d MS |
774 | |
775 | # bison says 'parse error' instead of 'syntax error', | |
776 | # various yaccs may or may not capitalize 'syntax'. | |
777 | $results =~ s/^(syntax|parse) error/syntax error/mig; | |
778 | ||
4b0f0df6 | 779 | if ($is_vms) { |
eeabcb2d MS |
780 | # some tests will trigger VMS messages that won't be expected |
781 | $results =~ s/\n?%[A-Z]+-[SIWEF]-[A-Z]+,.*//; | |
782 | ||
783 | # pipes double these sometimes | |
784 | $results =~ s/\n\n/\n/g; | |
785 | } | |
786 | ||
e2c38acd JH |
787 | # Use the first line of the program as a name if none was given |
788 | unless( $name ) { | |
789 | ($first_line, $name) = $prog =~ /^((.{1,50}).*)/; | |
11ea18f2 | 790 | $name = $name . '...' if length $first_line > length $name; |
e2c38acd | 791 | } |
eeabcb2d | 792 | |
55280a0d NC |
793 | # Historically this was implemented using a closure, but then that means |
794 | # that the tests for closures avoid using this code. Given that there | |
795 | # are exactly two callers, doing exactly two things, the simpler approach | |
796 | # feels like a better trade off. | |
797 | my $pass; | |
798 | if ($action eq 'eq') { | |
799 | $pass = is($results, $expect, $name); | |
800 | } elsif ($action eq '=~') { | |
801 | $pass = like($results, $expect, $name); | |
802 | } else { | |
803 | die "_fresh_perl can't process action '$action'"; | |
804 | } | |
805 | ||
806 | unless ($pass) { | |
807 | _diag "# PROG: \n$prog\n"; | |
808 | _diag "# STATUS: $status\n"; | |
809 | } | |
810 | ||
811 | return $pass; | |
f5cda331 JH |
812 | } |
813 | ||
814 | # | |
141f445b | 815 | # fresh_perl_is |
f5cda331 JH |
816 | # |
817 | # Combination of run_perl() and is(). | |
818 | # | |
819 | ||
820 | sub fresh_perl_is { | |
821 | my($prog, $expected, $runperl_args, $name) = @_; | |
50f17f89 MS |
822 | |
823 | # _fresh_perl() is going to clip the trailing newlines off the result. | |
824 | # This will make it so the test author doesn't have to know that. | |
825 | $expected =~ s/\n+$//; | |
826 | ||
dcc7f481 | 827 | local $Level = 2; |
55280a0d | 828 | _fresh_perl($prog, 'eq', $expected, $runperl_args, $name); |
f5cda331 JH |
829 | } |
830 | ||
831 | # | |
141f445b | 832 | # fresh_perl_like |
f5cda331 JH |
833 | # |
834 | # Combination of run_perl() and like(). | |
835 | # | |
836 | ||
837 | sub fresh_perl_like { | |
838 | my($prog, $expected, $runperl_args, $name) = @_; | |
dcc7f481 | 839 | local $Level = 2; |
55280a0d | 840 | _fresh_perl($prog, '=~', $expected, $runperl_args, $name); |
eeabcb2d MS |
841 | } |
842 | ||
ebf2da99 NC |
843 | # Many tests use the same format in __DATA__ or external files to specify a |
844 | # sequence of (fresh) tests to run, extra files they may temporarily need, and | |
845 | # what the expected output is. So have excatly one copy of the code to run that | |
846 | ||
847 | sub run_multiple_progs { | |
5f7e0818 NC |
848 | my $up = shift; |
849 | my @prgs; | |
850 | if ($up) { | |
851 | # The tests in lib run in a temporary subdirectory of t, and always | |
852 | # pass in a list of "programs" to run | |
853 | @prgs = @_; | |
854 | } else { | |
855 | # The tests below t run in t and pass in a file handle. | |
856 | my $fh = shift; | |
857 | local $/; | |
858 | @prgs = split "\n########\n", <$fh>; | |
859 | } | |
860 | ||
ebf2da99 NC |
861 | my $tmpfile = tempfile(); |
862 | ||
863 | for (@prgs){ | |
864 | unless (/\n/) { | |
865 | print "# From $_\n"; | |
866 | next; | |
867 | } | |
868 | my $switch = ""; | |
869 | my @temps ; | |
870 | my @temp_path; | |
871 | if (s/^(\s*-\w+)//) { | |
872 | $switch = $1; | |
873 | } | |
874 | my ($prog, $expected) = split(/\nEXPECT(?:\n|$)/, $_, 2); | |
875 | ||
876 | my %reason; | |
877 | foreach my $what (qw(skip todo)) { | |
878 | $prog =~ s/^#\s*\U$what\E\s*(.*)\n//m and $reason{$what} = $1; | |
879 | # If the SKIP reason starts ? then it's taken as a code snippet to | |
880 | # evaluate. This provides the flexibility to have conditional SKIPs | |
881 | if ($reason{$what} && $reason{$what} =~ s/^\?//) { | |
882 | my $temp = eval $reason{$what}; | |
883 | if ($@) { | |
884 | die "# In \U$what\E code reason:\n# $reason{$what}\n$@"; | |
885 | } | |
886 | $reason{$what} = $temp; | |
887 | } | |
888 | } | |
889 | ||
890 | if ($prog =~ /--FILE--/) { | |
891 | my @files = split(/\n--FILE--\s*([^\s\n]*)\s*\n/, $prog) ; | |
892 | shift @files ; | |
893 | die "Internal error: test $_ didn't split into pairs, got " . | |
894 | scalar(@files) . "[" . join("%%%%", @files) ."]\n" | |
895 | if @files % 2; | |
896 | while (@files > 2) { | |
897 | my $filename = shift @files; | |
898 | my $code = shift @files; | |
899 | push @temps, $filename; | |
900 | if ($filename =~ m#(.*)/# && $filename !~ m#^\.\./#) { | |
901 | require File::Path; | |
902 | File::Path::mkpath($1); | |
903 | push(@temp_path, $1); | |
904 | } | |
905 | open my $fh, '>', $filename or die "Cannot open $filename: $!\n"; | |
906 | print $fh $code; | |
907 | close $fh or die "Cannot close $filename: $!\n"; | |
908 | } | |
909 | shift @files; | |
910 | $prog = shift @files; | |
911 | } | |
912 | ||
913 | open my $fh, '>', $tmpfile or die "Cannot open >$tmpfile: $!"; | |
914 | print $fh q{ | |
915 | BEGIN { | |
916 | open STDERR, '>&', STDOUT | |
917 | or die "Can't dup STDOUT->STDERR: $!;"; | |
918 | } | |
919 | }; | |
920 | print $fh "\n#line 1\n"; # So the line numbers don't get messed up. | |
921 | print $fh $prog,"\n"; | |
922 | close $fh or die "Cannot close $tmpfile: $!"; | |
5f7e0818 NC |
923 | my $results = runperl( stderr => 1, progfile => $tmpfile, $up |
924 | ? (switches => ["-I$up/lib", $switch], nolib => 1) | |
925 | : (switches => [$switch]) | |
926 | ); | |
ebf2da99 NC |
927 | my $status = $?; |
928 | $results =~ s/\n+$//; | |
929 | # allow expected output to be written as if $prog is on STDIN | |
930 | $results =~ s/$::tempfile_regexp/-/g; | |
931 | if ($^O eq 'VMS') { | |
932 | # some tests will trigger VMS messages that won't be expected | |
933 | $results =~ s/\n?%[A-Z]+-[SIWEF]-[A-Z]+,.*//; | |
934 | ||
935 | # pipes double these sometimes | |
936 | $results =~ s/\n\n/\n/g; | |
937 | } | |
938 | # bison says 'parse error' instead of 'syntax error', | |
939 | # various yaccs may or may not capitalize 'syntax'. | |
940 | $results =~ s/^(syntax|parse) error/syntax error/mig; | |
941 | # allow all tests to run when there are leaks | |
942 | $results =~ s/Scalars leaked: \d+\n//g; | |
943 | ||
944 | $expected =~ s/\n+$//; | |
945 | my $prefix = ($results =~ s#^PREFIX(\n|$)##) ; | |
946 | # any special options? (OPTIONS foo bar zap) | |
947 | my $option_regex = 0; | |
948 | my $option_random = 0; | |
949 | if ($expected =~ s/^OPTIONS? (.+)\n//) { | |
950 | foreach my $option (split(' ', $1)) { | |
951 | if ($option eq 'regex') { # allow regular expressions | |
952 | $option_regex = 1; | |
953 | } | |
954 | elsif ($option eq 'random') { # all lines match, but in any order | |
955 | $option_random = 1; | |
956 | } | |
957 | else { | |
958 | die "$0: Unknown OPTION '$option'\n"; | |
959 | } | |
960 | } | |
961 | } | |
962 | die "$0: can't have OPTION regex and random\n" | |
963 | if $option_regex + $option_random > 1; | |
964 | my $ok = 0; | |
965 | if ($results =~ s/^SKIPPED\n//) { | |
966 | print "$results\n" ; | |
967 | $ok = 1; | |
968 | } | |
969 | elsif ($option_random) { | |
970 | my @got = sort split "\n", $results; | |
971 | my @expected = sort split "\n", $expected; | |
972 | ||
973 | $ok = "@got" eq "@expected"; | |
974 | } | |
975 | elsif ($option_regex) { | |
976 | $ok = $results =~ /^$expected/; | |
977 | } | |
978 | elsif ($prefix) { | |
979 | $ok = $results =~ /^\Q$expected/; | |
980 | } | |
981 | else { | |
982 | $ok = $results eq $expected; | |
983 | } | |
984 | ||
985 | local $::TODO = $reason{todo}; | |
986 | ||
987 | unless ($ok) { | |
988 | my $err_line = "PROG: $switch\n$prog\n" . | |
989 | "EXPECTED:\n$expected\n" . | |
990 | "GOT:\n$results\n"; | |
991 | if ($::TODO) { | |
992 | $err_line =~ s/^/# /mg; | |
993 | print $err_line; # Harness can't filter it out from STDERR. | |
994 | } | |
995 | else { | |
996 | print STDERR $err_line; | |
997 | } | |
998 | } | |
999 | ||
1000 | ok($ok); | |
1001 | ||
1002 | foreach (@temps) { | |
1003 | unlink $_ if $_; | |
1004 | } | |
1005 | foreach (@temp_path) { | |
1006 | File::Path::rmtree $_ if -d $_; | |
1007 | } | |
1008 | } | |
1009 | } | |
1010 | ||
35a60386 RGS |
1011 | sub can_ok ($@) { |
1012 | my($proto, @methods) = @_; | |
1013 | my $class = ref $proto || $proto; | |
1014 | ||
1015 | unless( @methods ) { | |
1016 | return _ok( 0, _where(), "$class->can(...)" ); | |
1017 | } | |
1018 | ||
1019 | my @nok = (); | |
1020 | foreach my $method (@methods) { | |
1021 | local($!, $@); # don't interfere with caller's $@ | |
1022 | # eval sometimes resets $! | |
1023 | eval { $proto->can($method) } || push @nok, $method; | |
1024 | } | |
1025 | ||
1026 | my $name; | |
8210c8d3 | 1027 | $name = @methods == 1 ? "$class->can('$methods[0]')" |
35a60386 | 1028 | : "$class->can(...)"; |
8210c8d3 | 1029 | |
35a60386 RGS |
1030 | _ok( !@nok, _where(), $name ); |
1031 | } | |
1032 | ||
ad4e703e MS |
1033 | |
1034 | # Call $class->new( @$args ); and run the result through isa_ok. | |
1035 | # See Test::More::new_ok | |
1036 | sub new_ok { | |
1037 | my($class, $args, $obj_name) = @_; | |
1038 | $args ||= []; | |
1039 | $object_name = "The object" unless defined $obj_name; | |
1040 | ||
1041 | local $Level = $Level + 1; | |
1042 | ||
1043 | my $obj; | |
1044 | my $ok = eval { $obj = $class->new(@$args); 1 }; | |
1045 | my $error = $@; | |
1046 | ||
1047 | if($ok) { | |
1048 | isa_ok($obj, $class, $object_name); | |
1049 | } | |
1050 | else { | |
1051 | ok( 0, "new() died" ); | |
1052 | diag("Error was: $@"); | |
1053 | } | |
1054 | ||
1055 | return $obj; | |
1056 | ||
1057 | } | |
1058 | ||
1059 | ||
35a60386 RGS |
1060 | sub isa_ok ($$;$) { |
1061 | my($object, $class, $obj_name) = @_; | |
1062 | ||
1063 | my $diag; | |
1064 | $obj_name = 'The object' unless defined $obj_name; | |
1065 | my $name = "$obj_name isa $class"; | |
1066 | if( !defined $object ) { | |
1067 | $diag = "$obj_name isn't defined"; | |
1068 | } | |
1069 | elsif( !ref $object ) { | |
1070 | $diag = "$obj_name isn't a reference"; | |
1071 | } | |
1072 | else { | |
1073 | # We can't use UNIVERSAL::isa because we want to honor isa() overrides | |
1074 | local($@, $!); # eval sometimes resets $! | |
1075 | my $rslt = eval { $object->isa($class) }; | |
1076 | if( $@ ) { | |
1077 | if( $@ =~ /^Can't call method "isa" on unblessed reference/ ) { | |
1078 | if( !UNIVERSAL::isa($object, $class) ) { | |
1079 | my $ref = ref $object; | |
1080 | $diag = "$obj_name isn't a '$class' it's a '$ref'"; | |
1081 | } | |
1082 | } else { | |
1083 | die <<WHOA; | |
1084 | WHOA! I tried to call ->isa on your object and got some weird error. | |
1085 | This should never happen. Please contact the author immediately. | |
1086 | Here's the error. | |
1087 | $@ | |
1088 | WHOA | |
1089 | } | |
1090 | } | |
1091 | elsif( !$rslt ) { | |
1092 | my $ref = ref $object; | |
1093 | $diag = "$obj_name isn't a '$class' it's a '$ref'"; | |
1094 | } | |
1095 | } | |
1096 | ||
1097 | _ok( !$diag, _where(), $name ); | |
1098 | } | |
1099 | ||
9eb41b69 NC |
1100 | # Purposefully avoiding a closure. |
1101 | sub __capture { | |
1102 | push @::__capture, join "", @_; | |
1103 | } | |
1104 | ||
3fbaac97 NC |
1105 | sub capture_warnings { |
1106 | my $code = shift; | |
1107 | ||
9eb41b69 NC |
1108 | local @::__capture; |
1109 | local $SIG {__WARN__} = \&__capture; | |
3fbaac97 | 1110 | &$code; |
9eb41b69 | 1111 | return @::__capture; |
3fbaac97 NC |
1112 | } |
1113 | ||
1114 | # This will generate a variable number of tests. | |
1115 | # Use done_testing() instead of a fixed plan. | |
1116 | sub warnings_like { | |
1117 | my ($code, $expect, $name) = @_; | |
f4554ed5 NC |
1118 | local $Level = $Level + 1; |
1119 | ||
3fbaac97 NC |
1120 | my @w = capture_warnings($code); |
1121 | ||
1122 | cmp_ok(scalar @w, '==', scalar @$expect, $name); | |
1123 | foreach my $e (@$expect) { | |
f4554ed5 | 1124 | if (ref $e) { |
3fbaac97 | 1125 | like(shift @w, $e, $name); |
4d18b353 | 1126 | } else { |
3fbaac97 | 1127 | is(shift @w, $e, $name); |
4d18b353 | 1128 | } |
96980024 | 1129 | } |
3fbaac97 NC |
1130 | if (@w) { |
1131 | diag("Saw these additional warnings:"); | |
1132 | diag($_) foreach @w; | |
1133 | } | |
1134 | } | |
1135 | ||
1136 | sub _fail_excess_warnings { | |
1137 | my($expect, $got, $name) = @_; | |
1138 | local $Level = $Level + 1; | |
1139 | # This will fail, and produce diagnostics | |
1140 | is($expect, scalar @$got, $name); | |
1141 | diag("Saw these warnings:"); | |
1142 | diag($_) foreach @$got; | |
c11a8df3 NC |
1143 | } |
1144 | ||
4d18b353 NC |
1145 | sub warning_is { |
1146 | my ($code, $expect, $name) = @_; | |
1147 | die sprintf "Expect must be a string or undef, not a %s reference", ref $expect | |
1148 | if ref $expect; | |
f4554ed5 | 1149 | local $Level = $Level + 1; |
3fbaac97 NC |
1150 | my @w = capture_warnings($code); |
1151 | if (@w > 1) { | |
1152 | _fail_excess_warnings(0 + defined $expect, \@w, $name); | |
1153 | } else { | |
1154 | is($w[0], $expect, $name); | |
1155 | } | |
4d18b353 NC |
1156 | } |
1157 | ||
1158 | sub warning_like { | |
1159 | my ($code, $expect, $name) = @_; | |
1160 | die sprintf "Expect must be a regexp object" | |
1161 | unless ref $expect eq 'Regexp'; | |
f4554ed5 | 1162 | local $Level = $Level + 1; |
3fbaac97 NC |
1163 | my @w = capture_warnings($code); |
1164 | if (@w > 1) { | |
1165 | _fail_excess_warnings(0 + defined $expect, \@w, $name); | |
1166 | } else { | |
1167 | like($w[0], $expect, $name); | |
1168 | } | |
4d18b353 NC |
1169 | } |
1170 | ||
087986a7 | 1171 | # Set a watchdog to timeout the entire test file |
5fe9b82b JH |
1172 | # NOTE: If the test file uses 'threads', then call the watchdog() function |
1173 | # _AFTER_ the 'threads' module is loaded. | |
5732108f | 1174 | sub watchdog ($;$) |
087986a7 JH |
1175 | { |
1176 | my $timeout = shift; | |
36436324 | 1177 | my $method = shift || ""; |
087986a7 JH |
1178 | my $timeout_msg = 'Test process timed out - terminating'; |
1179 | ||
e07ce2e4 GG |
1180 | # Valgrind slows perl way down so give it more time before dying. |
1181 | $timeout *= 10 if $ENV{PERL_VALGRIND}; | |
1182 | ||
087986a7 JH |
1183 | my $pid_to_kill = $$; # PID for this process |
1184 | ||
5732108f GG |
1185 | if ($method eq "alarm") { |
1186 | goto WATCHDOG_VIA_ALARM; | |
1187 | } | |
1188 | ||
140f5369 MS |
1189 | # shut up use only once warning |
1190 | my $threads_on = $threads::threads && $threads::threads; | |
1191 | ||
5fe9b82b JH |
1192 | # Don't use a watchdog process if 'threads' is loaded - |
1193 | # use a watchdog thread instead | |
140f5369 | 1194 | if (!$threads_on) { |
5fe9b82b JH |
1195 | |
1196 | # On Windows and VMS, try launching a watchdog process | |
1197 | # using system(1, ...) (see perlport.pod) | |
4b0f0df6 | 1198 | if ($is_mswin || $is_vms) { |
5fe9b82b | 1199 | # On Windows, try to get the 'real' PID |
4b0f0df6 | 1200 | if ($is_mswin) { |
5fe9b82b JH |
1201 | eval { require Win32; }; |
1202 | if (defined(&Win32::GetCurrentProcessId)) { | |
1203 | $pid_to_kill = Win32::GetCurrentProcessId(); | |
1204 | } | |
087986a7 | 1205 | } |
087986a7 | 1206 | |
5fe9b82b JH |
1207 | # If we still have a fake PID, we can't use this method at all |
1208 | return if ($pid_to_kill <= 0); | |
1209 | ||
1210 | # Launch watchdog process | |
1211 | my $watchdog; | |
1212 | eval { | |
1213 | local $SIG{'__WARN__'} = sub { | |
1214 | _diag("Watchdog warning: $_[0]"); | |
1215 | }; | |
4b0f0df6 | 1216 | my $sig = $is_vms ? 'TERM' : 'KILL'; |
9b7a5066 CB |
1217 | my $cmd = _create_runperl( prog => "sleep($timeout);" . |
1218 | "warn qq/# $timeout_msg" . '\n/;' . | |
c1c45e36 | 1219 | "kill($sig, $pid_to_kill);"); |
9b7a5066 | 1220 | $watchdog = system(1, $cmd); |
5fe9b82b JH |
1221 | }; |
1222 | if ($@ || ($watchdog <= 0)) { | |
1223 | _diag('Failed to start watchdog'); | |
1224 | _diag($@) if $@; | |
1225 | undef($watchdog); | |
1226 | return; | |
1227 | } | |
087986a7 | 1228 | |
5fe9b82b JH |
1229 | # Add END block to parent to terminate and |
1230 | # clean up watchdog process | |
7e1027b9 JH |
1231 | eval "END { local \$! = 0; local \$? = 0; |
1232 | wait() if kill('KILL', $watchdog); };"; | |
5fe9b82b | 1233 | return; |
087986a7 | 1234 | } |
087986a7 | 1235 | |
5fe9b82b JH |
1236 | # Try using fork() to generate a watchdog process |
1237 | my $watchdog; | |
1238 | eval { $watchdog = fork() }; | |
1239 | if (defined($watchdog)) { | |
1240 | if ($watchdog) { # Parent process | |
1241 | # Add END block to parent to terminate and | |
1242 | # clean up watchdog process | |
7e1027b9 JH |
1243 | eval "END { local \$! = 0; local \$? = 0; |
1244 | wait() if kill('KILL', $watchdog); };"; | |
5fe9b82b JH |
1245 | return; |
1246 | } | |
1247 | ||
1248 | ### Watchdog process code | |
087986a7 | 1249 | |
5fe9b82b JH |
1250 | # Load POSIX if available |
1251 | eval { require POSIX; }; | |
087986a7 | 1252 | |
5fe9b82b JH |
1253 | # Execute the timeout |
1254 | sleep($timeout - 2) if ($timeout > 2); # Workaround for perlbug #49073 | |
1255 | sleep(2); | |
087986a7 | 1256 | |
5fe9b82b JH |
1257 | # Kill test process if still running |
1258 | if (kill(0, $pid_to_kill)) { | |
1259 | _diag($timeout_msg); | |
1260 | kill('KILL', $pid_to_kill); | |
1261 | } | |
087986a7 | 1262 | |
5fe9b82b JH |
1263 | # Don't execute END block (added at beginning of this file) |
1264 | $NO_ENDING = 1; | |
087986a7 | 1265 | |
5fe9b82b JH |
1266 | # Terminate ourself (i.e., the watchdog) |
1267 | POSIX::_exit(1) if (defined(&POSIX::_exit)); | |
1268 | exit(1); | |
087986a7 JH |
1269 | } |
1270 | ||
5fe9b82b | 1271 | # fork() failed - fall through and try using a thread |
087986a7 JH |
1272 | } |
1273 | ||
5fe9b82b JH |
1274 | # Use a watchdog thread because either 'threads' is loaded, |
1275 | # or fork() failed | |
cb01154c | 1276 | if (eval {require threads; 1}) { |
b296285b | 1277 | 'threads'->create(sub { |
087986a7 JH |
1278 | # Load POSIX if available |
1279 | eval { require POSIX; }; | |
1280 | ||
1281 | # Execute the timeout | |
c1c45e36 | 1282 | my $time_left = $timeout; |
a6c9a815 | 1283 | do { |
11ea18f2 | 1284 | $time_left = $time_left - sleep($time_left); |
a6c9a815 | 1285 | } while ($time_left > 0); |
087986a7 JH |
1286 | |
1287 | # Kill the parent (and ourself) | |
5fe9b82b | 1288 | select(STDERR); $| = 1; |
087986a7 JH |
1289 | _diag($timeout_msg); |
1290 | POSIX::_exit(1) if (defined(&POSIX::_exit)); | |
4b0f0df6 | 1291 | my $sig = $is_vms ? 'TERM' : 'KILL'; |
c1c45e36 | 1292 | kill($sig, $pid_to_kill); |
087986a7 JH |
1293 | })->detach(); |
1294 | return; | |
1295 | } | |
1296 | ||
5fe9b82b | 1297 | # If everything above fails, then just use an alarm timeout |
5732108f | 1298 | WATCHDOG_VIA_ALARM: |
087986a7 JH |
1299 | if (eval { alarm($timeout); 1; }) { |
1300 | # Load POSIX if available | |
1301 | eval { require POSIX; }; | |
1302 | ||
1303 | # Alarm handler will do the actual 'killing' | |
1304 | $SIG{'ALRM'} = sub { | |
5fe9b82b | 1305 | select(STDERR); $| = 1; |
087986a7 JH |
1306 | _diag($timeout_msg); |
1307 | POSIX::_exit(1) if (defined(&POSIX::_exit)); | |
4b0f0df6 | 1308 | my $sig = $is_vms ? 'TERM' : 'KILL'; |
c1c45e36 | 1309 | kill($sig, $pid_to_kill); |
087986a7 JH |
1310 | }; |
1311 | } | |
1312 | } | |
1313 | ||
f69d9fdf KW |
1314 | my $cp_0037 = # EBCDIC code page 0037 |
1315 | '\x00\x01\x02\x03\x37\x2D\x2E\x2F\x16\x05\x25\x0B\x0C\x0D\x0E\x0F' . | |
1316 | '\x10\x11\x12\x13\x3C\x3D\x32\x26\x18\x19\x3F\x27\x1C\x1D\x1E\x1F' . | |
1317 | '\x40\x5A\x7F\x7B\x5B\x6C\x50\x7D\x4D\x5D\x5C\x4E\x6B\x60\x4B\x61' . | |
1318 | '\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\x7A\x5E\x4C\x7E\x6E\x6F' . | |
1319 | '\x7C\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xD1\xD2\xD3\xD4\xD5\xD6' . | |
1320 | '\xD7\xD8\xD9\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xBA\xE0\xBB\xB0\x6D' . | |
1321 | '\x79\x81\x82\x83\x84\x85\x86\x87\x88\x89\x91\x92\x93\x94\x95\x96' . | |
1322 | '\x97\x98\x99\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xC0\x4F\xD0\xA1\x07' . | |
1323 | '\x20\x21\x22\x23\x24\x15\x06\x17\x28\x29\x2A\x2B\x2C\x09\x0A\x1B' . | |
1324 | '\x30\x31\x1A\x33\x34\x35\x36\x08\x38\x39\x3A\x3B\x04\x14\x3E\xFF' . | |
1325 | '\x41\xAA\x4A\xB1\x9F\xB2\x6A\xB5\xBD\xB4\x9A\x8A\x5F\xCA\xAF\xBC' . | |
1326 | '\x90\x8F\xEA\xFA\xBE\xA0\xB6\xB3\x9D\xDA\x9B\x8B\xB7\xB8\xB9\xAB' . | |
1327 | '\x64\x65\x62\x66\x63\x67\x9E\x68\x74\x71\x72\x73\x78\x75\x76\x77' . | |
1328 | '\xAC\x69\xED\xEE\xEB\xEF\xEC\xBF\x80\xFD\xFE\xFB\xFC\xAD\xAE\x59' . | |
1329 | '\x44\x45\x42\x46\x43\x47\x9C\x48\x54\x51\x52\x53\x58\x55\x56\x57' . | |
1330 | '\x8C\x49\xCD\xCE\xCB\xCF\xCC\xE1\x70\xDD\xDE\xDB\xDC\x8D\x8E\xDF'; | |
1331 | ||
1332 | my $cp_1047 = # EBCDIC code page 1047 | |
1333 | '\x00\x01\x02\x03\x37\x2D\x2E\x2F\x16\x05\x15\x0B\x0C\x0D\x0E\x0F' . | |
1334 | '\x10\x11\x12\x13\x3C\x3D\x32\x26\x18\x19\x3F\x27\x1C\x1D\x1E\x1F' . | |
1335 | '\x40\x5A\x7F\x7B\x5B\x6C\x50\x7D\x4D\x5D\x5C\x4E\x6B\x60\x4B\x61' . | |
1336 | '\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\x7A\x5E\x4C\x7E\x6E\x6F' . | |
1337 | '\x7C\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xD1\xD2\xD3\xD4\xD5\xD6' . | |
1338 | '\xD7\xD8\xD9\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xAD\xE0\xBD\x5F\x6D' . | |
1339 | '\x79\x81\x82\x83\x84\x85\x86\x87\x88\x89\x91\x92\x93\x94\x95\x96' . | |
1340 | '\x97\x98\x99\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xC0\x4F\xD0\xA1\x07' . | |
1341 | '\x20\x21\x22\x23\x24\x25\x06\x17\x28\x29\x2A\x2B\x2C\x09\x0A\x1B' . | |
1342 | '\x30\x31\x1A\x33\x34\x35\x36\x08\x38\x39\x3A\x3B\x04\x14\x3E\xFF' . | |
1343 | '\x41\xAA\x4A\xB1\x9F\xB2\x6A\xB5\xBB\xB4\x9A\x8A\xB0\xCA\xAF\xBC' . | |
1344 | '\x90\x8F\xEA\xFA\xBE\xA0\xB6\xB3\x9D\xDA\x9B\x8B\xB7\xB8\xB9\xAB' . | |
1345 | '\x64\x65\x62\x66\x63\x67\x9E\x68\x74\x71\x72\x73\x78\x75\x76\x77' . | |
1346 | '\xAC\x69\xED\xEE\xEB\xEF\xEC\xBF\x80\xFD\xFE\xFB\xFC\xBA\xAE\x59' . | |
1347 | '\x44\x45\x42\x46\x43\x47\x9C\x48\x54\x51\x52\x53\x58\x55\x56\x57' . | |
1348 | '\x8C\x49\xCD\xCE\xCB\xCF\xCC\xE1\x70\xDD\xDE\xDB\xDC\x8D\x8E\xDF'; | |
1349 | ||
1350 | my $cp_bc = # EBCDIC code page POSiX-BC | |
1351 | '\x00\x01\x02\x03\x37\x2D\x2E\x2F\x16\x05\x15\x0B\x0C\x0D\x0E\x0F' . | |
1352 | '\x10\x11\x12\x13\x3C\x3D\x32\x26\x18\x19\x3F\x27\x1C\x1D\x1E\x1F' . | |
1353 | '\x40\x5A\x7F\x7B\x5B\x6C\x50\x7D\x4D\x5D\x5C\x4E\x6B\x60\x4B\x61' . | |
1354 | '\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\x7A\x5E\x4C\x7E\x6E\x6F' . | |
1355 | '\x7C\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xD1\xD2\xD3\xD4\xD5\xD6' . | |
1356 | '\xD7\xD8\xD9\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xBB\xBC\xBD\x6A\x6D' . | |
1357 | '\x4A\x81\x82\x83\x84\x85\x86\x87\x88\x89\x91\x92\x93\x94\x95\x96' . | |
1358 | '\x97\x98\x99\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xFB\x4F\xFD\xFF\x07' . | |
1359 | '\x20\x21\x22\x23\x24\x25\x06\x17\x28\x29\x2A\x2B\x2C\x09\x0A\x1B' . | |
1360 | '\x30\x31\x1A\x33\x34\x35\x36\x08\x38\x39\x3A\x3B\x04\x14\x3E\x5F' . | |
1361 | '\x41\xAA\xB0\xB1\x9F\xB2\xD0\xB5\x79\xB4\x9A\x8A\xBA\xCA\xAF\xA1' . | |
1362 | '\x90\x8F\xEA\xFA\xBE\xA0\xB6\xB3\x9D\xDA\x9B\x8B\xB7\xB8\xB9\xAB' . | |
1363 | '\x64\x65\x62\x66\x63\x67\x9E\x68\x74\x71\x72\x73\x78\x75\x76\x77' . | |
1364 | '\xAC\x69\xED\xEE\xEB\xEF\xEC\xBF\x80\xE0\xFE\xDD\xFC\xAD\xAE\x59' . | |
1365 | '\x44\x45\x42\x46\x43\x47\x9C\x48\x54\x51\x52\x53\x58\x55\x56\x57' . | |
1366 | '\x8C\x49\xCD\xCE\xCB\xCF\xCC\xE1\x70\xC0\xDE\xDB\xDC\x8D\x8E\xDF'; | |
1367 | ||
1368 | my $straight = # Avoid ranges | |
1369 | '\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F' . | |
1370 | '\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F' . | |
1371 | '\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2A\x2B\x2C\x2D\x2E\x2F' . | |
1372 | '\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3A\x3B\x3C\x3D\x3E\x3F' . | |
1373 | '\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4A\x4B\x4C\x4D\x4E\x4F' . | |
1374 | '\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5A\x5B\x5C\x5D\x5E\x5F' . | |
1375 | '\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6A\x6B\x6C\x6D\x6E\x6F' . | |
1376 | '\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7A\x7B\x7C\x7D\x7E\x7F' . | |
1377 | '\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F' . | |
1378 | '\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9A\x9B\x9C\x9D\x9E\x9F' . | |
1379 | '\xA0\xA1\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xAA\xAB\xAC\xAD\xAE\xAF' . | |
1380 | '\xB0\xB1\xB2\xB3\xB4\xB5\xB6\xB7\xB8\xB9\xBA\xBB\xBC\xBD\xBE\xBF' . | |
1381 | '\xC0\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xCA\xCB\xCC\xCD\xCE\xCF' . | |
1382 | '\xD0\xD1\xD2\xD3\xD4\xD5\xD6\xD7\xD8\xD9\xDA\xDB\xDC\xDD\xDE\xDF' . | |
1383 | '\xE0\xE1\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xEA\xEB\xEC\xED\xEE\xEF' . | |
1384 | '\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\xFA\xFB\xFC\xFD\xFE\xFF'; | |
1385 | ||
1386 | # The following 2 functions allow tests to work on both EBCDIC and | |
1387 | # ASCII-ish platforms. They convert string scalars between the native | |
1388 | # character set and the set of 256 characters which is usually called | |
1389 | # Latin1. | |
1390 | # | |
1391 | # These routines don't work on UTF-EBCDIC and UTF-8. | |
1392 | ||
1393 | sub native_to_latin1($) { | |
1394 | my $string = shift; | |
1395 | ||
1396 | return $string if ord('^') == 94; # ASCII, Latin1 | |
1397 | my $cp; | |
1398 | if (ord('^') == 95) { # EBCDIC 1047 | |
1399 | $cp = \$cp_1047; | |
1400 | } | |
1401 | elsif (ord('^') == 106) { # EBCDIC POSIX-BC | |
1402 | $cp = \$cp_bc; | |
1403 | } | |
1404 | elsif (ord('^') == 176) { # EBCDIC 037 */ | |
1405 | $cp = \$cp_0037; | |
1406 | } | |
1407 | else { | |
1408 | die "Unknown native character set"; | |
1409 | } | |
1410 | ||
1411 | eval '$string =~ tr/' . $$cp . '/' . $straight . '/'; | |
1412 | return $string; | |
1413 | } | |
1414 | ||
1415 | sub latin1_to_native($) { | |
1416 | my $string = shift; | |
1417 | ||
1418 | return $string if ord('^') == 94; # ASCII, Latin1 | |
1419 | my $cp; | |
1420 | if (ord('^') == 95) { # EBCDIC 1047 | |
1421 | $cp = \$cp_1047; | |
1422 | } | |
1423 | elsif (ord('^') == 106) { # EBCDIC POSIX-BC | |
1424 | $cp = \$cp_bc; | |
1425 | } | |
1426 | elsif (ord('^') == 176) { # EBCDIC 037 */ | |
1427 | $cp = \$cp_0037; | |
1428 | } | |
1429 | else { | |
1430 | die "Unknown native character set"; | |
1431 | } | |
1432 | ||
1433 | eval '$string =~ tr/' . $straight . '/' . $$cp . '/'; | |
1434 | return $string; | |
1435 | } | |
1436 | ||
883cce4a | 1437 | sub ord_latin1_to_native { |
ec0363d9 KW |
1438 | # given an input code point, return the platform's native |
1439 | # equivalent value. Anything above latin1 is itself. | |
883cce4a | 1440 | |
ec0363d9 KW |
1441 | my $ord = shift; |
1442 | return $ord if $ord > 255; | |
1443 | return ord latin1_to_native(chr $ord); | |
883cce4a KW |
1444 | } |
1445 | ||
1446 | sub ord_native_to_latin1 { | |
ec0363d9 KW |
1447 | # given an input platform code point, return the latin1 equivalent value. |
1448 | # Anything above latin1 is itself. | |
883cce4a | 1449 | |
ec0363d9 KW |
1450 | my $ord = shift; |
1451 | return $ord if $ord > 255; | |
1452 | return ord native_to_latin1(chr $ord); | |
883cce4a KW |
1453 | } |
1454 | ||
69026470 | 1455 | 1; |