File Coverage

blib/lib/Error/TypeTiny/Assertion.pm
Criterion Covered Total %
statement 48 48 100.0
branch 16 22 72.7
condition 7 12 58.3
subroutine 17 17 100.0
pod 11 11 100.0
total 99 110 90.0


line stmt bran cond sub pod time code
1             package Error::TypeTiny::Assertion;
2              
3 293     293   7468 use 5.008001;
  293         1121  
4 293     293   1847 use strict;
  293         657  
  293         9571  
5 293     293   1485 use warnings;
  293         745  
  293         26282  
6              
7             BEGIN {
8 293     293   1214 $Error::TypeTiny::Assertion::AUTHORITY = 'cpan:TOBYINK';
9 293         288974 $Error::TypeTiny::Assertion::VERSION = '2.010001';
10             }
11              
12             $Error::TypeTiny::Assertion::VERSION =~ tr/_//d;
13              
14             require Error::TypeTiny;
15             our @ISA = 'Error::TypeTiny';
16              
17 378     378 1 1848 sub type { $_[0]{type} }
18 374     374 1 1681 sub value { $_[0]{value} }
19 1050   100 1050 1 6595 sub varname { $_[0]{varname} ||= '$_' }
20 2     2 1 7 sub attribute_step { $_[0]{attribute_step} }
21 2     2 1 9 sub attribute_name { $_[0]{attribute_name} }
22              
23 372     372 1 1427 sub has_type { defined $_[0]{type} }; # sic
24 2     2 1 8 sub has_attribute_step { exists $_[0]{attribute_step} }
25 2     2 1 844 sub has_attribute_name { exists $_[0]{attribute_name} }
26              
27             sub new {
28 439     439 1 262185 my $class = shift;
29 439         2385 my $self = $class->SUPER::new( @_ );
30            
31             # Supported but undocumented parameter is `mgaca`.
32             # This indicates whether Error::TypeTiny::Assertion
33             # should attempt to figure out which attribute caused
34             # the error from Method::Generate::Accessor's info.
35             # Can be set to true/false or not set. If not set,
36             # the current behaviour is true, but this may change
37             # in the future. If set to false, will ignore the
38             # $Method::Generate::Accessor::CurrentAttribute hashref.
39             #
40            
41 439 50 33     1668 if ( ref $Method::Generate::Accessor::CurrentAttribute
      66        
42             and $self->{mgaca} || !exists $self->{mgaca} )
43             {
44 26         145 require B;
45 26         36 my %d = %{$Method::Generate::Accessor::CurrentAttribute};
  26         109  
46 26 50       101 $self->{attribute_name} = $d{name} if defined $d{name};
47 26 50       73 $self->{attribute_step} = $d{step} if defined $d{step};
48            
49 26 100       59 if ( defined $d{init_arg} ) {
    50          
50 21         166 $self->{varname} = sprintf( '$args->{%s}', B::perlstring( $d{init_arg} ) );
51             }
52             elsif ( defined $d{name} ) {
53 5         49 $self->{varname} = sprintf( '$self->{%s}', B::perlstring( $d{name} ) );
54             }
55             } #/ if ( ref $Method::Generate::Accessor::CurrentAttribute...)
56            
57 439         2602 return $self;
58             } #/ sub new
59              
60             sub message {
61 354     354 1 2046 my $e = shift;
62 354 100       1121 $e->varname eq '$_'
63             ? $e->SUPER::message
64             : sprintf( '%s (in %s)', $e->SUPER::message, $e->varname );
65             }
66              
67             sub _build_message {
68 1     1   4 my $e = shift;
69 1 50       5 $e->has_type
70             ? sprintf(
71             '%s did not pass type constraint "%s"',
72             Type::Tiny::_dd( $e->value ), $e->type
73             )
74             : sprintf(
75             '%s did not pass type constraint',
76             Type::Tiny::_dd( $e->value )
77             );
78             } #/ sub _build_message
79              
80             *to_string = sub {
81 348     348   900 my $e = shift;
82 348         1178 my $msg = $e->message;
83            
84 348         1256 my $c = $e->context;
85 348 50 50     2912 $msg .= sprintf( " at %s line %s", $c->{file} || 'file?', $c->{line} || 'NaN' )
      50        
86             if $c;
87            
88 348         1084 my $explain = $e->explain;
89 348 100       779 return "$msg\n" unless @{ $explain || [] };
  348 100       1439  
90            
91 347         2490 $msg .= "\n";
92 347         925 for my $line ( @$explain ) {
93 2133         4046 $msg .= " $line\n";
94             }
95            
96 347         2382 return $msg;
97             }
98             if $] >= 5.008;
99            
100             sub explain {
101 371     371 1 788 my $e = shift;
102 371 100       1150 return undef unless $e->has_type;
103 370         1171 $e->type->validate_explain( $e->value, $e->varname );
104             }
105              
106             1;
107              
108             __END__