File Coverage

blib/lib/Repl/Spec/Type/CheckedArrayType.pm
Criterion Covered Total %
statement 28 32 87.5
branch 2 4 50.0
condition 1 3 33.3
subroutine 5 6 83.3
pod 3 3 100.0
total 39 48 81.2


line stmt bran cond sub pod time code
1             =head1 NAME
2            
3             Repl::Spec::Type::CheckedArrayType - A parameter guard for arrays.
4            
5             =head1 SYNOPSIS
6            
7             This type guard ensures that an array was passed containing elements
8             of another nested type guard provided by the user. An example: a list of integers
9             is ensured by creating a CheckedArrayType with nested IntegerType.
10             The guards can be nested arbitrarily to obtain a guard for whatever complex nested type.
11            
12             =head1 DESCRIPTION
13            
14             =head1 Methods
15            
16             =over 4
17            
18             =item C
19            
20             Parameters: A nested guard that will be applied to the elements of the array.
21            
22             =item C
23            
24             Parameters: A single expression.
25             Returns: A new array where the elements are converted by the nested type guard.
26            
27             =item C
28            
29             =head1 SEE ALSO
30            
31             L
32             L
33             L
34             L
35             L
36             L
37             L
38             L
39            
40             =cut
41            
42             package Repl::Spec::Type::CheckedArrayType;
43            
44 1     1   5 use strict;
  1         1  
  1         32  
45 1     1   6 use warnings;
  1         2  
  1         28  
46 1     1   5 use Carp;
  1         1  
  1         323  
47            
48             # Parameters:
49             # - The type of the array elements.
50             sub new
51             {
52 2     2 1 3 my $invocant = shift;
53 2   33     10 my $class = ref($invocant) || $invocant;
54            
55 2         3 my $eltyp = shift;
56            
57 2         5 my $self= {TYPE=>$eltyp};
58 2         155 return bless $self, $class;
59             }
60            
61             sub guard
62             {
63 51     51 1 58 my $self = shift;
64 51         55 my $arg = shift;
65            
66 51         69 my $eltyp = $self->{TYPE};
67            
68 51 50       99 if(ref($arg) eq 'ARRAY')
69             {
70 51         67 my $result = [];
71 51         57 my $idx = 0;
72 51         74 foreach my $el (@$arg)
73             {
74 102         89 my $val;
75 102         97 eval {$val = $eltyp->guard($el)};
  102         230  
76 102 50       179 croak sprintf("Expected %s but the value nr. %d does not comply.\n%s.", $self->name(), $idx, $@)if ($@);
77 102         147 push(@$result, $val);
78 102         166 $idx = $idx + 1;
79             }
80 51         354 return $result;
81             }
82             else
83             {
84 0           croak sprintf("Expected %s but received '%s'.", $self->name(), $arg);
85             }
86             }
87            
88             sub name
89             {
90 0     0 1   my $self = shift;
91 0           my $eltyp = $self->{TYPE};
92 0           return sprintf("ARRAY of %s", $eltyp->name());
93             }
94            
95             1;