File Coverage

lib/CGI/ValidOp/Check/length.pm
Criterion Covered Total %
statement 28 29 96.5
branch 20 22 90.9
condition 24 30 80.0
subroutine 6 6 100.0
pod 0 1 0.0
total 78 88 88.6


line stmt bran cond sub pod time code
1             package CGI::ValidOp::Check::length;
2 1     1   7 use strict;
  1         2  
  1         44  
3 1     1   5 use warnings;
  1         3  
  1         39  
4              
5 1     1   5 use base qw/ CGI::ValidOp::Check /;
  1         2  
  1         560  
6 1     1   6 use Carp;
  1         3  
  1         429  
7              
8             sub default {
9 22     22 0 31 my $self = shift;
10             sub {
11             # this code is brutally verbose, but all the tests pass
12 22     22   63 my( $value, $min, $max ) = @_;
13            
14 22         56 $self->allow_tainted( 1 );
15 22 50       62 return $self->pass unless defined $value;
16              
17             # length
18             # length(0)
19             # length(0,0)
20 22 100 100     78 return $self->pass( $value ) unless $min or $max;
21              
22             # length(3)
23             # length(3,3)
24 19 100 100     305 if(( $min and ! defined $max ) or $min and $min == $max ) {
    100 100        
    100 66        
    100 66        
    50 100        
      66        
      100        
      66        
      33        
25 6 100       51 return $self->pass( $value ) if $value =~ /^.{$min}$/;
26 4         23 return $self->fail( "\$label length must be exactly $min characters long." );
27             }
28             # length(3,0)
29             elsif( $min and defined $max and $max == 0 ) {
30 3 100       40 return $self->pass( $value ) if $value =~ /^.{$min,}$/;
31 1         6 return $self->fail( "\$label length must be at least $min characters long." );
32             }
33             # length(6,3)
34             elsif( $min and defined $max and $min > $max ) {
35 1         287 croak "Length 'min' must be less than 'max.'"
36             }
37             # length(0,3)
38             elsif( $min == 0 and $max ) {
39 3 100       37 return $self->pass( $value ) if $value =~ /^.{$min,$max}$/;
40 1         6 return $self->fail( "\$label length must be at most $max characters long." );
41             }
42             # length(3,6)
43             elsif( $min and $max ) {
44 6 100       63 return $self->pass( $value ) if $value =~ /^.{$min,$max}$/;
45 2         13 return $self->fail( "\$label length must be between $min and $max characters long." );
46             }
47 0           croak 'Something has gone horribly wrong with length check.';
48             }
49 22         143 }
50              
51             1;
52              
53              
54             __END__