File Coverage

blib/lib/Repl/Spec/Type/StringEnumType.pm
Criterion Covered Total %
statement 23 23 100.0
branch 2 2 100.0
condition 1 3 33.3
subroutine 6 6 100.0
pod 3 3 100.0
total 35 37 94.5


line stmt bran cond sub pod time code
1             =head1 NAME
2            
3             Repl::Spec::Type::StringEnumType - A parameter guard for strings that can
4             have a value from a restricted set of string constants.
5            
6             =head1 SYNOPSIS
7            
8             This type guard ensures that a string parameter was passed by the user
9             matching a string out of a set of string constants.
10            
11             =head1 DESCRIPTION
12            
13             =head1 Methods
14            
15             =over 4
16            
17             =item C
18            
19             A set of strings from which the values are choosen.
20            
21             =item C
22            
23             Parameters: A single expression.
24             Returns: The string value.
25            
26             =item C
27            
28             =head1 SEE ALSO
29            
30             L
31             L
32             L
33             L
34             L
35             L
36             L
37             L
38            
39             =cut
40            
41             package Repl::Spec::Type::StringEnumType;
42            
43 2     2   2463 use strict;
  2         4  
  2         86  
44 2     2   12 use warnings;
  2         4  
  2         62  
45 2     2   11 use Carp;
  2         4  
  2         1322  
46            
47             # Parameters:
48             # - A list of string constants.
49             sub new
50             {
51 2     2 1 14 my $invocant = shift;
52 2   33     8 my $class = ref($invocant) || $invocant;
53            
54 2         7 my @values = @_;
55            
56 2         6 my $self= {VALUES=>\@values};
57 2         7 return bless $self, $class;
58             }
59            
60             sub guard
61             {
62 9     9 1 13 my $self = shift;
63 9         17 my $arg = shift;
64 9         14 my $values = $self->{VALUES};
65            
66 9         23 foreach my $enum (@$values)
67             {
68 13 100       44 return $arg if($enum eq $arg);
69             }
70 1         6 croak sprintf("Expected %s but received '%s'.", $self->name(), $arg);
71             }
72            
73             sub name
74             {
75 3     3 1 5 my $self = shift;
76 3         5 my $values = $self->{VALUES};
77            
78 3         260 return sprintf('enum %s', join("|", @$values));
79             }
80            
81             1;