File Coverage

blib/lib/PerlX/Assert/Keyword.pm
Criterion Covered Total %
statement 46 48 95.8
branch 12 14 85.7
condition 5 7 71.4
subroutine 10 10 100.0
pod n/a
total 73 79 92.4


line stmt bran cond sub pod time code
1 4     4   53 use 5.012000;
  4         9  
  4         173  
2 4     4   18 use strict;
  4         4  
  4         116  
3 4     4   16 use warnings;
  4         5  
  4         99  
4 4     4   21 no warnings qw( uninitialized void once );
  4         5  
  4         130  
5              
6 4     4   1705 use Keyword::Simple ();
  4         101699  
  4         89  
7 4     4   26 use PerlX::Assert ();
  4         15  
  4         1663  
8              
9             package PerlX::Assert::Keyword;
10              
11             our $AUTHORITY = 'cpan:TOBYINK';
12             our $VERSION = '0.904';
13             our @ISA = qw( PerlX::Assert );
14              
15             sub _install_assert
16             {
17 9     9   11 my $class = shift;
18 9         11 my ($subname, $globals) = @_;
19 9         11 my $caller = $globals->{into};
20 9         10 my $active = $globals->{check};
21              
22             Keyword::Simple::define($subname, sub
23             {
24 34     34   467 my $ref = shift;
25 34         48 _eat_space($ref);
26              
27 34         29 my $name;
28 34 100       77 if ($$ref =~ /\A(qq\b|q\b|'|")/)
29             {
30 16         1318 require Text::Balanced;
31 16         15684 $name = Text::Balanced::extract_quotelike($$ref);
32 16         778 _eat_space($ref);
33            
34 16 100       39 if ($$ref =~ /\A,/)
35             {
36 8         13 substr($$ref, 0, 1) = '';
37 8         11 _eat_space($ref);
38 8 50       18 if ($$ref =~ /\A\{/)
39             {
40 0         0 require Carp;
41 0         0 Carp::croak("Unexpected comma between assertion name and block");
42             };
43             }
44             }
45            
46 34         167 substr($$ref, 0, 0) = $class->_injection(
47             $active,
48             $name,
49             scalar($$ref =~ /\A\{/),
50             );
51 9         37 });
52             }
53            
54             sub _eat_space
55             {
56 58     58   57 my $ref = shift;
57 58         48 my $X;
58 58   100     364 while (
      50        
      66        
59             ($$ref =~ m{\A( \s+ )}x and $X = 1)
60             or ($$ref =~ m{\A\#} and $X = 2)
61             ) {
62 50 50       340 $X==2
63             ? ($$ref =~ s{\A\#.+?\n}{}sm)
64             : (substr($$ref, 0, length($1)) = '');
65             }
66 58         72 return;
67             }
68              
69             sub _injection
70             {
71 34     34   28 shift;
72 34         45 my ($active, $name, $do) = @_;
73 34 100       54 $do = $do ? "do " : "";
74            
75 34 100       245 return "() and $do"
76             if not $active;
77            
78 29 100       1067 return "die sprintf q[Assertion failed: %s], $name unless $do"
79             if defined $name;
80            
81 13         562 return "die q[Assertion failed] unless $do";
82             }
83              
84             __PACKAGE__
85             __END__