File Coverage

blib/lib/Number/Tolerant/Type/to.pm
Criterion Covered Total %
statement 27 27 100.0
branch 13 14 92.8
condition n/a
subroutine 6 6 100.0
pod 3 3 100.0
total 49 50 98.0


line stmt bran cond sub pod time code
1 26     26   151 use strict;
  26         41  
  26         745  
2 26     26   296 use warnings;
  26         48  
  26         748  
3             # ABSTRACT: a tolerance "m to n"
4              
5             package
6             Number::Tolerant::Type::to;
7 26     26   119 use parent qw(Number::Tolerant::Type);
  26         36  
  26         89  
8              
9 32     32 1 35 sub construct { shift;
10 32         81 ($_[0],$_[1]) = sort { $a <=> $b } ($_[0],$_[1]);
  32         83  
11             {
12 32         157 value => ($_[0]+$_[1])/2,
13             variance => $_[1] - ($_[0]+$_[1])/2,
14             min => $_[0],
15             max => $_[1]
16             }
17             }
18              
19             sub parse {
20 20     20 1 40 my ($self, $string, $factory) = @_;
21 20         71 my $number = $self->number_re;
22 20         55 my $X = $self->variable_re;
23              
24 20 100       449 return $factory->new("$1", 'to', "$2")
25             if ($string =~ m!\A($number)\s*<=$X<=\s*($number)\z!);
26 19 100       442 return $factory->new("$2", 'to', "$1")
27             if ($string =~ m!\A($number)\s*>=$X>=\s*($number)\z!);
28 18 100       425 return $factory->new("$1", 'to', "$2")
29             if ($string =~ m!\A($number)\s+to\s+($number)\z!);
30 17         82 return;
31             }
32              
33             sub valid_args {
34 112     112 1 149 my $self = shift;
35              
36 112 100       154 return unless 3 == grep { defined } @_;
  267         510  
37 45 100       106 return unless $_[1] eq 'to';
38              
39 33 50       102 return unless defined (my $from = $self->normalize_number($_[0]));
40 33 100       161 return unless defined (my $to = $self->normalize_number($_[2]));
41              
42 32         90 return ($from, $to);
43             }
44              
45             1;
46              
47             __END__