[PATCH lib/vmsish.t] Small test name abuse.
[p5sagit/p5-mst-13.2.git] / t / test.pl
CommitLineData
69026470 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 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 20 $planned = $n;
21}
22
23END {
24 my $ran = $test - 1;
b6345914 25 if (!$NO_ENDING && defined $planned && $planned != $ran) {
26 print STDOUT "# Looks like you planned $planned tests but ran $ran.\n";
69026470 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 35 }
36 exit(0);
37}
38
39sub _ok {
7d932aad 40 my ($pass, $where, $name, @mess) = @_;
69026470 41 # Do not try to microoptimize by factoring out the "not ".
42 # VMS will avenge.
7d932aad 43 my $out;
44 if ($name) {
b734d6c9 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 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 58
59 # Ensure that the message is properly escaped.
ad20d923 60 print STDOUT map { /^#/ ? "$_\n" : "# $_\n" }
61 map { split /\n/ } @mess if @mess;
7d932aad 62
69026470 63 $test++;
1577bb16 64
65 return $pass;
69026470 66}
67
68sub _where {
69 my @caller = caller(1);
70 return "at $caller[1] line $caller[2]";
71}
72
73sub ok {
7d932aad 74 my ($pass, $name, @mess) = @_;
75 _ok($pass, _where(), $name, @mess);
69026470 76}
77
b3c72391 78sub _q {
79 my $x = shift;
80 return 'undef' unless defined $x;
81 my $q = $x;
82 $q =~ s/'/\\'/;
83 return "'$q'";
84}
85
69026470 86sub is {
7d932aad 87 my ($got, $expected, $name, @mess) = @_;
69026470 88 my $pass = $got eq $expected;
89 unless ($pass) {
b3c72391 90 unshift(@mess, "# got "._q($got)."\n",
91 "# expected "._q($expected)."\n");
69026470 92 }
7d932aad 93 _ok($pass, _where(), $name, @mess);
69026470 94}
95
3e90d5a3 96sub isnt {
97 my ($got, $isnt, $name, @mess) = @_;
98 my $pass = $got ne $isnt;
99 unless( $pass ) {
b3c72391 100 unshift(@mess, "# it should not be "._q($got)."\n",
3e90d5a3 101 "# but it is.\n");
102 }
103 _ok($pass, _where(), $name, @mess);
104}
105
69026470 106# Note: this isn't quite as fancy as Test::More::like().
107sub like {
7d932aad 108 my ($got, $expected, $name, @mess) = @_;
69026470 109 my $pass;
110 if (ref $expected eq 'Regexp') {
111 $pass = $got =~ $expected;
112 unless ($pass) {
7d932aad 113 unshift(@mess, "# got '$got'\n");
69026470 114 }
115 } else {
116 $pass = $got =~ /$expected/;
117 unless ($pass) {
7d932aad 118 unshift(@mess, "# got '$got'\n",
119 "# expected /$expected/\n");
69026470 120 }
121 }
7d932aad 122 _ok($pass, _where(), $name, @mess);
69026470 123}
124
125sub pass {
126 _ok(1, '', @_);
127}
128
129sub fail {
130 _ok(0, _where(), @_);
131}
132
ad20d923 133sub curr_test {
134 return $test;
135}
136
3e90d5a3 137sub next_test {
138 $test++
139}
140
69026470 141# Note: can't pass multipart messages since we try to
142# be compatible with Test::More::skip().
143sub skip {
7d932aad 144 my $why = shift;
982b7cb7 145 my $n = @_ ? shift : 1;
69026470 146 for (1..$n) {
ad20d923 147 print STDOUT "ok $test # skip: $why\n";
e6c299c8 148 $test++;
69026470 149 }
150 local $^W = 0;
151 last SKIP;
152}
153
154sub eq_array {
155 my ($ra, $rb) = @_;
156 return 0 unless $#$ra == $#$rb;
157 for my $i (0..$#$ra) {
158 return 0 unless $ra->[$i] eq $rb->[$i];
159 }
160 return 1;
161}
162
163sub require_ok {
164 my ($require) = @_;
165 eval <<REQUIRE_OK;
166require $require;
167REQUIRE_OK
1577bb16 168 _ok(!$@, _where(), "require $require");
69026470 169}
170
171sub use_ok {
172 my ($use) = @_;
173 eval <<USE_OK;
174use $use;
175USE_OK
1577bb16 176 _ok(!$@, _where(), "use $use");
69026470 177}
178
137352a2 179# runperl - Runs a separate perl interpreter.
180# Arguments :
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 ]
cb9c5e20 188# verbose => print the command line
137352a2 189
190my $is_mswin = $^O eq 'MSWin32';
191my $is_netware = $^O eq 'NetWare';
192my $is_macos = $^O eq 'MacOS';
193my $is_vms = $^O eq 'VMS';
194
cb9c5e20 195sub _quote_args {
196 my ($runperl, $args) = @_;
197
198 foreach (@$args) {
199 # In VMS protect with doublequotes because otherwise
200 # DCL will lowercase -- unless already doublequoted.
201 $_ = q(").$_.q(") if $is_vms && !/^\"/;
202 $$runperl .= ' ' . $_;
203 }
204}
205
137352a2 206sub runperl {
207 my %args = @_;
208 my $runperl = $^X;
f93a5f07 209 if ($args{switches}) {
cb9c5e20 210 _quote_args(\$runperl, $args{switches});
137352a2 211 }
f93a5f07 212 unless ($args{nolib}) {
213 if ($is_macos) {
cb9c5e20 214 $runperl .= ' -I::lib';
f93a5f07 215 # Use UNIX style error messages instead of MPW style.
cb9c5e20 216 $runperl .= ' -MMac::err=unix' if $args{stderr};
137352a2 217 }
218 else {
cb9c5e20 219 $runperl .= ' "-I../lib"'; # doublequotes because of VMS
137352a2 220 }
221 }
222 if (defined $args{prog}) {
223 if ($is_mswin || $is_netware || $is_vms) {
224 $runperl .= qq( -e ") . $args{prog} . qq(");
225 }
226 else {
227 $runperl .= qq( -e ') . $args{prog} . qq(');
228 }
229 } elsif (defined $args{progfile}) {
230 $runperl .= qq( "$args{progfile}");
231 }
232 if (defined $args{stdin}) {
5ae09a77 233 # so we don't try to put literal newlines and crs onto the
234 # command line.
235 $args{stdin} =~ s/\n/\\n/g;
236 $args{stdin} =~ s/\r/\\r/g;
237
137352a2 238 if ($is_mswin || $is_netware || $is_vms) {
f93a5f07 239 $runperl = qq{$^X -e "print qq(} .
137352a2 240 $args{stdin} . q{)" | } . $runperl;
241 }
242 else {
f93a5f07 243 $runperl = qq{$^X -e 'print qq(} .
137352a2 244 $args{stdin} . q{)' | } . $runperl;
245 }
246 }
247 if (defined $args{args}) {
cb9c5e20 248 _quote_args(\$runperl, $args{args});
249 }
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;
ad20d923 255 print STDOUT "# $runperldisplay\n";
137352a2 256 }
137352a2 257 my $result = `$runperl`;
258 $result =~ s/\n\n/\n/ if $is_vms; # XXX pipes sometimes double these
259 return $result;
260}
261
8799135f 262
263sub BAILOUT {
ad20d923 264 print STDOUT "Bail out! @_\n";
8799135f 265 exit;
266}
267
268
6cb8f8aa 269# A way to display scalars containing control characters and Unicode.
270sub display {
c3ded147 271 map { join("", map { $_ > 255 ? sprintf("\\x{%x}", $_) : chr($_) =~ /[[:cntrl:]]/ ? sprintf("\\%03o", $_) : chr($_) } unpack("U*", $_)) } @_;
6cb8f8aa 272}
273
274
b5fe401b 275# A somewhat safer version of the sometimes wrong $^X.
17a740d5 276my $Perl;
277sub which_perl {
278 unless (defined $Perl) {
279 $Perl = $^X;
280
281 my $exe;
282 eval "require Config; Config->import";
85363d30 283 if ($@) {
17a740d5 284 warn "test.pl had problems loading Config: $@";
285 $exe = '';
85363d30 286 } else {
17a740d5 287 $exe = $Config{_exe};
85363d30 288 }
17a740d5 289
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.
293
294 if ($Perl =~ /^perl\Q$exe\E$/i) {
8db06b02 295 my $perl = "perl$exe";
17a740d5 296 eval "require File::Spec";
297 if ($@) {
298 warn "test.pl had problems loading File::Spec: $@";
8db06b02 299 $Perl = "./$perl";
17a740d5 300 } else {
8db06b02 301 $Perl = File::Spec->catfile(File::Spec->curdir(), $perl);
17a740d5 302 }
303 }
304
8db06b02 305 warn "which_perl: cannot find $Perl from $^X" unless -f $Perl;
17a740d5 306
307 # For subcommands to use.
308 $ENV{PERLEXE} = $Perl;
85363d30 309 }
17a740d5 310 return $Perl;
b5fe401b 311}
312
69026470 3131;