blib/lib/Macro/Simple.pm

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