2 # t/test.pl - most of Test::More functionality without the fuss
18 print STDOUT "1..$n\n";
24 if (defined $planned && $planned != $ran) {
25 print STDOUT "# Looks like you planned $planned tests but ran $ran.\n";
31 print STDOUT "1..0 - @_\n";
33 print STDOUT "1..0\n";
39 my ($pass, $where, $name, @mess) = @_;
40 # Do not try to microoptimize by factoring out the "not ".
44 # escape out '#' or it will interfere with '# skip' and such
46 $out = $pass ? "ok $test - $name" : "not ok $test - $name";
48 $out = $pass ? "ok $test" : "not ok $test";
51 $out .= " # TODO $TODO" if $TODO;
52 print STDOUT "$out\n";
55 print STDOUT "# Failed $where\n";
58 # Ensure that the message is properly escaped.
59 print STDOUT map { /^#/ ? "$_\n" : "# $_\n" }
60 map { split /\n/ } @mess if @mess;
68 my @caller = caller(1);
69 return "at $caller[1] line $caller[2]";
73 my ($pass, $name, @mess) = @_;
74 _ok($pass, _where(), $name, @mess);
79 return 'undef' unless defined $x;
86 my ($got, $expected, $name, @mess) = @_;
87 my $pass = $got eq $expected;
89 unshift(@mess, "# got "._q($got)."\n",
90 "# expected "._q($expected)."\n");
92 _ok($pass, _where(), $name, @mess);
96 my ($got, $isnt, $name, @mess) = @_;
97 my $pass = $got ne $isnt;
99 unshift(@mess, "# it should not be "._q($got)."\n",
102 _ok($pass, _where(), $name, @mess);
105 # Note: this isn't quite as fancy as Test::More::like().
107 my ($got, $expected, $name, @mess) = @_;
109 if (ref $expected eq 'Regexp') {
110 $pass = $got =~ $expected;
112 unshift(@mess, "# got '$got'\n");
115 $pass = $got =~ /$expected/;
117 unshift(@mess, "# got '$got'\n",
118 "# expected /$expected/\n");
121 _ok($pass, _where(), $name, @mess);
129 _ok(0, _where(), @_);
140 # Note: can't pass multipart messages since we try to
141 # be compatible with Test::More::skip().
144 my $n = @_ ? shift : 1;
146 print STDOUT "ok $test # skip: $why\n";
155 return 0 unless $#$ra == $#$rb;
156 for my $i (0..$#$ra) {
157 return 0 unless $ra->[$i] eq $rb->[$i];
167 _ok(!$@, _where(), "require $require");
175 _ok(!$@, _where(), "use $use");
178 # runperl - Runs a separate perl interpreter.
180 # switches => [ command-line switches ]
181 # nolib => 1 # don't use -I../lib (included by default)
182 # prog => one-liner (avoid quotes)
183 # progfile => perl script
184 # stdin => string to feed the stdin
185 # stderr => redirect stderr to stdout
186 # args => [ command-line arguments to the perl program ]
187 # verbose => print the command line
189 my $is_mswin = $^O eq 'MSWin32';
190 my $is_netware = $^O eq 'NetWare';
191 my $is_macos = $^O eq 'MacOS';
192 my $is_vms = $^O eq 'VMS';
195 my ($runperl, $args) = @_;
198 # In VMS protect with doublequotes because otherwise
199 # DCL will lowercase -- unless already doublequoted.
200 $_ = q(").$_.q(") if $is_vms && !/^\"/;
201 $$runperl .= ' ' . $_;
208 if ($args{switches}) {
209 _quote_args(\$runperl, $args{switches});
211 unless ($args{nolib}) {
213 $runperl .= ' -I::lib';
214 # Use UNIX style error messages instead of MPW style.
215 $runperl .= ' -MMac::err=unix' if $args{stderr};
218 $runperl .= ' "-I../lib"'; # doublequotes because of VMS
221 if (defined $args{prog}) {
222 if ($is_mswin || $is_netware || $is_vms) {
223 $runperl .= qq( -e ") . $args{prog} . qq(");
226 $runperl .= qq( -e ') . $args{prog} . qq(');
228 } elsif (defined $args{progfile}) {
229 $runperl .= qq( "$args{progfile}");
231 if (defined $args{stdin}) {
232 # so we don't try to put literal newlines and crs onto the
234 $args{stdin} =~ s/\n/\\n/g;
235 $args{stdin} =~ s/\r/\\r/g;
237 if ($is_mswin || $is_netware || $is_vms) {
238 $runperl = qq{$^X -e "print qq(} .
239 $args{stdin} . q{)" | } . $runperl;
242 $runperl = qq{$^X -e 'print qq(} .
243 $args{stdin} . q{)' | } . $runperl;
246 if (defined $args{args}) {
247 _quote_args(\$runperl, $args{args});
249 $runperl .= ' 2>&1' if $args{stderr} && !$is_macos;
250 $runperl .= " \xB3 Dev:Null" if !$args{stderr} && $is_macos;
251 if ($args{verbose}) {
252 my $runperldisplay = $runperl;
253 $runperldisplay =~ s/\n/\n\#/g;
254 print STDOUT "# $runperldisplay\n";
256 my $result = `$runperl`;
257 $result =~ s/\n\n/\n/ if $is_vms; # XXX pipes sometimes double these
263 print STDOUT "Bail out! @_\n";
268 # A way to display scalars containing control characters and Unicode.
270 map { join("", map { $_ > 255 ? sprintf("\\x{%x}", $_) : chr($_) =~ /[[:cntrl:]]/ ? sprintf("\\%03o", $_) : chr($_) } unpack("U*", $_)) } @_;
274 # A somewhat safer version of the sometimes wrong $^X.
277 unless (defined $Perl) {
281 eval "require Config; Config->import";
283 warn "test.pl had problems loading Config: $@";
286 $exe = $Config{_exe};
289 # This doesn't absolutize the path: beware of future chdirs().
290 # We could do File::Spec->abs2rel() but that does getcwd()s,
291 # which is a bit heavyweight to do here.
293 if ($Perl =~ /^perl\Q$exe\E$/i) {
294 my $perl = "perl$exe";
295 eval "require File::Spec";
297 warn "test.pl had problems loading File::Spec: $@";
300 $Perl = File::Spec->catfile(File::Spec->curdir(), $perl);
304 warn "which_perl: cannot find $Perl from $^X" unless -f $Perl;
306 # For subcommands to use.
307 $ENV{PERLEXE} = $Perl;