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   71 use 5.012000;
  4         13  
  4         141  
2 4     4   18 use strict;
  4         8  
  4         124  
3 4     4   17 use warnings;
  4         7  
  4         130  
4 4     4   19 no warnings qw( uninitialized void once );
  4         16  
  4         156  
5              
6 4     4   3192 use Keyword::Simple ();
  4         148965  
  4         185  
7 4     4   42 use PerlX::Assert ();
  4         30  
  4         2259  
8              
9             package PerlX::Assert::Keyword;
10              
11             our $AUTHORITY = 'cpan:TOBYINK';
12             our $VERSION = '0.903';
13             our @ISA = qw( PerlX::Assert );
14              
15             sub _install_assert
16             {
17 9     9   17 my $class = shift;
18 9         20 my ($subname, $globals) = @_;
19 9         16 my $caller = $globals->{into};
20 9         22 my $active = $globals->{check};
21              
22             Keyword::Simple::define($subname, sub
23             {
24 34     34   824 my $ref = shift;
25 34         73 _eat_space($ref);
26              
27 34         41 my $name;
28 34 100       133 if ($$ref =~ /\A(qq\b|q\b|'|")/)
29             {
30 16         3603 require Text::Balanced;
31 16         25517 $name = Text::Balanced::extract_quotelike($$ref);
32 16         1079 _eat_space($ref);
33            
34 16 100       52 if ($$ref =~ /\A,/)
35             {
36 8         15 substr($$ref, 0, 1) = '';
37 8         15 _eat_space($ref);
38 8 50       22 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         270 substr($$ref, 0, 0) = $class->_injection(
47             $active,
48             $name,
49             scalar($$ref =~ /\A\{/),
50             );
51 9         62 });
52             }
53            
54             sub _eat_space
55             {
56 58     58   77 my $ref = shift;
57 58         69 my $X;
58 58   100     492 while (
      50        
      66        
59             ($$ref =~ m{\A( \s+ )}x and $X = 1)
60             or ($$ref =~ m{\A\#} and $X = 2)
61             ) {
62 50 50       517 $X==2
63             ? ($$ref =~ s{\A\#.+?\n}{}sm)
64             : (substr($$ref, 0, length($1)) = '');
65             }
66 58         97 return;
67             }
68              
69             sub _injection
70             {
71 34     34   47 shift;
72 34         65 my ($active, $name, $do) = @_;
73 34 100       81 $do = $do ? "do " : "";
74            
75 34 100       334 return "() and $do"
76             if not $active;
77            
78 29 100       1237 return "die sprintf q[Assertion failed: %s], $name unless $do"
79             if defined $name;
80            
81 13         1655 return "die q[Assertion failed] unless $do";
82             }
83              
84             __PACKAGE__
85             __END__