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   163 use strict;
  26         50  
  26         868  
2 26     26   370 use warnings;
  26         57  
  26         872  
3             # ABSTRACT: a tolerance "m to n"
4              
5             package
6             Number::Tolerant::Type::to;
7 26     26   132 use parent qw(Number::Tolerant::Type);
  26         44  
  26         107  
8              
9 32     32 1 49 sub construct { shift;
10 32         107 ($_[0],$_[1]) = sort { $a <=> $b } ($_[0],$_[1]);
  32         102  
11             {
12 32         167 value => ($_[0]+$_[1])/2,
13             variance => $_[1] - ($_[0]+$_[1])/2,
14             min => $_[0],
15             max => $_[1]
16             }
17             }
18              
19             sub parse {
20 21     21 1 45 my ($self, $string, $factory) = @_;
21 21         70 my $number = $self->number_re;
22 21         52 my $X = $self->variable_re;
23              
24 21 100       430 return $factory->new("$1", 'to', "$2")
25             if ($string =~ m!\A($number)\s*<=$X<=\s*($number)\z!);
26 20 100       395 return $factory->new("$2", 'to', "$1")
27             if ($string =~ m!\A($number)\s*>=$X>=\s*($number)\z!);
28 19 100       386 return $factory->new("$1", 'to', "$2")
29             if ($string =~ m!\A($number)\s+to\s+($number)\z!);
30 18         74 return;
31             }
32              
33             sub valid_args {
34 88     88 1 136 my $self = shift;
35              
36 88 100       131 return unless 3 == grep { defined } @_;
  219         440  
37 47 100       135 return unless $_[1] eq 'to';
38              
39 33 50       146 return unless defined (my $from = $self->normalize_number($_[0]));
40 33 100       87 return unless defined (my $to = $self->normalize_number($_[2]));
41              
42 32         99 return ($from, $to);
43             }
44              
45             1;
46              
47             __END__