File Coverage

lib/Test/Chai/Core/Assertions/CloseTo.pm
Criterion Covered Total %
statement 26 26 100.0
branch 3 4 75.0
condition 2 3 66.6
subroutine 7 7 100.0
pod 0 1 0.0
total 38 41 92.6


line stmt bran cond sub pod time code
1             package Test::Chai::Core::Assertions::CloseTo;
2 2     2   11 use strict;
  2         4  
  2         53  
3 2     2   11 use warnings;
  2         3  
  2         48  
4 2     2   11 use utf8;
  2         3  
  2         15  
5              
6 2     2   62 use Exporter qw/import/;
  2         4  
  2         116  
7             our @EXPORT_OK = qw/assert_close_to/;
8              
9 2     2   10 use Scalar::Util qw/looks_like_number/;
  2         4  
  2         132  
10              
11 2     2   11 use Test::Chai::Util::Flag qw/flag/;
  2         4  
  2         420  
12              
13             sub assert_close_to {
14 10     10 0 21 my ($self, $expected, $delta, $msg) = @_;
15              
16 10 50       28 flag($self, 'message', $msg) if defined $msg;
17 10         21 my $obj = flag($self, 'object');
18              
19 10         36 ref($self)->new($obj, $msg)->is->a('Num');
20              
21 10 100 66     98 if (!looks_like_number($expected) || !looks_like_number($delta)) {
22 2         9 $self->_fail('the arguments to close_to must be numbers');
23 2         15 return;
24             }
25              
26             $self->assert(
27 8         76 abs($obj - $expected) <= $delta,
28             'expected #{this} to be close to ' . $expected . ' +/- ' . $delta,
29             'expected #{this} not to be close to ' . $expected . ' +/- ' . $delta
30             );
31             }
32              
33             1;