File Coverage

blib/lib/Specio/Constraint/Role/DoesType.pm
Criterion Covered Total %
statement 47 47 100.0
branch 13 14 92.8
condition 5 6 83.3
subroutine 11 11 100.0
pod n/a
total 76 78 97.4


line stmt bran cond sub pod time code
1              
2             use strict;
3 4     4   34 use warnings;
  4         8  
  4         106  
4 4     4   18  
  4         6  
  4         141  
5             our $VERSION = '0.48';
6              
7             use Role::Tiny;
8 4     4   20 use Scalar::Util qw( blessed );
  4         6  
  4         20  
9 4     4   532 use Specio::PartialDump qw( partial_dump );
  4         8  
  4         200  
10 4     4   24 use Storable qw( dclone );
  4         12  
  4         207  
11 4     4   29  
  4         10  
  4         151  
12             use Specio::Constraint::Role::Interface;
13 4     4   20 with 'Specio::Constraint::Role::Interface';
  4         6  
  4         1935  
14              
15             {
16             ## no critic (Subroutines::ProtectPrivateSubs)
17             my $attrs = dclone( Specio::Constraint::Role::Interface::_attrs() );
18             ## use critic
19              
20             for my $name (qw( parent _inline_generator )) {
21             $attrs->{$name}{init_arg} = undef;
22             $attrs->{$name}{builder}
23             = $name =~ /^_/ ? '_build' . $name : '_build_' . $name;
24             }
25              
26             $attrs->{role} = {
27             isa => 'Str',
28             required => 1,
29             };
30              
31             ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
32             return $attrs;
33             }
34 14     14   26 }
35              
36             ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
37             my $self = shift;
38             my $generator = shift;
39              
40 10     10   17 my $type = ( split /::/, blessed $self)[-1];
41 10         22 my $role = $self->role;
42             my $allow_classes = $self->_allow_classes;
43 10         49  
44 10         28 unless ( defined $generator ) {
45 10         43 $generator = sub {
46             shift;
47 10 50       27 my $value = shift;
48              
49 102     102   163 return "An undef will never pass an $type check (wants $role)"
50 102         144 unless defined $value;
51              
52 102 100       286 if ( ref $value && !blessed $value ) {
53             my $dump = partial_dump($value);
54             return
55 98 100 100     494 "An unblessed reference ($dump) will never pass an $type check (wants $role)";
56 16         64 }
57              
58 16         109 if ( !blessed $value) {
59             return
60             "An empty string will never pass an $type check (wants $role)"
61 82 100       222 unless length $value;
62              
63 34 100       99 if (
64             $value =~ /\A
65             \s*
66 30 100       151 -?[0-9]+(?:\.[0-9]+)?
67             (?:[Ee][\-+]?[0-9]+)?
68             \s*
69             \z/xs
70             ) {
71             return
72             "A number ($value) will never pass an $type check (wants $role)";
73             }
74              
75 17         103 if ( !$allow_classes ) {
76             my $dump = partial_dump($value);
77             return
78 13 100       31 "A plain scalar ($dump) will never pass an $type check (wants $role)";
79 7         19 }
80             }
81 7         44  
82             my $got = blessed $value;
83             $got ||= $value;
84              
85 54         123 return "The $got class does not consume the $role role";
86 54   66     144 };
87             }
88 54         293  
89 10         84 return sub { $generator->( undef, @_ ) };
90             }
91             ## use critic
92 10     102   40  
  102         3556  
93             1;
94              
95             # ABSTRACT: Provides a common implementation for Specio::Constraint::AnyDoes and Specio::Constraint::ObjectDoes
96              
97              
98             =pod
99              
100             =encoding UTF-8
101              
102             =head1 NAME
103              
104             Specio::Constraint::Role::DoesType - Provides a common implementation for Specio::Constraint::AnyDoes and Specio::Constraint::ObjectDoes
105              
106             =head1 VERSION
107              
108             version 0.48
109              
110             =head1 DESCRIPTION
111              
112             See L<Specio::Constraint::AnyDoes> and L<Specio::Constraint::ObjectDoes> for
113             details.
114              
115             =head1 SUPPORT
116              
117             Bugs may be submitted at L<https://github.com/houseabsolute/Specio/issues>.
118              
119             =head1 SOURCE
120              
121             The source code repository for Specio can be found at L<https://github.com/houseabsolute/Specio>.
122              
123             =head1 AUTHOR
124              
125             Dave Rolsky <autarch@urth.org>
126              
127             =head1 COPYRIGHT AND LICENSE
128              
129             This software is Copyright (c) 2012 - 2022 by Dave Rolsky.
130              
131             This is free software, licensed under:
132              
133             The Artistic License 2.0 (GPL Compatible)
134              
135             The full text of the license can be found in the
136             F<LICENSE> file included with this distribution.
137              
138             =cut