blib/lib/App/EvalServerAdvanced/ConstantCalc.pm

filter: Statementstmt 98.7% 155 / 157 Branchbran 54.6% 35 / 64 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 100% 40 / 40 Podpod 66.6% 2 / 3 totaltotal 84.2% 235 / 279 CC 36 SCAR 13.4
CC
36
Cov
84%
add_constant
13.4
new
4.2
get_value
4.2
CRAP 3.8
↑ Summary
1 package App::EvalServerAdvanced::ConstantCalc;
2  
3 1 our $VERSION = '0.06';
4  
5 # ABSTRACT: turns strings and constants into values
6  
7 1 ▶use v5.24;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Function::Parameters;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶has constants => (is => 'ro', default => sub {+{}});
Subroutine __ANON__: called CC=1 SCAR=0.0
13 1 ▶has _parser => (is => 'ro', default => sub {App::EvalServerAdvanced::ConstantCalc::Parser->new(consts => $_[0])});
Subroutine __ANON__: called CC=1 SCAR=0.0
14  
15 2 ▶method get_value($key) {
Subroutine get_value: called CC=4 SCAR=14.3
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
@_ < 2 and Function::Parameters::_croak('Too few arguments for method get_value (expected 2, got ' . @_ . ')') 0 2 2
@_ > 2 and Function::Parameters::_croak('Too many arguments for method get_value (expected 2, got ' . @_ . ')') 0 2 2
16 2 ▶ die "Missing constant [$key]" unless exists($self->constants->{$key});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->constants->{$key} 0 2 2
17  
18 2 return $self->constants->{$key};
19 1 }
20  
21 3 ▶method add_constant($key, $value) {
Subroutine add_constant: called CC=9 SCAR=26.0
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
@_ < 3 and Function::Parameters::_croak('Too few arguments for method add_constant (expected 3, got ' . @_ . ')') 0 3 3
@_ > 3 and Function::Parameters::_croak('Too many arguments for method add_constant (expected 3, got ' . @_ . ')') 0 3 3
22 3 ▶ die "Invalid key [$key]" if ($key =~ /\s/ || $key =~ /^\s*\d/);
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /\s/u or $key =~ /^\s*\d/u 0 3 3
Condition33%1/3
$key =~ /\s/u or $key =~ /^\s*\d/u
l!l&&r!l&&!r
003
MC/DC0%0/2
$key =~ /\s/u or $key =~ /^\s*\d/u
$key =~ /\s/u $key =~ /^\s*\d/u
Truth table33%1/3
A or B
A= $key =~ /\s/uB= $key =~ /^\s*\d/u
ABresult
1X1
011
000
23  
24 3 ▶ if (exists($self->constants->{$key}) && defined(my $eval = $self->constants->{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->constants->{$key} and defined(my $eval = $self->constants->{$key})) 0 3 3
Condition33%1/3
exists $self->constants->{$key} and defined(my $eval = $self->constants->{$key})
!ll&&!rl&&r
300
MC/DC0%0/2
exists $self->constants->{$key} and defined(my $eval = $self->constants->{$key})
exists $self->constants->{$key} defined(my $eval = $self->constants->{$key})
Truth table33%1/3
A and B
A= exists $self->constants->{$key}B= defined(my $eval = $self->constants->{$key})
ABresult
0X0
100
111
25 0 die "Cannot redefine a constant [$key]. Existing value [$eval] new value [$value]"
26 }
27  
28 3 ▶ die "Value undefined for [$key]" unless defined($value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 3 3
29 3 ▶ die "Value [$value] for [$key] must be an integer" if ($value =~ /[^xob\d\-+_]/i);
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /[^xob\d\-+_]/iu 0 3 3
30  
31 3 $self->constants->{$key} = App::EvalServerAdvanced::ConstantCalc::Parser::_to_int($value);
32 1 }
33  
34 7 ▶method calculate($string) {
Subroutine calculate: called CC=3 SCAR=11.2
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
@_ < 2 and Function::Parameters::_croak('Too few arguments for method calculate (expected 2, got ' . @_ . ')') 0 7 7
@_ > 2 and Function::Parameters::_croak('Too many arguments for method calculate (expected 2, got ' . @_ . ')') 0 7 7
35 7 return $self->_parser->from_string($string);
36 1 }
37  
38 package
39 App::EvalServerAdvanced::ConstantCalc::Parser;
40  
41 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 # Ensure we can't accidentally turn to strings, or floats, or anything other than an integer
45 1 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 1 ▶no warnings 'experimental::bitwise';
Subroutine BEGIN: called CC=1 SCAR=0.0
47 1 ▶use feature 'bitwise';
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 1 ▶use parent qw/Parser::MGC/;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 1 ▶use Function::Parameters;
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 1 ▶method new($class: %args) {
Subroutine new: called CC=4 SCAR=14.5
Branch: 2 branches
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for method new (expected 1, got ' . @_ . ')') 0 1 1
@_ > 1 and (@_ - 1) % 2 and Function::Parameters::_croak("Odd number of paired arguments for method new") 0 1 1
Condition33%1/3
@_ > 1 and (@_ - 1) % 2
!ll&&!rl&&r
010
MC/DC0%0/2
@_ > 1 and (@_ - 1) % 2
@_ > 1 (@_ - 1) % 2
Truth table33%1/3
A and B
A= @_ > 1B= (@_ - 1) % 2
ABresult
0X0
100
111
53 1 my $consts = delete $args{consts};
54  
55 1 my $self = $class->SUPER::new(%args);
56  
57 1 $self->{_private}{consts} = $consts;
58  
59 1 return $self;
60 1 }
61  
62 2 ▶method consts() {
Subroutine consts: called CC=3 SCAR=11.3
Branch: 2 branches
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for method consts (expected 1, got ' . @_ . ')') 0 2 2
@_ > 1 and Function::Parameters::_croak('Too many arguments for method consts (expected 1, got ' . @_ . ')') 0 2 2
63 2 return $self->{_private}{consts};
64 1 }
65  
66 20 ▶method parse_upper() {
Subroutine parse_upper: called CC=4 SCAR=13.9
Branch: 2 branches
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for method parse_upper (expected 1, got ' . @_ . ')') 0 20 20
@_ > 1 and Function::Parameters::_croak('Too many arguments for method parse_upper (expected 1, got ' . @_ . ')') 0 20 20
67 20 my $val = $self->parse_term();
68  
69 1 while $self->any_of(
70 26 ▶ sub {$self->expect("&"); $val &= $self->parse_term(); 1},
Subroutine __ANON__: called CC=1 SCAR=0.0
71 20 ▶ sub {0}
Subroutine __ANON__: called CC=1 SCAR=0.0
72 20 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$self->any_of(sub { $self->expect("&"); $val &= $self->parse_term; 1; } , sub { 0; } ) and do { '???' } 6 20 26
73  
74 20 return $val;
75 1 }
76  
77 11 ▶method parse() {
Subroutine parse: called CC=4 SCAR=13.9
Branch: 2 branches
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for method parse (expected 1, got ' . @_ . ')') 0 11 11
@_ > 1 and Function::Parameters::_croak('Too many arguments for method parse (expected 1, got ' . @_ . ')') 0 11 11
78 11 my $val = $self->parse_upper();
79  
80 1 while $self->any_of(
81 20 ▶ sub {$self->expect("^"); $val ^= $self->parse_upper(); 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
82 16 ▶ sub {$self->expect("|"); $val |= $self->parse_upper(); 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
83 11 ▶ sub {0}
Subroutine __ANON__: called CC=1 SCAR=0.0
84 11 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$self->any_of(sub { $self->expect("^"); $val ^= $self->parse_upper; 1; } , sub { $self->expect("|"); $val |= $self->parse_upper; 1; } , sub { 0; } ) and do { '???' } 9 11 20
85  
86 11 return $val;
87 1 }
88  
89 28 ▶method parse_term() {
Subroutine parse_term: called CC=3 SCAR=11.1
Branch: 2 branches
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for method parse_term (expected 1, got ' . @_ . ')') 0 28 28
@_ > 1 and Function::Parameters::_croak('Too many arguments for method parse_term (expected 1, got ' . @_ . ')') 0 28 28
90 $self->any_of(
91 28 ▶ sub { $self->scope_of( "(", sub { $self->parse }, ")" ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
92 24 ▶ sub { $self->expect('~['); my $bitdepth=$self->token_int; $self->expect(']'); my $val = $self->parse_term; (~ ($val & _get_mask($bitdepth))) & _get_mask($bitdepth)},
Subroutine __ANON__: called CC=1 SCAR=0.0
93 0 ▶ sub { $self->expect('~'); ~$self->parse_term},
Subroutine __ANON__: called CC=1 SCAR=1.2
94 22 ▶ sub { $self->token_constant },
Subroutine __ANON__: called CC=1 SCAR=0.0
95 20 ▶ sub { $self->token_int },
Subroutine __ANON__: called CC=1 SCAR=0.0
96 28 );
97 1 }
98  
99 22 ▶method token_int() {
Subroutine token_int: called CC=3 SCAR=11.1
Branch: 2 branches
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for method token_int (expected 1, got ' . @_ . ')') 0 22 22
@_ > 1 and Function::Parameters::_croak('Too many arguments for method token_int (expected 1, got ' . @_ . ')') 0 22 22
100 0+$self->any_of(
101 22 ▶ sub {_to_int($self->expect(qr/0x[0-9A-F_]+/i));},
Subroutine __ANON__: called CC=1 SCAR=0.0
102 21 ▶ sub {_to_int($self->expect(qr/0b[0-7_]+/i));},
Subroutine __ANON__: called CC=1 SCAR=0.0
103 20 ▶ sub {_to_int($self->expect(qr/0o?[0-7_]+/i));},
Subroutine __ANON__: called CC=1 SCAR=0.0
104 19 ▶ sub {$self->expect(qr/\d+/)}
Subroutine __ANON__: called CC=1 SCAR=0.0
105 22 );
106 1 }
107  
108 22 ▶method token_constant() {
Subroutine token_constant: called CC=3 SCAR=11.2
Branch: 2 branches
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for method token_constant (expected 1, got ' . @_ . ')') 0 22 22
@_ > 1 and Function::Parameters::_croak('Too many arguments for method token_constant (expected 1, got ' . @_ . ')') 0 22 22
109 22 my $const = $self->expect(qr/[a-z_][a-z_0-9]+/i);
110  
111 2 $self->consts->get_value($const);
112 1 }
113  
114 4 ▶fun _get_mask($size) {
Subroutine _get_mask: called CC=3 SCAR=11.3
Branch: 2 branches
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for fun _get_mask (expected 1, got ' . @_ . ')') 0 4 4
@_ > 1 and Function::Parameters::_croak('Too many arguments for fun _get_mask (expected 1, got ' . @_ . ')') 0 4 4
115 4 return 2**($size)-1;
116 1 }
117  
118  
119 6 ▶fun _to_int($val) {
Subroutine _to_int: called CC=4 SCAR=14.0
Branch: 2 branches
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for fun _to_int (expected 1, got ' . @_ . ')') 0 6 6
@_ > 1 and Function::Parameters::_croak('Too many arguments for fun _to_int (expected 1, got ' . @_ . ')') 0 6 6
120 6 $val =~ s/^0o/0/i;
121  
122 6 ▶ if ($val =~ /^0/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /^0/u) { } 4 2 6
123 4 return oct $val;
124 } else {
125 2 return 0+$val;
126 }
127 1 }
128  
129 1 1;
130  
131 __END__
↑ Summary