Modbus/Unit/Route.pm

filter: Statementstmt 100% 48 / 48 Branchbran 100% 18 / 18 Conditioncond 100% 9 / 9 MC/DCmcdc 100% 6 / 6 Subroutinesub 100% 11 / 11 Podpod 0.0% 0 / 4 totaltotal 95.8% 92 / 96 CC 17 SCAR 15.3
CC
17
Cov
100%
new
9.0
_build_tests
4.0
test_route
3.0
CRAP 4.6
« Modbus/Exception.pm ↑ Summary Modbus/Unit.pm »
1 package Device::Modbus::Unit::Route;
2  
3 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 42 ▶sub new {
Subroutine new: called CC=9 SCAR=22.0
Pod undocumented
8 42 my ($class, %args) = @_;
9
10 42 my %valid_zone = (
11 discrete_coils => 'rw',
12 discrete_inputs => 'ro',
13 input_registers => 'ro',
14 holding_registers => 'rw',
15 );
16  
17 42 foreach my $field (qw/address quantity zone read_write routine/) {
18 croak "Missing required arguments: $field"
19 200 ▶ unless exists $args{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{$field} 5 195 200
20 }
21 croak "Invalid Modbus model type: zone '$args{zone}' does not exist"
22 37 ▶ unless exists $valid_zone{$args{zone}};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $valid_zone{$args{"zone"}} 1 36 37
23 croak "Modbus zone '$args{zone}' is read-only"
24 36 ▶ if $args{read_write} eq 'write' && $valid_zone{$args{zone}} eq 'ro';
Branch: 1 branch
BranchTrueFalseTotal
if $args{"read_write"} eq "write" and $valid_zone{$args{"zone"}} eq "ro" 2 34 36
Condition100%3/3
$args{"read_write"} eq "write" and $valid_zone{$args{"zone"}} eq "ro"
!ll&&!rl&&r
2682
MC/DC100%2/2
$args{"read_write"} eq "write" and $valid_zone{$args{"zone"}} eq "ro"
$args{"read_write"} eq "write" $valid_zone{$args{"zone"}} eq "ro"
Truth table100%3/3
A and B
A= $args{"read_write"} eq "write"B= $valid_zone{$args{"zone"}} eq "ro"
ABresult
0X0
100
111
25 croak "Parameter read_write must be either 'read' or 'write'"
26 34 ▶ unless $args{read_write} =~ /^read|write$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"read_write"} =~ /^read|write$/ 1 33 34
27 croak "The routine for an address must be a code reference"
28 33 ▶ unless ref $args{routine} && ref $args{routine} eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args{"routine"} and ref $args{"routine"} eq "CODE" 2 31 33
Condition100%3/3
ref $args{"routine"} and ref $args{"routine"} eq "CODE"
!ll&&!rl&&r
1131
MC/DC100%2/2
ref $args{"routine"} and ref $args{"routine"} eq "CODE"
ref $args{"routine"} ref $args{"routine"} eq "CODE"
Truth table100%3/3
A and B
A= ref $args{"routine"}B= ref $args{"routine"} eq "CODE"
ABresult
0X0
100
111
29  
30 31 $args{route_tests} = _build_tests($args{address});
31 31 $args{qty_tests} = _build_tests($args{quantity});
32  
33 31 my $self = bless \%args, $class;
34 }
35  
36 12 ▶sub routine {
Subroutine routine: called CC=1 SCAR=0.0
Pod undocumented
37 12 my $self = shift;
38 12 return $self->{routine};
39 }
40  
41 # Receives a route string and converts it into an array reference of
42 # anonymous subroutines. Each subroutine will test if a given value
43 # matches a part of the route.
44 62 ▶sub _build_tests {
Subroutine _build_tests: called CC=4 SCAR=13.9
45 62 my $route = shift;
46  
47 # Star matches always
48 62 ▶ return [ sub { 1 } ] if $route =~ /\*/;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if $route =~ /\*/ 2 60 62
49  
50 60 $route =~ s/\s+//g;
51 60 my @atoms = split /,/, $route;
52 60 my @tests;
53 60 foreach my $atom (@atoms) {
54 # Range test
55 77 ▶ if ($atom =~ /^(\d+)\-(\d+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($atom =~ /^(\d+)\-(\d+)$/) { } 11 66 77
56 11 my ($min, $max) = ($1, $2);
57 11 ▶ push @tests, sub { my $val = shift; return $val >= $min && $val <= $max; };
Subroutine __ANON__: called CC=2 SCAR=6.9
Condition100%3/3
$val >= $min && $val <= $max
!ll&&!rl&&r
224938
MC/DC100%2/2
$val >= $min && $val <= $max
$val >= $min $val <= $max
Truth table100%3/3
A && B
A= $val >= $minB= $val <= $max
ABresult
0X0
100
111
58 }
59 # Equality test
60 else {
61 66 ▶ push @tests, sub { return shift == $atom; };
Subroutine __ANON__: called CC=1 SCAR=0.0
62 }
63 }
64  
65 60 return \@tests;
66 }
67  
68 # Tests an address
69 93 ▶sub test_route {
Subroutine test_route: called CC=3 SCAR=11.0
Pod undocumented
70 93 my ($self, $value) = @_;
71 93 foreach my $test (@{$self->{route_tests}}) {
72 181 ▶ return 1 if $test->($value);
Branch: 1 branch
BranchTrueFalseTotal
if &$test($value) 46 135 181
73 }
74 47 return 0;
75 }
76  
77 # Tests a quantity
78 80 ▶sub test_quantity {
Subroutine test_quantity: called CC=3 SCAR=11.0
Pod undocumented
79 80 my ($self, $value) = @_;
80 80 foreach my $test (@{$self->{qty_tests}}) {
81 169 ▶ return 1 if $test->($value);
Branch: 1 branch
BranchTrueFalseTotal
if &$test($value) 45 124 169
82 }
83 35 return 0;
84 }
85  
86 7 1;
« Modbus/Exception.pm ↑ Summary Modbus/Unit.pm »