File Coverage

blib/lib/PerlX/Assert/Keyword.pm
Criterion Covered Total %
statement 45 47 95.7
branch 12 14 85.7
condition 5 7 71.4
subroutine 10 10 100.0
pod n/a
total 72 78 92.3


line stmt bran cond sub pod time code
1 4     4   54 use 5.012000;
  4         12  
2 4     4   18 use strict;
  4         8  
  4         64  
3 4     4   16 use warnings;
  4         8  
  4         97  
4 4     4   17 no warnings qw( uninitialized void once );
  4         8  
  4         96  
5              
6 4     4   1407 use Keyword::Simple ();
  4         88558  
  4         87  
7 4     4   26 use PerlX::Assert ();
  4         14  
  4         1488  
8              
9             package PerlX::Assert::Keyword;
10              
11             our $AUTHORITY = 'cpan:TOBYINK';
12             our $VERSION = '0.905';
13             our @ISA = qw( PerlX::Assert );
14              
15             sub _install_assert
16             {
17 9     9   17 my $class = shift;
18 9         16 my ($subname, $globals) = @_;
19 9         16 my $caller = $globals->{into};
20 9         15 my $active = $globals->{check};
21              
22             Keyword::Simple::define($subname, sub
23             {
24 34     34   484 my $ref = shift;
25 34         82 _eat_space($ref);
26              
27 34         48 my $name;
28 34 100       99 if ($$ref =~ /\A(qq\b|q\b|'|")/)
29             {
30 16         1095 require Text::Balanced;
31 16         13474 $name = Text::Balanced::extract_quotelike($$ref);
32 16         988 _eat_space($ref);
33            
34 16 100       46 if ($$ref =~ /\A,/)
35             {
36 8         16 substr($$ref, 0, 1) = '';
37 8         20 _eat_space($ref);
38 8 50       21 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         171 substr($$ref, 0, 0) = $class->_injection(
47             $active,
48             $name,
49             scalar($$ref =~ /\A\{/),
50             );
51 9         40 });
52             }
53            
54             sub _eat_space
55             {
56 58     58   86 my $ref = shift;
57 58         78 my $X;
58 58   100     376 while (
      50        
      66        
59             ($$ref =~ m{\A( \s+ )}x and $X = 1)
60             or ($$ref =~ m{\A\#} and $X = 2)
61             ) {
62 50 50       372 $X==2
63             ? ($$ref =~ s{\A\#.+?\n}{}sm)
64             : (substr($$ref, 0, length($1)) = '');
65             }
66 58         136 return;
67             }
68              
69             sub _injection
70             {
71 34     34   52 shift;
72 34         69 my ($active, $name, $do) = @_;
73 34 100       68 $do = $do ? "do " : "";
74            
75 34 100       287 return "() and $do"
76             if not $active;
77            
78 29 100       868 return "die sprintf q[Assertion failed: %s], $name unless $do"
79             if defined $name;
80            
81 13         581 return "die q[Assertion failed] unless $do";
82             }
83              
84             __PACKAGE__
85             __END__