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.25";
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 43 ▶sub import {
Subroutine import: called CC=5 SCAR=17.0
14 43 my $class = shift;
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 159 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
33 159 my $class = shift;
34 159 my %args = @_;
35  
36 my $this = {
37 INPUT => create_stream($args{'input'}),
38 OUTPUT => create_stream($args{'output'}),
39 OPERATION => $args{'operation'},
40 159 KEEPER => $args{'keeper'},
41 };
42  
43 159 bless $this, $class;
44  
45 159 return $this;
46 }
47  
48 318 ▶sub create_stream {
Subroutine create_stream: called CC=6 SCAR=19.0
Pod undocumented
49 318 my $input = shift;
50  
51 318 ▶ return undef unless ( $input );
Branch: 1 branch
BranchTrueFalseTotal
unless $input 62 256 318
52 256 ▶ return $input if ( ref($input) eq 'ARRAY' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $input eq "ARRAY" 21 235 256
53  
54 235 ▶ if ( UNIVERSAL::isa($input, 'App::RecordStream::InputStream') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($input, "App::RecordStream::InputStream")) 0 235 235
55 0 return $input;
56 }
57  
58 235 ▶ if ( (not ($input =~ m/\n/m)) && -e $input ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $input =~ /\n/m and -e $input) 0 235 235
Condition66%2/3
not $input =~ /\n/m and -e $input
!ll&&!rl&&r
23140
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 235 return App::RecordStream::InputStream->new(STRING => $input);
63 }
64  
65 158 ▶sub matches {
Subroutine matches: called CC=24 SCAR=33.6
Pod undocumented
66 158 my $this = shift;
67 158 ▶ my $name = shift || 'unnamed';
Condition50%1/2
shift() || 'unnamed'
l!l
0158
MC/DC0%0/1
shift() || 'unnamed'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
68  
69 158 my $op = $this->{'OPERATION'};
70 158 my $input = $this->{'INPUT'};
71  
72 158 ▶ if ( $op->wants_input() && $input ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op->wants_input and $input) 139 19 158
Condition100%3/3
$op->wants_input and $input
!ll&&!rl&&r
163139
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 139 ▶ if ( ref($input) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $input eq 'ARRAY') { } 21 118 139
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 118 App::RecordStream::Operation::set_current_filename($input->get_filename());
98 118 ▶ while ( my $r = $input->get_record() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = $input->get_record) 792 112 904
99 792 ▶ if ( ! $op->accept_record($r) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($op->accept_record($r)) 4 786 790
100 4 last;
101 }
102 }
103 }
104 }
105 156 $op->finish();
106  
107 154 my $output = $this->{'OUTPUT'};
108 154 my $results = $this->{'KEEPER'}->get_records();
109 154 my $i = 0;
110  
111 #ok(0, "DIE");
112 154 my @output_records;
113 154 ▶ if ( $output ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output) 117 37 154
114 117 ▶ while ( my $record = $output->get_record() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $record = $output->get_record) 423 117 540
115 423 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 154 my ($level_to_testfile, $file) = (0, (caller(0))[1]);
122 154 ▶ while (defined $file and $file !~ /\.t$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file and not $file =~ /\.t$/) 185 154 339
Condition66%2/3
defined $file and not $file =~ /\.t$/
!ll&&!rl&&r
0154185
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 185 $level_to_testfile++;
124 185 $file = (caller($level_to_testfile))[1];
125 }
126  
127 154 local $Test::Builder::Level = $Test::Builder::Level + $level_to_testfile + 1;
128  
129 154 my $is_ok = 1;
130 154 for my $record (@$results) {
131 423 ▶ $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 423 423
132 }
133  
134 154 ▶ $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 154 154
135  
136 154 ▶ $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 154 154
137  
138 154 ▶ if ( ! $is_ok ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_ok) 0 154 154
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 154 return $is_ok;
150 }
151  
152 121 ▶sub do_match {
Subroutine do_match: called CC=4 SCAR=13.9
Pod undocumented
153 121 my $class = shift;
154 121 my $operation_name = shift;
155 121 my $args = shift;
156 121 my $input = shift;
157 121 my $output = shift;
158  
159 121 my $operation_class = "App::RecordStream::Operation::$operation_name";
160 121 my $keeper = App::RecordStream::Test::OperationHelper::Keeper->new();
161 121 my $op = $operation_class->new($args, $keeper);
162  
163 121 ▶ if ( $op->wants_input() && @$args ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op->wants_input and @$args) 5 116 121
Condition100%3/3
$op->wants_input and @$args
!ll&&!rl&&r
161005
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 121 ok($op, "Operation initialization");
173  
174 121 my $helper = $class->new(
175 operation => $op,
176 keeper => $keeper,
177 input => $input,
178 output => $output,
179 );
180  
181 121 $helper->matches();
182  
183 117 return $helper;
184 }
185  
186 37 ▶sub test_output {
Subroutine test_output: called CC=1 SCAR=0.0
Pod undocumented
187 37 my $class = shift;
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 159 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
217 159 my $class = shift;
218 159 my $this = { RECORDS => [], LINES => [] };
219 159 bless $this, $class;
220 159 return $this;
221 }
222  
223 426 ▶sub accept_record {
Subroutine accept_record: called CC=1 SCAR=0.0
224 426 my $this = shift;
225 426 my $record = shift;
226  
227 426 push @{$this->{'RECORDS'}}, $record;
228  
229 426 return 1;
230 }
231  
232 154 ▶sub get_records {
Subroutine get_records: called CC=1 SCAR=0.0
233 154 my $this = shift;
234 154 return $this->{'RECORDS'};
235 }
236  
237 458 ▶sub accept_line {
Subroutine accept_line: called CC=1 SCAR=0.0
238 458 my $this = shift;
239 458 my $line = shift;
240  
241 458 push @{$this->{'LINES'}}, $line;
242  
243 458 return 1;
244 }
245  
246 38 ▶sub get_lines {
Subroutine get_lines: called CC=1 SCAR=0.0
247 38 my $this = shift;
248 38 return $this->{'LINES'};
249 }
250  
251 154 ▶sub has_called_finish {
Subroutine has_called_finish: called CC=1 SCAR=0.0
252 154 my $this = shift;
253 154 return $this->{'CALLED_FINISH'};
254 }
255  
256 155 ▶sub finish {
Subroutine finish: called CC=1 SCAR=0.0
257 155 my $this = shift;
258 155 $this->{'CALLED_FINISH'} = 1;
259 }
260  
261 43 1;
« blib/lib/App/RecordStream/Operation/fromtcpdump.pm ↑ Summary blib/lib/App/RecordStream/Operation/tognuplot.pm »