File Coverage

blib/lib/Lab/Connection/Mock.pm
Criterion Covered Total %
statement 55 60 91.6
branch 7 12 58.3
condition 2 3 66.6
subroutine 13 13 100.0
pod 0 2 0.0
total 77 90 85.5


line stmt bran cond sub pod time code
1             package Lab::Connection::Mock;
2             #ABSTRACT: ???
3             $Lab::Connection::Mock::VERSION = '3.881';
4 6     6   3292 use v5.20;
  6         36  
5              
6 6     6   42 use warnings;
  6         20  
  6         187  
7 6     6   46 use strict;
  6         18  
  6         178  
8              
9 6     6   3435 use Class::Method::Modifiers;
  6         10613  
  6         405  
10 6     6   2122 use YAML::XS qw/Dump LoadFile/;
  6         14802  
  6         362  
11 6     6   48 use Data::Dumper;
  6         14  
  6         287  
12 6     6   2669 use autodie;
  6         70779  
  6         41  
13 6     6   41267 use Carp;
  6         16  
  6         425  
14              
15 6     6   2953 use Lab::Connection::LogMethodCall qw/dump_method_call/;
  6         19  
  6         332  
16 6     6   47 use parent 'Lab::Connection';
  6         15  
  6         40  
17              
18              
19             our %fields = (
20             logfile => undef,
21             log_index => 0,
22             log_list => undef,
23             );
24              
25             around 'new' => sub {
26             my $orig = shift;
27             my $proto = shift;
28             my $class = ref($proto) || $proto;
29             my $twin = undef;
30              
31             # getting fields and _permitted from parent class
32             my $self = $class->$orig(@_);
33              
34             $self->_construct($class);
35              
36             # Open the log file.
37             my $logfile = $self->logfile();
38             if ( not defined $logfile ) {
39             croak 'missing "logfile" parameter in connection';
40             }
41              
42             my @logs = LoadFile($logfile);
43             $self->log_list( [@logs] );
44              
45             return $self;
46             };
47              
48             # If all values are scalars, we don't need stuff like Data::Compare.
49             sub compare_hashs {
50 664     664 0 936 my $a = shift;
51 664         885 my $b = shift;
52              
53 664         856 my @keys_a = keys %{$a};
  664         1944  
54 664         984 my @keys_b = keys %{$b};
  664         1505  
55              
56 664         1025 my $len_a = @keys_a;
57 664         914 my $len_b = @keys_b;
58              
59             # compare size
60 664 50       1371 if ( $len_a != $len_b ) {
61 0         0 return 1;
62             }
63 664         1149 for my $key (@keys_a) {
64 1676 50       2872 if ( ref $a->{$key} ) {
65 0         0 die "expected scalar";
66             }
67 1676 50       2829 if ( not exists $b->{$key} ) {
68 0         0 return 1;
69             }
70 1676 50       3841 if ( $a->{$key} ne $b->{$key} ) {
71 0         0 return 1;
72             }
73             }
74 664         1680 return 0;
75             }
76              
77             sub process_call {
78 672     672 0 1037 my $method = shift;
79 672         904 my $self = shift;
80              
81 672         2995 my $index = $self->log_index();
82              
83             # Hack: $self->timeout is called early in Lab::Connection::configure.
84 672 100 66     2566 if ( not defined $self->log_list() and $method eq 'timeout' ) {
85 8         34 return $self->{config}->{timeout};
86             }
87              
88 664         1905 my $received = dump_method_call( $index, $method, @_ );
89              
90 664         2590 my $expected = $self->log_list()->[$index];
91              
92 664         1676 my $retval = delete $expected->{retval};
93              
94 664 50       1284 if ( compare_hashs( $received, $expected ) ) {
95 0         0 croak "Mock connection:\nreceived:\n", Dump($received),
96             "\nexpected:\n", Dump($expected);
97             }
98              
99 664         3056 $self->log_index( ++$index );
100 664         2913 return $retval;
101             }
102              
103             for my $method (
104             qw/Clear Write Read Query BrutalRead LongQuery BrutalQuery timeout
105             block_connection unblock_connection is_blocked/
106             ) {
107             around $method => sub {
108             my $orig = shift;
109             return process_call( $method, @_ );
110             };
111             }
112              
113             sub _setbus {
114              
115             # No bus for this connection, so do nothing.
116 5     5   25 return;
117             }
118             1;
119              
120             __END__
121              
122             =pod
123              
124             =encoding UTF-8
125              
126             =head1 NAME
127              
128             Lab::Connection::Mock - ???
129              
130             =head1 VERSION
131              
132             version 3.881
133              
134             =head1 COPYRIGHT AND LICENSE
135              
136             This software is copyright (c) 2023 by the Lab::Measurement team; in detail:
137              
138             Copyright 2016 Simon Reinhardt
139             2017 Andreas K. Huettel
140             2020 Andreas K. Huettel
141              
142              
143             This is free software; you can redistribute it and/or modify it under
144             the same terms as the Perl 5 programming language system itself.
145              
146             =cut