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