Legacy.pm

filter: Statementstmt 95.8% 93 / 97 Branchbran 85.7% 24 / 28 Conditioncond 66.6% 8 / 12 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 85.7% 18 / 21 Podpod 0.0% 0 / 3 totaltotal 86.3% 146 / 169 CC 20 SCAR 14.1
CC
20
Cov
91%
plan
10.3
ok
5.0
END
3.0
CRAP 4.1
↑ Summary Legacy/More.pm »
1 =head1 NAME
2  
3 Test::Legacy - Test.pm workalike that plays well with other Test modules
4  
5  
6 =head1 SYNOPSIS
7  
8 # use Test;
9 use Test::Legacy;
10  
11 ...leave all else the same...
12  
13  
14 =head1 DESCRIPTION
15  
16 Test.pm suffers from the problem of not working well with other Test
17 modules. If you have a test written using Test.pm and want to use another
18 module, Test::Exception for example, you cannot.
19  
20 Test::Legacy is a reimplementation of Test.pm using Test::Builder.
21 What this means is Test::Legacy can be used with other Test::Builder
22 derived modules (such as Test::More, Test::Exception, and most
23 everything released in the last couple years) in the same test script.
24  
25 Test::Legacy strives to work as much like Test.pm as possible. It
26 allows one to continue to take advantage of additional Test modules
27 without having to immediately rewrite all your tests to use Test::More.
28  
29  
30 =head2 Test::Legacy and Test::More
31  
32 You're often going to be wanting to use Test::Legacy in conjunction
33 with Test::More. Because they export a bunch of the same functions
34 they can get a little annoying to deal with. Fortunately,
35 L<Test::Legacy::More> is provided to smooth things out.
36  
37  
38 =head1 DIFFERENCES
39  
40 Test::Legacy does have some differences from Test.pm. Here are the known
41 ones. Patches welcome.
42  
43 =over 4
44  
45 =item * diagnostics
46  
47 Because Test::Legacy uses Test::Builder for most of the work, failure
48 diagnostics are not the same as Test.pm and are unlikely to ever be.
49  
50  
51 =item * onfail
52  
53 Currently the onfail subroutine does not get passed a description of test
54 failures. This is slated to be fixed in the future.
55  
56 =back
57  
58  
59 =head1 AUTHOR
60  
61 Michael G Schwern E<lt>schwern@pobox.comE<gt>
62  
63  
64 =head1 COPYRIGHT
65  
66 Copyright 2004, 2005 by Michael G Schwern E<lt>schwern@pobox.comE<gt>.
67  
68 This program is free software; you can redistribute it and/or
69 modify it under the same terms as Perl itself.
70  
71 See F<http://www.perl.com/perl/misc/Artistic.html>
72  
73  
74 =head1 NOTES
75  
76 This is an emulation of Test.pm 1.25.
77  
78 =head1 SEE ALSO
79  
80 L<Test>, L<Test::More>, L<Test::Legacy::More>
81  
82 =cut
83  
84  
85 package Test::Legacy;
86  
87 13 require 5.004_05;
88  
89 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
90 13 ▶use vars qw($VERSION
Subroutine BEGIN: called CC=1 SCAR=0.0
91 @ISA @EXPORT @EXPORT_OK
92 $TESTERR $TESTOUT
93 $ntest
94 13 );
95  
96 13 $VERSION = '1.2502';
97  
98  
99 13 require Exporter;
100  
101 13 @ISA = qw(Exporter);
102 13 @EXPORT = qw(plan ok skip);
103 13 @EXPORT_OK = qw($ntest $TESTOUT $TESTERR);
104  
105  
106 13 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
107  
108  
109 13 ▶use Test::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
110 13 my $TB = Test::Builder->new;
111 13 ▶my $Self = { todo => {}, onfail => sub {} };
Subroutine __ANON__: not called CC=1 SCAR=0.0
112  
113  
114 13 tie $TESTOUT, 'Test::Legacy::FH', $TB, 'output', 'todo_output';
115 13 tie $TESTERR, 'Test::Legacy::FH', $TB, 'failure_output';
116  
117 13 tie $ntest, 'Test::Legacy::ntest', $TB;
118  
119  
120 45 ▶sub _print {
Subroutine _print: called CC=1 SCAR=0.0
121 45 local($\, $,); # guard against -l and other things that screw with
122 # print
123  
124 45 print $TESTOUT @_
125 }
126  
127  
128 13 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
129 13 my $class = shift;
130  
131 13 my $caller = caller;
132  
133 13 $TB->exported_to($caller);
134  
135 13 $class->export_to_level(1, $class, @_);
136 }
137  
138  
139 13 my %Plan_Keys = map { $_ => 1 } qw(test tests todo onfail);
140 15 ▶sub plan {
Subroutine plan: called CC=9 SCAR=23.3
Pod undocumented
141 15 my %args = @_;
142  
143 15 ▶ croak "Test::plan(%args): odd number of arguments" if @_ & 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ & 1 0 15 15
144  
145 15 ▶ if( my @unrecognized = grep !$Plan_Keys{$_}, keys %args ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@unrecognized) = grep((!$Plan_Keys{$_}), keys %args)) 0 15 15
146 0 carp "Test::plan(): skipping unrecognized directive(s) @unrecognized";
147 }
148  
149 15 ▶ $Self->{todo} = { map { $_ => 1 } @{$args{todo}} } if $args{todo};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"todo"} 4 11 15
150 15 ▶ $Self->{onfail} = $args{onfail} if $args{onfail};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"onfail"} 1 14 15
151  
152 15 ▶ $TB->plan( tests => $args{test} || $args{tests} );
Condition66%2/3
$args{'test'} || $args{'tests'}
l!l&&r!l&&!r
1140
MC/DC0%0/2
$args{'test'} || $args{'tests'}
$args{'test'} $args{'tests'}
Truth table66%2/3
A || B
A= $args{'test'}B= $args{'tests'}
ABresult
1X1
011
000
153  
154 #### Taken from Test.pm 1.25
155 15 _print "# Running under perl version $] for $^O",
156 (chr(65) eq 'A') ? "\n" : " in a non-ASCII world\n";
157  
158 15 ▶ _print "# Win32::BuildNumber ", &Win32::BuildNumber(), "\n"
Branch: 1 branch
BranchTrueFalseTotal
if defined &Win32::BuildNumber and defined &Win32::BuildNumber() 0 15 15
Condition33%1/3
defined &Win32::BuildNumber and defined &Win32::BuildNumber()
!ll&&!rl&&r
1500
MC/DC0%0/2
defined &Win32::BuildNumber and defined &Win32::BuildNumber()
defined &Win32::BuildNumber defined &Win32::BuildNumber()
Truth table33%1/3
A and B
A= defined &Win32::BuildNumberB= defined &Win32::BuildNumber()
ABresult
0X0
100
111
159 if defined(&Win32::BuildNumber) and defined &Win32::BuildNumber();
160  
161 15 ▶ _print "# MacPerl version $MacPerl::Version\n"
Branch: 1 branch
BranchTrueFalseTotal
if defined $MacPerl::Version 0 15 15
162 if defined $MacPerl::Version;
163  
164 15 _print sprintf
165 "# Current time local: %s\n# Current time GMT: %s\n",
166 scalar(localtime($^T)), scalar(gmtime($^T));
167 ### End
168  
169 15 _print "# Using Test::Legacy version $VERSION\n";
170 }
171  
172  
173 13 ▶END {
Subroutine END: called CC=3 SCAR=11.1
174 13 ▶ $Self->{onfail}->() if $Self->{onfail} and _is_failing($TB);
Branch: 1 branch
BranchTrueFalseTotal
if $Self->{"onfail"} and _is_failing($TB) 1 12 13
Condition66%2/3
$Self->{"onfail"} and _is_failing($TB)
!ll&&!rl&&r
0121
MC/DC50%1/2
$Self->{"onfail"} and _is_failing($TB)
$Self->{"onfail"} _is_failing($TB)
Truth table66%2/3
A and B
A= $Self->{"onfail"}B= _is_failing($TB)
ABresult
0X0
100
111
175 }
176  
177 13 ▶sub _is_failing {
Subroutine _is_failing: called CC=2 SCAR=6.9
178 13 my $tb = shift;
179  
180 13 ▶ return grep(!$_, $tb->summary) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
grep((!$_), $tb->summary) ? : 1 12 13
181 }
182  
183 0 ▶sub _make_faildetail {
Subroutine _make_faildetail: not called CC=1 SCAR=6.9
184 0 my $tb = shift;
185  
186 # package, repetition, result
187  
188 }
189  
190  
191 # Taken from Test.pm 1.25
192 147 ▶sub _to_value {
Subroutine _to_value: called CC=2 SCAR=6.9
193 147 my ($v) = @_;
194 147 ▶ return ref $v eq 'CODE' ? $v->() : $v;
Branch: 1 branch
BranchTrueFalseTotal
ref $v eq 'CODE' ? : 5 142 147
195 }
196  
197  
198 70 ▶sub ok ($;$$) {
Subroutine ok: called CC=5 SCAR=16.1
Pod undocumented
199 70 my($got, $expected, $diag) = @_;
200 70 ($got, $expected) = map _to_value($_), ($got, $expected);
201  
202 70 my($caller, $file, $line) = caller;
203  
204 # local doesn't work with soft refs in 5.5.4. So we do it manually.
205 70 my $todo;
206 {
207 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
208 70 $todo = \${ $caller .'::TODO' };
209 }
210 70 my $orig_todo = $$todo;
211  
212 70 ▶ if( $Self->{todo}{$TB->current_test + 1} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Self->{"todo"}{$TB->current_test + 1}) 14 56 70
213 14 $$todo = "set in plan, $file at line $line";
214 }
215  
216 70 my $ok = 0;
217 70 ▶ if( @_ == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1) { } 19 51 70
elsif (defined $expected and $TB->maybe_regex($expected)) { } 4 47 51
218 19 $ok = $TB->ok(@_)
219 }
220 ▶ elsif( defined $expected && $TB->maybe_regex($expected) ) {
Condition100%3/3
defined $expected and $TB->maybe_regex($expected)
!ll&&!rl&&r
2454
MC/DC100%2/2
defined $expected and $TB->maybe_regex($expected)
defined $expected $TB->maybe_regex($expected)
Truth table100%3/3
A and B
A= defined $expectedB= $TB->maybe_regex($expected)
ABresult
0X0
100
111
221 4 $ok = $TB->like($got, $expected);
222 }
223 else {
224 47 $ok = $TB->is_eq($got, $expected);
225 }
226  
227 70 $$todo = $orig_todo;
228  
229 70 return $ok;
230 }
231  
232  
233 7 ▶sub skip ($;$$$) {
Subroutine skip: called CC=3 SCAR=11.0
Pod undocumented
234 7 my $reason = _to_value(shift);
235  
236 7 ▶ if( $reason ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reason) { } 6 1 7
237 6 ▶ $reason = '' if $reason !~ /\D/;
Branch: 1 branch
BranchTrueFalseTotal
unless $reason =~ /\D/ 2 4 6
238 6 return $TB->skip($reason);
239 }
240 else {
241 1 goto &ok;
242 }
243 }
244  
245  
246 package Test::Legacy::FH;
247  
248 26 ▶sub TIESCALAR {
Subroutine TIESCALAR: called CC=1 SCAR=0.0
249 26 my($class, $tb, @methods) = @_;
250 26 bless { tb => $tb, methods => \@methods }, $_[0];
251 }
252  
253 20 ▶sub STORE {
Subroutine STORE: called CC=2 SCAR=6.9
254 20 my($self, $arg) = @_;
255  
256 20 my $tb = $self->{tb};
257 20 my @meths = @{ $self->{methods} };
258  
259 20 foreach my $meth (@meths) {
260 30 $tb->$meth($arg);
261 }
262  
263 20 return $arg;
264 }
265  
266 45 ▶sub FETCH {
Subroutine FETCH: called CC=1 SCAR=0.0
267 45 my $self = shift;
268  
269 45 my $tb = $self->{tb};
270 45 my($meth) = @{ $self->{methods} };
271  
272 45 return $tb->$meth();
273 }
274  
275  
276 package Test::Legacy::ntest;
277  
278 13 ▶sub TIESCALAR {
Subroutine TIESCALAR: called CC=1 SCAR=0.0
279 13 my($class, $tb) = @_;
280  
281 13 bless { tb => $tb }, $class;
282 }
283  
284 0 ▶sub FETCH {
Subroutine FETCH: not called CC=1 SCAR=6.9
285 0 my $self = shift;
286  
287 0 return $self->{tb}->current_test;
288 }
289  
290 5 ▶sub STORE {
Subroutine STORE: called CC=1 SCAR=0.0
291 5 my($self, $val) = @_;
292  
293 5 return $self->{tb}->current_test($val - 1);
294 }
295  
296 13 1;
↑ Summary Legacy/More.pm »