Moose/Connection/Mock.pm

filter: Statementstmt 78.7% 122 / 155 Branchbran 46.6% 14 / 30 Conditioncond 40.0% 6 / 15 MC/DCmcdc 20.0% 2 / 10 Subroutinesub 92.3% 12 / 13 Podpod 0.0% 0 / 2 totaltotal 69.3% 156 / 225 SCAR 36.9
CC
25
Cov
71%
Clear
6.1
Query
6.1
Read
6.1
CRAP 40.2
« Connection/USBtmc.pm ↑ Summary Measurement/KeyboardHandling.pm »
1 package Lab::Moose::Connection::Mock;
2 1 $Lab::Moose::Connection::Mock::VERSION = '3.881';
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 1 has log_file => (
16 is => 'ro',
17 isa => 'Str',
18 predicate => 'has_log_file',
19 );
20  
21 1 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 1 has logs => (
30 is => 'ro',
31 isa => 'ArrayRef',
32 writer => '_logs',
33 init_arg => undef,
34 );
35  
36 1 has id => (
37 is => 'ro',
38 isa => 'Int',
39 writer => '_id',
40 init_arg => undef,
41 default => 0,
42 );
43  
44 sub log_build_fh {
45 13 ▶ my $self = shift;
Subroutine log_build_fh: called CC=2 SCAR=7.0
Pod undocumented
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}': $!") 0 13 13
48 or croak "cannot open logfile '$file': $!";
49 13 return $fh;
50 }
51  
52 sub BUILD {
53 13 ▶ my $self = shift;
Subroutine BUILD: called CC=4 SCAR=14.8
Pod undocumented
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: $!") 0 13 13
63 or croak "cannot close log_fh: $!";
64 }
65  
66 1 my $meta = __PACKAGE__->meta();
67  
68 1 for my $method (qw/Read Write Query Clear/) {
69 $meta->add_method(
70 $method => sub {
71 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=5 SCAR=18.1
Subroutine Read: called CC=5 SCAR=18.1
Subroutine Write: called CC=5 SCAR=18.1
Subroutine Clear: called CC=5 SCAR=18.1
72 0 my @params = @_;
73 0 my %arg;
74 0 ▶ if ( ref $params[0] eq 'HASH' ) {
Branch: 4 branches
BranchTrueFalseTotal
if (ref $params[0] eq 'HASH') { } 0 0 0
if (ref $params[0] eq 'HASH') { } 0 270 270
if (ref $params[0] eq 'HASH') { } 0 148 148
if (ref $params[0] eq 'HASH') { } 0 124 124
75 0 %arg = %{ $params[0] };
76 }
77 else {
78 0 %arg = @params;
79 }
80 0 $arg{method} = $method;
81 0 my $id = $self->id();
82 0 $arg{id} = $id;
83 0 $self->_id( ++$id );
84 0 my $log = shift @{ $self->logs };
85  
86 0 my $retval = delete $log->{retval};
87 0 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 0 ▶ if ( $arg_yaml ne $log_yaml ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($arg_yaml ne $log_yaml) 0 0 0
if ($arg_yaml ne $log_yaml) 0 270 270
if ($arg_yaml ne $log_yaml) 0 148 148
if ($arg_yaml ne $log_yaml) 0 124 124
93 0 croak <<"EOF";
94 mismatch in Mock Connection:
95 logged:
96 $log_yaml
97 received:
98 $arg_yaml
99 EOF
100 }
101 0 ▶ if ( defined $retval_enc && $retval_enc eq 'hex' ) {
Branch: 4 branches
BranchTrueFalseTotal
if (defined $retval_enc and $retval_enc eq "hex") 0 0 0
if (defined $retval_enc and $retval_enc eq "hex") 0 270 270
if (defined $retval_enc and $retval_enc eq "hex") 3 145 148
if (defined $retval_enc and $retval_enc eq "hex") 6 118 124
Condition41%5/12
defined $retval_enc and $retval_enc eq "hex"
!ll&&!rl&&r
000
defined $retval_enc and $retval_enc eq "hex"
!ll&&!rl&&r
27000
defined $retval_enc and $retval_enc eq "hex"
!ll&&!rl&&r
14503
defined $retval_enc and $retval_enc eq "hex"
!ll&&!rl&&r
11806
MC/DC0%0/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"
MC/DC50%1/2
defined $retval_enc and $retval_enc eq "hex"
defined $retval_enc $retval_enc eq "hex"
Truth table0%0/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
Truth table66%2/3
A and B
A= defined $retval_encB= $retval_enc eq "hex"
ABresult
0X0
100
111
102 0 $retval = pack( 'H*', $retval );
103 }
104 0 return $retval;
105 }
106 4 );
107 }
108  
109 1 with 'Lab::Moose::Connection';
110  
111 1 $meta->make_immutable();
112 1 1;
113  
114 __END__
115  
116 =pod
117  
118 =encoding UTF-8
119  
120 =head1 NAME
121  
122 Lab::Moose::Connection::Mock - Mock connection, supplying instrument responses from a log file
123  
124 =head1 VERSION
125  
126 version 3.881
127  
128 =head1 SYNOPSIS
129  
130 use Lab::Moose;
131  
132 my $instrument = instrument(
133 type => 'some_instrument',
134 connection_type => 'Mock',
135 connection_options => { log_file => 'log.yml' }, # or log_fh => $fh,
136 );
137  
138 =head1 DESCRIPTION
139  
140 Mock connection object for unit testing. Uses a log recorded previously
141 with a real instrument using L<Lab::Moose::Instrument::Log>.
142  
143 =head1 COPYRIGHT AND LICENSE
144  
145 This software is copyright (c) 2023 by the Lab::Measurement team; in detail:
146  
147 Copyright 2016 Simon Reinhardt
148 2017 Andreas K. Huettel, Simon Reinhardt
149 2020 Andreas K. Huettel
150  
151  
152 This is free software; you can redistribute it and/or modify it under
153 the same terms as the Perl 5 programming language system itself.
154  
155 =cut
« Connection/USBtmc.pm ↑ Summary Measurement/KeyboardHandling.pm »