File Coverage

blib/lib/Devel/Assert.pm
Criterion Covered Total %
statement 36 37 97.3
branch 9 10 90.0
condition 4 5 80.0
subroutine 5 6 83.3
pod 0 2 0.0
total 54 60 90.0


line stmt bran cond sub pod time code
1             package Devel::Assert;
2 7     7   95498 use 5.014;
  7         25  
  7         253  
3 7     7   37 use strict;
  7         11  
  7         1106  
4              
5             our $VERSION = '1.06';
6              
7             our $__ASSERT_GLOBAL = 0;
8              
9             require XSLoader;
10             XSLoader::load('Devel::Assert', $VERSION);
11              
12             sub import {
13 13     13   1586 my ($class, $arg) = @_;
14 13         39 my $caller = caller;
15              
16 13 100       191 $__ASSERT_GLOBAL = 1 if $arg eq 'global';
17              
18 13 100 66     103 my $ref = $arg eq 'off' || !$__ASSERT_GLOBAL && $arg ne 'on' ? \&assert_off : \&assert_on;
19             {
20 7     7   46 no strict 'refs';
  7         12  
  7         2135  
  13         19  
21 13 100       14 *{"${caller}::assert"} = $ref if !defined *{"${caller}::assert"}{CODE};
  12         5588  
  13         1359  
22             }
23             }
24              
25 0     0 0 0 sub assert_off {}
26              
27             sub assert_fail {
28 12     12 0 11423 my ($op, $cop, $upcv) = @_;
29              
30             # idea taken from Zefram's Debug::Show
31             # this code knows too much about B::Deparser internals
32              
33 12 100       47 unless (state $init_done++) {
34 5         43 require B;
35 5         24 require B::Deparse;
36              
37 5         21 require Carp;
38 5         17 $Carp::Internal{'Devel::Assert'}++;
39             }
40              
41 12         437 my $deparser = B::Deparse->new;
42 12         20 $deparser->{curcop} = $cop;
43 12         24 $deparser->{curcv} = $upcv;
44              
45 12         14 my $deparsed;
46             {
47 12         14 local $@;
  12         19  
48 12         46 local $SIG{__DIE__};
49              
50 12   100     21 $deparsed = eval {
51             $deparser->indent($deparser->deparse($op->sibling, 50));
52             } || "0";
53 12 50       85 warn $@ if $@;
54              
55 12         23 $deparsed =~ s/\n[\t ]*/ /g;
56 12         35 $deparsed =~ s/^[(]//;
57 12         51 $deparsed =~ s/[)]$//;
58             }
59              
60 12         222 Carp::confess("Assertion '$deparsed' failed");
61             }
62              
63             1;
64