9 Test::Leaner - A slimmer Test::More for when you favor performance over completeness.
17 our $VERSION = '0.01';
21 use Test::Leaner tests => 10_000;
24 is $one, 1, "checking situation $_";
30 When profiling some L<Test::More>-based test script that contained about 10 000 unit tests, I realized that 60% of the time was spent in L<Test::Builder> itself, even though every single test actually involved a costly C<eval STRING>.
32 This module aims to be a partial replacement to L<Test::More> in those situations where you want to run a large number of simple tests.
33 Its functions behave the same as their L<Test::More> counterparts, except for the following differences :
39 Stringification isn't forced on the test operands.
40 However, L</ok> honors C<'bool'> overloading, L</is> honors C<'eq'> overloading and L</cmp_ok> honors whichever overloading category corresponds to the specified operator.
44 L</pass>, L</fail>, L</ok>, L</is>, L</isnt>, L</like>, L</unlike> and L</cmp_ok> are all guaranteed to return the truth value of the test.
48 C<use_ok>, C<require_ok>, C<can_ok>, C<isa_ok>, C<new_ok>, C<subtest>, C<explain>, C<TODO> blocks and C<todo_skip> are not implemented.
57 if ($] >= 5.008 and $INC{'threads.pm'}) {
58 my $use_ithreads = do {
61 $Config::Config{useithreads};
64 require threads::shared;
65 *THREADSAFE = sub () { 1 };
68 unless (defined &Test::Leaner::THREADSAFE) {
69 *THREADSAFE = sub () { 0 }
73 my ($TAP_STREAM, $DIAG_STREAM);
75 my ($plan, $test, $failed, $no_diag, $done_testing);
78 sub SKIP_ALL () { -2 }
82 threads::shared::share($_) for $plan, $test, $failed, $no_diag, $done_testing;
85 lock $plan if THREADSAFE;
93 my $level = 1 + ($Test::Builder::Level || 0);
94 my ($file, $line) = (caller $level)[1, 2];
95 warn @_, " at $file line $line.\n";
99 my $level = 1 + ($Test::Builder::Level || 0);
100 my ($file, $line) = (caller $level)[1, 2];
101 die @_, " at $file line $line.\n";
104 sub sanitize_comment {
107 $_[0] =~ s/\n/\n# /g;
112 The following functions from L<Test::More> are implemented and exported by default.
114 =head2 C<< plan [ tests => $count | 'no_plan' | skip_all => $reason ] >>
119 my ($key, $value) = @_;
123 lock $plan if THREADSAFE;
125 croak("You tried to plan twice") if defined $plan;
129 if ($key eq 'no_plan') {
130 croak("no_plan takes no arguments") if $value;
132 } elsif ($key eq 'tests') {
133 croak("Got an undefined number of tests") unless defined $value;
134 croak("You said to run 0 tests") unless $value;
135 croak("Number of tests must be a positive integer. You gave it '$value'")
136 unless $value =~ /^\+?[0-9]+$/;
138 $plan_str = "1..$value";
139 } elsif ($key eq 'skip_all') {
141 $plan_str = '1..0 # SKIP';
142 if (defined $value) {
143 sanitize_comment($value);
144 $plan_str .= " $value" if length $value;
147 my @args = grep defined, $key, $value;
148 croak("plan() doesn't understand @args");
151 if (defined $plan_str) {
153 print $TAP_STREAM "$plan_str\n";
156 exit 0 if $plan == SKIP_ALL;
188 if ($item eq 'import') {
189 push @imports, @{ $_[$i+1] };
191 } elsif ($item eq 'no_diag') {
192 lock $plan if THREADSAFE;
198 splice @_, $i, $splice;
205 local $Test::Builder::Level = ($Test::Builder::Level || 0) + 1;
209 @_ = ($class, @imports);
210 goto &Exporter::import;
213 =head2 C<skip_all $reason>
218 @_ = (skip_all => $_[0]);
222 =head2 C<< skip $reason => $count >>
227 my ($reason, $count) = @_;
229 lock $plan if THREADSAFE;
231 if (not defined $count) {
232 carp("skip() needs to know \$how_many tests are in the block")
233 unless defined $plan and $plan == NO_PLAN;
235 } elsif ($count =~ /[^0-9]/) {
236 carp('skip() was passed a non-numeric number of tests. Did you get the arguments backwards?');
243 my $skip_str = "ok $test # skip";
244 if (defined $reason) {
245 sanitize_comment($reason);
246 $skip_str .= " $reason" if length $reason;
250 print $TAP_STREAM "$skip_str\n";
253 no warnings 'exiting';
257 =head2 C<done_testing [ $count ]>
264 lock $plan if THREADSAFE;
266 $count = $test unless defined $count;
267 croak("Number of tests must be a positive integer. You gave it '$count'")
268 unless $count =~ /^\+?[0-9]+$/;
270 if (not defined $plan or $plan == NO_PLAN) {
271 $plan = $count; # $plan can't be NO_PLAN anymore
274 print $TAP_STREAM "1..$plan\n";
277 @_ = ('done_testing() was already called');
279 } elsif ($plan != $count) {
280 @_ = ("planned to run $plan tests but done_testing() expects $count");
288 =head2 C<ok $ok [, $desc ]>
293 my ($ok, $desc) = @_;
295 lock $plan if THREADSAFE;
299 my $test_str = "ok $test";
301 $test_str = "not $test_str";
305 sanitize_comment($desc);
306 $test_str .= " - $desc" if length $desc;
310 print $TAP_STREAM "$test_str\n";
315 =head2 C<pass [ $desc ]>
324 =head2 C<fail [ $desc ]>
333 =head2 C<is $got, $expected [, $desc ]>
338 my ($got, $expected, $desc) = @_;
339 no warnings 'uninitialized';
341 (not(defined $got xor defined $expected) and $got eq $expected),
347 =head2 C<isnt $got, $expected [, $desc ]>
352 my ($got, $expected, $desc) = @_;
353 no warnings 'uninitialized';
355 ((defined $got xor defined $expected) or $got ne $expected),
387 '~~' => 'smartmatch',
392 sub _create_binop_handler {
394 my $name = $binops{$op};
395 croak("Operator $op not supported") unless defined $name;
399 sub is_$name (\$\$;\$) {
400 my (\$got, \$expected, \$desc) = \@_;
401 \@_ = ((\$got $op \$expected), \$desc);
407 $binop_handlers{$op} = do {
409 \&{__PACKAGE__."::is_$name"};
413 =head2 C<like $got, $regexp_expected [, $desc ]>
415 =head2 C<unlike $got, $regexp_expected, [, $desc ]>
421 *like = _create_binop_handler('=~');
422 *unlike = _create_binop_handler('!~');
425 =head2 C<cmp_ok $got, $op, $expected [, $desc ]>
430 my ($got, $op, $expected, $desc) = @_;
431 my $handler = $binop_handlers{$op};
433 local $Test::More::Level = ($Test::More::Level || 0) + 1;
434 $handler = _create_binop_handler($op);
436 @_ = ($got, $expected, $desc);
445 lock $plan if THREADSAFE;
448 my $msg = join '', map { defined($_) ? $_ : 'undef' } @_;
449 sanitize_comment($msg);
450 return unless length $msg;
453 print $fh "# $msg\n";
463 unshift @_, $DIAG_STREAM;
472 unshift @_, $TAP_STREAM;
476 =head2 C<BAIL_OUT [ $desc ]>
483 lock $plan if THREADSAFE;
485 my $bail_out_str = 'Bail out!';
487 sanitize_comment($desc);
488 $bail_out_str .= " $desc" if length $desc; # Two spaces
492 print $TAP_STREAM "$bail_out_str\n";
499 lock $plan if THREADSAFE;
503 $? = $failed <= 254 ? $failed : 254;
504 } elsif ($plan >= 0) {
505 $? = $test == $plan ? 0 : 255;
506 } elsif ($plan == NO_PLAN) {
508 print $TAP_STREAM "1..$test\n";
516 L<Test::Leaner> also provides some functions of its own, which are never exported.
518 =head2 C<tap_stream [ $fh ]>
520 Read/write accessor for the filehandle to which the tests are outputted.
521 On write, it also turns autoflush on onto C<$fh>.
523 Note that it can only be used as a write accessor before you start any thread, as L<threads::shared> cannot reliably share filehandles.
525 Defaults to C<STDOUT>.
529 sub tap_stream (;*) {
533 my $fh = select $TAP_STREAM;
543 =head2 C<diag_stream [ $fh ]>
545 Read/write accessor for the filehandle to which the diagnostics are printed.
546 On write, it also turns autoflush on onto C<$fh>.
548 Just like L</tap_stream>, it can only be used as a write accessor before you start any thread, as L<threads::shared> cannot reliably share filehandles.
550 Defaults to C<STDERR>.
554 sub diag_stream (;*) {
556 $DIAG_STREAM = $_[0];
558 my $fh = select $DIAG_STREAM;
572 L<Exporter>, L<Test::More>
576 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
578 You can contact me by mail or on C<irc.perl.org> (vincent).
582 Please report any bugs or feature requests to C<bug-test-leaner at rt.cpan.org>, or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test-Leaner>.
583 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
587 You can find documentation for this module with the perldoc command.
591 =head1 COPYRIGHT & LICENSE
593 Copyright 2010 Vincent Pit, all rights reserved.
595 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
599 1; # End of Test::Leaner