Commit | Line | Data |
---|---|---|
69026470 JH |
1 | # |
2 | # t/test.pl - most of Test::More functionality without the fuss | |
3 | # | |
4 | ||
5 | my $test = 1; | |
6 | my $planned; | |
7 | ||
7d932aad | 8 | $TODO = 0; |
b6345914 | 9 | $NO_ENDING = 0; |
7d932aad | 10 | |
69026470 JH |
11 | sub 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 | ||
23 | END { | |
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 | ||
30 | sub 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 | ||
39 | sub _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) { |
75385f53 | 56 | print STDERR "# Failed $where\n"; |
69026470 | 57 | } |
7d932aad MS |
58 | |
59 | # Ensure that the message is properly escaped. | |
75385f53 | 60 | print STDERR map { /^#/ ? "$_\n" : "# $_\n" } |
ad20d923 | 61 | map { split /\n/ } @mess if @mess; |
7d932aad | 62 | |
69026470 | 63 | $test++; |
1577bb16 MS |
64 | |
65 | return $pass; | |
69026470 JH |
66 | } |
67 | ||
68 | sub _where { | |
69 | my @caller = caller(1); | |
70 | return "at $caller[1] line $caller[2]"; | |
71 | } | |
72 | ||
73 | sub ok { | |
7d932aad MS |
74 | my ($pass, $name, @mess) = @_; |
75 | _ok($pass, _where(), $name, @mess); | |
69026470 JH |
76 | } |
77 | ||
b3c72391 JH |
78 | sub _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 |
87 | sub _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 | |
93 | my %backslash_escape; | |
94 | foreach 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. | |
99 | sub 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 | 123 | sub 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 |
133 | sub 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(). |
144 | sub 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) { | |
435e7af6 NC |
150 | unshift(@mess, "# got '$got'\n", |
151 | "# expected /$expected/\n"); | |
69026470 JH |
152 | } |
153 | } else { | |
154 | $pass = $got =~ /$expected/; | |
155 | unless ($pass) { | |
7d932aad MS |
156 | unshift(@mess, "# got '$got'\n", |
157 | "# expected /$expected/\n"); | |
69026470 JH |
158 | } |
159 | } | |
7d932aad | 160 | _ok($pass, _where(), $name, @mess); |
69026470 JH |
161 | } |
162 | ||
163 | sub pass { | |
164 | _ok(1, '', @_); | |
165 | } | |
166 | ||
167 | sub fail { | |
168 | _ok(0, _where(), @_); | |
169 | } | |
170 | ||
ad20d923 MS |
171 | sub curr_test { |
172 | return $test; | |
173 | } | |
174 | ||
3e90d5a3 MS |
175 | sub next_test { |
176 | $test++ | |
177 | } | |
178 | ||
69026470 JH |
179 | # Note: can't pass multipart messages since we try to |
180 | # be compatible with Test::More::skip(). | |
181 | sub skip { | |
7d932aad | 182 | my $why = shift; |
982b7cb7 | 183 | my $n = @_ ? shift : 1; |
69026470 | 184 | for (1..$n) { |
ad20d923 | 185 | print STDOUT "ok $test # skip: $why\n"; |
e6c299c8 | 186 | $test++; |
69026470 JH |
187 | } |
188 | local $^W = 0; | |
189 | last SKIP; | |
190 | } | |
191 | ||
192 | sub eq_array { | |
193 | my ($ra, $rb) = @_; | |
194 | return 0 unless $#$ra == $#$rb; | |
195 | for my $i (0..$#$ra) { | |
196 | return 0 unless $ra->[$i] eq $rb->[$i]; | |
197 | } | |
198 | return 1; | |
199 | } | |
200 | ||
677fb045 NC |
201 | sub eq_hash { |
202 | my ($orig, $suspect) = @_; | |
203 | my $fail; | |
204 | while (my ($key, $value) = each %$suspect) { | |
205 | # Force a hash recompute if this perl's internals can cache the hash key. | |
206 | $key = "" . $key; | |
207 | if (exists $orig->{$key}) { | |
208 | if ($orig->{$key} ne $value) { | |
75385f53 | 209 | print STDERR "# key ", _qq($key), " was ", _qq($orig->{$key}), |
677fb045 NC |
210 | " now ", _qq($value), "\n"; |
211 | $fail = 1; | |
212 | } | |
213 | } else { | |
75385f53 MS |
214 | print STDERR "# key ", _qq($key), " is ", _qq($value), |
215 | ", not in original.\n"; | |
677fb045 NC |
216 | $fail = 1; |
217 | } | |
218 | } | |
219 | foreach (keys %$orig) { | |
220 | # Force a hash recompute if this perl's internals can cache the hash key. | |
221 | $_ = "" . $_; | |
222 | next if (exists $suspect->{$_}); | |
75385f53 | 223 | print STDERR "# key ", _qq($_), " was ", _qq($orig->{$_}), " now missing.\n"; |
677fb045 NC |
224 | $fail = 1; |
225 | } | |
226 | !$fail; | |
227 | } | |
228 | ||
69026470 JH |
229 | sub require_ok { |
230 | my ($require) = @_; | |
231 | eval <<REQUIRE_OK; | |
232 | require $require; | |
233 | REQUIRE_OK | |
1577bb16 | 234 | _ok(!$@, _where(), "require $require"); |
69026470 JH |
235 | } |
236 | ||
237 | sub use_ok { | |
238 | my ($use) = @_; | |
239 | eval <<USE_OK; | |
240 | use $use; | |
241 | USE_OK | |
1577bb16 | 242 | _ok(!$@, _where(), "use $use"); |
69026470 JH |
243 | } |
244 | ||
137352a2 RGS |
245 | # runperl - Runs a separate perl interpreter. |
246 | # Arguments : | |
247 | # switches => [ command-line switches ] | |
248 | # nolib => 1 # don't use -I../lib (included by default) | |
249 | # prog => one-liner (avoid quotes) | |
250 | # progfile => perl script | |
251 | # stdin => string to feed the stdin | |
252 | # stderr => redirect stderr to stdout | |
253 | # args => [ command-line arguments to the perl program ] | |
cb9c5e20 | 254 | # verbose => print the command line |
137352a2 RGS |
255 | |
256 | my $is_mswin = $^O eq 'MSWin32'; | |
257 | my $is_netware = $^O eq 'NetWare'; | |
258 | my $is_macos = $^O eq 'MacOS'; | |
259 | my $is_vms = $^O eq 'VMS'; | |
260 | ||
cb9c5e20 JH |
261 | sub _quote_args { |
262 | my ($runperl, $args) = @_; | |
263 | ||
264 | foreach (@$args) { | |
265 | # In VMS protect with doublequotes because otherwise | |
266 | # DCL will lowercase -- unless already doublequoted. | |
267 | $_ = q(").$_.q(") if $is_vms && !/^\"/; | |
268 | $$runperl .= ' ' . $_; | |
269 | } | |
270 | } | |
271 | ||
137352a2 RGS |
272 | sub runperl { |
273 | my %args = @_; | |
274 | my $runperl = $^X; | |
f93a5f07 | 275 | if ($args{switches}) { |
cb9c5e20 | 276 | _quote_args(\$runperl, $args{switches}); |
137352a2 | 277 | } |
f93a5f07 JH |
278 | unless ($args{nolib}) { |
279 | if ($is_macos) { | |
cb9c5e20 | 280 | $runperl .= ' -I::lib'; |
f93a5f07 | 281 | # Use UNIX style error messages instead of MPW style. |
cb9c5e20 | 282 | $runperl .= ' -MMac::err=unix' if $args{stderr}; |
137352a2 RGS |
283 | } |
284 | else { | |
cb9c5e20 | 285 | $runperl .= ' "-I../lib"'; # doublequotes because of VMS |
137352a2 RGS |
286 | } |
287 | } | |
288 | if (defined $args{prog}) { | |
289 | if ($is_mswin || $is_netware || $is_vms) { | |
290 | $runperl .= qq( -e ") . $args{prog} . qq("); | |
291 | } | |
292 | else { | |
293 | $runperl .= qq( -e ') . $args{prog} . qq('); | |
294 | } | |
295 | } elsif (defined $args{progfile}) { | |
296 | $runperl .= qq( "$args{progfile}"); | |
297 | } | |
298 | if (defined $args{stdin}) { | |
5ae09a77 MS |
299 | # so we don't try to put literal newlines and crs onto the |
300 | # command line. | |
301 | $args{stdin} =~ s/\n/\\n/g; | |
302 | $args{stdin} =~ s/\r/\\r/g; | |
303 | ||
137352a2 | 304 | if ($is_mswin || $is_netware || $is_vms) { |
f93a5f07 | 305 | $runperl = qq{$^X -e "print qq(} . |
137352a2 RGS |
306 | $args{stdin} . q{)" | } . $runperl; |
307 | } | |
308 | else { | |
f93a5f07 | 309 | $runperl = qq{$^X -e 'print qq(} . |
137352a2 RGS |
310 | $args{stdin} . q{)' | } . $runperl; |
311 | } | |
312 | } | |
313 | if (defined $args{args}) { | |
cb9c5e20 JH |
314 | _quote_args(\$runperl, $args{args}); |
315 | } | |
316 | $runperl .= ' 2>&1' if $args{stderr} && !$is_macos; | |
317 | $runperl .= " \xB3 Dev:Null" if !$args{stderr} && $is_macos; | |
318 | if ($args{verbose}) { | |
319 | my $runperldisplay = $runperl; | |
320 | $runperldisplay =~ s/\n/\n\#/g; | |
75385f53 | 321 | print STDERR "# $runperldisplay\n"; |
137352a2 | 322 | } |
137352a2 RGS |
323 | my $result = `$runperl`; |
324 | $result =~ s/\n\n/\n/ if $is_vms; # XXX pipes sometimes double these | |
325 | return $result; | |
326 | } | |
327 | ||
8799135f | 328 | |
c4fbe247 | 329 | sub DIE { |
75385f53 | 330 | print STDERR "# @_\n"; |
c4fbe247 | 331 | exit 1; |
8799135f MS |
332 | } |
333 | ||
b5fe401b | 334 | # A somewhat safer version of the sometimes wrong $^X. |
17a740d5 JH |
335 | my $Perl; |
336 | sub which_perl { | |
337 | unless (defined $Perl) { | |
338 | $Perl = $^X; | |
339 | ||
340 | my $exe; | |
341 | eval "require Config; Config->import"; | |
85363d30 | 342 | if ($@) { |
17a740d5 JH |
343 | warn "test.pl had problems loading Config: $@"; |
344 | $exe = ''; | |
85363d30 | 345 | } else { |
17a740d5 | 346 | $exe = $Config{_exe}; |
85363d30 | 347 | } |
da405c16 | 348 | $exe = '' unless defined $exe; |
17a740d5 JH |
349 | |
350 | # This doesn't absolutize the path: beware of future chdirs(). | |
351 | # We could do File::Spec->abs2rel() but that does getcwd()s, | |
352 | # which is a bit heavyweight to do here. | |
353 | ||
354 | if ($Perl =~ /^perl\Q$exe\E$/i) { | |
8db06b02 | 355 | my $perl = "perl$exe"; |
17a740d5 JH |
356 | eval "require File::Spec"; |
357 | if ($@) { | |
358 | warn "test.pl had problems loading File::Spec: $@"; | |
8db06b02 | 359 | $Perl = "./$perl"; |
17a740d5 | 360 | } else { |
8db06b02 | 361 | $Perl = File::Spec->catfile(File::Spec->curdir(), $perl); |
17a740d5 JH |
362 | } |
363 | } | |
364 | ||
c880be78 MS |
365 | # Its like this. stat on Cygwin treats 'perl' to mean 'perl.exe' |
366 | # but open does not. This can get confusing, so to be safe we | |
367 | # always put the .exe on the end on Cygwin. | |
368 | $Perl .= $exe if $^O eq 'cygwin' && $Perl !~ /\Q$exe\E$/; | |
369 | ||
8db06b02 | 370 | warn "which_perl: cannot find $Perl from $^X" unless -f $Perl; |
17a740d5 JH |
371 | |
372 | # For subcommands to use. | |
373 | $ENV{PERLEXE} = $Perl; | |
85363d30 | 374 | } |
17a740d5 | 375 | return $Perl; |
b5fe401b MS |
376 | } |
377 | ||
435e7af6 NC |
378 | sub unlink_all { |
379 | foreach my $file (@_) { | |
380 | 1 while unlink $file; | |
75385f53 | 381 | print STDERR "# Couldn't unlink '$file': $!\n" if -f $file; |
435e7af6 NC |
382 | } |
383 | } | |
69026470 | 384 | 1; |