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 L</cmp_ok> throws an exception if the given operator isn't a valid Perl binary operator (except C<'='> and variants).
49 It also tests in scalar context, so C<'..'> will be treated as the flip-flop operator and not the range operator.
53 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.
62 if ($] >= 5.008 and $INC{'threads.pm'}) {
63 my $use_ithreads = do {
66 $Config::Config{useithreads};
69 require threads::shared;
70 *THREADSAFE = sub () { 1 };
73 unless (defined &Test::Leaner::THREADSAFE) {
74 *THREADSAFE = sub () { 0 }
78 my ($TAP_STREAM, $DIAG_STREAM);
80 my ($plan, $test, $failed, $no_diag, $done_testing);
83 sub SKIP_ALL () { -2 }
87 threads::shared::share($_) for $plan, $test, $failed, $no_diag, $done_testing;
90 lock $plan if THREADSAFE;
98 my $level = 1 + ($Test::Builder::Level || 0);
99 my ($file, $line) = (caller $level)[1, 2];
100 warn @_, " at $file line $line.\n";
104 my $level = 1 + ($Test::Builder::Level || 0);
105 my ($file, $line) = (caller $level)[1, 2];
106 die @_, " at $file line $line.\n";
109 sub _sanitize_comment {
112 $_[0] =~ s/\n/\n# /g;
117 The following functions from L<Test::More> are implemented and exported by default.
119 =head2 C<< plan [ tests => $count | 'no_plan' | skip_all => $reason ] >>
124 my ($key, $value) = @_;
128 lock $plan if THREADSAFE;
130 croak("You tried to plan twice") if defined $plan;
134 if ($key eq 'no_plan') {
135 croak("no_plan takes no arguments") if $value;
137 } elsif ($key eq 'tests') {
138 croak("Got an undefined number of tests") unless defined $value;
139 croak("You said to run 0 tests") unless $value;
140 croak("Number of tests must be a positive integer. You gave it '$value'")
141 unless $value =~ /^\+?[0-9]+$/;
143 $plan_str = "1..$value";
144 } elsif ($key eq 'skip_all') {
146 $plan_str = '1..0 # SKIP';
147 if (defined $value) {
148 _sanitize_comment($value);
149 $plan_str .= " $value" if length $value;
152 my @args = grep defined, $key, $value;
153 croak("plan() doesn't understand @args");
156 if (defined $plan_str) {
158 print $TAP_STREAM "$plan_str\n";
161 exit 0 if $plan == SKIP_ALL;
192 if ($item eq 'import') {
193 push @imports, @{ $_[$i+1] };
195 } elsif ($item eq 'no_diag') {
196 lock $plan if THREADSAFE;
202 splice @_, $i, $splice;
209 local $Test::Builder::Level = ($Test::Builder::Level || 0) + 1;
213 @_ = ($class, @imports);
214 goto &Exporter::import;
217 =head2 C<< skip $reason => $count >>
222 my ($reason, $count) = @_;
224 lock $plan if THREADSAFE;
226 if (not defined $count) {
227 carp("skip() needs to know \$how_many tests are in the block")
228 unless defined $plan and $plan == NO_PLAN;
230 } elsif ($count =~ /[^0-9]/) {
231 carp('skip() was passed a non-numeric number of tests. Did you get the arguments backwards?');
238 my $skip_str = "ok $test # skip";
239 if (defined $reason) {
240 _sanitize_comment($reason);
241 $skip_str .= " $reason" if length $reason;
245 print $TAP_STREAM "$skip_str\n";
248 no warnings 'exiting';
252 =head2 C<done_testing [ $count ]>
259 lock $plan if THREADSAFE;
261 $count = $test unless defined $count;
262 croak("Number of tests must be a positive integer. You gave it '$count'")
263 unless $count =~ /^\+?[0-9]+$/;
265 if (not defined $plan or $plan == NO_PLAN) {
266 $plan = $count; # $plan can't be NO_PLAN anymore
269 print $TAP_STREAM "1..$plan\n";
272 @_ = ('done_testing() was already called');
274 } elsif ($plan != $count) {
275 @_ = ("planned to run $plan tests but done_testing() expects $count");
283 =head2 C<ok $ok [, $desc ]>
288 my ($ok, $desc) = @_;
290 lock $plan if THREADSAFE;
294 my $test_str = "ok $test";
296 $test_str = "not $test_str";
300 _sanitize_comment($desc);
301 $test_str .= " - $desc" if length $desc;
305 print $TAP_STREAM "$test_str\n";
310 =head2 C<pass [ $desc ]>
319 =head2 C<fail [ $desc ]>
328 =head2 C<is $got, $expected [, $desc ]>
333 my ($got, $expected, $desc) = @_;
334 no warnings 'uninitialized';
336 (not(defined $got xor defined $expected) and $got eq $expected),
342 =head2 C<isnt $got, $expected [, $desc ]>
347 my ($got, $expected, $desc) = @_;
348 no warnings 'uninitialized';
350 ((defined $got xor defined $expected) or $got ne $expected),
362 ('//' => 'dor') x ($] >= 5.010),
387 ('~~' => 'smartmatch') x ($] >= 5.010),
399 '...' => 'altflipflop',
406 sub _create_binop_handler {
408 my $name = $binops{$op};
409 croak("Operator $op not supported") unless defined $name;
413 sub is_$name (\$\$;\$) {
414 my (\$got, \$expected, \$desc) = \@_;
415 \@_ = (scalar(\$got $op \$expected), \$desc);
421 $binop_handlers{$op} = do {
423 \&{__PACKAGE__."::is_$name"};
427 =head2 C<like $got, $regexp_expected [, $desc ]>
429 =head2 C<unlike $got, $regexp_expected, [, $desc ]>
435 *like = _create_binop_handler('=~');
436 *unlike = _create_binop_handler('!~');
439 =head2 C<cmp_ok $got, $op, $expected [, $desc ]>
444 my ($got, $op, $expected, $desc) = @_;
445 my $handler = $binop_handlers{$op};
447 local $Test::More::Level = ($Test::More::Level || 0) + 1;
448 $handler = _create_binop_handler($op);
450 @_ = ($got, $expected, $desc);
459 lock $plan if THREADSAFE;
462 my $msg = join '', map { defined($_) ? $_ : 'undef' } @_;
463 _sanitize_comment($msg);
464 return unless length $msg;
467 print $fh "# $msg\n";
477 unshift @_, $DIAG_STREAM;
486 unshift @_, $TAP_STREAM;
490 =head2 C<BAIL_OUT [ $desc ]>
497 lock $plan if THREADSAFE;
499 my $bail_out_str = 'Bail out!';
501 _sanitize_comment($desc);
502 $bail_out_str .= " $desc" if length $desc; # Two spaces
506 print $TAP_STREAM "$bail_out_str\n";
513 lock $plan if THREADSAFE;
517 $? = $failed <= 254 ? $failed : 254;
518 } elsif ($plan >= 0) {
519 $? = $test == $plan ? 0 : 255;
520 } elsif ($plan == NO_PLAN) {
522 print $TAP_STREAM "1..$test\n";
530 L<Test::Leaner> also provides some functions of its own, which are never exported.
532 =head2 C<tap_stream [ $fh ]>
534 Read/write accessor for the filehandle to which the tests are outputted.
535 On write, it also turns autoflush on onto C<$fh>.
537 Note that it can only be used as a write accessor before you start any thread, as L<threads::shared> cannot reliably share filehandles.
539 Defaults to C<STDOUT>.
543 sub tap_stream (;*) {
547 my $fh = select $TAP_STREAM;
557 =head2 C<diag_stream [ $fh ]>
559 Read/write accessor for the filehandle to which the diagnostics are printed.
560 On write, it also turns autoflush on onto C<$fh>.
562 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.
564 Defaults to C<STDERR>.
568 sub diag_stream (;*) {
570 $DIAG_STREAM = $_[0];
572 my $fh = select $DIAG_STREAM;
584 This constant evaluates to true if and only if L<Test::Leaner> is thread-safe, i.e. when this version of C<perl> is at least 5.8, has been compiled with C<useithreads> defined, and L<threads> has been loaded B<before> L<Test::Leaner>.
585 In that case, it also needs a working L<threads::shared>.
591 L<Exporter>, L<Test::More>
595 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
597 You can contact me by mail or on C<irc.perl.org> (vincent).
601 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>.
602 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
606 You can find documentation for this module with the perldoc command.
610 =head1 COPYRIGHT & LICENSE
612 Copyright 2010 Vincent Pit, all rights reserved.
614 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
618 1; # End of Test::Leaner