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   130 use strict;
  26         49  
  26         1068  
2 26     26   133 use warnings;
  26         45  
  26         923  
3             # ABSTRACT: a tolerance "m to n"
4              
5             package
6             Number::Tolerant::Type::to;
7 26     26   128 use parent qw(Number::Tolerant::Type);
  26         44  
  26         127  
8              
9 32     32 1 46 sub construct { shift;
10 32         111 ($_[0],$_[1]) = sort { $a <=> $b } ($_[0],$_[1]);
  32         110  
11             {
12 32         233 value => ($_[0]+$_[1])/2,
13             variance => $_[1] - ($_[0]+$_[1])/2,
14             min => $_[0],
15             max => $_[1]
16             }
17             }
18              
19             sub parse {
20 18     18 1 41 my ($self, $string, $factory) = @_;
21 18         81 my $number = $self->number_re;
22 18         81 my $X = $self->variable_re;
23              
24 18 100       639 return $factory->new("$1", 'to', "$2")
25             if ($string =~ m!\A($number)\s*<=$X<=\s*($number)\z!);
26 17 100       643 return $factory->new("$2", 'to', "$1")
27             if ($string =~ m!\A($number)\s*>=$X>=\s*($number)\z!);
28 16 100       658 return $factory->new("$1", 'to', "$2")
29             if ($string =~ m!\A($number)\s+to\s+($number)\z!);
30 15         104 return;
31             }
32              
33             sub valid_args {
34 90     90 1 142 my $self = shift;
35              
36 90 100       229 return unless 3 == grep { defined } @_;
  223         747  
37 48 100       213 return unless $_[1] eq 'to';
38              
39 33 50       150 return unless defined (my $from = $self->normalize_number($_[0]));
40 33 100       213 return unless defined (my $to = $self->normalize_number($_[2]));
41              
42 32         164 return ($from, $to);
43             }
44              
45             1;
46              
47             __END__