2 # t/test.pl - most of Test::More functionality without the fuss
19 print STDOUT "1..$n\n";
25 if (!$NO_ENDING && defined $planned && $planned != $ran) {
26 print STDOUT "# Looks like you planned $planned tests but ran $ran.\n";
32 print STDOUT "1..0 - @_\n";
34 print STDOUT "1..0\n";
40 my ($pass, $where, $name, @mess) = @_;
41 # Do not try to microoptimize by factoring out the "not ".
45 # escape out '#' or it will interfere with '# skip' and such
47 $out = $pass ? "ok $test - $name" : "not ok $test - $name";
49 $out = $pass ? "ok $test" : "not ok $test";
52 $out .= " # TODO $TODO" if $TODO;
53 print STDOUT "$out\n";
56 print STDOUT "# Failed $where\n";
59 # Ensure that the message is properly escaped.
60 print STDOUT map { /^#/ ? "$_\n" : "# $_\n" }
61 map { split /\n/ } @mess if @mess;
69 my @caller = caller(1);
70 return "at $caller[1] line $caller[2]";
74 my ($pass, $name, @mess) = @_;
75 _ok($pass, _where(), $name, @mess);
80 return 'undef' unless defined $x;
87 my ($got, $expected, $name, @mess) = @_;
88 my $pass = $got eq $expected;
90 unshift(@mess, "# got "._q($got)."\n",
91 "# expected "._q($expected)."\n");
93 _ok($pass, _where(), $name, @mess);
97 my ($got, $isnt, $name, @mess) = @_;
98 my $pass = $got ne $isnt;
100 unshift(@mess, "# it should not be "._q($got)."\n",
103 _ok($pass, _where(), $name, @mess);
106 # Note: this isn't quite as fancy as Test::More::like().
108 my ($got, $expected, $name, @mess) = @_;
110 if (ref $expected eq 'Regexp') {
111 $pass = $got =~ $expected;
113 unshift(@mess, "# got '$got'\n");
116 $pass = $got =~ /$expected/;
118 unshift(@mess, "# got '$got'\n",
119 "# expected /$expected/\n");
122 _ok($pass, _where(), $name, @mess);
130 _ok(0, _where(), @_);
141 # Note: can't pass multipart messages since we try to
142 # be compatible with Test::More::skip().
145 my $n = @_ ? shift : 1;
147 print STDOUT "ok $test # skip: $why\n";
156 return 0 unless $#$ra == $#$rb;
157 for my $i (0..$#$ra) {
158 return 0 unless $ra->[$i] eq $rb->[$i];
168 _ok(!$@, _where(), "require $require");
176 _ok(!$@, _where(), "use $use");
179 # runperl - Runs a separate perl interpreter.
181 # switches => [ command-line switches ]
182 # nolib => 1 # don't use -I../lib (included by default)
183 # prog => one-liner (avoid quotes)
184 # progfile => perl script
185 # stdin => string to feed the stdin
186 # stderr => redirect stderr to stdout
187 # args => [ command-line arguments to the perl program ]
188 # verbose => print the command line
190 my $is_mswin = $^O eq 'MSWin32';
191 my $is_netware = $^O eq 'NetWare';
192 my $is_macos = $^O eq 'MacOS';
193 my $is_vms = $^O eq 'VMS';
196 my ($runperl, $args) = @_;
199 # In VMS protect with doublequotes because otherwise
200 # DCL will lowercase -- unless already doublequoted.
201 $_ = q(").$_.q(") if $is_vms && !/^\"/;
202 $$runperl .= ' ' . $_;
209 if ($args{switches}) {
210 _quote_args(\$runperl, $args{switches});
212 unless ($args{nolib}) {
214 $runperl .= ' -I::lib';
215 # Use UNIX style error messages instead of MPW style.
216 $runperl .= ' -MMac::err=unix' if $args{stderr};
219 $runperl .= ' "-I../lib"'; # doublequotes because of VMS
222 if (defined $args{prog}) {
223 if ($is_mswin || $is_netware || $is_vms) {
224 $runperl .= qq( -e ") . $args{prog} . qq(");
227 $runperl .= qq( -e ') . $args{prog} . qq(');
229 } elsif (defined $args{progfile}) {
230 $runperl .= qq( "$args{progfile}");
232 if (defined $args{stdin}) {
233 # so we don't try to put literal newlines and crs onto the
235 $args{stdin} =~ s/\n/\\n/g;
236 $args{stdin} =~ s/\r/\\r/g;
238 if ($is_mswin || $is_netware || $is_vms) {
239 $runperl = qq{$^X -e "print qq(} .
240 $args{stdin} . q{)" | } . $runperl;
243 $runperl = qq{$^X -e 'print qq(} .
244 $args{stdin} . q{)' | } . $runperl;
247 if (defined $args{args}) {
248 _quote_args(\$runperl, $args{args});
250 $runperl .= ' 2>&1' if $args{stderr} && !$is_macos;
251 $runperl .= " \xB3 Dev:Null" if !$args{stderr} && $is_macos;
252 if ($args{verbose}) {
253 my $runperldisplay = $runperl;
254 $runperldisplay =~ s/\n/\n\#/g;
255 print STDOUT "# $runperldisplay\n";
257 my $result = `$runperl`;
258 $result =~ s/\n\n/\n/ if $is_vms; # XXX pipes sometimes double these
264 print STDOUT "Bail out! @_\n";
269 # A way to display scalars containing control characters and Unicode.
271 map { join("", map { $_ > 255 ? sprintf("\\x{%x}", $_) : chr($_) =~ /[[:cntrl:]]/ ? sprintf("\\%03o", $_) : chr($_) } unpack("U*", $_)) } @_;
275 # A somewhat safer version of the sometimes wrong $^X.
278 unless (defined $Perl) {
282 eval "require Config; Config->import";
284 warn "test.pl had problems loading Config: $@";
287 $exe = $Config{_exe};
290 # This doesn't absolutize the path: beware of future chdirs().
291 # We could do File::Spec->abs2rel() but that does getcwd()s,
292 # which is a bit heavyweight to do here.
294 if ($Perl =~ /^perl\Q$exe\E$/i) {
295 my $perl = "perl$exe";
296 eval "require File::Spec";
298 warn "test.pl had problems loading File::Spec: $@";
301 $Perl = File::Spec->catfile(File::Spec->curdir(), $perl);
305 # Its like this. stat on Cygwin treats 'perl' to mean 'perl.exe'
306 # but open does not. This can get confusing, so to be safe we
307 # always put the .exe on the end on Cygwin.
308 $Perl .= $exe if $^O eq 'cygwin' && $Perl !~ /\Q$exe\E$/;
310 warn "which_perl: cannot find $Perl from $^X" unless -f $Perl;
312 # For subcommands to use.
313 $ENV{PERLEXE} = $Perl;