blib/lib/App/RecordStream/Test/OperationHelper.pm

filter: Statementstmt 91.0% 132 / 145 Branchbran 74.0% 37 / 50 Conditioncond 78.5% 11 / 14 MC/DCmcdc 55.5% 5 / 9 Subroutinesub 100% 20 / 20 Podpod 0.0% 0 / 5 totaltotal 84.3% 205 / 243 CC 36 SCAR 26.7
CC
36
Cov
86%
matches
28.9
create_stream
6.7
import
5.5
CRAP 14.4
« blib/lib/App/RecordStream/Operation/fromtcpdump.pm ↑ Summary blib/lib/App/RecordStream/Operation/tognuplot.pm »
1 package App::RecordStream::Test::OperationHelper;
2  
3 43 our $VERSION = "4.0.23";
4  
5 43 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 43 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 43 ▶use Test::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 43 ▶use App::RecordStream::InputStream;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 43 ▶use App::RecordStream::OutputStream;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 43 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 sub import {
14 43 ▶ my $class = shift;
Subroutine import: called CC=5 SCAR=17.0
15  
16 43 require App::RecordStream::OptionalRequire;
17 43 local $App::RecordStream::OptionalRequire::PRINT_WARNING = 0;
18  
19 43 for my $op (@_) {
20 4 ▶ croak "invalid package name: '$op'"
Branch: 1 branch
BranchTrueFalseTotal
unless $op =~ /^[a-z0-9_]+$/ 0 4 4
21 unless $op =~ /^[a-z0-9_]+$/;
22 4 ▶ if (not eval "require App::RecordStream::Operation::$op; 1;") {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval "require App::RecordStream::Operation::$op; 1;") 4 0 4
23 4 ▶ if ($@ =~ /Please install missing modules/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ =~ /Please install missing modules/) { } 4 0 4
24 4 plan skip_all => "Missing deps for operation $op";
25 } else {
26 0 die $@;
27 }
28 }
29 }
30 }
31  
32 sub new {
33 157 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
34 157 my %args = @_;
35  
36 my $this = {
37 INPUT => create_stream($args{'input'}),
38 OUTPUT => create_stream($args{'output'}),
39 OPERATION => $args{'operation'},
40 157 KEEPER => $args{'keeper'},
41 };
42  
43 157 bless $this, $class;
44  
45 157 return $this;
46 }
47  
48 sub create_stream {
49 314 ▶ my $input = shift;
Subroutine create_stream: called CC=6 SCAR=19.0
Pod undocumented
50  
51 314 ▶ return undef unless ( $input );
Branch: 1 branch
BranchTrueFalseTotal
unless $input 62 252 314
52 252 ▶ return $input if ( ref($input) eq 'ARRAY' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $input eq "ARRAY" 21 231 252
53  
54 231 ▶ if ( UNIVERSAL::isa($input, 'App::RecordStream::InputStream') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($input, "App::RecordStream::InputStream")) 0 231 231
55 0 return $input;
56 }
57  
58 231 ▶ if ( (not ($input =~ m/\n/m)) && -e $input ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $input =~ /\n/m and -e $input) 0 231 231
Condition66%2/3
not $input =~ /\n/m and -e $input
!ll&&!rl&&r
22740
MC/DC0%0/2
not $input =~ /\n/m and -e $input
not $input =~ /\n/m -e $input
Truth table66%2/3
not(A) and B
A= not $input =~ /\n/mB= -e $input
ABresult
0X0
100
111
59 0 return App::RecordStream::InputStream->new(FILE => $input);
60 }
61  
62 231 return App::RecordStream::InputStream->new(STRING => $input);
63 }
64  
65 sub matches {
66 156 ▶ my $this = shift;
Subroutine matches: called CC=24 SCAR=33.6
Pod undocumented
67 156 ▶ my $name = shift || 'unnamed';
Condition50%1/2
shift() || 'unnamed'
l!l
0156
MC/DC0%0/1
shift() || 'unnamed'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
68  
69 156 my $op = $this->{'OPERATION'};
70 156 my $input = $this->{'INPUT'};
71  
72 156 ▶ if ( $op->wants_input() && $input ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op->wants_input and $input) 137 19 156
Condition100%3/3
$op->wants_input and $input
!ll&&!rl&&r
163137
MC/DC100%2/2
$op->wants_input and $input
$op->wants_input $input
Truth table100%3/3
A and B
A= $op->wants_inputB= $input
ABresult
0X0
100
111
73 137 ▶ if ( ref($input) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $input eq 'ARRAY') { } 21 116 137
74 21 my ($t, @v) = @$input;
75 21 ▶ if ( $t eq 'LINES' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($t eq 'LINES') { } 16 5 21
elsif ($t eq 'FILES') { } 5 0 5
76 16 for my $l (@v) {
77 64 ▶ if ( ! $op->accept_line($l) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($op->accept_line($l)) 0 64 64
78 0 last;
79 }
80 }
81 }
82 elsif ( $t eq 'FILES' ) {
83 5 local @ARGV = @v;
84 5 ▶ while(my $l = <>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $l = readline ARGV)) 22 5 27
85 22 App::RecordStream::Operation::set_current_filename($ARGV);
86 22 chomp $l;
87 22 ▶ if ( ! $op->accept_line($l) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($op->accept_line($l)) 0 22 22
88 0 last;
89 }
90 }
91 }
92 else {
93 0 die;
94 }
95 }
96 else {
97 116 App::RecordStream::Operation::set_current_filename($input->get_filename());
98 116 ▶ while ( my $r = $input->get_record() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = $input->get_record) 782 109 891
99 782 ▶ if ( ! $op->accept_record($r) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($op->accept_record($r)) 4 776 780
100 4 last;
101 }
102 }
103 }
104 }
105 153 $op->finish();
106  
107 151 my $output = $this->{'OUTPUT'};
108 151 my $results = $this->{'KEEPER'}->get_records();
109 151 my $i = 0;
110  
111 #ok(0, "DIE");
112 151 my @output_records;
113 151 ▶ if ( $output ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output) 114 37 151
114 114 ▶ while ( my $record = $output->get_record() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $record = $output->get_record) 418 114 532
115 418 push @output_records, $record;
116 }
117 }
118  
119 # Find the call level of the originating test file for better diagnostic
120 # reporting if we fail tests below
121 151 my ($level_to_testfile, $file) = (0, (caller(0))[1]);
122 151 ▶ while (defined $file and $file !~ /\.t$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file and not $file =~ /\.t$/) 182 151 333
Condition66%2/3
defined $file and not $file =~ /\.t$/
!ll&&!rl&&r
0151182
MC/DC50%1/2
defined $file and not $file =~ /\.t$/
defined $file not $file =~ /\.t$/
Truth table66%2/3
A and not(B)
A= defined $fileB= not $file =~ /\.t$/
ABresult
0X0
100
111
123 182 $level_to_testfile++;
124 182 $file = (caller($level_to_testfile))[1];
125 }
126  
127 151 local $Test::Builder::Level = $Test::Builder::Level + $level_to_testfile + 1;
128  
129 151 my $is_ok = 1;
130 151 for my $record (@$results) {
131 418 ▶ $is_ok = 0 if ( ! ok(UNIVERSAL::isa($record, 'App::RecordStream::Record'), "Record is a App::RecordStream::Record") );
Branch: 1 branch
BranchTrueFalseTotal
unless &ok(scalar UNIVERSAL::isa($record, "App::RecordStream::Record"), "Record is a App::RecordStream::Record") 0 418 418
132 }
133  
134 151 ▶ $is_ok = 0 if ( ! is_deeply($results, \@output_records, "Records match: $name") );
Branch: 1 branch
BranchTrueFalseTotal
unless is_deeply($results, \@output_records, "Records match: $name") 0 151 151
135  
136 151 ▶ $is_ok = 0 if ( ! ok($this->{'KEEPER'}->has_called_finish(), "Has called finish: $name") );
Branch: 1 branch
BranchTrueFalseTotal
unless &ok(scalar $this->{"KEEPER"}->has_called_finish, "Has called finish: $name") 0 151 151
137  
138 151 ▶ if ( ! $is_ok ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_ok) 0 151 151
139 0 warn "Expected and output differed!\nExpected:\n";
140 0 for my $record (@output_records) {
141 0 print STDERR App::RecordStream::OutputStream::hashref_string($record) . "\n";
142 }
143 0 warn "Output from module:\n";
144 0 for my $record (@$results) {
145 0 print STDERR App::RecordStream::OutputStream::hashref_string($record) . "\n";
146 }
147 }
148  
149 151 return $is_ok;
150 }
151  
152 sub do_match {
153 119 ▶ my $class = shift;
Subroutine do_match: called CC=4 SCAR=13.9
Pod undocumented
154 119 my $operation_name = shift;
155 119 my $args = shift;
156 119 my $input = shift;
157 119 my $output = shift;
158  
159 119 my $operation_class = "App::RecordStream::Operation::$operation_name";
160 119 my $keeper = App::RecordStream::Test::OperationHelper::Keeper->new();
161 119 my $op = $operation_class->new($args, $keeper);
162  
163 119 ▶ if ( $op->wants_input() && @$args ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op->wants_input and @$args) 5 114 119
Condition100%3/3
$op->wants_input and @$args
!ll&&!rl&&r
16985
MC/DC100%2/2
$op->wants_input and @$args
$op->wants_input @$args
Truth table100%3/3
A and B
A= $op->wants_inputB= @$args
ABresult
0X0
100
111
164 5 ▶ if ( $input ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input) { } 0 5 5
165 0 fail("Both extra args [" . join(", ", @$args) . "] and input provided?");
166 }
167 else {
168 5 $input = ['FILES', @$args];
169 }
170 }
171  
172 119 ok($op, "Operation initialization");
173  
174 119 my $helper = $class->new(
175 operation => $op,
176 keeper => $keeper,
177 input => $input,
178 output => $output,
179 );
180  
181 119 $helper->matches();
182  
183 114 return $helper;
184 }
185  
186 sub test_output {
187 37 ▶ my $class = shift;
Subroutine test_output: called CC=1 SCAR=0.0
Pod undocumented
188 37 my $operation_name = shift;
189 37 my $args = shift;
190 37 my $input = shift;
191 37 my $output = shift;
192  
193 37 my $operation_class = "App::RecordStream::Operation::$operation_name";
194 37 my $keeper = App::RecordStream::Test::OperationHelper::Keeper->new();
195 37 my $op = $operation_class->new($args, $keeper);
196  
197 37 ok($op, "Object initialization");
198  
199 37 my $helper = __PACKAGE__->new(
200 operation => $op,
201 keeper => $keeper,
202 input => $input,
203 output => '',
204 );
205  
206 37 $helper->matches();
207  
208 37 is(join ('', map { "$_\n" } @{$keeper->get_lines()}), $output, "Output matches expected");
209 }
210  
211  
212 package App::RecordStream::Test::OperationHelper::Keeper;
213  
214 43 ▶use base qw(App::RecordStream::Stream::Base);
Subroutine BEGIN: called CC=1 SCAR=0.0
215  
216 sub new {
217 157 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
218 157 my $this = { RECORDS => [], LINES => [] };
219 157 bless $this, $class;
220 157 return $this;
221 }
222  
223 sub accept_record {
224 421 ▶ my $this = shift;
Subroutine accept_record: called CC=1 SCAR=0.0
225 421 my $record = shift;
226  
227 421 push @{$this->{'RECORDS'}}, $record;
228  
229 421 return 1;
230 }
231  
232 sub get_records {
233 151 ▶ my $this = shift;
Subroutine get_records: called CC=1 SCAR=0.0
234 151 return $this->{'RECORDS'};
235 }
236  
237 sub accept_line {
238 458 ▶ my $this = shift;
Subroutine accept_line: called CC=1 SCAR=0.0
239 458 my $line = shift;
240  
241 458 push @{$this->{'LINES'}}, $line;
242  
243 458 return 1;
244 }
245  
246 sub get_lines {
247 38 ▶ my $this = shift;
Subroutine get_lines: called CC=1 SCAR=0.0
248 38 return $this->{'LINES'};
249 }
250  
251 sub has_called_finish {
252 151 ▶ my $this = shift;
Subroutine has_called_finish: called CC=1 SCAR=0.0
253 151 return $this->{'CALLED_FINISH'};
254 }
255  
256 sub finish {
257 152 ▶ my $this = shift;
Subroutine finish: called CC=1 SCAR=0.0
258 152 $this->{'CALLED_FINISH'} = 1;
259 }
260  
261 43 1;
« blib/lib/App/RecordStream/Operation/fromtcpdump.pm ↑ Summary blib/lib/App/RecordStream/Operation/tognuplot.pm »