| 1 | package Test::AtRuntime; | ||||||||||
| 2 | |||||||||||
| 3 | =head1 NAME | ||||||||||
| 4 | |||||||||||
| 5 | Test::AtRuntime - Put tests in your code and run them as your program runs | ||||||||||
| 6 | |||||||||||
| 7 | |||||||||||
| 8 | =head1 SYNOPSIS | ||||||||||
| 9 | |||||||||||
| 10 | use Test::AtRuntime 'logfile'; | ||||||||||
| 11 | use Test::More; | ||||||||||
| 12 | |||||||||||
| 13 | sub foo { | ||||||||||
| 14 | # This test runs. | ||||||||||
| 15 | TEST { pass('foo ran'); } | ||||||||||
| 16 | } | ||||||||||
| 17 | |||||||||||
| 18 | no Test::AtRuntime; | ||||||||||
| 19 | |||||||||||
| 20 | sub bar { | ||||||||||
| 21 | # This test is not run. | ||||||||||
| 22 | TEST { pass('bar ran') } | ||||||||||
| 23 | } | ||||||||||
| 24 | |||||||||||
| 25 | foo(); | ||||||||||
| 26 | bar(); | ||||||||||
| 27 | |||||||||||
| 28 | =head1 DESCRIPTION | ||||||||||
| 29 | |||||||||||
| 30 | Test::AtRuntime lets you use Test::More and other Test::Builder based modules | ||||||||||
| 31 | directly in your source code providing a way to test your program as it | ||||||||||
| 32 | runs. Similar to the concept of an assertion, except instead of dying | ||||||||||
| 33 | when it fails, normal "not ok" output will be seen. | ||||||||||
| 34 | |||||||||||
| 35 | =head2 Compiling out | ||||||||||
| 36 | |||||||||||
| 37 | Like assertions, they can be turned on or off as needed. Tests are put | ||||||||||
| 38 | inside of a TEST block like so: | ||||||||||
| 39 | |||||||||||
| 40 | TEST { like( $totally, qr/rad/ ) } | ||||||||||
| 41 | |||||||||||
| 42 | C<use Test::AtRuntime> runs these tests. C<no Test::AtRuntime> means these | ||||||||||
| 43 | tests will not be run. In fact, they will be completely removed from the | ||||||||||
| 44 | program so that performance will not be effected (except some startup | ||||||||||
| 45 | performance for the filtering). | ||||||||||
| 46 | |||||||||||
| 47 | =head2 Logfile | ||||||||||
| 48 | |||||||||||
| 49 | C<use Test::AtRuntime> takes an argument, a logfile to append your tests to. | ||||||||||
| 50 | If no logfile is given, tests will be outputed like normal. | ||||||||||
| 51 | |||||||||||
| 52 | |||||||||||
| 53 | =head1 CAVEATS | ||||||||||
| 54 | |||||||||||
| 55 | Due to what appears to be a bug in Filter::Simple, this won't work as | ||||||||||
| 56 | expected: | ||||||||||
| 57 | |||||||||||
| 58 | use Test::AtRuntime; | ||||||||||
| 59 | |||||||||||
| 60 | ...run tests... | ||||||||||
| 61 | |||||||||||
| 62 | |||||||||||
| 63 | no Test::AtRuntime; | ||||||||||
| 64 | |||||||||||
| 65 | ...don't run tests... | ||||||||||
| 66 | |||||||||||
| 67 | use Test::AtRuntime; | ||||||||||
| 68 | |||||||||||
| 69 | ...run tests... <--- BUG | ||||||||||
| 70 | |||||||||||
| 71 | Once you stop running tests, they can't be made to run again. | ||||||||||
| 72 | |||||||||||
| 73 | |||||||||||
| 74 | =head1 TODO | ||||||||||
| 75 | |||||||||||
| 76 | =over 4 | ||||||||||
| 77 | |||||||||||
| 78 | =item * suppress ok | ||||||||||
| 79 | |||||||||||
| 80 | It'll probably be useful to suppress the 'ok' messages so only | ||||||||||
| 81 | failures are seen. Then again, "tail -f logfile | grep '^ok '" does a | ||||||||||
| 82 | good job of that. Also, Test::Builder doesn't support that yet. | ||||||||||
| 83 | |||||||||||
| 84 | =item * honor environment variables | ||||||||||
| 85 | |||||||||||
| 86 | Test::AtRuntime should honor the same NDEBUG and PERL_NDEBUG | ||||||||||
| 87 | environment variables as Carp::Assert and possibly an additional one | ||||||||||
| 88 | just for Test::AtRuntime. | ||||||||||
| 89 | |||||||||||
| 90 | =item * stack trace on failure | ||||||||||
| 91 | |||||||||||
| 92 | Failing test should be accompanied by a stack trace to help figure out | ||||||||||
| 93 | what's going wrong. | ||||||||||
| 94 | |||||||||||
| 95 | =back | ||||||||||
| 96 | |||||||||||
| 97 | |||||||||||
| 98 | =head1 SEE ALSO | ||||||||||
| 99 | |||||||||||
| 100 | Test::More, Carp::Assert, Carp::Assert::More, Test::Inline, Test::Class | ||||||||||
| 101 | |||||||||||
| 102 | =cut | ||||||||||
| 103 | |||||||||||
| 104 | |||||||||||
| 105 | 4 | $VERSION = 0.02; | |||||||||
| 106 | |||||||||||
| 107 | 4 | ▶ | use Filter::Simple; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 108 | 4 | ▶ | use File::Spec; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 109 | 4 | ▶ | use Regexp::Common; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 110 | 4 | ▶ | use Test::Builder; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 111 | |||||||||||
| 112 | |||||||||||
| 113 | 4 | my $TB = Test::Builder->new; | |||||||||
| 114 | 4 | $TB->plan('no_plan'); | |||||||||
| 115 | 4 | $TB->use_numbers(0); | |||||||||
| 116 | 4 | $TB->no_header(0); | |||||||||
| 117 | |||||||||||
| 118 | 7 | ▶ | sub import { | ||||||||
|
Subroutine
import: called CC=2 SCAR=6.9
| |||||||||||
| 119 | 7 | my($class, $logfile) = @_; | |||||||||
| 120 | 7 | ▶ | $Testing = $Not_Testing ? 0 : 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 121 | # print STDERR "import: NT $Not_Testing T $Testing\n"; | ||||||||||
| 122 | 7 | set_log($logfile); | |||||||||
| 123 | 7 | $Not_Testing = 0; | |||||||||
| 124 | } | ||||||||||
| 125 | |||||||||||
| 126 | 3 | ▶ | sub not_testing { | ||||||||
|
Subroutine
not_testing: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 127 | 3 | my($class, $logfile) = @_; | |||||||||
| 128 | 3 | $Not_Testing = 1; | |||||||||
| 129 | # print STDERR "not_testing\n"; | ||||||||||
| 130 | 3 | goto &import; | |||||||||
| 131 | } | ||||||||||
| 132 | |||||||||||
| 133 | 7 | ▶ | sub set_log { | ||||||||
|
Subroutine
set_log: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 134 | 7 | my($logfile) = @_; | |||||||||
| 135 | |||||||||||
| 136 | 7 | ▶ | if( defined $logfile ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 137 | 1 | ▶ | open(LOGFILE, ">>$logfile") || die $!; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 138 | 1 | my $oldfh = select LOGFILE; | |||||||||
| 139 | 1 | $| = 1; | |||||||||
| 140 | 1 | select $oldfh; | |||||||||
| 141 | |||||||||||
| 142 | 1 | $TB->output(\*LOGFILE); | |||||||||
| 143 | 1 | $TB->failure_output(\*LOGFILE); | |||||||||
| 144 | 1 | $TB->todo_output(File::Spec->devnull); | |||||||||
| 145 | } | ||||||||||
| 146 | } | ||||||||||
| 147 | |||||||||||
| 148 | |||||||||||
| 149 | FILTER_ONLY( | ||||||||||
| 150 | 11 | ▶ | executable => sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 151 | 11 | ▶ | $Testing ? s[ \bTEST \s+ ($RE{balanced}{-parens=>'{}'}) ][$1]xg | ||||||||
|
Branch: 1 branch
| |||||||||||
| 152 | : s[ \bTEST \s+ $RE{balanced}{-parens=>'{}'} ][]xg | ||||||||||
| 153 | }, | ||||||||||
| 154 | # all => sub { print }, | ||||||||||
| 155 | # qr/^\s*(?:use|no)\s+Test::AtRuntime\b/ | ||||||||||
| 156 | 4 | ); | |||||||||
| 157 | |||||||||||
| 158 | 4 | ▶ | no warnings 'redefine'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 159 | 4 | (*unimport, *import) = (\¬_testing, \&import); | |||||||||
| 160 | |||||||||||
| 161 | 4 | 1; | |||||||||
| 162 | |||||||||||
| 163 | |||||||||||