Moose/Connection/Mock.pm

filter: Statementstmt 91.7% 122 / 133 Branchbran 60.0% 18 / 30 Conditioncond 53.3% 8 / 15 MC/DCmcdc 30.0% 3 / 10 Subroutinesub 100% 13 / 13 Podpod 0.0% 0 / 2 totaltotal 80.7% 164 / 203 CC 21 SCAR 13.9
CC
21
Cov
83%
Clear
5.2
Query
5.2
Read
5.2
CRAP 4.0
« Moose/Plot.pm ↑ Summary Moose/Connection/Socket.pm »
1 package Lab::Moose::Connection::Mock;
2 5 $Lab::Moose::Connection::Mock::VERSION = '3.931';
3 #ABSTRACT: Mock connection, supplying instrument responses from a log file
4  
5 17 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 17 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 17 ▶use MooseX::Params::Validate;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 17 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 17 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 17 ▶use YAML::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 17 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15 5 has log_file => (
16 is => 'ro',
17 isa => 'Str',
18 predicate => 'has_log_file',
19 );
20  
21 5 has log_fh => (
22 is => 'ro',
23 isa => 'FileHandle',
24 builder => 'log_build_fh',
25 predicate => 'has_log_fh',
26 lazy => 1,
27 );
28  
29 5 has logs => (
30 is => 'ro',
31 isa => 'ArrayRef',
32 writer => '_logs',
33 init_arg => undef,
34 );
35  
36 5 has id => (
37 is => 'ro',
38 isa => 'Int',
39 writer => '_id',
40 init_arg => undef,
41 default => 0,
42 );
43  
44 13 ▶sub log_build_fh {
Subroutine log_build_fh: called CC=2 SCAR=7.0
Pod undocumented
45 13 my $self = shift;
46 13 my $file = $self->log_file();
47 13 ▶ open my $fh, '<', $file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or croak("cannot open logfile '${file}': $!") 13 0 13
48 or croak "cannot open logfile '$file': $!";
49 13 return $fh;
50 }
51  
52 13 ▶sub BUILD {
Subroutine BUILD: called CC=4 SCAR=14.8
Pod undocumented
53 13 my $self = shift;
54 13 ▶ if ( !( $self->has_log_file() || $self->has_log_fh() ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->has_log_file or $self->has_log_fh) 0 13 13
Condition33%1/3
$self->has_log_file or $self->has_log_fh
l!l&&r!l&&!r
1300
MC/DC0%0/2
$self->has_log_file or $self->has_log_fh
$self->has_log_file $self->has_log_fh
Truth table33%1/3
A or B
A= $self->has_log_fileB= $self->has_log_fh
ABresult
1X1
011
000
55 0 croak "no log_file in Mock connection";
56 }
57  
58 13 my $fh = $self->log_fh();
59 13 my $yaml = do { local $/; <$fh> };
60 13 my @logs = Load($yaml);
61 13 $self->_logs( \@logs );
62 13 ▶ close $fh
Branch: 1 branch
BranchTrueFalseTotal
close $fh or croak("cannot close log_fh: $!") 13 0 13
63 or croak "cannot close log_fh: $!";
64 }
65  
66 5 my $meta = __PACKAGE__->meta();
67  
68 5 for my $method (qw/Read Write Query Clear/) {
69 $meta->add_method(
70 93 ▶ $method => sub {
Subroutine Clear: called CC=5 SCAR=16.5
Subroutine Write: called CC=5 SCAR=16.5
Subroutine Read: called CC=5 SCAR=16.5
Subroutine Query: called CC=5 SCAR=16.5
71 93 my $self = shift;
72 93 my @params = @_;
73 93 my %arg;
74 93 ▶ if ( ref $params[0] eq 'HASH' ) {
Branch: 4 branches
BranchTrueFalseTotal
if (ref $params[0] eq 'HASH') { } 0 93 93
if (ref $params[0] eq 'HASH') { } 0 263 263
if (ref $params[0] eq 'HASH') { } 0 82 82
if (ref $params[0] eq 'HASH') { } 0 104 104
75 0 %arg = %{ $params[0] };
76 }
77 else {
78 93 %arg = @params;
79 }
80 0 $arg{method} = $method;
81 93 my $id = $self->id();
82 0 $arg{id} = $id;
83 93 $self->_id( ++$id );
84 93 my $log = shift @{ $self->logs };
85  
86 93 my $retval = delete $log->{retval};
87 93 my $retval_enc = delete $log->{retval_enc};
88  
89 # Compare:
90 0 my $arg_yaml = Dump( \%arg );
91 0 my $log_yaml = Dump($log);
92 93 ▶ if ( $arg_yaml ne $log_yaml ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($arg_yaml ne $log_yaml) 0 93 93
if ($arg_yaml ne $log_yaml) 0 263 263
if ($arg_yaml ne $log_yaml) 0 82 82
if ($arg_yaml ne $log_yaml) 0 104 104
93 0 croak <<"EOF";
94 mismatch in Mock Connection:
95 logged:
96 $log_yaml
97 received:
98 $arg_yaml
99 EOF
100 }
101 93 ▶ if ( defined $retval_enc && $retval_enc eq 'hex' ) {
Branch: 4 branches
BranchTrueFalseTotal
if (defined $retval_enc and $retval_enc eq "hex") 3 90 93
if (defined $retval_enc and $retval_enc eq "hex") 3 260 263
if (defined $retval_enc and $retval_enc eq "hex") 0 82 82
if (defined $retval_enc and $retval_enc eq "hex") 3 101 104
Condition58%7/12
defined $retval_enc and $retval_enc eq "hex"
!ll&&!rl&&r
9003
defined $retval_enc and $retval_enc eq "hex"
!ll&&!rl&&r
26003
defined $retval_enc and $retval_enc eq "hex"
!ll&&!rl&&r
8200
defined $retval_enc and $retval_enc eq "hex"
!ll&&!rl&&r
10103
MC/DC50%1/2
defined $retval_enc and $retval_enc eq "hex"
defined $retval_enc $retval_enc eq "hex"
MC/DC50%1/2
defined $retval_enc and $retval_enc eq "hex"
defined $retval_enc $retval_enc eq "hex"
MC/DC0%0/2
defined $retval_enc and $retval_enc eq "hex"
defined $retval_enc $retval_enc eq "hex"
MC/DC50%1/2
defined $retval_enc and $retval_enc eq "hex"
defined $retval_enc $retval_enc eq "hex"
Truth table66%2/3
A and B
A= defined $retval_encB= $retval_enc eq "hex"
ABresult
0X0
100
111
Truth table66%2/3
A and B
A= defined $retval_encB= $retval_enc eq "hex"
ABresult
0X0
100
111
Truth table33%1/3
A and B
A= defined $retval_encB= $retval_enc eq "hex"
ABresult
0X0
100
111
Truth table66%2/3
A and B
A= defined $retval_encB= $retval_enc eq "hex"
ABresult
0X0
100
111
102 3 $retval = pack( 'H*', $retval );
103 }
104 93 return $retval;
105 }
106 20 );
107 }
108  
109 5 with 'Lab::Moose::Connection';
110  
111 5 $meta->make_immutable();
112 5 1;
113  
114 __END__
« Moose/Plot.pm ↑ Summary Moose/Connection/Socket.pm »