blib/lib/Macro/Simple.pm

filter: Statementstmt 82.2% 97 / 118 Branchbran 62.5% 25 / 40 Conditioncond 40.0% 12 / 30 MC/DCmcdc 11.1% 2 / 18 Subroutinesub 85.0% 17 / 20 Podpod 100% 2 / 2 totaltotal 67.9% 155 / 228 CC 32 SCAR 28.9
CC
32
Cov
71%
_parse
35.2
handle_generator
10.5
BEGIN
6.0
CRAP 18.0
↑ Summary
1 1 ▶use 5.008003;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Macro::Simple;
6  
7 1 our $AUTHORITY = 'cpan:TOBYINK';
8 1 our $VERSION = '0.003';
9  
10 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use constant DO_MACRO => (
Subroutine BEGIN: called CC=4 SCAR=17.9
Condition33%3/9
$] ge 5.014 && require Parse::Keyword && require PPI && require Sub::Boolean
!ll&&!rl&&r
001
$] ge "5.014" and require Parse::Keyword and require PPI
!ll&&!rl&&r
001
$] ge "5.014" and require Parse::Keyword
!ll&&!rl&&r
001
MC/DC0%0/4
$] ge 5.014 && require Parse::Keyword && require PPI && require Sub::Boolean
$] ge "5.014" require Parse::Keyword require PPI require Sub::Boolean
Truth table20%1/5
A and B and C && D
A= $] ge "5.014"B= require Parse::KeywordC= require PPID= require Sub::Boolean
ABCDresult
0XXX0
10XX0
110X0
11100
11111
13 $] ge 5.014000 and
14 require Parse::Keyword and
15 require PPI and
16 require Sub::Boolean
17 1 );
18  
19 1 ▶use constant DO_CLEAN => eval { require namespace::clean };
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
22 1 my ( $class, $macros ) = ( shift, @_ );
23 1 my $caller = caller;
24 1 $class->setup_for( $caller, $macros );
25 }
26  
27 1 ▶sub setup_for {
Subroutine setup_for: called CC=2 SCAR=6.9
Pod documented
28 1 my ( $class, $caller, $macros ) = ( shift, @_ );
29
30 1 my $installer = DO_MACRO ? '_setup_using_parse_keyword' : '_setup_fallback';
31
32 1 for my $key ( sort keys %$macros ) {
33 2 my ( $subname, $prototype ) = ( $key =~ m{\A(\w+)(.+)\z} );
34 2 my $generator = $class->handle_generator( $macros->{$key} );
35
36 2 $class->$installer( {
37 caller => $caller,
38 subname => $subname,
39 prototype => $prototype,
40 generator => $generator,
41 } );
42 }
43 }
44  
45 2 ▶sub handle_generator {
Subroutine handle_generator: called CC=6 SCAR=23.5
Pod documented
46 2 my ( $class, $generator ) = ( shift, @_ );
47
48 2 ▶ if ( 'HASH' eq ref $generator and $generator->{is} ) {
Branch: 3 branches
BranchTrueFalseTotal
if ('HASH' eq ref $generator and $generator->{'is'}) { } 0 2 2
elsif ('HASH' eq ref $generator and $generator->{'assert'}) { } 0 2 2
elsif (not ref $generator) { } 1 1 2
Condition33%1/3
"HASH" eq ref $generator and $generator->{"is"}
!ll&&!rl&&r
200
MC/DC0%0/2
"HASH" eq ref $generator and $generator->{"is"}
"HASH" eq ref $generator $generator->{"is"}
Truth table33%1/3
A and B
A= "HASH" eq ref $generatorB= $generator->{"is"}
ABresult
0X0
100
111
49 0 my $code = $generator->{is}->inline_check( '$x' );
50 0 ▶ $generator = sub { sprintf 'my $x = %s; %s', $_[0], $code };
Subroutine __ANON__: not called CC=1 SCAR=6.9
51 }
52 ▶ elsif ( 'HASH' eq ref $generator and $generator->{assert} ) {
Condition33%1/3
"HASH" eq ref $generator and $generator->{"assert"}
!ll&&!rl&&r
200
MC/DC0%0/2
"HASH" eq ref $generator and $generator->{"assert"}
"HASH" eq ref $generator $generator->{"assert"}
Truth table33%1/3
A and B
A= "HASH" eq ref $generatorB= $generator->{"assert"}
ABresult
0X0
100
111
53 0 my $code = $generator->{assert}->inline_assert( '$x' );
54 0 ▶ $generator = sub { sprintf 'my $x = %s; %s', $_[0], $code };
Subroutine __ANON__: not called CC=1 SCAR=6.9
55 }
56 elsif ( not ref $generator ) {
57 1 my $format = $generator;
58 1 ▶ $generator = sub { sprintf $format, @_ };
Subroutine __ANON__: called CC=1 SCAR=0.0
59 }
60
61 2 return $generator;
62 }
63  
64 2 ▶sub _setup_using_parse_keyword {
Subroutine _setup_using_parse_keyword: called CC=1 SCAR=0.0
65 2 my ( $class, $opt ) = ( shift, @_ );
66 2 my ( $caller, $subname ) = @{$opt}{qw/ caller subname /};
67 2 Sub::Boolean::make_true("$caller\::$subname");
68 1 ▶ no strict qw( refs );
Subroutine BEGIN: called CC=1 SCAR=0.0
69 Parse::Keyword::install_keyword_handler(
70 2 \&{ "$caller\::$subname" },
71 2 ▶ sub { $class->_parse( $opt ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
72 2 );
73 2 $class->_clean( $caller, $subname );
74 }
75  
76 0 ▶sub _setup_fallback {
Subroutine _setup_fallback: not called CC=1 SCAR=2.9
77 0 my ( $class, $opt ) = ( shift, @_ );
78 my ( $caller, $subname, $prototype, $generator ) =
79 0 @{$opt}{qw/ caller subname prototype generator /};
80 0 my $code = $generator->( map "\$_[$_]", 0 .. 100 );
81 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
82 0 *{"$caller\::$subname"} = eval "sub $prototype { $code }";
83 0 $class->_clean( $caller, $subname );
84 }
85  
86 2 ▶sub _clean {
Subroutine _clean: called CC=1 SCAR=0.0
87 2 my ( $class, $caller, $subname ) = ( shift, @_ );
88 2 'namespace::clean'->import( -cleanee => $caller, $subname ) if DO_CLEAN;
89 }
90  
91 2 ▶sub _parse {
Subroutine _parse: called CC=23 SCAR=35.6
92 2 my ( $class, $opt ) = ( shift, @_ );
93 my ( $caller, $subname, $prototype, $generator ) =
94 2 @{$opt}{qw/ caller subname prototype generator /};
95
96 2 require Parse::Keyword;
97 2 require PPI;
98 2 my $str = Parse::Keyword::lex_peek( 1000 );
99 2 my $ppi = 'PPI::Document'->new( \$str );
100 2 my $list = $ppi->find_first( 'Structure::List' );
101 2 my @tokens = $list->find_first( 'Statement::Expression' )->children;
102 2 my $length = 2;
103
104 2 my @args = undef;
105 2 ▶ while ( my $t = shift @tokens ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $t = shift @tokens) 8 2 10
106 8 $length += length( "$t" );
107
108 8 ▶ if ( $t->isa( 'PPI::Token::Operator' ) and $t =~ m{\A(,|\=\>)\z} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($t->isa('PPI::Token::Operator') and $t =~ /\A(,|\=\>)\z/) { } 2 6 8
elsif (defined $args[-1] or not $t->isa('PPI::Token::Whitespace')) { } 4 2 6
Condition66%2/3
$t->isa("PPI::Token::Operator") and $t =~ /\A(,|\=\>)\z/
!ll&&!rl&&r
602
MC/DC50%1/2
$t->isa("PPI::Token::Operator") and $t =~ /\A(,|\=\>)\z/
$t->isa("PPI::Token::Operator") $t =~ /\A(,|\=\>)\z/
Truth table66%2/3
A and B
A= $t->isa("PPI::Token::Operator")B= $t =~ /\A(,|\=\>)\z/
ABresult
0X0
100
111
109 2 push @args, undef;
110 }
111 ▶ elsif ( defined $args[-1] or not $t->isa( 'PPI::Token::Whitespace' ) ) {
Condition66%2/3
defined $args[-1] or not $t->isa("PPI::Token::Whitespace")
l!l&&r!l&&!r
042
MC/DC50%1/2
defined $args[-1] or not $t->isa("PPI::Token::Whitespace")
defined $args[-1] not $t->isa("PPI::Token::Whitespace")
Truth table66%2/3
A or not(B)
A= defined $args[-1]B= not $t->isa("PPI::Token::Whitespace")
ABresult
1X1
011
000
112 1 ▶ no warnings qw(uninitialized);
Subroutine BEGIN: called CC=1 SCAR=0.0
113 4 $args[-1] .= "$t";
114 }
115 }
116 2 ▶ pop @args unless defined $args[-1];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args[-1] 0 2 2
117
118 2 ▶ if ( $prototype =~ /\A\((.+)\)\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prototype =~ /\A\((.+)\)\z/) 2 0 2
119 2 my $i = 0;
120 2 local $_ = $1;
121 2 my $saw_semicolon = 0;
122 2 my $saw_slurpy = 0;
123 2 ▶ while ( length ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $_) 4 2 6
124 5 my $backslashed = 0;
125 5 my $chars = '';
126
127 5 ▶ if ( /\A;/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\A;/) 1 4 5
128 1 $saw_semicolon++;
129 1 s/\A.//;
130 1 redo;
131 }
132
133 4 ▶ if ( /\A\\/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\A\\/) 0 4 4
134 0 $backslashed++;
135 0 s/\A.//;
136 }
137
138 4 ▶ if ( /\A\[(.+?)\]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\A\[(.+?)\]/) { } 0 4 4
139 0 $chars = $1;
140 0 s/\A\[(.+?)\]//;
141 }
142 else {
143 4 $chars = substr $_, 0, 1;
144 4 s/\A.//;
145 }
146
147 4 ▶ if (!$saw_semicolon) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($saw_semicolon) 3 1 4
148 3 ▶ $#args >= $i
Branch: 1 branch
BranchTrueFalseTotal
$#args >= $i or croak("Not enough arguments for macro $subname$prototype") 3 0 3
149 or croak "Not enough arguments for macro $subname$prototype";
150 }
151
152 4 my $arg = $args[$i];
153 4 ▶ if ( $backslashed and $chars eq '@' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($backslashed and $chars eq '@') { } 0 4 4
elsif ($backslashed and $chars eq '%') { } 0 4 4
elsif ($chars =~ /[\@%]/) { } 0 4 4
Condition33%1/3
$backslashed and $chars eq "\@"
!ll&&!rl&&r
400
MC/DC0%0/2
$backslashed and $chars eq "\@"
$backslashed $chars eq "\@"
Truth table33%1/3
A and B
A= $backslashedB= $chars eq "\@"
ABresult
0X0
100
111
154 0 ▶ $arg =~ /\A\s*\@/
Branch: 1 branch
BranchTrueFalseTotal
$arg =~ /\A\s*\@/ or croak("Expected array for argument $i to macro $subname$prototype; got: $arg") 0 0 0
155 or croak "Expected array for argument $i to macro $subname$prototype; got: $arg";
156 }
157 ▶ elsif ( $backslashed and $chars eq '%' ) {
Condition33%1/3
$backslashed and $chars eq "%"
!ll&&!rl&&r
400
MC/DC0%0/2
$backslashed and $chars eq "%"
$backslashed $chars eq "%"
Truth table33%1/3
A and B
A= $backslashedB= $chars eq "%"
ABresult
0X0
100
111
158 0 ▶ $arg =~ /\A\s*\%/
Branch: 1 branch
BranchTrueFalseTotal
$arg =~ /\A\s*\%/ or croak("Expected hash for argument $i to macro $subname$prototype; got: $arg") 0 0 0
159 or croak "Expected hash for argument $i to macro $subname$prototype; got: $arg";
160 }
161 elsif ( $chars =~ /[@%]/ ) {
162 0 $saw_slurpy++;
163 }
164
165 4 $i++;
166 }
167
168 2 ▶ if ( $#args >= $i and !$saw_slurpy ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#args >= $i and not $saw_slurpy) 0 2 2
Condition33%1/3
$#args >= $i and not $saw_slurpy
!ll&&!rl&&r
200
MC/DC0%0/2
$#args >= $i and not $saw_slurpy
$#args >= $i not $saw_slurpy
Truth table33%1/3
A and not(B)
A= $#args >= $iB= not $saw_slurpy
ABresult
0X0
100
111
169 0 croak "Too many arguments for macro $subname$prototype";
170 }
171 }
172
173 2 Parse::Keyword::lex_read( $length );
174 2 Parse::Keyword::lex_stuff( sprintf ' && do { %s }', $generator->(@args) );
175 2 return \&Sub::Boolean::truthy; # will never be called. sigh.
176 }
177  
178 1 1;
179  
180 __END__
↑ Summary