| 1 | package Test::LectroTest::Compat; | ||||||||||||||||||||||||||||
| 2 | { | ||||||||||||||||||||||||||||
| 3 | 9 | $Test::LectroTest::Compat::VERSION = '0.5001'; | |||||||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 9 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 9 | ▶ | use Filter::Util::Call; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 9 | ▶ | use Test::Builder; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 9 | ▶ | use Test::LectroTest::TestRunner; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 9 | require Test::LectroTest::Property; | |||||||||||||||||||||||||||
| 13 | 9 | require Test::LectroTest::Generator; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | =head1 NAME | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | Test::LectroTest::Compat - Use LectroTest property checks in a Test::Simple world | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | =head1 VERSION | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | version 0.5001 | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | #!/usr/bin/perl -w | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | use MyModule; # contains code we want to test | ||||||||||||||||||||||||||||
| 28 | use Test::More tests => 2; | ||||||||||||||||||||||||||||
| 29 | use Test::LectroTest::Compat; | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | # property specs can now use Test::Builder-based | ||||||||||||||||||||||||||||
| 32 | # tests such as Test::More's cmp_ok() | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | my $prop_nonnegative = Property { | ||||||||||||||||||||||||||||
| 35 | ##[ x <- Int, y <- Int ]## | ||||||||||||||||||||||||||||
| 36 | cmp_ok(MyModule::my_function( $x, $y ), '>=', 0); | ||||||||||||||||||||||||||||
| 37 | }, name => "my_function output is non-negative" ; | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | # and we can now check whether properties hold | ||||||||||||||||||||||||||||
| 40 | # as a Test::Builder-style test that integrates | ||||||||||||||||||||||||||||
| 41 | # with other T::B tests | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | holds( $prop_nonnegative ); # test whether prop holds | ||||||||||||||||||||||||||||
| 44 | cmp_ok( 0, '<', 1, "trivial 0<1 test" ); # a "normal" test | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | This module lets you use mix LectroTest property checking with other | ||||||||||||||||||||||||||||
| 49 | popular Test::* modules. With it, you can use C<is()>- and | ||||||||||||||||||||||||||||
| 50 | C<ok()>-style assertions from Test::* modules within your LectroTest | ||||||||||||||||||||||||||||
| 51 | property specifications and you can check LectroTest properties as | ||||||||||||||||||||||||||||
| 52 | part of a Test::Simple or Test::More test plan. (You can actually | ||||||||||||||||||||||||||||
| 53 | take advantage of any module based on Test::Builder, not just | ||||||||||||||||||||||||||||
| 54 | Test::Simple and Test::More.) | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | The module exports a single function C<holds> which is described | ||||||||||||||||||||||||||||
| 57 | below. | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | =head2 holds(I<property>, I<opts>...) | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | holds( $prop_nonnegative ); # check prop_nonnegative | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | holds( $prop_nonnegative, trials => 100 ); | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | holds( | ||||||||||||||||||||||||||||
| 66 | Property { | ||||||||||||||||||||||||||||
| 67 | ##[ x <- Int ]## | ||||||||||||||||||||||||||||
| 68 | my_function2($x) < 0; | ||||||||||||||||||||||||||||
| 69 | }, name => "my_function2 is non-positive" | ||||||||||||||||||||||||||||
| 70 | ); | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | Checks whether the given property holds. | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | When called, this method creates a new | ||||||||||||||||||||||||||||
| 75 | Test::LectroTest::TestRunner, asks the TestRunner to check the | ||||||||||||||||||||||||||||
| 76 | property, and then reports the result to Test::Builder, which in | ||||||||||||||||||||||||||||
| 77 | turn reports to you as part of a typical Test::Simple- or | ||||||||||||||||||||||||||||
| 78 | Test::More-style test plan. Any options you provide to C<holds> after | ||||||||||||||||||||||||||||
| 79 | the property will be passed to the C<TestRunner> so you can change the | ||||||||||||||||||||||||||||
| 80 | number of trials to run and so on. (See the docs for C<new> in | ||||||||||||||||||||||||||||
| 81 | L<Test::LectroTest::TestRunner> for the complete list of | ||||||||||||||||||||||||||||
| 82 | options.) | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | =head1 TESTING FOR REGRESSIONS AND CORNER CASES | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | LectroTest can record failure-causing test cases to a file, and it can | ||||||||||||||||||||||||||||
| 88 | play those test cases back as part of its normal testing strategy. | ||||||||||||||||||||||||||||
| 89 | The easiest way to take advantage of this feature is to set the | ||||||||||||||||||||||||||||
| 90 | I<regressions> parameter when you C<use> this module: | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | use Test::LectroTest::Compat | ||||||||||||||||||||||||||||
| 93 | regressions => "regressions.txt"; | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | This tells LectroTest to use the file "regressions.txt" for both | ||||||||||||||||||||||||||||
| 96 | recording and playing back failures. If you want to record and | ||||||||||||||||||||||||||||
| 97 | play back from separate files, or want only to record I<or> play | ||||||||||||||||||||||||||||
| 98 | back, use the I<record_failures> and/or | ||||||||||||||||||||||||||||
| 99 | I<playback_failures> options: | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | use Test::LectroTest::Compat | ||||||||||||||||||||||||||||
| 102 | playback_failures => "regression_suite_for_my_module.txt", | ||||||||||||||||||||||||||||
| 103 | record_failures => "failures_in_the_field.txt"; | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | See L<Test::LectroTest::RegressionTesting> for more. | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | B<NOTE:> If you pass any of the recording or playback parameters | ||||||||||||||||||||||||||||
| 108 | to Test::LectroTest::Compat, you must have version 0.3500 or | ||||||||||||||||||||||||||||
| 109 | greater of LectroTest installed. Module authors, update your | ||||||||||||||||||||||||||||
| 110 | modules' build dependencies accordingly. | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | =cut | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 9 | my $Test = Test::Builder->new(); | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 8 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 118 | 8 | my $self = shift; | |||||||||||||||||||||||||||
| 119 | 8 | my $caller = caller; | |||||||||||||||||||||||||||
| 120 | 9 | ▶ | { no strict 'refs'; *{$caller.'::holds'} = \&holds; } | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 121 | 8 | $Test->exported_to($caller); | |||||||||||||||||||||||||||
| 122 | 8 | $Test->plan(_filter_recorder_opts(@_)); | |||||||||||||||||||||||||||
| 123 | 8 | Test::LectroTest::Property->export_to_level(1, $self); | |||||||||||||||||||||||||||
| 124 | 8 | Test::LectroTest::Generator->export_to_level(1, $self, ':all'); | |||||||||||||||||||||||||||
| 125 | 8 | filter_add(Test::LectroTest::Property->_make_code_filter); | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 16 | ▶ | sub holds { | ||||||||||||||||||||||||||
|
Subroutine
holds: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 129 | 16 | my ($diag_store, $results) = _check_property(@_); | |||||||||||||||||||||||||||
| 130 | 16 | my $success = $results->success; | |||||||||||||||||||||||||||
| 131 | 16 | (my $name = $results->summary) =~ s/^.*?- /property /; | |||||||||||||||||||||||||||
| 132 | 16 | $Test->ok($success, $name); | |||||||||||||||||||||||||||
| 133 | 16 | ▶ | $Test->diag(@$diag_store) if @$diag_store; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 16 | my $details = $results->details; | |||||||||||||||||||||||||||
| 135 | 16 | $details =~ s/^.*?\n//; # remove summary line | |||||||||||||||||||||||||||
| 136 | 16 | $details =~ s/^\# / /mg; # replace commenting w/ indent | |||||||||||||||||||||||||||
| 137 | 16 | ▶ | $Test->diag($details) if $details; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 16 | ▶ | return $success ? 1 : 0; # same result policy as Test::Builder::ok | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 9 | my ($playback_failures, $record_failures); | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 16 | ▶ | sub _check_property { | ||||||||||||||||||||||||||
|
Subroutine
_check_property: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 144 | 9 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 145 | 16 | my $diag_store = []; | |||||||||||||||||||||||||||
| 146 | 16 | my $property = shift; | |||||||||||||||||||||||||||
| 147 | 16 | local *Test::Builder::ok = \&_disconnected_ok; | |||||||||||||||||||||||||||
| 148 | 16 | ▶ | local *Test::Builder::diag = sub { shift; push @$diag_store, @_; 0 }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | # for efficiency, we recycle any recorders that the TestRunner | ||||||||||||||||||||||||||||
| 151 | # may have created (the recorders cache test cases) | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 16 | ▶ | my @opts = ( | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 154 | $playback_failures ? (playback_failures => $playback_failures) : (), | ||||||||||||||||||||||||||||
| 155 | $record_failures ? (record_failures => $record_failures) : (), | ||||||||||||||||||||||||||||
| 156 | @_ # passed-in options go last to override defaults | ||||||||||||||||||||||||||||
| 157 | ); | ||||||||||||||||||||||||||||
| 158 | 16 | my $runner = Test::LectroTest::TestRunner->new(@opts); | |||||||||||||||||||||||||||
| 159 | 16 | my @results = ($diag_store, $runner->run($property)); | |||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | # the TestRunner may have converted file names into TestRecorder | ||||||||||||||||||||||||||||
| 162 | # objects, so we just "upgrade" to these objects if they exist | ||||||||||||||||||||||||||||
| 163 | # and we're still holding filenames | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 16 | ▶ | $playback_failures = $runner->playback_failures | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$playback_failures and not ref $playback_failures
MC/DC100%2/2
$playback_failures and not ref $playback_failures
$playback_failures
not ref $playback_failures
Truth table100%3/3
A and not(B)
A= $playback_failuresB= not ref $playback_failures
| |||||||||||||||||||||||||||||
| 166 | if $playback_failures && !ref($playback_failures); | ||||||||||||||||||||||||||||
| 167 | 16 | ▶ | $record_failures = $runner->record_failures | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$record_failures and not ref $record_failures
MC/DC50%1/2
$record_failures and not ref $record_failures
$record_failures
not ref $record_failures
Truth table66%2/3
A and not(B)
A= $record_failuresB= not ref $record_failures
| |||||||||||||||||||||||||||||
| 168 | if $record_failures && !ref($record_failures); | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 16 | return @results; | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 9 | my @RECORDER_OPTS = (qw( record_failures playback_failures regressions )); | |||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 8 | ▶ | sub _filter_recorder_opts { | ||||||||||||||||||||||||||
|
Subroutine
_filter_recorder_opts: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 176 | 8 | my (@opts); | |||||||||||||||||||||||||||
| 177 | 8 | ▶ | while (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 178 | 23 | ▶ | unless (grep $_ eq $_[0], @RECORDER_OPTS) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 179 | 16 | push @opts, shift; | |||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | else { | ||||||||||||||||||||||||||||
| 182 | 7 | my ($ropt, $rval) = (shift, shift); | |||||||||||||||||||||||||||
| 183 | 7 | ▶ | if ($ropt eq "regressions") { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 184 | 3 | $playback_failures = $record_failures = $rval; | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | elsif ($ropt eq "playback_failures") { | ||||||||||||||||||||||||||||
| 187 | 3 | $playback_failures = $rval; | |||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | else { | ||||||||||||||||||||||||||||
| 190 | 1 | $record_failures = $rval; | |||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | 8 | return @opts; | |||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | # the following sub replaces Test::Builder's | ||||||||||||||||||||||||||||
| 198 | # ok() method when we want to disable T::B's | ||||||||||||||||||||||||||||
| 199 | # test harness | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 2001 | ▶ | sub _disconnected_ok { $_[1] ? 1 : 0 } | ||||||||||||||||||||||||||
|
Subroutine
_disconnected_ok: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 9 | 1; | |||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | =head1 BUGS | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | In order to integrate with the L<Test::Builder> testing harness (whose | ||||||||||||||||||||||||||||
| 209 | underlying testing model is somewhat incompatible with the needs of | ||||||||||||||||||||||||||||
| 210 | random trial-based testing) this module redefines two Test::Builder | ||||||||||||||||||||||||||||
| 211 | functions (C<ok()> and C<diag()>) for the duration of each property | ||||||||||||||||||||||||||||
| 212 | check. | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | For a gentle introduction to LectroTest, see | ||||||||||||||||||||||||||||
| 218 | L<Test::LectroTest::Tutorial>. Also, the slides from my LectroTest | ||||||||||||||||||||||||||||
| 219 | talk for the Pittsburgh Perl Mongers make for a great introduction. | ||||||||||||||||||||||||||||
| 220 | Download a copy from the LectroTest home (see below). | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | L<Test::LectroTest::RegressionTesting> explains how to test for | ||||||||||||||||||||||||||||
| 223 | regressions and corner cases using LectroTest. | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | L<Test::LectroTest::Property> explains in detail what | ||||||||||||||||||||||||||||
| 226 | you can put inside of your property specifications. | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | L<Test::LectroTest::Generator> describes the many generators and | ||||||||||||||||||||||||||||
| 229 | generator combinators that you can use to define the test or | ||||||||||||||||||||||||||||
| 230 | condition space that you want LectroTest to search for bugs. | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | L<Test::LectroTest::TestRunner> describes the objects that check your | ||||||||||||||||||||||||||||
| 233 | properties and tells you how to turn their control knobs. You'll want | ||||||||||||||||||||||||||||
| 234 | to look here if you're interested in customizing the testing | ||||||||||||||||||||||||||||
| 235 | procedure. | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | L<Test::Simple> and L<Test::More> explain how to do simple | ||||||||||||||||||||||||||||
| 238 | case-based testing in Perl. | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | L<Test::Builder> is the test harness upon which this module | ||||||||||||||||||||||||||||
| 241 | is built. | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | Tom Moertel (tom@moertel.com) | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | =head1 INSPIRATION | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | The LectroTest project was inspired by Haskell's | ||||||||||||||||||||||||||||
| 251 | QuickCheck module by Koen Claessen and John Hughes: | ||||||||||||||||||||||||||||
| 252 | http://www.cs.chalmers.se/~rjmh/QuickCheck/. | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | =head1 COPYRIGHT and LICENSE | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | Copyright (c) 2004-13 by Thomas G Moertel. All rights reserved. | ||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | This program is free software; you can redistribute it and/or | ||||||||||||||||||||||||||||
| 259 | modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | =cut | ||||||||||||||||||||||||||||