Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

output() and friends now support scalar references.

This will make life a lot easier while testing itself.  No more having to
screw around with TieOut.  Had to slurp in IO::Scalar for 5.6 support.
I started with just TieOut but getting the self-tie right was too much
of a PITA so I figured just go with something that works.
  • Loading branch information...
commit db3f05ee29205d7673b68630babc9c73ae09cff6 1 parent e6d4006
Michael G. Schwern schwern authored
1  Changes
@@ -2,6 +2,7 @@
2 2 New Features
3 3 * done_testing() allows you to declare that you have finished running tests,
4 4 and how many you ran. It is a safer no_plan and effectively replaces it.
  5 + * output() now supports scalar references.
5 6
6 7 Feature Changes
7 8 * You can now run a test without first declaring a plan. This allows
1  MANIFEST
@@ -7,6 +7,7 @@ Makefile.PL
7 7 README
8 8 TODO
9 9 lib/Test/Builder.pm
  10 +lib/Test/Builder/IO/Scalar.pm
10 11 lib/Test/Builder/Module.pm
11 12 lib/Test/Builder/Tester.pm
12 13 lib/Test/Builder/Tester/Color.pm
51 lib/Test/Builder.pm
@@ -7,6 +7,13 @@ use warnings;
7 7 our $VERSION = '0.86';
8 8 $VERSION = eval $VERSION; ## no critic (BuiltinFunctions::ProhibitStringyEval)
9 9
  10 +BEGIN {
  11 + if( $] < 5.008 ) {
  12 + require Test::Builder::IO::Scalar;
  13 + }
  14 +}
  15 +
  16 +
10 17 # Make Test::Builder thread-safe for ithreads.
11 18 BEGIN {
12 19 use Config;
@@ -1409,28 +1416,32 @@ sub _print_to_fh {
1409 1416
1410 1417 =item B<output>
1411 1418
1412   - $Test->output($fh);
1413   - $Test->output($file);
1414   -
1415   -Where normal "ok/not ok" test output should go.
  1419 +=item B<failure_output>
1416 1420
1417   -Defaults to STDOUT.
  1421 +=item B<todo_output>
1418 1422
1419   -=item B<failure_output>
  1423 + my $filehandle = $Test->output;
  1424 + $Test->output($filehandle);
  1425 + $Test->output($filename);
  1426 + $Test->output(\$scalar);
1420 1427
1421   - $Test->failure_output($fh);
1422   - $Test->failure_output($file);
  1428 +These methods control where Test::Builder will print it's output.
  1429 +They take either an open $filehandle, a $filename to open and write to
  1430 +or a $scalar reference to append to. It will always return a $filehandle.
1423 1431
1424   -Where diagnostic output on test failures and diag() should go.
  1432 +B<output> is where normal "ok/not ok" test output goes.
1425 1433
1426   -Defaults to STDERR.
  1434 +Defaults to STDOUT.
1427 1435
1428   -=item B<todo_output>
  1436 +B<failure_output> is where diagnostic output on test failures and
  1437 +diag() goes. It is normally not read by Test::Harness and instead is
  1438 +displayed to the user.
1429 1439
1430   - $Test->todo_output($fh);
1431   - $Test->todo_output($file);
  1440 +Defaults to STDERR.
1432 1441
1433   -Where diagnostics about todo test failures and diag() should go.
  1442 +B<todo_output> is used instead of C<<failure_output()>> for the
  1443 +diagnostics of a failing TODO test. These will not be seen by the
  1444 +user.
1434 1445
1435 1446 Defaults to STDOUT.
1436 1447
@@ -1471,6 +1482,18 @@ sub _new_fh {
1471 1482 if( $self->is_fh($file_or_fh) ) {
1472 1483 $fh = $file_or_fh;
1473 1484 }
  1485 + elsif( ref $file_or_fh eq 'SCALAR' ) {
  1486 + # Scalar refs as filehandles was added in 5.8.
  1487 + if( $] >= 5.008 ) {
  1488 + open $fh, ">", $file_or_fh
  1489 + or $self->croak("Can't open scalar ref $file_or_fh: $!");
  1490 + }
  1491 + # Emulate scalar ref filehandles with a tie.
  1492 + else {
  1493 + $fh = Test::Builder::IO::Scalar->new($file_or_fh)
  1494 + or $self->croak("Can't tie scalar ref $file_or_fh");
  1495 + }
  1496 + }
1474 1497 else {
1475 1498 open $fh, ">", $file_or_fh
1476 1499 or $self->croak("Can't open test output log $file_or_fh: $!");
21 t/Builder/output.t
@@ -13,13 +13,11 @@ BEGIN {
13 13 }
14 14 chdir 't';
15 15
16   -
17   -use TieOut;
18 16 use Test::Builder;
19 17
20 18 # The real Test::Builder
21 19 my $Test = Test::Builder->new;
22   -$Test->plan( tests => 4 );
  20 +$Test->plan( tests => 5 );
23 21
24 22
25 23 # The one we're going to test.
@@ -62,10 +60,20 @@ END { 1 while unlink($tmpfile) }
62 60 }
63 61
64 62
  63 +# Test output to a scalar ref
  64 +{
  65 + my $scalar = '';
  66 + my $out = $tb->output(\$scalar);
  67 +
  68 + print $out "Hey hey hey!\n";
  69 + $Test->is_eq($scalar, "Hey hey hey!\n");
  70 +}
  71 +
  72 +
65 73 # Ensure stray newline in name escaping works.
66 74 {
67   - my $out = tie *FAKEOUT, 'TieOut';
68   - $tb->output(\*FAKEOUT);
  75 + my $fakeout = '';
  76 + my $out = $tb->output(\$fakeout);
69 77 $tb->exported_to(__PACKAGE__);
70 78 $tb->no_ending(1);
71 79 $tb->plan(tests => 5);
@@ -76,8 +84,7 @@ END { 1 while unlink($tmpfile) }
76 84 $tb->skip("wibble\nmoof");
77 85 $tb->todo_skip("todo\nskip\n");
78 86
79   - my $output = $out->read;
80   - $Test->is_eq( $output, <<OUTPUT ) || print STDERR $output;
  87 + $Test->is_eq( $fakeout, <<OUTPUT ) || print STDERR $fakeout;
81 88 1..5
82 89 ok 1 - ok
83 90 ok 2 - ok

0 comments on commit db3f05e

Please sign in to comment.
Something went wrong with that request. Please try again.