This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
(was Re: [PATCH perl@13600] cat2type in tests for VMS)
[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
JH
25 if (!$NO_ENDING && defined $planned && $planned != $ran) {
26 print STDOUT "# Looks like you planned $planned tests but ran $ran.\n";
69026470
JH
27 }
28}
29
30sub skip_all {
31 if (@_) {
ad20d923 32 print STDOUT "1..0 - @_\n";
69026470 33 } else {
ad20d923 34 print STDOUT "1..0\n";
69026470
JH
35 }
36 exit(0);
37}
38
39sub _ok {
7d932aad 40 my ($pass, $where, $name, @mess) = @_;
69026470
JH
41 # Do not try to microoptimize by factoring out the "not ".
42 # VMS will avenge.
7d932aad
MS
43 my $out;
44 if ($name) {
b734d6c9
MS
45 # escape out '#' or it will interfere with '# skip' and such
46 $name =~ s/#/\\#/g;
7d932aad 47 $out = $pass ? "ok $test - $name" : "not ok $test - $name";
69026470 48 } else {
7d932aad 49 $out = $pass ? "ok $test" : "not ok $test";
69026470 50 }
7d932aad
MS
51
52 $out .= " # TODO $TODO" if $TODO;
ad20d923 53 print STDOUT "$out\n";
7d932aad 54
69026470 55 unless ($pass) {
ad20d923 56 print STDOUT "# Failed $where\n";
69026470 57 }
7d932aad
MS
58
59 # Ensure that the message is properly escaped.
ad20d923
MS
60 print STDOUT map { /^#/ ? "$_\n" : "# $_\n" }
61 map { split /\n/ } @mess if @mess;
7d932aad 62
69026470 63 $test++;
1577bb16
MS
64
65 return $pass;
69026470
JH
66}
67
68sub _where {
69 my @caller = caller(1);
70 return "at $caller[1] line $caller[2]";
71}
72
73sub ok {
7d932aad
MS
74 my ($pass, $name, @mess) = @_;
75 _ok($pass, _where(), $name, @mess);
69026470
JH
76}
77
b3c72391
JH
78sub _q {
79 my $x = shift;
80 return 'undef' unless defined $x;
81 my $q = $x;
677fb045 82 $q =~ s/\\/\\\\/;
b3c72391
JH
83 $q =~ s/'/\\'/;
84 return "'$q'";
85}
86
677fb045
NC
87sub _qq {
88 my $x = shift;
89 return defined $x ? '"' . display ($x) . '"' : 'undef';
90};
91
92# keys are the codes \n etc map to, values are 2 char strings such as \n
93my %backslash_escape;
94foreach my $x (split //, 'nrtfa\\\'"') {
95 $backslash_escape{ord eval "\"\\$x\""} = "\\$x";
96}
97# A way to display scalars containing control characters and Unicode.
98# Trying to avoid setting $_, or relying on local $_ to work.
99sub display {
100 my @result;
101 foreach my $x (@_) {
102 if (defined $x and not ref $x) {
103 my $y = '';
104 foreach my $c (unpack("U*", $x)) {
105 if ($c > 255) {
106 $y .= sprintf "\\x{%x}", $c;
107 } elsif ($backslash_escape{$c}) {
108 $y .= $backslash_escape{$c};
109 } else {
110 my $z = chr $c; # Maybe we can get away with a literal...
111 $z = sprintf "\\%03o", $c if $z =~ /[[:^print:]]/;
112 $y .= $z;
113 }
114 }
115 $x = $y;
116 }
117 return $x unless wantarray;
118 push @result, $x;
119 }
120 return @result;
121}
122
69026470 123sub is {
7d932aad 124 my ($got, $expected, $name, @mess) = @_;
69026470
JH
125 my $pass = $got eq $expected;
126 unless ($pass) {
b3c72391
JH
127 unshift(@mess, "# got "._q($got)."\n",
128 "# expected "._q($expected)."\n");
69026470 129 }
7d932aad 130 _ok($pass, _where(), $name, @mess);
69026470
JH
131}
132
3e90d5a3
MS
133sub isnt {
134 my ($got, $isnt, $name, @mess) = @_;
135 my $pass = $got ne $isnt;
136 unless( $pass ) {
b3c72391 137 unshift(@mess, "# it should not be "._q($got)."\n",
3e90d5a3
MS
138 "# but it is.\n");
139 }
140 _ok($pass, _where(), $name, @mess);
141}
142
69026470
JH
143# Note: this isn't quite as fancy as Test::More::like().
144sub like {
7d932aad 145 my ($got, $expected, $name, @mess) = @_;
69026470
JH
146 my $pass;
147 if (ref $expected eq 'Regexp') {
148 $pass = $got =~ $expected;
149 unless ($pass) {
7d932aad 150 unshift(@mess, "# got '$got'\n");
69026470
JH
151 }
152 } else {
153 $pass = $got =~ /$expected/;
154 unless ($pass) {
7d932aad
MS
155 unshift(@mess, "# got '$got'\n",
156 "# expected /$expected/\n");
69026470
JH
157 }
158 }
7d932aad 159 _ok($pass, _where(), $name, @mess);
69026470
JH
160}
161
162sub pass {
163 _ok(1, '', @_);
164}
165
166sub fail {
167 _ok(0, _where(), @_);
168}
169
ad20d923
MS
170sub curr_test {
171 return $test;
172}
173
3e90d5a3
MS
174sub next_test {
175 $test++
176}
177
69026470
JH
178# Note: can't pass multipart messages since we try to
179# be compatible with Test::More::skip().
180sub skip {
7d932aad 181 my $why = shift;
982b7cb7 182 my $n = @_ ? shift : 1;
69026470 183 for (1..$n) {
ad20d923 184 print STDOUT "ok $test # skip: $why\n";
e6c299c8 185 $test++;
69026470
JH
186 }
187 local $^W = 0;
188 last SKIP;
189}
190
191sub eq_array {
192 my ($ra, $rb) = @_;
193 return 0 unless $#$ra == $#$rb;
194 for my $i (0..$#$ra) {
195 return 0 unless $ra->[$i] eq $rb->[$i];
196 }
197 return 1;
198}
199
677fb045
NC
200sub eq_hash {
201 my ($orig, $suspect) = @_;
202 my $fail;
203 while (my ($key, $value) = each %$suspect) {
204 # Force a hash recompute if this perl's internals can cache the hash key.
205 $key = "" . $key;
206 if (exists $orig->{$key}) {
207 if ($orig->{$key} ne $value) {
208 print "# key ", _qq($key), " was ", _qq($orig->{$key}),
209 " now ", _qq($value), "\n";
210 $fail = 1;
211 }
212 } else {
213 print "# key ", _qq($key), " is ", _qq($value), ", not in original.\n";
214 $fail = 1;
215 }
216 }
217 foreach (keys %$orig) {
218 # Force a hash recompute if this perl's internals can cache the hash key.
219 $_ = "" . $_;
220 next if (exists $suspect->{$_});
221 print "# key ", _qq($_), " was ", _qq($orig->{$_}), " now missing.\n";
222 $fail = 1;
223 }
224 !$fail;
225}
226
69026470
JH
227sub require_ok {
228 my ($require) = @_;
229 eval <<REQUIRE_OK;
230require $require;
231REQUIRE_OK
1577bb16 232 _ok(!$@, _where(), "require $require");
69026470
JH
233}
234
235sub use_ok {
236 my ($use) = @_;
237 eval <<USE_OK;
238use $use;
239USE_OK
1577bb16 240 _ok(!$@, _where(), "use $use");
69026470
JH
241}
242
137352a2
RGS
243# runperl - Runs a separate perl interpreter.
244# Arguments :
245# switches => [ command-line switches ]
246# nolib => 1 # don't use -I../lib (included by default)
247# prog => one-liner (avoid quotes)
248# progfile => perl script
249# stdin => string to feed the stdin
250# stderr => redirect stderr to stdout
251# args => [ command-line arguments to the perl program ]
cb9c5e20 252# verbose => print the command line
137352a2
RGS
253
254my $is_mswin = $^O eq 'MSWin32';
255my $is_netware = $^O eq 'NetWare';
256my $is_macos = $^O eq 'MacOS';
257my $is_vms = $^O eq 'VMS';
258
cb9c5e20
JH
259sub _quote_args {
260 my ($runperl, $args) = @_;
261
262 foreach (@$args) {
263 # In VMS protect with doublequotes because otherwise
264 # DCL will lowercase -- unless already doublequoted.
265 $_ = q(").$_.q(") if $is_vms && !/^\"/;
266 $$runperl .= ' ' . $_;
267 }
268}
269
137352a2
RGS
270sub runperl {
271 my %args = @_;
272 my $runperl = $^X;
f93a5f07 273 if ($args{switches}) {
cb9c5e20 274 _quote_args(\$runperl, $args{switches});
137352a2 275 }
f93a5f07
JH
276 unless ($args{nolib}) {
277 if ($is_macos) {
cb9c5e20 278 $runperl .= ' -I::lib';
f93a5f07 279 # Use UNIX style error messages instead of MPW style.
cb9c5e20 280 $runperl .= ' -MMac::err=unix' if $args{stderr};
137352a2
RGS
281 }
282 else {
cb9c5e20 283 $runperl .= ' "-I../lib"'; # doublequotes because of VMS
137352a2
RGS
284 }
285 }
286 if (defined $args{prog}) {
287 if ($is_mswin || $is_netware || $is_vms) {
288 $runperl .= qq( -e ") . $args{prog} . qq(");
289 }
290 else {
291 $runperl .= qq( -e ') . $args{prog} . qq(');
292 }
293 } elsif (defined $args{progfile}) {
294 $runperl .= qq( "$args{progfile}");
295 }
296 if (defined $args{stdin}) {
5ae09a77
MS
297 # so we don't try to put literal newlines and crs onto the
298 # command line.
299 $args{stdin} =~ s/\n/\\n/g;
300 $args{stdin} =~ s/\r/\\r/g;
301
137352a2 302 if ($is_mswin || $is_netware || $is_vms) {
f93a5f07 303 $runperl = qq{$^X -e "print qq(} .
137352a2
RGS
304 $args{stdin} . q{)" | } . $runperl;
305 }
306 else {
f93a5f07 307 $runperl = qq{$^X -e 'print qq(} .
137352a2
RGS
308 $args{stdin} . q{)' | } . $runperl;
309 }
310 }
311 if (defined $args{args}) {
cb9c5e20
JH
312 _quote_args(\$runperl, $args{args});
313 }
314 $runperl .= ' 2>&1' if $args{stderr} && !$is_macos;
315 $runperl .= " \xB3 Dev:Null" if !$args{stderr} && $is_macos;
316 if ($args{verbose}) {
317 my $runperldisplay = $runperl;
318 $runperldisplay =~ s/\n/\n\#/g;
ad20d923 319 print STDOUT "# $runperldisplay\n";
137352a2 320 }
137352a2
RGS
321 my $result = `$runperl`;
322 $result =~ s/\n\n/\n/ if $is_vms; # XXX pipes sometimes double these
323 return $result;
324}
325
8799135f
MS
326
327sub BAILOUT {
ad20d923 328 print STDOUT "Bail out! @_\n";
8799135f
MS
329 exit;
330}
331
b5fe401b 332# A somewhat safer version of the sometimes wrong $^X.
17a740d5
JH
333my $Perl;
334sub which_perl {
335 unless (defined $Perl) {
336 $Perl = $^X;
337
338 my $exe;
339 eval "require Config; Config->import";
85363d30 340 if ($@) {
17a740d5
JH
341 warn "test.pl had problems loading Config: $@";
342 $exe = '';
85363d30 343 } else {
17a740d5 344 $exe = $Config{_exe};
85363d30 345 }
da405c16 346 $exe = '' unless defined $exe;
17a740d5
JH
347
348 # This doesn't absolutize the path: beware of future chdirs().
349 # We could do File::Spec->abs2rel() but that does getcwd()s,
350 # which is a bit heavyweight to do here.
351
352 if ($Perl =~ /^perl\Q$exe\E$/i) {
8db06b02 353 my $perl = "perl$exe";
17a740d5
JH
354 eval "require File::Spec";
355 if ($@) {
356 warn "test.pl had problems loading File::Spec: $@";
8db06b02 357 $Perl = "./$perl";
17a740d5 358 } else {
8db06b02 359 $Perl = File::Spec->catfile(File::Spec->curdir(), $perl);
17a740d5
JH
360 }
361 }
362
c880be78
MS
363 # Its like this. stat on Cygwin treats 'perl' to mean 'perl.exe'
364 # but open does not. This can get confusing, so to be safe we
365 # always put the .exe on the end on Cygwin.
366 $Perl .= $exe if $^O eq 'cygwin' && $Perl !~ /\Q$exe\E$/;
367
8db06b02 368 warn "which_perl: cannot find $Perl from $^X" unless -f $Perl;
17a740d5
JH
369
370 # For subcommands to use.
371 $ENV{PERLEXE} = $Perl;
85363d30 372 }
17a740d5 373 return $Perl;
b5fe401b
MS
374}
375
69026470 3761;