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 MS |
8 | $TODO = 0; |
9 | ||
69026470 JH |
10 | sub plan { |
11 | my $n; | |
12 | if (@_ == 1) { | |
13 | $n = shift; | |
14 | } else { | |
15 | my %plan = @_; | |
16 | $n = $plan{tests}; | |
17 | } | |
18 | print "1..$n\n"; | |
19 | $planned = $n; | |
20 | } | |
21 | ||
22 | END { | |
23 | my $ran = $test - 1; | |
24 | if (defined $planned && $planned != $ran) { | |
25 | print "# Looks like you planned $planned tests but ran $ran.\n"; | |
26 | } | |
27 | } | |
28 | ||
29 | sub skip_all { | |
30 | if (@_) { | |
31 | print "1..0 - @_\n"; | |
32 | } else { | |
33 | print "1..0\n"; | |
34 | } | |
35 | exit(0); | |
36 | } | |
37 | ||
38 | sub _ok { | |
7d932aad | 39 | my ($pass, $where, $name, @mess) = @_; |
69026470 JH |
40 | # Do not try to microoptimize by factoring out the "not ". |
41 | # VMS will avenge. | |
7d932aad MS |
42 | my $out; |
43 | if ($name) { | |
44 | $out = $pass ? "ok $test - $name" : "not ok $test - $name"; | |
69026470 | 45 | } else { |
7d932aad | 46 | $out = $pass ? "ok $test" : "not ok $test"; |
69026470 | 47 | } |
7d932aad MS |
48 | |
49 | $out .= " # TODO $TODO" if $TODO; | |
50 | print "$out\n"; | |
51 | ||
69026470 JH |
52 | unless ($pass) { |
53 | print "# Failed $where\n"; | |
54 | } | |
7d932aad MS |
55 | |
56 | # Ensure that the message is properly escaped. | |
57 | print map { /^#/ ? "$_\n" : "# $_\n" } | |
58 | map { split /\n/ } @mess if @mess; | |
59 | ||
69026470 | 60 | $test++; |
1577bb16 MS |
61 | |
62 | return $pass; | |
69026470 JH |
63 | } |
64 | ||
65 | sub _where { | |
66 | my @caller = caller(1); | |
67 | return "at $caller[1] line $caller[2]"; | |
68 | } | |
69 | ||
70 | sub ok { | |
7d932aad MS |
71 | my ($pass, $name, @mess) = @_; |
72 | _ok($pass, _where(), $name, @mess); | |
69026470 JH |
73 | } |
74 | ||
b3c72391 JH |
75 | sub _q { |
76 | my $x = shift; | |
77 | return 'undef' unless defined $x; | |
78 | my $q = $x; | |
79 | $q =~ s/'/\\'/; | |
80 | return "'$q'"; | |
81 | } | |
82 | ||
69026470 | 83 | sub is { |
7d932aad | 84 | my ($got, $expected, $name, @mess) = @_; |
69026470 JH |
85 | my $pass = $got eq $expected; |
86 | unless ($pass) { | |
b3c72391 JH |
87 | unshift(@mess, "# got "._q($got)."\n", |
88 | "# expected "._q($expected)."\n"); | |
69026470 | 89 | } |
7d932aad | 90 | _ok($pass, _where(), $name, @mess); |
69026470 JH |
91 | } |
92 | ||
3e90d5a3 MS |
93 | sub isnt { |
94 | my ($got, $isnt, $name, @mess) = @_; | |
95 | my $pass = $got ne $isnt; | |
96 | unless( $pass ) { | |
b3c72391 | 97 | unshift(@mess, "# it should not be "._q($got)."\n", |
3e90d5a3 MS |
98 | "# but it is.\n"); |
99 | } | |
100 | _ok($pass, _where(), $name, @mess); | |
101 | } | |
102 | ||
69026470 JH |
103 | # Note: this isn't quite as fancy as Test::More::like(). |
104 | sub like { | |
7d932aad | 105 | my ($got, $expected, $name, @mess) = @_; |
69026470 JH |
106 | my $pass; |
107 | if (ref $expected eq 'Regexp') { | |
108 | $pass = $got =~ $expected; | |
109 | unless ($pass) { | |
7d932aad | 110 | unshift(@mess, "# got '$got'\n"); |
69026470 JH |
111 | } |
112 | } else { | |
113 | $pass = $got =~ /$expected/; | |
114 | unless ($pass) { | |
7d932aad MS |
115 | unshift(@mess, "# got '$got'\n", |
116 | "# expected /$expected/\n"); | |
69026470 JH |
117 | } |
118 | } | |
7d932aad | 119 | _ok($pass, _where(), $name, @mess); |
69026470 JH |
120 | } |
121 | ||
122 | sub pass { | |
123 | _ok(1, '', @_); | |
124 | } | |
125 | ||
126 | sub fail { | |
127 | _ok(0, _where(), @_); | |
128 | } | |
129 | ||
3e90d5a3 MS |
130 | sub next_test { |
131 | $test++ | |
132 | } | |
133 | ||
69026470 JH |
134 | # Note: can't pass multipart messages since we try to |
135 | # be compatible with Test::More::skip(). | |
136 | sub skip { | |
7d932aad | 137 | my $why = shift; |
982b7cb7 | 138 | my $n = @_ ? shift : 1; |
69026470 | 139 | for (1..$n) { |
e6c299c8 JH |
140 | print "ok $test # skip: $why\n"; |
141 | $test++; | |
69026470 JH |
142 | } |
143 | local $^W = 0; | |
144 | last SKIP; | |
145 | } | |
146 | ||
147 | sub eq_array { | |
148 | my ($ra, $rb) = @_; | |
149 | return 0 unless $#$ra == $#$rb; | |
150 | for my $i (0..$#$ra) { | |
151 | return 0 unless $ra->[$i] eq $rb->[$i]; | |
152 | } | |
153 | return 1; | |
154 | } | |
155 | ||
156 | sub require_ok { | |
157 | my ($require) = @_; | |
158 | eval <<REQUIRE_OK; | |
159 | require $require; | |
160 | REQUIRE_OK | |
1577bb16 | 161 | _ok(!$@, _where(), "require $require"); |
69026470 JH |
162 | } |
163 | ||
164 | sub use_ok { | |
165 | my ($use) = @_; | |
166 | eval <<USE_OK; | |
167 | use $use; | |
168 | USE_OK | |
1577bb16 | 169 | _ok(!$@, _where(), "use $use"); |
69026470 JH |
170 | } |
171 | ||
137352a2 RGS |
172 | # runperl - Runs a separate perl interpreter. |
173 | # Arguments : | |
174 | # switches => [ command-line switches ] | |
175 | # nolib => 1 # don't use -I../lib (included by default) | |
176 | # prog => one-liner (avoid quotes) | |
177 | # progfile => perl script | |
178 | # stdin => string to feed the stdin | |
179 | # stderr => redirect stderr to stdout | |
180 | # args => [ command-line arguments to the perl program ] | |
cb9c5e20 | 181 | # verbose => print the command line |
137352a2 RGS |
182 | |
183 | my $is_mswin = $^O eq 'MSWin32'; | |
184 | my $is_netware = $^O eq 'NetWare'; | |
185 | my $is_macos = $^O eq 'MacOS'; | |
186 | my $is_vms = $^O eq 'VMS'; | |
187 | ||
cb9c5e20 JH |
188 | sub _quote_args { |
189 | my ($runperl, $args) = @_; | |
190 | ||
191 | foreach (@$args) { | |
192 | # In VMS protect with doublequotes because otherwise | |
193 | # DCL will lowercase -- unless already doublequoted. | |
194 | $_ = q(").$_.q(") if $is_vms && !/^\"/; | |
195 | $$runperl .= ' ' . $_; | |
196 | } | |
197 | } | |
198 | ||
137352a2 RGS |
199 | sub runperl { |
200 | my %args = @_; | |
201 | my $runperl = $^X; | |
f93a5f07 | 202 | if ($args{switches}) { |
cb9c5e20 | 203 | _quote_args(\$runperl, $args{switches}); |
137352a2 | 204 | } |
f93a5f07 JH |
205 | unless ($args{nolib}) { |
206 | if ($is_macos) { | |
cb9c5e20 | 207 | $runperl .= ' -I::lib'; |
f93a5f07 | 208 | # Use UNIX style error messages instead of MPW style. |
cb9c5e20 | 209 | $runperl .= ' -MMac::err=unix' if $args{stderr}; |
137352a2 RGS |
210 | } |
211 | else { | |
cb9c5e20 | 212 | $runperl .= ' "-I../lib"'; # doublequotes because of VMS |
137352a2 RGS |
213 | } |
214 | } | |
215 | if (defined $args{prog}) { | |
216 | if ($is_mswin || $is_netware || $is_vms) { | |
217 | $runperl .= qq( -e ") . $args{prog} . qq("); | |
218 | } | |
219 | else { | |
220 | $runperl .= qq( -e ') . $args{prog} . qq('); | |
221 | } | |
222 | } elsif (defined $args{progfile}) { | |
223 | $runperl .= qq( "$args{progfile}"); | |
224 | } | |
225 | if (defined $args{stdin}) { | |
5ae09a77 MS |
226 | # so we don't try to put literal newlines and crs onto the |
227 | # command line. | |
228 | $args{stdin} =~ s/\n/\\n/g; | |
229 | $args{stdin} =~ s/\r/\\r/g; | |
230 | ||
137352a2 | 231 | if ($is_mswin || $is_netware || $is_vms) { |
f93a5f07 | 232 | $runperl = qq{$^X -e "print qq(} . |
137352a2 RGS |
233 | $args{stdin} . q{)" | } . $runperl; |
234 | } | |
235 | else { | |
f93a5f07 | 236 | $runperl = qq{$^X -e 'print qq(} . |
137352a2 RGS |
237 | $args{stdin} . q{)' | } . $runperl; |
238 | } | |
239 | } | |
240 | if (defined $args{args}) { | |
cb9c5e20 JH |
241 | _quote_args(\$runperl, $args{args}); |
242 | } | |
243 | $runperl .= ' 2>&1' if $args{stderr} && !$is_macos; | |
244 | $runperl .= " \xB3 Dev:Null" if !$args{stderr} && $is_macos; | |
245 | if ($args{verbose}) { | |
246 | my $runperldisplay = $runperl; | |
247 | $runperldisplay =~ s/\n/\n\#/g; | |
248 | print "# $runperldisplay\n"; | |
137352a2 | 249 | } |
137352a2 RGS |
250 | my $result = `$runperl`; |
251 | $result =~ s/\n\n/\n/ if $is_vms; # XXX pipes sometimes double these | |
252 | return $result; | |
253 | } | |
254 | ||
8799135f MS |
255 | |
256 | sub BAILOUT { | |
257 | print "Bail out! @_\n"; | |
258 | exit; | |
259 | } | |
260 | ||
261 | ||
69026470 | 262 | 1; |