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.
39 if ($] >= 5.008 and $INC{'threads.pm'}) {
40 my $use_ithreads = do {
43 $Config::Config{useithreads};
46 require threads::shared;
47 *THREADSAFE = sub () { 1 };
50 unless (defined &Test::Leaner::THREADSAFE) {
51 *THREADSAFE = sub () { 0 }
55 my ($TAP_STREAM, $DIAG_STREAM);
57 my ($plan, $test, $failed, $no_diag, $done_testing);
60 sub SKIP_ALL () { -2 }
64 threads::shared::share($_) for $plan, $test, $failed, $no_diag, $done_testing;
67 lock $plan if THREADSAFE;
75 my $level = 1 + ($Test::Builder::Level || 0);
76 my ($file, $line) = (caller $level)[1, 2];
77 warn @_, " at $file line $line.\n";
81 my $level = 1 + ($Test::Builder::Level || 0);
82 my ($file, $line) = (caller $level)[1, 2];
83 die @_, " at $file line $line.\n";
86 sub sanitize_comment {
94 The following functions from L<Test::More> are implemented and exported by default.
96 =head2 C<< plan [ tests => $count | 'no_plan' | skip_all => $reason ] >>
101 my ($key, $value) = @_;
105 lock $plan if THREADSAFE;
107 croak("You tried to plan twice") if defined $plan;
111 if ($key eq 'no_plan') {
112 croak("no_plan takes no arguments") if $value;
114 } elsif ($key eq 'tests') {
115 croak("Got an undefined number of tests") unless defined $value;
116 croak("You said to run 0 tests") unless $value;
117 croak("Number of tests must be a positive integer. You gave it '$value'")
118 unless $value =~ /^\+?[0-9]+$/;
120 $plan_str = "1..$value";
121 } elsif ($key eq 'skip_all') {
123 $plan_str = '1..0 # SKIP';
124 if (defined $value) {
125 sanitize_comment($value);
126 $plan_str .= " $value" if length $value;
129 my @args = grep defined, $key, $value;
130 croak("plan() doesn't understand @args");
133 if (defined $plan_str) {
135 print $TAP_STREAM "$plan_str\n";
138 exit 0 if $plan == SKIP_ALL;
170 if ($item eq 'import') {
171 push @imports, @{ $_[$i+1] };
173 } elsif ($item eq 'no_diag') {
174 lock $plan if THREADSAFE;
180 splice @_, $i, $splice;
187 local $Test::Builder::Level = ($Test::Builder::Level || 0) + 1;
191 @_ = ($class, @imports);
192 goto &Exporter::import;
195 =head2 C<skip_all $reason>
200 @_ = (skip_all => $_[0]);
204 =head2 C<< skip $reason => $count >>
209 my ($reason, $count) = @_;
211 lock $plan if THREADSAFE;
213 if (not defined $count) {
214 carp("skip() needs to know \$how_many tests are in the block")
215 unless defined $plan and $plan == NO_PLAN;
217 } elsif ($count =~ /[^0-9]/) {
218 carp('skip() was passed a non-numeric number of tests. Did you get the arguments backwards?');
225 my $skip_str = "ok $test # skip";
226 if (defined $reason) {
227 sanitize_comment($reason);
228 $skip_str .= " $reason" if length $reason;
232 print $TAP_STREAM "$skip_str\n";
235 no warnings 'exiting';
239 =head2 C<done_testing [ $count ]>
246 lock $plan if THREADSAFE;
248 $count = $test unless defined $count;
249 croak("Number of tests must be a positive integer. You gave it '$count'")
250 unless $count =~ /^\+?[0-9]+$/;
252 if (not defined $plan or $plan == NO_PLAN) {
253 $plan = $count; # $plan can't be NO_PLAN anymore
256 print $TAP_STREAM "1..$plan\n";
259 @_ = ('done_testing() was already called');
261 } elsif ($plan != $count) {
262 @_ = ("planned to run $plan tests but done_testing() expects $count");
270 =head2 C<ok $ok [, $desc ]>
275 my ($ok, $desc) = @_;
277 lock $plan if THREADSAFE;
281 my $test_str = "ok $test";
283 $test_str = "not $test_str";
287 sanitize_comment($desc);
288 $test_str .= " - $desc" if length $desc;
292 print $TAP_STREAM "$test_str\n";
297 =head2 C<pass [ $desc ]>
306 =head2 C<fail [ $desc ]>
315 =head2 C<is $got, $expected [, $desc ]>
320 my ($got, $expected, $desc) = @_;
321 no warnings 'uninitialized';
323 (not(defined $got xor defined $expected) and $got eq $expected),
329 =head2 C<isnt $got, $expected [, $desc ]>
334 my ($got, $expected, $desc) = @_;
335 no warnings 'uninitialized';
337 ((defined $got xor defined $expected) or $got ne $expected),
369 '~~' => 'smartmatch',
374 sub _create_binop_handler {
376 my $name = $binops{$op};
377 croak("Operator $op not supported") unless defined $name;
381 sub is_$name (\$\$;\$) {
382 my (\$got, \$expected, \$desc) = \@_;
383 \@_ = ((\$got $op \$expected), \$desc);
389 $binop_handlers{$op} = do {
391 \&{__PACKAGE__."::is_$name"};
395 =head2 C<like $got, $regexp_expected [, $desc ]>
397 =head2 C<unlike $got, $regexp_expected, [, $desc ]>
403 *like = _create_binop_handler('=~');
404 *unlike = _create_binop_handler('!~');
407 =head2 C<cmp_ok $got, $op, $expected [, $desc ]>
412 my ($got, $op, $expected, $desc) = @_;
413 my $handler = $binop_handlers{$op};
415 local $Test::More::Level = ($Test::More::Level || 0) + 1;
416 $handler = _create_binop_handler($op);
418 @_ = ($got, $expected, $desc);
427 lock $plan if THREADSAFE;
430 my $msg = join '', map { defined($_) ? $_ : 'undef' } @_;
431 sanitize_comment($msg);
432 return unless length $msg;
435 print $fh "# $msg\n";
445 unshift @_, $DIAG_STREAM;
454 unshift @_, $TAP_STREAM;
458 =head2 C<BAIL_OUT [ $desc ]>
465 lock $plan if THREADSAFE;
467 my $bail_out_str = 'Bail out!';
469 sanitize_comment($desc);
470 $bail_out_str .= " $desc" if length $desc; # Two spaces
474 print $TAP_STREAM "$bail_out_str\n";
481 lock $plan if THREADSAFE;
485 $? = $failed <= 254 ? $failed : 254;
486 } elsif ($plan >= 0) {
487 $? = $test == $plan ? 0 : 255;
488 } elsif ($plan == NO_PLAN) {
490 print $TAP_STREAM "1..$test\n";
498 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.
499 Their L<Test::More> counterparts behave the same, but it is not documented anywhere.
501 L<Test::Leaner> also provides some functions of its own, which are never exported.
503 =head2 C<tap_stream [ $fh ]>
505 Read/write accessor for the filehandle to which the tests are outputted.
506 On write, it also turns autoflush on onto C<$fh>.
508 Note that it can only be used as a write accessor before you start any thread, as L<threads::shared> cannot reliably share filehandles.
510 Defaults to C<STDOUT>.
514 sub tap_stream (;*) {
518 my $fh = select $TAP_STREAM;
528 =head2 C<diag_stream [ $fh ]>
530 Read/write accessor for the filehandle to which the diagnostics are printed.
531 On write, it also turns autoflush on onto C<$fh>.
533 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.
535 Defaults to C<STDERR>.
539 sub diag_stream (;*) {
541 $DIAG_STREAM = $_[0];
543 my $fh = select $DIAG_STREAM;
557 L<Exporter>, L<Test::More>
561 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
563 You can contact me by mail or on C<irc.perl.org> (vincent).
567 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>.
568 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
572 You can find documentation for this module with the perldoc command.
576 =head1 COPYRIGHT & LICENSE
578 Copyright 2010 Vincent Pit, all rights reserved.
580 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
584 1; # End of Test::Leaner