| 1 | package assertions; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | # use strict; | ||||||||||||||||||||||||||||
| 6 | # use warnings; | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 2 | my $hint=0x01000000; | |||||||||||||||||||||||||||
| 9 | 2 | my $seen_hint=0x02000000; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 5 | ▶ | sub _syntax_error ($$) { | ||||||||||||||||||||||||||
|
Subroutine
_syntax_error: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 5 | my ($expr, $why)=@_; | |||||||||||||||||||||||||||
| 13 | 5 | require Carp; | |||||||||||||||||||||||||||
| 14 | 5 | Carp::croak("syntax error on assertion filter '$expr' ($why)"); | |||||||||||||||||||||||||||
| 15 | } | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 2 | ▶ | sub _carp { | ||||||||||||||||||||||||||
|
Subroutine
_carp: called CC=2 SCAR=9.2
| |||||||||||||||||||||||||||||
| 18 | 2 | require warnings; | |||||||||||||||||||||||||||
| 19 | 2 | ▶ | if (warnings::enabled('assertions')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | 0 | require Carp; | |||||||||||||||||||||||||||
| 21 | 0 | Carp::carp(@_); | |||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 42 | ▶ | sub _calc_expr { | ||||||||||||||||||||||||||
|
Subroutine
_calc_expr: called CC=26 SCAR=32.6
| |||||||||||||||||||||||||||||
| 26 | 42 | my $expr=shift; | |||||||||||||||||||||||||||
| 27 | 42 | my @tokens=split / \s* | |||||||||||||||||||||||||||
| 28 | ( && # and | ||||||||||||||||||||||||||||
| 29 | | \|\| # or | ||||||||||||||||||||||||||||
| 30 | | \( # parents | ||||||||||||||||||||||||||||
| 31 | | \) ) | ||||||||||||||||||||||||||||
| 32 | \s* | ||||||||||||||||||||||||||||
| 33 | | \s+ # spaces out | ||||||||||||||||||||||||||||
| 34 | /x, $expr; | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | # print STDERR "tokens: -", join('-',@tokens), "-\n"; | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 42 | my @now=1; | |||||||||||||||||||||||||||
| 39 | 42 | my @op='start'; | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 42 | for my $t (@tokens) { | |||||||||||||||||||||||||||
| 42 | 161 | ▶ | next if (!defined $t or $t eq ''); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $t or $t eq ""
MC/DC100%2/2
not defined $t or $t eq ""
not defined $t
$t eq ""
Truth table100%3/3
not(A) or B
A= not defined $tB= $t eq ""
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 137 | ▶ | if ($t eq '(') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 13 | unshift @now, 1; | |||||||||||||||||||||||||||
| 46 | 13 | unshift @op, 'start'; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | else { | ||||||||||||||||||||||||||||
| 49 | 124 | ▶ | if ($t eq '||') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 50 | 12 | ▶ | defined $op[0] | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | and _syntax_error $expr, 'consecutive operators'; | ||||||||||||||||||||||||||||
| 52 | 11 | $op[0]='||'; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | elsif ($t eq '&&') { | ||||||||||||||||||||||||||||
| 55 | 25 | ▶ | defined $op[0] | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | and _syntax_error $expr, 'consecutive operators'; | ||||||||||||||||||||||||||||
| 57 | 24 | $op[0]='&&'; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | else { | ||||||||||||||||||||||||||||
| 60 | 87 | ▶ | if ($t eq ')') { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 61 | 13 | ▶ | @now==1 and | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | _syntax_error $expr, 'unbalanced parens'; | ||||||||||||||||||||||||||||
| 63 | 12 | ▶ | defined $op[0] and | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | _syntax_error $expr, "key missing after operator '$op[0]'"; | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 12 | $t=shift @now; | |||||||||||||||||||||||||||
| 67 | 12 | shift @op; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | elsif ($t eq '_') { | ||||||||||||||||||||||||||||
| 70 | 8 | ▶ | unless ($^H & $seen_hint) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 2 | _carp "assertion status '_' referenced but not previously defined"; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | 8 | ▶ | $t=($^H & $hint) ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | ▶ | elsif ($t ne '0' and $t ne '1') { | |||||||||||||||||||||||||||
|
Condition100%3/3
$t ne 0 and $t ne 1
MC/DC100%2/2
$t ne 0 and $t ne 1
$t ne 0
$t ne 1
Truth table100%3/3
A and B
A= $t ne 0B= $t ne 1
| |||||||||||||||||||||||||||||
| 76 | 22 | ▶ | $t = ( grep { ref $_ eq 'Regexp' | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 77 | ? $t=~$_ | ||||||||||||||||||||||||||||
| 78 | : $_->check($t) | ||||||||||||||||||||||||||||
| 79 | } @{^ASSERTING} ) ? 1 : 0; | ||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 86 | ▶ | defined $op[0] or | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | _syntax_error $expr, 'operator expected'; | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 86 | ▶ | if ($op[0] eq 'start') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 86 | 54 | $now[0]=$t; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | elsif ($op[0] eq '||') { | ||||||||||||||||||||||||||||
| 89 | 11 | ▶ | $now[0]||=$t; | ||||||||||||||||||||||||||
|
Condition100%3/3
$now[0] ||= $t
MC/DC100%2/2
$now[0] ||= $t
$now[0]
$t
Truth table100%3/3
A ||= B
A= $now[0]B= $t
| |||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | else { | ||||||||||||||||||||||||||||
| 92 | 21 | ▶ | $now[0]&&=$t; | ||||||||||||||||||||||||||
|
Condition100%3/3
$now[0] &&= $t
MC/DC100%2/2
$now[0] &&= $t
$now[0]
$t
Truth table100%3/3
A &&= B
A= $now[0]B= $t
| |||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | 86 | undef $op[0]; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | 39 | ▶ | @now==1 or _syntax_error $expr, 'unbalanced parens'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 38 | ▶ | defined $op[0] and _syntax_error $expr, "expression ends on operator '$op[0]'"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 37 | return $now[0]; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 14 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 106 | # print STDERR "\@_=", join("|", @_), "\n"; | ||||||||||||||||||||||||||||
| 107 | 14 | shift; | |||||||||||||||||||||||||||
| 108 | 14 | ▶ | @_=(scalar(caller)) unless @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 14 | foreach my $expr (@_) { | |||||||||||||||||||||||||||
| 110 | 14 | ▶ | unless (_calc_expr $expr) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | # print STDERR "assertions deactived"; | ||||||||||||||||||||||||||||
| 112 | 4 | $^H &= ~$hint; | |||||||||||||||||||||||||||
| 113 | 4 | $^H |= $seen_hint; | |||||||||||||||||||||||||||
| 114 | 4 | return; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | # print STDERR "assertions actived"; | ||||||||||||||||||||||||||||
| 118 | 10 | $^H |= $hint|$seen_hint; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 1 | ▶ | sub unimport { | ||||||||||||||||||||||||||
|
Subroutine
unimport: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||
| 122 | 1 | ▶ | @_ > 1 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | and _carp($_[0]."->unimport arguments are being ignored"); | ||||||||||||||||||||||||||||
| 124 | 1 | $^H &= ~$hint; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 6 | ▶ | sub enabled { | ||||||||||||||||||||||||||
|
Subroutine
enabled: called CC=4 SCAR=19.7
Pod
documented
| |||||||||||||||||||||||||||||
| 128 | 6 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 0 | ▶ | if ($_[0]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 0 | $^H |= $hint; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | else { | ||||||||||||||||||||||||||||
| 133 | 0 | $^H &= ~$hint; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | 0 | $^H |= $seen_hint; | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | 6 | ▶ | return $^H & $hint ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 0 | ▶ | sub seen { | ||||||||||||||||||||||||||
|
Subroutine
seen: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 141 | 0 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | 0 | ▶ | if ($_[0]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 0 | $^H |= $seen_hint; | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | else { | ||||||||||||||||||||||||||||
| 146 | 0 | $^H &= ~$seen_hint; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | 0 | ▶ | return $^H & $seen_hint ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 2 | 1; | |||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | __END__ | ||||||||||||||||||||||||||||