This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Re: [Patch @14129] fixes Unicode::Normalize
[perl5.git] / t / test.pl
CommitLineData
69026470
JH
1#
2# t/test.pl - most of Test::More functionality without the fuss
3#
4
5my $test = 1;
6my $planned;
7
7d932aad 8$TODO = 0;
b6345914 9$NO_ENDING = 0;
7d932aad 10
69026470
JH
11sub plan {
12 my $n;
13 if (@_ == 1) {
14 $n = shift;
15 } else {
16 my %plan = @_;
17 $n = $plan{tests};
18 }
ad20d923 19 print STDOUT "1..$n\n";
69026470
JH
20 $planned = $n;
21}
22
23END {
24 my $ran = $test - 1;
b6345914 25 if (!$NO_ENDING && defined $planned && $planned != $ran) {
75385f53 26 print STDERR "# Looks like you planned $planned tests but ran $ran.\n";
69026470
JH
27 }
28}
29
de522f7a
MS
30# Use this instead of "print STDERR" when outputing failure diagnostic
31# messages
32sub _diag {
33 my $fh = $TODO ? *STDOUT : *STDERR;
34 print $fh @_;
35}
36
69026470
JH
37sub skip_all {
38 if (@_) {
ad20d923 39 print STDOUT "1..0 - @_\n";
69026470 40 } else {
ad20d923 41 print STDOUT "1..0\n";
69026470
JH
42 }
43 exit(0);
44}
45
46sub _ok {
7d932aad 47 my ($pass, $where, $name, @mess) = @_;
69026470
JH
48 # Do not try to microoptimize by factoring out the "not ".
49 # VMS will avenge.
7d932aad
MS
50 my $out;
51 if ($name) {
b734d6c9
MS
52 # escape out '#' or it will interfere with '# skip' and such
53 $name =~ s/#/\\#/g;
7d932aad 54 $out = $pass ? "ok $test - $name" : "not ok $test - $name";
69026470 55 } else {
7d932aad 56 $out = $pass ? "ok $test" : "not ok $test";
69026470 57 }
7d932aad
MS
58
59 $out .= " # TODO $TODO" if $TODO;
ad20d923 60 print STDOUT "$out\n";
7d932aad 61
69026470 62 unless ($pass) {
de522f7a 63 _diag "# Failed $where\n";
69026470 64 }
7d932aad
MS
65
66 # Ensure that the message is properly escaped.
de522f7a
MS
67 _diag map { /^#/ ? "$_\n" : "# $_\n" }
68 map { split /\n/ } @mess if @mess;
7d932aad 69
69026470 70 $test++;
1577bb16
MS
71
72 return $pass;
69026470
JH
73}
74
75sub _where {
76 my @caller = caller(1);
77 return "at $caller[1] line $caller[2]";
78}
79
80sub ok {
7d932aad
MS
81 my ($pass, $name, @mess) = @_;
82 _ok($pass, _where(), $name, @mess);
69026470
JH
83}
84
b3c72391
JH
85sub _q {
86 my $x = shift;
87 return 'undef' unless defined $x;
88 my $q = $x;
677fb045 89 $q =~ s/\\/\\\\/;
b3c72391
JH
90 $q =~ s/'/\\'/;
91 return "'$q'";
92}
93
677fb045
NC
94sub _qq {
95 my $x = shift;
96 return defined $x ? '"' . display ($x) . '"' : 'undef';
97};
98
99# keys are the codes \n etc map to, values are 2 char strings such as \n
100my %backslash_escape;
101foreach my $x (split //, 'nrtfa\\\'"') {
102 $backslash_escape{ord eval "\"\\$x\""} = "\\$x";
103}
104# A way to display scalars containing control characters and Unicode.
105# Trying to avoid setting $_, or relying on local $_ to work.
106sub display {
107 my @result;
108 foreach my $x (@_) {
109 if (defined $x and not ref $x) {
110 my $y = '';
111 foreach my $c (unpack("U*", $x)) {
112 if ($c > 255) {
113 $y .= sprintf "\\x{%x}", $c;
114 } elsif ($backslash_escape{$c}) {
115 $y .= $backslash_escape{$c};
116 } else {
117 my $z = chr $c; # Maybe we can get away with a literal...
118 $z = sprintf "\\%03o", $c if $z =~ /[[:^print:]]/;
119 $y .= $z;
120 }
121 }
122 $x = $y;
123 }
124 return $x unless wantarray;
125 push @result, $x;
126 }
127 return @result;
128}
129
69026470 130sub is {
7d932aad 131 my ($got, $expected, $name, @mess) = @_;
69026470
JH
132 my $pass = $got eq $expected;
133 unless ($pass) {
b3c72391
JH
134 unshift(@mess, "# got "._q($got)."\n",
135 "# expected "._q($expected)."\n");
69026470 136 }
7d932aad 137 _ok($pass, _where(), $name, @mess);
69026470
JH
138}
139
3e90d5a3
MS
140sub isnt {
141 my ($got, $isnt, $name, @mess) = @_;
142 my $pass = $got ne $isnt;
143 unless( $pass ) {
b3c72391 144 unshift(@mess, "# it should not be "._q($got)."\n",
3e90d5a3
MS
145 "# but it is.\n");
146 }
147 _ok($pass, _where(), $name, @mess);
148}
149
69026470
JH
150# Note: this isn't quite as fancy as Test::More::like().
151sub like {
7d932aad 152 my ($got, $expected, $name, @mess) = @_;
69026470
JH
153 my $pass;
154 if (ref $expected eq 'Regexp') {
155 $pass = $got =~ $expected;
156 unless ($pass) {
435e7af6
NC
157 unshift(@mess, "# got '$got'\n",
158 "# expected /$expected/\n");
69026470
JH
159 }
160 } else {
161 $pass = $got =~ /$expected/;
162 unless ($pass) {
7d932aad
MS
163 unshift(@mess, "# got '$got'\n",
164 "# expected /$expected/\n");
69026470
JH
165 }
166 }
7d932aad 167 _ok($pass, _where(), $name, @mess);
69026470
JH
168}
169
170sub pass {
171 _ok(1, '', @_);
172}
173
174sub fail {
175 _ok(0, _where(), @_);
176}
177
ad20d923
MS
178sub curr_test {
179 return $test;
180}
181
3e90d5a3
MS
182sub next_test {
183 $test++
184}
185
69026470
JH
186# Note: can't pass multipart messages since we try to
187# be compatible with Test::More::skip().
188sub skip {
7d932aad 189 my $why = shift;
982b7cb7 190 my $n = @_ ? shift : 1;
69026470 191 for (1..$n) {
ad20d923 192 print STDOUT "ok $test # skip: $why\n";
e6c299c8 193 $test++;
69026470
JH
194 }
195 local $^W = 0;
196 last SKIP;
197}
198
199sub eq_array {
200 my ($ra, $rb) = @_;
201 return 0 unless $#$ra == $#$rb;
202 for my $i (0..$#$ra) {
203 return 0 unless $ra->[$i] eq $rb->[$i];
204 }
205 return 1;
206}
207
677fb045
NC
208sub eq_hash {
209 my ($orig, $suspect) = @_;
210 my $fail;
211 while (my ($key, $value) = each %$suspect) {
212 # Force a hash recompute if this perl's internals can cache the hash key.
213 $key = "" . $key;
214 if (exists $orig->{$key}) {
215 if ($orig->{$key} ne $value) {
de522f7a
MS
216 print STDOUT "# key ", _qq($key), " was ", _qq($orig->{$key}),
217 " now ", _qq($value), "\n";
677fb045
NC
218 $fail = 1;
219 }
220 } else {
de522f7a 221 print STDOUT "# key ", _qq($key), " is ", _qq($value),
75385f53 222 ", not in original.\n";
677fb045
NC
223 $fail = 1;
224 }
225 }
226 foreach (keys %$orig) {
227 # Force a hash recompute if this perl's internals can cache the hash key.
228 $_ = "" . $_;
229 next if (exists $suspect->{$_});
de522f7a 230 print STDOUT "# key ", _qq($_), " was ", _qq($orig->{$_}), " now missing.\n";
677fb045
NC
231 $fail = 1;
232 }
233 !$fail;
234}
235
69026470
JH
236sub require_ok {
237 my ($require) = @_;
238 eval <<REQUIRE_OK;
239require $require;
240REQUIRE_OK
1577bb16 241 _ok(!$@, _where(), "require $require");
69026470
JH
242}
243
244sub use_ok {
245 my ($use) = @_;
246 eval <<USE_OK;
247use $use;
248USE_OK
1577bb16 249 _ok(!$@, _where(), "use $use");
69026470
JH
250}
251
137352a2
RGS
252# runperl - Runs a separate perl interpreter.
253# Arguments :
254# switches => [ command-line switches ]
255# nolib => 1 # don't use -I../lib (included by default)
256# prog => one-liner (avoid quotes)
257# progfile => perl script
258# stdin => string to feed the stdin
259# stderr => redirect stderr to stdout
260# args => [ command-line arguments to the perl program ]
cb9c5e20 261# verbose => print the command line
137352a2
RGS
262
263my $is_mswin = $^O eq 'MSWin32';
264my $is_netware = $^O eq 'NetWare';
265my $is_macos = $^O eq 'MacOS';
266my $is_vms = $^O eq 'VMS';
267
cb9c5e20
JH
268sub _quote_args {
269 my ($runperl, $args) = @_;
270
271 foreach (@$args) {
272 # In VMS protect with doublequotes because otherwise
273 # DCL will lowercase -- unless already doublequoted.
274 $_ = q(").$_.q(") if $is_vms && !/^\"/;
275 $$runperl .= ' ' . $_;
276 }
277}
278
137352a2
RGS
279sub runperl {
280 my %args = @_;
281 my $runperl = $^X;
f93a5f07 282 if ($args{switches}) {
8da5ed17 283 _quote_args(\$runperl, $args{switches});
137352a2 284 }
f93a5f07
JH
285 unless ($args{nolib}) {
286 if ($is_macos) {
cb9c5e20 287 $runperl .= ' -I::lib';
f93a5f07 288 # Use UNIX style error messages instead of MPW style.
cb9c5e20 289 $runperl .= ' -MMac::err=unix' if $args{stderr};
137352a2
RGS
290 }
291 else {
cb9c5e20 292 $runperl .= ' "-I../lib"'; # doublequotes because of VMS
137352a2
RGS
293 }
294 }
295 if (defined $args{prog}) {
296 if ($is_mswin || $is_netware || $is_vms) {
297 $runperl .= qq( -e ") . $args{prog} . qq(");
298 }
299 else {
300 $runperl .= qq( -e ') . $args{prog} . qq(');
301 }
302 } elsif (defined $args{progfile}) {
303 $runperl .= qq( "$args{progfile}");
304 }
305 if (defined $args{stdin}) {
5ae09a77
MS
306 # so we don't try to put literal newlines and crs onto the
307 # command line.
308 $args{stdin} =~ s/\n/\\n/g;
309 $args{stdin} =~ s/\r/\\r/g;
310
137352a2 311 if ($is_mswin || $is_netware || $is_vms) {
f93a5f07 312 $runperl = qq{$^X -e "print qq(} .
137352a2
RGS
313 $args{stdin} . q{)" | } . $runperl;
314 }
315 else {
f93a5f07 316 $runperl = qq{$^X -e 'print qq(} .
137352a2
RGS
317 $args{stdin} . q{)' | } . $runperl;
318 }
319 }
320 if (defined $args{args}) {
cb9c5e20
JH
321 _quote_args(\$runperl, $args{args});
322 }
323 $runperl .= ' 2>&1' if $args{stderr} && !$is_macos;
324 $runperl .= " \xB3 Dev:Null" if !$args{stderr} && $is_macos;
325 if ($args{verbose}) {
326 my $runperldisplay = $runperl;
327 $runperldisplay =~ s/\n/\n\#/g;
75385f53 328 print STDERR "# $runperldisplay\n";
137352a2 329 }
137352a2
RGS
330 my $result = `$runperl`;
331 $result =~ s/\n\n/\n/ if $is_vms; # XXX pipes sometimes double these
332 return $result;
333}
334
8799135f 335
c4fbe247 336sub DIE {
75385f53 337 print STDERR "# @_\n";
c4fbe247 338 exit 1;
8799135f
MS
339}
340
b5fe401b 341# A somewhat safer version of the sometimes wrong $^X.
17a740d5
JH
342my $Perl;
343sub which_perl {
344 unless (defined $Perl) {
345 $Perl = $^X;
346
347 my $exe;
348 eval "require Config; Config->import";
85363d30 349 if ($@) {
17a740d5
JH
350 warn "test.pl had problems loading Config: $@";
351 $exe = '';
85363d30 352 } else {
17a740d5 353 $exe = $Config{_exe};
85363d30 354 }
da405c16 355 $exe = '' unless defined $exe;
17a740d5
JH
356
357 # This doesn't absolutize the path: beware of future chdirs().
358 # We could do File::Spec->abs2rel() but that does getcwd()s,
359 # which is a bit heavyweight to do here.
360
361 if ($Perl =~ /^perl\Q$exe\E$/i) {
8db06b02 362 my $perl = "perl$exe";
17a740d5
JH
363 eval "require File::Spec";
364 if ($@) {
365 warn "test.pl had problems loading File::Spec: $@";
8db06b02 366 $Perl = "./$perl";
17a740d5 367 } else {
8db06b02 368 $Perl = File::Spec->catfile(File::Spec->curdir(), $perl);
17a740d5
JH
369 }
370 }
196918b0
PG
371
372 # Build up the name of the executable file from the name of
373 # the command.
374
375 if ($Perl !~ /\Q$exe\E$/i) {
376 $Perl .= $exe;
377 }
c880be78 378
8db06b02 379 warn "which_perl: cannot find $Perl from $^X" unless -f $Perl;
17a740d5
JH
380
381 # For subcommands to use.
382 $ENV{PERLEXE} = $Perl;
85363d30 383 }
17a740d5 384 return $Perl;
b5fe401b
MS
385}
386
435e7af6
NC
387sub unlink_all {
388 foreach my $file (@_) {
389 1 while unlink $file;
75385f53 390 print STDERR "# Couldn't unlink '$file': $!\n" if -f $file;
435e7af6
NC
391 }
392}
eeabcb2d
MS
393
394
395my $tmpfile = "misctmp000";
3961 while -f ++$tmpfile;
397END { unlink_all $tmpfile }
398
399sub kill_perl {
400 my($prog, $expected, $runperl_args, $name) = @_;
401
402 $runperl_args ||= {};
403 $runperl_args->{progfile} = $tmpfile;
404 $runperl_args->{stderr} = 1;
405
406 open TEST, ">$tmpfile" or die "Cannot open $tmpfile: $!";
407
408 # VMS adjustments
409 if( $^O eq 'VMS' ) {
410 $prog =~ s#/dev/null#NL:#;
411
412 # VMS file locking
413 $prog =~ s{if \(-e _ and -f _ and -r _\)}
414 {if (-e _ and -f _)}
415 }
416
417 print TEST $prog, "\n";
418 close TEST or die "Cannot close $tmpfile: $!";
419
420 my $results = runperl(%$runperl_args);
421 my $status = $?;
422
423 # Clean up the results into something a bit more predictable.
424 $results =~ s/\n+$//;
425 $results =~ s/at\s+misctmp\d+\s+line/at - line/g;
426 $results =~ s/of\s+misctmp\d+\s+aborted/of - aborted/g;
427
428 # bison says 'parse error' instead of 'syntax error',
429 # various yaccs may or may not capitalize 'syntax'.
430 $results =~ s/^(syntax|parse) error/syntax error/mig;
431
432 if ($^O eq 'VMS') {
433 # some tests will trigger VMS messages that won't be expected
434 $results =~ s/\n?%[A-Z]+-[SIWEF]-[A-Z]+,.*//;
435
436 # pipes double these sometimes
437 $results =~ s/\n\n/\n/g;
438 }
439
440 $expected =~ s/\n+$//;
441
442 my $pass = $results eq $expected;
443 unless ($pass) {
444 print STDERR "# PROG: $switch\n$prog\n";
445 print STDERR "# EXPECTED:\n$expected\n";
446 print STDERR "# GOT:\n$results\n";
447 print STDERR "# STATUS: $status\n";
448 }
449
450 ($name) = $prog =~ /^(.{1,35})/ unless $name;
451
452 _ok($pass, _where(), "kill_perl - $name");
453}
454
69026470 4551;