File Coverage

blib/lib/Type/Tiny/Eq.pm
Criterion Covered Total %
statement 33 35 94.2
branch 8 16 50.0
condition 1 3 33.3
subroutine 12 13 92.3
pod 6 6 100.0
total 60 73 82.1


line stmt bran cond sub pod time code
1             package Type::Tiny::Eq;
2 2     2   16 use strict;
  2         8  
  2         88  
3 2     2   11 use warnings;
  2         4  
  2         144  
4              
5             our $VERSION = "0.03";
6              
7 2     2   1015 use parent qw( Type::Tiny );
  2         745  
  2         17  
8              
9 2     2   19 sub _croak ($;@) { require Error::TypeTiny; goto \&Error::TypeTiny::croak }
  2         17  
10              
11             sub new {
12 12     12 1 33 my $class = shift;
13              
14 12 50       75 my %opts = ( @_ == 1 ) ? %{ $_[0] } : @_;
  0         0  
15              
16             _croak "Eq type constraints cannot have a parent constraint passed to the constructor"
17 12 50       51 if exists $opts{parent};
18              
19             _croak "Eq type constraints cannot have a constraint coderef passed to the constructor"
20 12 50       50 if exists $opts{constraint};
21              
22             _croak "Eq type constraints cannot have a inlining coderef passed to the constructor"
23 12 50       48 if exists $opts{inlined};
24              
25 12 50       41 _croak "Need to supply value" unless exists $opts{value};
26              
27 12 100       57 _croak "Eq value must be defined" unless defined $opts{value};
28              
29 10         60 $opts{value} = "$opts{value}"; # stringify
30              
31 10         94 return $class->SUPER::new( %opts );
32             }
33              
34 30     30 1 28878 sub value { $_[0]{value} }
35              
36             sub _build_display_name {
37 10     10   131 my $self = shift;
38 10         30 sprintf( "Eq['%s']", $self->value );
39             }
40              
41             sub has_parent {
42 6     6 1 15616 !!0;
43             }
44              
45 10   33 10 1 10281 sub constraint { $_[0]{constraint} ||= $_[0]->_build_constraint }
46              
47             sub _build_constraint {
48 10     10   26 my $self = shift;
49             return sub {
50 0 0   0   0 defined $_ && $_ eq $self->value;
51 10         86 };
52             }
53              
54             sub can_be_inlined {
55 18     18 1 19900 !!1;
56             }
57              
58             sub inline_check {
59 19     19 1 6765 my $self = shift;
60              
61 19         92 my $value = $self->value;
62 19         53 my $code = "(defined($_[0]) && $_[0] eq '$value')";
63              
64 19 50       65 return "do { $Type::Tiny::SafePackage $code }"
65             if $Type::Tiny::AvoidCallbacks; ## no critic (Variables::ProhibitPackageVars)
66 19         114 return $code;
67             }
68              
69             1;
70             __END__