blib/lib/Test/LectroTest/FailureRecorder.pm

filter: Statementstmt 97.9% 48 / 49 Branchbran 62.5% 5 / 8 Conditioncond 52.9% 9 / 17 MC/DCmcdc 20.0% 2 / 10 Subroutinesub 100% 8 / 8 Podpod 100% 3 / 3 totaltotal 78.9% 75 / 95 CC 11 SCAR 16.1
CC
11
Cov
84%
_platform
9.6
record_failure_for_property
3.0
_store
3.0
CRAP 5.0
« blib/lib/Test/LectroTest/Property.pm ↑ Summary blib/lib/Test/LectroTest/TestRunner.pm »
1 package Test::LectroTest::FailureRecorder;
2 {
3 25 $Test::LectroTest::FailureRecorder::VERSION = '0.5001';
4 }
5  
6 25 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 25 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 25 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12 =head1 NAME
13  
14 Test::LectroTest::FailureRecorder - Records/plays failures for regression testing
15  
16 =head1 VERSION
17  
18 version 0.5001
19  
20 =head1 SYNOPSIS
21  
22 use Test::LectroTest::Recorder;
23  
24 my $recorder = Test::LectroTest::Recorder->new("storage_file.txt");
25  
26 my $recorder->record_failure_for_property(
27 "property name",
28 $input_hashref_from_counterexample
29 );
30  
31 my $failures = $recorder->get_failures_for_property("property name");
32 for my $input_hashref (@$failures) {
33 # do something with hashref
34 }
35  
36  
37 =head1 DESCRIPTION
38  
39 This module provides a simple means of recording property-check
40 failures so they can be reused as regression tests. You do not need
41 to use this module yourself because the higher-level LectroTest
42 modules will use it for you when needed. (These docs are mainly
43 for LectroTest developers.)
44  
45 The basic idea is to record a failure as a pair of the form
46  
47 [ <property_name>, <input hash from counterexample> ]
48  
49 and Dump these pairs into a text file, each record terminated by blank
50 line so that the file can be read using paragraph-slurp mode.
51  
52 The module provides methods to add such pairs to a recorder file and
53 to retrieve the recorded failures by property name. It uses a cache
54 to avoid repetitive reads.
55  
56  
57 =head1 METHODS
58  
59 =head2 new(I<storage-file>)
60  
61 my $recorder = Test::LectroTest::Recorder->new("/path/to/storage.txt");
62  
63 Creates a new recorder object and tells it to use I<storage-file>
64 for the reading and writing of failures.
65  
66 The recorder will not access the storage file until you attempt to
67 get or record a failure. Thus it is OK to specify a storage file that
68 does not yet exist, provided you record failures to it before you
69 attempt to get failures from it.
70  
71 =cut
72  
73 15 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
74 15 my $class = shift;
75 15 return bless { file => $_[0] }, $class;
76 }
77  
78 # get failure store from cache or file
79  
80 40 ▶sub _store {
Subroutine _store: called CC=3 SCAR=11.0
81 40 my ($self) = @_;
82 40 my $file = $self->{file};
83 40 ▶ $self->{cache} ||= do {
Condition100%3/3
$self->{"cache"} ||= do { open my $fh, $file or die "could not open ${file}: $!"; local $/ = ""; my(@recs) = map(eval($_), readline $fh); close $fh; \@recs }
l!l&&r!l&&!r
26104
MC/DC100%2/2
$self->{"cache"} ||= do { open my $fh, $file or die "could not open ${file}: $!"; local $/ = ""; my(@recs) = map(eval($_), readline $fh); close $fh; \@recs }
$self->{"cache"} do { open my $fh, $file or die "could not open ${file}: $!"; local $/ = ""; my(@recs) = map(eval($_), readline $fh); close $fh; \@recs }
Truth table100%3/3
A ||= B
A= $self->{"cache"}B= do { open my $fh, $file or die "could not open ${file}: $!"; local $/ = ""; my(@recs) = map(eval($_), readline $fh); close $fh; \@recs }
ABresult
1X1
011
000
84 14 ▶ open my $fh, $file or die "could not open $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, $file or die "could not open ${file}: $!" 10 4 14
85 10 local $/ = ""; # paragraph slurp mode
86 10 my @recs = map eval($_), <$fh>;
87 10 close $fh;
88 10 \@recs;
89 };
90 }
91  
92 =pod
93  
94 =head2 get_failures_for_property(I<propname>)
95  
96 my $failures = $recorder->get_failures_for_property("property name");
97 for my $input_hashref (@$failures) {
98 # do something with hashref
99 while (my ($var, $value) = each %$input_hashref) {
100 # ...
101 }
102 }
103  
104 Returns a reference to an array that contains the recorded failures
105 for the property with the name I<propname>. In the event no
106 such failures exist, the array will be empty.
107 Each failure is represented by a hash containing the inputs that
108 caused the failure.
109  
110 If the recorder's storage file does not exist or cannot be
111 opened for reading, this method dies. Thus, you should call
112 it from within an C<eval> block.
113  
114 =cut
115  
116 40 ▶sub get_failures_for_property {
Subroutine get_failures_for_property: called CC=1 SCAR=0.0
Pod documented
117 40 my ($self, $property_name) = @_;
118 40 [ map $_->[1], grep { $_->[0] eq $property_name } @{$self->_store} ];
119 }
120  
121 =pod
122  
123 =head2 record_failure_for_property(I<propname>, I<input-hashref>)
124  
125 my $recorder->record_failure_for_property(
126 "property name",
127 $input_hashref_from_counterexample
128 );
129  
130 Adds a failure record for the property named I<propname>. The
131 record captures the counterexample represented by the I<input-hashref>.
132 The record is immediately appended to the recorder's storage file.
133  
134 Returns 1 upon success; dies otherwise.
135  
136 If the recorder's storage file cannot be opened for writing, this
137 method dies. Thus, you should call it from within an C<eval> block.
138  
139 =cut
140  
141 10 ▶sub record_failure_for_property {
Subroutine record_failure_for_property: called CC=3 SCAR=11.1
Pod documented
142 10 my ($self, $property_name, $input_hash) = @_;
143 10 my $file = $self->{file};
144 10 my $rec = [ $property_name, $input_hash ];
145 10 local $\ = "\n\n";
146 10 local $Data::Dumper::Indent = 0;
147 10 local $Data::Dumper::Purity = 1;
148 10 local $Data::Dumper::Terse = 1;
149 10 local $Data::Dumper::Deepcopy = 1;
150 10 local $Data::Dumper::Useqq = 1;
151 10 ▶ open my $fh, ">>$file" or die "could not open $file for appending: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">>$file" or die "could not open $file for appending: $!" 10 0 10
152 10 print $fh
153 '# ', scalar gmtime, "\n",
154 '# ', $self->_platform, "\n",
155 Dumper( $rec );
156 10 close $fh;
157 0 ▶ push @{$self->{cache}}, $rec if $self->{cache};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cache"} 0 10 10
158 10 1;
159 }
160  
161 10 ▶sub _platform {
Subroutine _platform: called CC=7 SCAR=22.6
162 10 ▶ shift->{platform} ||= do {
Condition66%2/3
+(shift())->{"platform"} ||= do { eval { do { local $_ = `$^X -v`; $_ and /^This is perl,(.*)/im and "perl$1" } } or sprintf "perl v%vd on %s", $^V, eval { do { require POSIX; if (my(@u) = POSIX::uname()) { return join($", @{[grep(defined($_), @u[0, 4, 2, 3])];}); } } } || $^O }
l!l&&r!l&&!r
910
MC/DC0%0/2
+(shift())->{"platform"} ||= do { eval { do { local $_ = `$^X -v`; $_ and /^This is perl,(.*)/im and "perl$1" } } or sprintf "perl v%vd on %s", $^V, eval { do { require POSIX; if (my(@u) = POSIX::uname()) { return join($", @{[grep(defined($_), @u[0, 4, 2, 3])];}); } } } || $^O }
+(shift())->{"platform"} do { eval { do { local $_ = `$^X -v`; $_ and /^This is perl,(.*)/im and "perl$1" } } or sprintf "perl v%vd on %s", $^V, eval { do { require POSIX; if (my(@u) = POSIX::uname()) { return join($", @{[grep(defined($_), @u[0, 4, 2, 3])];}); } } } || $^O }
Truth table66%2/3
A ||= B
A= +(shift())->{"platform"}B= do { eval { do { local $_ = `$^X -v`; $_ and /^This is perl,(.*)/im and "perl$1" } } or sprintf "perl v%vd on %s", $^V, eval { do { require POSIX; if (my(@u) = POSIX::uname()) { return join($", @{[grep(defined($_), @u[0, 4, 2, 3])];}); } } } || $^O }
ABresult
1X1
011
000
163 # first try to grab version line from `perl -v`
164 eval {
165 1 local $_ = `$^X -v`;
166 1 ▶ $_ && /^This is perl,(.*)/im && "perl$1";
Condition37%3/8
$_ && /^This is perl,(.*)/im && "perl$1"
!ll
10
$_ and /^This is perl,(.*)/im
!ll&&!rl&&r
010
eval { do { require POSIX; if (my(@u) = POSIX::uname()) { return join($", @{[grep(defined($_), @u[0, 4, 2, 3])];}); } } } || $^O
l!l&&r!l&&!r
100
MC/DC0%0/2
$_ && /^This is perl,(.*)/im && "perl$1"
$_ /^This is perl,(.*)/im
MC/DC0%0/2
eval { do { require POSIX; if (my(@u) = POSIX::uname()) { return join($", @{[grep(defined($_), @u[0, 4, 2, 3])];}); } } } || $^O
eval { do { require POSIX; if (my(@u) = POSIX::uname()) { return join($", @{[grep(defined($_), @u[0, 4, 2, 3])];}); } } } $^O
Truth table33%1/3
A and B
A= $_B= /^This is perl,(.*)/im
ABresult
0X0
100
111
Truth table33%1/3
A || B
A= eval { do { require POSIX; if (my(@u) = POSIX::uname()) { return join($", @{[grep(defined($_), @u[0, 4, 2, 3])];}); } } }B= $^O
ABresult
1X1
011
000
167 }
168 # if that fails, build our own version line
169 ||
170 sprintf("perl v%vd on %s", $^V,
171 # if uname works, get the platform info from it
172 1 ▶ eval {
Condition33%1/3
eval { do { local $_ = `$^X -v`; $_ and /^This is perl,(.*)/im and "perl$1" } } || sprintf('perl v%vd on %s', $^V, eval { do { require POSIX; if (my(@u) = POSIX::uname()) { return join($", @{[grep(defined($_), @u[0, 4, 2, 3])];}); } } } || $^O)
l!l&&r!l&&!r
010
MC/DC0%0/2
eval { do { local $_ = `$^X -v`; $_ and /^This is perl,(.*)/im and "perl$1" } } || sprintf('perl v%vd on %s', $^V, eval { do { require POSIX; if (my(@u) = POSIX::uname()) { return join($", @{[grep(defined($_), @u[0, 4, 2, 3])];}); } } } || $^O)
eval { do { local $_ = `$^X -v`; $_ and /^This is perl,(.*)/im and "perl$1" } } sprintf('perl v%vd on %s', $^V, eval { do { require POSIX; if (my(@u) = POSIX::uname()) { return join($", @{[grep(defined($_), @u[0, 4, 2, 3])];}); } } } || $^O)
Truth table33%1/3
A || B
A= eval { do { local $_ = `$^X -v`; $_ and /^This is perl,(.*)/im and "perl$1" } }B= sprintf('perl v%vd on %s', $^V, eval { do { require POSIX; if (my(@u) = POSIX::uname()) { return join($", @{[grep(defined($_), @u[0, 4, 2, 3])];}); } } } || $^O)
ABresult
1X1
011
000
173 1 require POSIX;
174 1 ▶ if (my @u = POSIX::uname()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@u) = POSIX::uname()) 1 0 1
175 1 return "@{[grep defined, @u[0,4,2,3]]}";
176 }
177 }
178 # otherwise, use the less informative Perl OS-name variable
179 ||
180 $^O
181 );
182 };
183 }
184  
185 25 1;
186  
187  
188  
189 =head1 SEE ALSO
190  
191 L<Test::LectroTest::TestRunner> explains the internal testing apparatus,
192 which uses the failure recorders to record and play back failures for
193 regression testing.
194  
195 =head1 AUTHOR
196  
197 Tom Moertel (tom@moertel.com)
198  
199 =head1 COPYRIGHT and LICENSE
200  
201 Copyright (c) 2004-13 by Thomas G Moertel. All rights reserved.
202  
203 This program is free software; you can redistribute it and/or
204 modify it under the same terms as Perl itself.
205  
206 =cut
« blib/lib/Test/LectroTest/Property.pm ↑ Summary blib/lib/Test/LectroTest/TestRunner.pm »