| 1 | package PerlX::Assert::PP; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Toby Inkster's assertion keyword in pure-Perl without XS dependency | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 152 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 152 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 152 | our $VERSION = '0.04'; | |||||||||||||||||||||||||||
| 8 | 152 | our $AUTHORITY = 'cpan:BRICKPOOL'; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 152 | ▶ | use Carp (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 152 | our @EXPORT = qw( assert ); | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | # Enables or disables the source filter for block-style assertions | ||||||||||||||||||||||||||||
| 15 | 152 | ▶ | use constant ASSERT_BLOCK => exists $ENV{PERLX_ASSERT_PP_FILTER} | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 16 | 152 | ▶ | && $ENV{PERLX_ASSERT_PP_FILTER}; | ||||||||||||||||||||||||||
|
Condition66%2/3
exists $ENV{'PERLX_ASSERT_PP_FILTER'} && $ENV{'PERLX_ASSERT_PP_FILTER'}
MC/DC50%1/2
exists $ENV{'PERLX_ASSERT_PP_FILTER'} && $ENV{'PERLX_ASSERT_PP_FILTER'}
exists $ENV{'PERLX_ASSERT_PP_FILTER'}
$ENV{'PERLX_ASSERT_PP_FILTER'}
Truth table66%2/3
A && B
A= exists $ENV{'PERLX_ASSERT_PP_FILTER'}B= $ENV{'PERLX_ASSERT_PP_FILTER'}
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 152 | ▶ | use if ASSERT_BLOCK, 'B'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 152 | ▶ | use if ASSERT_BLOCK, 'Filter::Simple'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 152 | ▶ | use if ASSERT_BLOCK, 'Text::Balanced', qw( | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | extract_quotelike | ||||||||||||||||||||||||||||
| 22 | extract_codeblock | ||||||||||||||||||||||||||||
| 23 | 152 | ); | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | # Assertions are enabled if either STRICT is true or -check was used | ||||||||||||||||||||||||||||
| 26 | 152 | ▶ | use constant STRICT => !!grep { exists $ENV{$_} && $ENV{$_} } qw( | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.0
Condition66%2/3
exists $ENV{$_} && $ENV{$_}
MC/DC50%1/2
exists $ENV{$_} && $ENV{$_}
exists $ENV{$_}
$ENV{$_}
Truth table66%2/3
A && B
A= exists $ENV{$_}B= $ENV{$_}
| |||||||||||||||||||||||||||||
| 27 | PERL_STRICT | ||||||||||||||||||||||||||||
| 28 | EXTENDED_TESTING | ||||||||||||||||||||||||||||
| 29 | AUTHOR_TESTING | ||||||||||||||||||||||||||||
| 30 | RELEASE_TESTING | ||||||||||||||||||||||||||||
| 31 | 152 | ); | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | # Debug mode | ||||||||||||||||||||||||||||
| 34 | 152 | ▶ | use constant DEBUG => do { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 35 | 152 | ▶ | no warnings 'uninitialized'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 152 | ▶ | 0+( exists $ENV{PERLX_ASSERT_PP_DEBUG} ? $ENV{PERLX_ASSERT_PP_DEBUG} : 0 ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 152 | }; | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 152 | ▶ | use constant { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | 152 | PHASE_MASK => 2**0, | |||||||||||||||||||||||||||
| 41 | PHASE_PARSE => 2**1, | ||||||||||||||||||||||||||||
| 42 | PHASE_INJECT => 2**2, | ||||||||||||||||||||||||||||
| 43 | 152 | }; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 152 | our $CHECK = 0; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | # ------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 48 | # import / unimport | ||||||||||||||||||||||||||||
| 49 | # ------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 1907 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 52 | 1907 | my ( $class, @args ) = @_; | |||||||||||||||||||||||||||
| 53 | 1907 | my $caller = caller(); | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | # process -check flag | ||||||||||||||||||||||||||||
| 56 | 1907 | ▶ | $CHECK = 1 if grep { $_ eq '-check' } @args; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | # export our functions | ||||||||||||||||||||||||||||
| 59 | 152 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 60 | 1907 | foreach ( @EXPORT ) { | |||||||||||||||||||||||||||
| 61 | 1907 | *{"${caller}::$_"} = \&$_ | |||||||||||||||||||||||||||
| 62 | 1907 | ▶ | unless *{"${caller}::$_"}{CODE}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | } #/ sub import | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 0 | ▶ | sub unimport { | ||||||||||||||||||||||||||
|
Subroutine
unimport: not called CC=3 SCAR=18.7
| |||||||||||||||||||||||||||||
| 67 | 0 | my ( $class ) = @_; | |||||||||||||||||||||||||||
| 68 | 0 | my $caller = caller(); | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 152 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 71 | 0 | foreach ( @EXPORT ) { | |||||||||||||||||||||||||||
| 72 | 0 | undef( *{"${caller}::$_"} ) | |||||||||||||||||||||||||||
| 73 | 0 | ▶ | if *{"${caller}::$_"}{CODE}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | # ------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 78 | # assert EXPR; | ||||||||||||||||||||||||||||
| 79 | # assert "name", EXPR; | ||||||||||||||||||||||||||||
| 80 | # ------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 48210 | ▶ | sub assert ($;$) { | ||||||||||||||||||||||||||
|
Subroutine
assert: called CC=5 SCAR=16.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 83 | 48210 | my ( $name, $bool ); | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | # only check when STRICT or CHECK is enabled | ||||||||||||||||||||||||||||
| 86 | 48210 | return unless STRICT || $CHECK; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 48210 | ▶ | if ( @_ == 2 ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 89 | # two-argument form: assert NAME, BOOL | ||||||||||||||||||||||||||||
| 90 | 13 | ( $name, $bool ) = @_; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | elsif ( @_ == 1 ) { | ||||||||||||||||||||||||||||
| 93 | # one-argument form: assert BOOL | ||||||||||||||||||||||||||||
| 94 | 48197 | $bool = $_[0]; | |||||||||||||||||||||||||||
| 95 | 48197 | $name = undef; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | else { | ||||||||||||||||||||||||||||
| 98 | 0 | Carp::croak( "assert() called with invalid number of arguments" ); | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | # assertion passes | ||||||||||||||||||||||||||||
| 102 | 48210 | ▶ | return if $bool; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | # assertion fails | ||||||||||||||||||||||||||||
| 105 | 335 | ▶ | if ( defined $name ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 8 | Carp::croak( "Assertion failed: $name" ); | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | else { | ||||||||||||||||||||||||||||
| 109 | 327 | Carp::croak( "Assertion failed" ); | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 0 | return; | |||||||||||||||||||||||||||
| 113 | } #/ sub assert ($;$) | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | # ------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 116 | # Filter entry point: mask > parse > inject + optional debug | ||||||||||||||||||||||||||||
| 117 | # | ||||||||||||||||||||||||||||
| 118 | # Source filter: | ||||||||||||||||||||||||||||
| 119 | # assert { BLOCK }; -> assert 'BLOCK', do { BLOCK }; | ||||||||||||||||||||||||||||
| 120 | # assert "name" { BLOCK }; -> assert "name", do { BLOCK }; | ||||||||||||||||||||||||||||
| 121 | # ------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 1 | ▶ | FILTER_ONLY( executable => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||
| 124 | 1 | my $src = $_; | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 1 | ▶ | unless ( index($src, 'assert') >= 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 0 | DEBUG and warn("quick bailout: no 'assert'\n"); | |||||||||||||||||||||||||||
| 128 | 0 | return; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 1 | my $scan = mask_strings_and_comments( $src ); | |||||||||||||||||||||||||||
| 132 | 1 | DEBUG & PHASE_MASK and do { | |||||||||||||||||||||||||||
| 133 | warn "=== START DEBUG MASK ===\n"; | ||||||||||||||||||||||||||||
| 134 | require Text::Diff; | ||||||||||||||||||||||||||||
| 135 | my $diff = Text::Diff::diff( \$src, \$scan ); | ||||||||||||||||||||||||||||
| 136 | warn "--- DIFF ---\n$diff\n"; | ||||||||||||||||||||||||||||
| 137 | warn "=== END MASK DEBUG ===\n"; | ||||||||||||||||||||||||||||
| 138 | }; | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 1 | my $asserts = parse_asserts( $src, $scan ); | |||||||||||||||||||||||||||
| 141 | 1 | DEBUG & PHASE_PARSE and do { | |||||||||||||||||||||||||||
| 142 | warn "=== START DEBUG PARSE ===\n"; | ||||||||||||||||||||||||||||
| 143 | require Data::Dumper; | ||||||||||||||||||||||||||||
| 144 | local $Data::Dumper::Sortkeys = 1; | ||||||||||||||||||||||||||||
| 145 | warn Data::Dumper->Dump( $asserts ); | ||||||||||||||||||||||||||||
| 146 | warn "=== END PARSE DEBUG ===\n"; | ||||||||||||||||||||||||||||
| 147 | }; | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 1 | $_ = inject_asserts( $src, $asserts ); | |||||||||||||||||||||||||||
| 150 | 1 | DEBUG & PHASE_INJECT and do { | |||||||||||||||||||||||||||
| 151 | warn "=== START DEBUG PHASE_INJECT ===\n"; | ||||||||||||||||||||||||||||
| 152 | require Text::Diff; | ||||||||||||||||||||||||||||
| 153 | my $diff = Text::Diff::diff( \$src, \$_ ); | ||||||||||||||||||||||||||||
| 154 | warn "--- DIFF ---\n$diff\n"; | ||||||||||||||||||||||||||||
| 155 | warn "=== END INJECT DEBUG ===\n"; | ||||||||||||||||||||||||||||
| 156 | }; | ||||||||||||||||||||||||||||
| 157 | 152 | } ) if ASSERT_BLOCK; | |||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | # ------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 160 | # Masking: masks all sections of the source string that contain | ||||||||||||||||||||||||||||
| 161 | # quotelikes and comments. | ||||||||||||||||||||||||||||
| 162 | # ------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 1 | ▶ | sub mask_strings_and_comments { | ||||||||||||||||||||||||||
|
Subroutine
mask_strings_and_comments: called CC=5 SCAR=16.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 165 | 1 | my ( $src ) = @_; | |||||||||||||||||||||||||||
| 166 | 1 | my $out = ''; | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 1 | ▶ | while ( length $src ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | 55 | ▶ | if ( $src =~ /(?<!\w)(["'`]|q[qwxr]?)/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | 54 | my $idx = $-[0]; | |||||||||||||||||||||||||||
| 171 | 54 | DEBUG & PHASE_MASK and do { | |||||||||||||||||||||||||||
| 172 | warn "[MASK]: next quote-like at index $idx\n"; | ||||||||||||||||||||||||||||
| 173 | }; | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 54 | $out .= substr( $src, 0, $idx ); | |||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 54 | my $tail = substr( $src, $idx ); | |||||||||||||||||||||||||||
| 178 | 54 | DEBUG & PHASE_MASK and do { | |||||||||||||||||||||||||||
| 179 | warn "[MASK]: tail starts with: " . substr( $tail, 0, 20 ) . "\n"; | ||||||||||||||||||||||||||||
| 180 | }; | ||||||||||||||||||||||||||||
| 181 | 54 | my ( $str, $remainder ) = extract_quotelike( $tail ); | |||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 54 | ▶ | if ( !defined $str || $str eq '' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $str or $str eq ""
MC/DC0%0/2
not defined $str or $str eq ""
not defined $str
$str eq ""
Truth table33%1/3
not(A) or B
A= not defined $strB= $str eq ""
| |||||||||||||||||||||||||||||
| 184 | 0 | DEBUG & PHASE_MASK and do { | |||||||||||||||||||||||||||
| 185 | warn "[MASK]: extract_quotelike returned undef, consuming 1 char\n"; | ||||||||||||||||||||||||||||
| 186 | }; | ||||||||||||||||||||||||||||
| 187 | 0 | $out .= substr( $src, $idx, 1 ); | |||||||||||||||||||||||||||
| 188 | 0 | $src = substr( $src, $idx + 1 ); | |||||||||||||||||||||||||||
| 189 | 0 | next; | |||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 54 | DEBUG & PHASE_MASK and do { | |||||||||||||||||||||||||||
| 193 | warn "[MASK]: extracted string: [$str]\n"; | ||||||||||||||||||||||||||||
| 194 | }; | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 54 | $out .= ' ' x length( $str ); | |||||||||||||||||||||||||||
| 197 | 54 | $src = $remainder; | |||||||||||||||||||||||||||
| 198 | } #/ if ( $src =~ ...) | ||||||||||||||||||||||||||||
| 199 | else { | ||||||||||||||||||||||||||||
| 200 | 1 | $out .= $src; | |||||||||||||||||||||||||||
| 201 | 1 | last; | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | } #/ while ( length $src ) | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | # masking all comments | ||||||||||||||||||||||||||||
| 206 | 1 | $out =~ s{(^|(?<=\s))\#.*$}{ $1 . (' ' x (length($&) - length($1))) }egm; | |||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | 1 | return $out; | |||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | # ------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 212 | # Parsing: find all assert block statements and save them as metadata. | ||||||||||||||||||||||||||||
| 213 | # | ||||||||||||||||||||||||||||
| 214 | # The $src parameter contains the original code and the $scan parameter | ||||||||||||||||||||||||||||
| 215 | # contains a modified source code where strings and comments have been | ||||||||||||||||||||||||||||
| 216 | # masked to prevent their content from being parsed. | ||||||||||||||||||||||||||||
| 217 | # | ||||||||||||||||||||||||||||
| 218 | # Returns a list of HashRef's: | ||||||||||||||||||||||||||||
| 219 | # { start => Int, length => Int, name => Str|undef, block => Str } | ||||||||||||||||||||||||||||
| 220 | # ------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | 1 | ▶ | sub parse_asserts { | ||||||||||||||||||||||||||
|
Subroutine
parse_asserts: called CC=6 SCAR=18.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 223 | 1 | my ( $src, $scan ) = @_; | |||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | 1 | my @asserts; | |||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | # Initialize parsing position | ||||||||||||||||||||||||||||
| 228 | 1 | pos( $src ) = 0; | |||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | 1 | while ( 1 ) { | |||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | # Find next occurrence of 'assert' in the scan string, | ||||||||||||||||||||||||||||
| 233 | 14 | my $idx = index( $scan, "assert", pos( $src ) ); | |||||||||||||||||||||||||||
| 234 | 14 | ▶ | last if $idx == -1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | 13 | DEBUG & PHASE_PARSE and do { | |||||||||||||||||||||||||||
| 237 | warn "[PARSE]: found 'assert' in scan at offset $idx\n"; | ||||||||||||||||||||||||||||
| 238 | }; | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | # Move source position to the start of this assert | ||||||||||||||||||||||||||||
| 241 | 13 | pos( $src ) = $idx; | |||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | # Consume the keyword 'assert' including trailing whitespace | ||||||||||||||||||||||||||||
| 244 | 13 | ▶ | $src =~ /\Gassert\s*/gc | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 245 | or next; # should not happen, but keeps parser robust | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | # Try to read an optional NAME (string-producing quotelike) | ||||||||||||||||||||||||||||
| 248 | 13 | my ( $name ) = extract_quotelike( $src ); | |||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | # Check whether a quote-like has been found and verify it. | ||||||||||||||||||||||||||||
| 251 | 13 | ▶ | if ( defined $name ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 252 | # 'qw', 'qx', or 'qr' are not valid for NAME -> skip it | ||||||||||||||||||||||||||||
| 253 | 5 | ▶ | next if $name =~ /\Aq[wxr]\b/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | 5 | DEBUG & PHASE_PARSE and do { | |||||||||||||||||||||||||||
| 256 | warn "[PARSE]: assert NAME in source: $name\n"; | ||||||||||||||||||||||||||||
| 257 | }; | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | # Skip whitespace after NAME | ||||||||||||||||||||||||||||
| 260 | 5 | $src =~ /\G\s*/gc; | |||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | # Extract the code block | ||||||||||||||||||||||||||||
| 264 | 13 | my ( $block ) = extract_codeblock( $src ); | |||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | # If no block is found, this is not a valid assert -> skip it | ||||||||||||||||||||||||||||
| 267 | 13 | ▶ | unless ( defined $block ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 268 | 0 | DEBUG & PHASE_PARSE and do { | |||||||||||||||||||||||||||
| 269 | warn "[PARSE]: extraction failed at offset ". pos( $src ) . "\n"; | ||||||||||||||||||||||||||||
| 270 | }; | ||||||||||||||||||||||||||||
| 271 | 0 | next; | |||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | # Determine the full length of the entire statement | ||||||||||||||||||||||||||||
| 275 | 13 | my $length = pos( $src ) - $idx; | |||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | 13 | DEBUG & PHASE_PARSE and do { | |||||||||||||||||||||||||||
| 278 | warn "[PARSE]: found { BLOCK } in scan at offset " . | ||||||||||||||||||||||||||||
| 279 | ( pos( $src ) - length( $block ) ) . "\n"; | ||||||||||||||||||||||||||||
| 280 | warn "[PARSE]: assert statement length: $length\n"; | ||||||||||||||||||||||||||||
| 281 | }; | ||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | # At this point: | ||||||||||||||||||||||||||||
| 284 | # - $name is undef or a valid string NAME | ||||||||||||||||||||||||||||
| 285 | # - $block contains the full { ... } block | ||||||||||||||||||||||||||||
| 286 | # - $length contains the statement length w/o optional ';' | ||||||||||||||||||||||||||||
| 287 | # - pos($src) is behind the block | ||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | 13 | push @asserts, { | |||||||||||||||||||||||||||
| 290 | start => $idx, | ||||||||||||||||||||||||||||
| 291 | length => $length, | ||||||||||||||||||||||||||||
| 292 | name => $name, | ||||||||||||||||||||||||||||
| 293 | block => $block, | ||||||||||||||||||||||||||||
| 294 | }; | ||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | # Continue searching for the next assert | ||||||||||||||||||||||||||||
| 297 | } #/ while ( 1 ) | ||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | 1 | return \@asserts; | |||||||||||||||||||||||||||
| 300 | } #/ sub parse_asserts | ||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | # ------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 303 | # Injection: build a new string with replacements for the block based | ||||||||||||||||||||||||||||
| 304 | # assert statements. | ||||||||||||||||||||||||||||
| 305 | # ------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | 1 | ▶ | sub inject_asserts { | ||||||||||||||||||||||||||
|
Subroutine
inject_asserts: called CC=5 SCAR=16.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 308 | 1 | my ( $src, $asserts ) = @_; | |||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | 1 | my $out = ''; | |||||||||||||||||||||||||||
| 311 | 1 | my $pos = 0; | |||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | 1 | foreach ( @$asserts ) { | |||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | 13 | my $off = $_->{start}; # start offset of the statement | |||||||||||||||||||||||||||
| 316 | 13 | my $len = $_->{length}; # full length of the statement | |||||||||||||||||||||||||||
| 317 | 13 | my $name = $_->{name}; # includes quote likes string or 'undef' | |||||||||||||||||||||||||||
| 318 | 13 | my $block = $_->{block}; # includes outer braces | |||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | # Overwrite the parameters if neither STRICT nor CHECK is enabled. | ||||||||||||||||||||||||||||
| 321 | 13 | ▶ | unless ( $CHECK || STRICT ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$PerlX::Assert::PP::CHECK or !!1
MC/DC0%0/1
$PerlX::Assert::PP::CHECK or !!1
$PerlX::Assert::PP::CHECK
Truth table50%1/2
A
A= $PerlX::Assert::PP::CHECK
| |||||||||||||||||||||||||||||
| 322 | 0 | $name = "undef"; | |||||||||||||||||||||||||||
| 323 | 0 | $block = "{ 1 }"; | |||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | 13 | DEBUG & PHASE_INJECT and do { | |||||||||||||||||||||||||||
| 327 | warn "[INJECT]: inject at offset $off\n"; | ||||||||||||||||||||||||||||
| 328 | }; | ||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | # copy everything before this assert block | ||||||||||||||||||||||||||||
| 331 | 13 | $out .= substr( $src, $pos, $off - $pos ); | |||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||
| 333 | # generate name if missing | ||||||||||||||||||||||||||||
| 334 | 13 | ▶ | unless ( defined $name ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 335 | 8 | $name = do { | |||||||||||||||||||||||||||
| 336 | # Quote { BLOCK } as Perl string literal | ||||||||||||||||||||||||||||
| 337 | 8 | local $_ = $block; | |||||||||||||||||||||||||||
| 338 | 8 | s/#.*$//gm; # remove comments | |||||||||||||||||||||||||||
| 339 | 8 | s/\s{2,}/ /g; # replace any white spaces with one space | |||||||||||||||||||||||||||
| 340 | 8 | B::perlstring( $_ ); | |||||||||||||||||||||||||||
| 341 | }; | ||||||||||||||||||||||||||||
| 342 | 8 | DEBUG & PHASE_INJECT and do { | |||||||||||||||||||||||||||
| 343 | warn "[INJECT]: generated name: $name\n"; | ||||||||||||||||||||||||||||
| 344 | }; | ||||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | # build injected code | ||||||||||||||||||||||||||||
| 348 | 13 | my $injected = "assert $name, do $block"; | |||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | 13 | DEBUG & PHASE_INJECT and do { | |||||||||||||||||||||||||||
| 351 | warn "[INJECT]: injected code: $injected\n"; | ||||||||||||||||||||||||||||
| 352 | }; | ||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | 13 | $out .= $injected; | |||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | # move cursor after the original block | ||||||||||||||||||||||||||||
| 357 | 13 | $pos = $off + $len; | |||||||||||||||||||||||||||
| 358 | } #/ foreach my $a ( @$asserts ) | ||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | # append remaining source | ||||||||||||||||||||||||||||
| 361 | 1 | $out .= substr( $src, $pos ); | |||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | 1 | return $out; | |||||||||||||||||||||||||||
| 364 | } | ||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | 152 | 1 | |||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | __END__ | ||||||||||||||||||||||||||||