File Coverage

blib/lib/Types/TypedCodeRef.pm
Criterion Covered Total %
statement 21 21 100.0
branch 2 2 100.0
condition 2 3 66.6
subroutine 7 7 100.0
pod 0 1 0.0
total 32 34 94.1


line stmt bran cond sub pod time code
1             package Types::TypedCodeRef;
2 4     4   903201 use 5.010001;
  4         43  
3 4     4   26 use strict;
  4         25  
  4         106  
4 4     4   24 use warnings;
  4         7  
  4         115  
5 4     4   719 use utf8;
  4         23  
  4         28  
6              
7             our $VERSION = "0.07";
8              
9             use Type::Library (
10 4         54 -base,
11             -declare => qw( TypedCodeRef ),
12 4     4   2040 );
  4         113552  
13              
14 4     4   5996 use Types::TypedCodeRef::Factory;
  4         18  
  4         955  
15              
16             {
17             my $factory =
18             Types::TypedCodeRef::Factory->new(sub_meta_finders => [\&get_sub_meta_from_sub_wrap_in_type]);
19             __PACKAGE__->add_type($factory->create());
20             }
21              
22             sub get_sub_meta_from_sub_wrap_in_type {
23 18     18 0 38 my $typed_code_ref = shift;
24 18 100 66     176 if ( Scalar::Util::blessed($typed_code_ref) && $typed_code_ref->isa('Sub::WrapInType') ) {
25 13         71 return Sub::Meta->new(
26             args => $typed_code_ref->params,
27             returns => $typed_code_ref->returns,
28             is_method => $typed_code_ref->is_method,
29             );
30             }
31 5         15 return;
32             }
33              
34             __PACKAGE__->meta->make_immutable;
35              
36             1;
37              
38             __END__
39              
40             =encoding utf-8
41              
42             =head1 NAME
43              
44             Types::TypedCodeRef - Type constraint for any typed subroutine.
45              
46             =head1 SYNOPSIS
47              
48             use Test2::V0;
49             use Types::TypedCodeRef -types;
50             use Types::Standard -types;
51             use Sub::WrapInType qw( wrap_sub );
52            
53             my $type = TypedCodeRef[ [Int, Int] => Int ];
54             ok $type->check(wrap_sub [Int, Int] => Int, sub { $_[0] + $_[1] });
55             ok !$type->check(0);
56             ok !$type->check([]);
57             ok !$type->check(sub {});
58              
59             =head1 DESCRIPTION
60              
61             Types::TypedCodeRef is type constraint for any typed subroutine (example, generated by Sub::WrapInType).
62              
63             =head1 TYPES
64              
65             =head2 TypedCodeRef[`p, `r]
66              
67             Only accepts typed subroutines.
68             A typed subroutine is a code reference in which the type of the parameter and the type of the return value are defined.
69             Types::TypedCodeRef is designed to inspect typed subroutines generated by Sub::WrapInType, but it is extensible enough to inspect typed subroutines created in other ways as well.
70              
71             The first type parameter is the subroutine parameters type, and the second type parameter is the subroutine return values type.
72              
73             =head1 LICENSE
74              
75             Copyright (C) ybrliiu.
76              
77             This library is free software; you can redistribute it and/or modify
78             it under the same terms as Perl itself.
79              
80             =head1 AUTHOR
81              
82             ybrliiu E<lt>raian@reeshome.orgE<gt>
83              
84             =head1 SEE ALSO
85              
86             L<Sub::WrapInType>
87              
88             L<Type::Tiny>
89              
90             =cut
91