File Coverage

lib/Unexpected/Types.pm
Criterion Covered Total %
statement 25 25 100.0
branch n/a
condition n/a
subroutine 9 9 100.0
pod n/a
total 34 34 100.0


line stmt bran cond sub pod time code
1             package Unexpected::Types;
2              
3 4     4   13 use strict;
  4         5  
  4         86  
4 4     4   16 use warnings;
  4         5  
  4         88  
5              
6 4     4   11 use English qw( -no_match_vars );
  4         5  
  4         21  
7 4     4   1249 use Module::Runtime qw( is_module_name require_module );
  4         4  
  4         28  
8 4     4   163 use Scalar::Util qw( blessed );
  4         5  
  4         235  
9 4         96 use Type::Library -base, -declare =>
10             qw( LoadableClass NonEmptySimpleStr
11             NonNumericSimpleStr NonZeroPositiveInt
12             NonZeroPositiveNum PositiveInt PositiveNum
13 4     4   1729 RequestFactory SimpleStr Tracer );
  4         58118  
14 4         30 use Type::Utils qw( as coerce extends from
15 4     4   5343 inline_as message subtype via where );
  4         12590  
16 4     4   2457 use Unexpected::Functions qw( inflate_message );
  4         4  
  4         29  
17              
18 4     4   12 BEGIN { extends 'Types::Standard' };
19              
20             my $LOADABLE_CLASS_ERROR;
21              
22             $Error::TypeTiny::CarpInternal{ 'Sub::Quote' }++;
23             $Error::TypeTiny::CarpInternal{ 'Unexpected::TraitFor::Throwing' }++;
24              
25             # Private functions
26             my $_constraint_for_loadable_class = sub {
27             my $class = shift; is_module_name( $class ) or return 0;
28              
29             local $EVAL_ERROR; eval { require_module( $class ) };
30              
31             $LOADABLE_CLASS_ERROR = $EVAL_ERROR;
32              
33             return $EVAL_ERROR ? 0 : 1;
34             };
35              
36             my $_exception_message_for_object_reference = sub {
37             return inflate_message( 'String [_1] is not an object reference', $_[ 0 ] );
38             };
39              
40             my $_exception_message_for_req_factory = sub {
41             blessed $_[ 0 ] and return inflate_message
42             'Object [_1] is missing the new_from_simple_request method',
43             blessed $_[ 0 ];
44              
45             return $_exception_message_for_object_reference->( $_[ 0 ] );
46             };
47              
48             my $_exception_message_for_tracer = sub {
49             blessed $_[ 0 ] and return inflate_message
50             'Object [_1] is missing a frames method', blessed $_[ 0 ];
51              
52             return $_exception_message_for_object_reference->( $_[ 0 ] );
53             };
54              
55             # Types
56             subtype NonEmptySimpleStr, as Str,
57             inline_as {
58             $_[ 0 ]->parent->inline_check( $_ )
59             ." and length $_ > 0 and length $_ < 255 and $_ !~ m{ [\\n] }mx" },
60             message {
61             inflate_message
62             ( 'Attribute value [_1] is not a non empty simple string', $_ ) },
63             where { length $_ > 0 and length $_ < 255 and $_ !~ m{ [\n] }mx };
64              
65             subtype NonZeroPositiveInt, as Int,
66             inline_as { $_[ 0 ]->parent->inline_check( $_ )." and $_ > 0" },
67             message {
68             inflate_message
69             ( 'Attribute value [_1] is not a non zero positive integer', $_ ) },
70             where { $_ > 0 };
71              
72             subtype NonZeroPositiveNum, as Num,
73             inline_as { $_[ 0 ]->parent->inline_check( $_ )." and $_ > 0" },
74             message {
75             inflate_message
76             ( 'Attribute value [_1] is not a non zero positive number', $_ ) },
77             where { $_ > 0 };
78              
79             subtype PositiveInt, as Int,
80             inline_as { $_[ 0 ]->parent->inline_check( $_ )." and $_ >= 0" },
81             message { inflate_message
82             ( 'Attribute value [_1] is not a positive integer', $_ ) },
83             where { $_ >= 0 };
84              
85             subtype PositiveNum, as Num,
86             inline_as { $_[ 0 ]->parent->inline_check( $_ )." and $_ >= 0" },
87             message { inflate_message
88             ( 'Attribute value [_1] is not a positive number', $_ ) },
89             where { $_ >= 0 };
90              
91             subtype RequestFactory, as Object,
92             inline_as { $_[ 0 ]->parent->inline_check( $_ )
93             ." and $_->can( 'new_from_simple_request' )" },
94             message { $_exception_message_for_req_factory->( $_ ) },
95             where { $_->can( 'new_from_simple_request' ) };
96              
97             subtype SimpleStr, as Str,
98             inline_as { $_[ 0 ]->parent->inline_check( $_ )
99             ." and length $_ < 255 and $_ !~ m{ [\\n] }mx" },
100             message { inflate_message
101             ( 'Attribute value [_1] is not a simple string', $_ ) },
102             where { length $_ < 255 and $_ !~ m{ [\n] }mx };
103              
104             subtype Tracer, as Object,
105             inline_as { $_[ 0 ]->parent->inline_check( $_ )." and $_->can( 'frames' )" },
106             message { $_exception_message_for_tracer->( $_ ) },
107             where { $_->can( 'frames' ) };
108              
109              
110             subtype LoadableClass, as NonEmptySimpleStr,
111             message { inflate_message( 'String [_1] is not a loadable class: [_2]',
112             $_, $LOADABLE_CLASS_ERROR ) },
113             where { $_constraint_for_loadable_class->( $_ ) };
114              
115             subtype NonNumericSimpleStr, as SimpleStr,
116             inline_as { $_[ 0 ]->parent->inline_check( $_ )." and $_ !~ m{ \\d+ }mx" },
117             message {
118             inflate_message
119             ( 'Attribute value [_1] is not a non numeric simple string', $_ ) },
120             where { $_ !~ m{ \d+ }mx };
121              
122             1;
123              
124             __END__