File Coverage

blib/lib/Repl/Spec/Type/IntegerRangeType.pm
Criterion Covered Total %
statement 20 35 57.1
branch 0 4 0.0
condition 1 6 16.6
subroutine 5 7 71.4
pod 3 3 100.0
total 29 55 52.7


line stmt bran cond sub pod time code
1             =head1 NAME
2            
3             Repl::Spec::Type::IntegerRangeType - A parameter guard for integer ranges.
4            
5             =head1 SYNOPSIS
6            
7             This type guard ensures that an integer, range bound parameter was passed by the user.
8            
9             =head1 DESCRIPTION
10            
11             =head1 Methods
12            
13             =over 4
14            
15             =item C
16            
17             Parameters: from and to to indicate the range.
18            
19             =item C
20            
21             Parameters: A single expression.
22             Returns: The integer value.
23            
24             =item C
25            
26             =head1 SEE ALSO
27            
28             L
29             L
30             L
31             L
32             L
33             L
34             L
35             L
36            
37             =cut
38            
39             package Repl::Spec::Type::IntegerRangeType;
40            
41 1     1   5 use strict;
  1         1  
  1         34  
42 1     1   5 use warnings;
  1         3  
  1         22  
43 1     1   4 use Carp;
  1         2  
  1         65  
44            
45 1     1   6 use Repl::Spec::Type::IntegerType;
  1         1  
  1         244  
46            
47             our @ISA = qw(Repl::Spec::Type::IntegerType);
48            
49             # Expects two integer arguments, the range of the integer.
50             # - from
51             # - to
52             sub new
53             {
54 5     5 1 6 my $invocant = shift;
55 5   33     18 my $class = ref($invocant) || $invocant;
56            
57 5         6 my $from = shift;
58 5         6 my $to = shift;
59            
60             # Invoke super constructor.
61 5         11 my $self= Repl::Spec::Type::IntegerType::new($class);
62 5         19 $self->{FROM} = $from;
63 5         6 $self->{TO} = $to;
64             # We don't have to bless.
65 5         253 return $self;
66             }
67            
68             sub guard
69             {
70 0     0 1   my $self = shift;
71 0           my $arg = shift;
72 0           my $from = $self->{FROM};
73 0           my $to = $self->{TO};
74            
75 0           eval {$arg = $self->SUPER::guard($arg)};
  0            
76 0 0         carp $@ if $@;
77 0 0 0       if($arg < $from || $arg > $to)
78             {
79 0           croak sprintf("Integer out of range. Expected %s but received '%s'.", $self->name(), $arg);
80             }
81             else
82             {
83 0           return $arg;
84             }
85 0           croak sprintf("Expected type integer but received '%s'.", $arg);
86             }
87            
88             sub name
89             {
90 0     0 1   my $self = shift;
91            
92 0           my $from = $self->{FROM};
93 0           my $to = $self->{TO};
94            
95 0           return sprintf("integer range %d..%d", $from, $to);
96             }
97            
98             1;