Test/Tolerant.pm

filter: Statementstmt 100% 36 / 36 Branchbran 64.2% 9 / 14 Conditioncond 66.6% 6 / 9 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 100% 8 / 8 Podpod 100% 1 / 1 totaltotal 84.9% 62 / 73 CC 11 SCAR 20.7
CC
11
Cov
86%
is_tol
12.3
BEGIN
1.0
BEGIN
1.0
CRAP 7.9
« Number/Tolerant.pm ↑ Summary Number/Tolerant/Type/offset.pm »
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Test::Tolerant 1.711; # TRIAL
4 # ABSTRACT: test routines for testing numbers against tolerances
5  
6 #pod =head1 SYNOPSIS
7 #pod
8 #pod use Test::More;
9 #pod use Test::Tolerant;
10 #pod
11 #pod my $total = rand(6) + rand(6) + rand(6);
12 #pod is_tol(10, [ qw( 3 to 18 ) ], "got an acceptable result from random dice");
13 #pod
14 #pod done_testing;
15 #pod
16 #pod =head1 FUNCTIONS
17 #pod
18 #pod =head2 is_tol
19 #pod
20 #pod is_tol($have, $want_spec, $comment);
21 #pod
22 #pod C<is_tol> is the only routine provided by Test::Tolerant, and is exported by
23 #pod default. It beahves like C<L<is|Test::More/is>> from Test::More, asserting
24 #pod that two values must be equal, but it will always use numeric equality, and the
25 #pod second argument is not always used as the right hand side of comparison
26 #pod directly, but it used to produce a L<Number::Tolerant> to compare to.
27 #pod
28 #pod C<$have_spec> can be:
29 #pod
30 #pod * a Number::Tolerant object
31 #pod * an arrayref of args to Number::Tolerant->new
32 #pod * a string to be passed to Number::Tolerant->from_string
33 #pod * a literal number falls under this group
34 #pod
35 #pod If the value is outside of spec, you'll get a diagnostic message something like
36 #pod this:
37 #pod
38 #pod given value is outside acceptable tolerances
39 #pod have: 3
40 #pod want: 5 < x
41 #pod
42 #pod =cut
43  
44 1 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
45 1 ▶use Number::Tolerant qw(tolerance);
Subroutine BEGIN: called CC=1 SCAR=0.0
46 1 ▶use Scalar::Util qw(blessed looks_like_number reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
47 1 ▶use Test::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 1 ▶use Sub::Exporter -setup => {
Subroutine BEGIN: called CC=1 SCAR=0.0
50 exports => [ qw(is_tol) ],
51 groups => [
52 default => [ qw(is_tol) ],
53 ],
54 1 };
55  
56 1 my $Test = Test::Builder->new;
57  
58 4 ▶sub is_tol {
Subroutine is_tol: called CC=11 SCAR=25.1
Pod documented
59 4 my ($have, $spec, $desc) = @_;
60  
61 4 my $want;
62  
63 4 ▶ if (blessed $spec and $spec->isa('Number::Tolerant')) {
Branch: 3 branches
BranchTrueFalseTotal
if (builtin::blessed($spec) and $spec->isa('Number::Tolerant')) { } 1 3 4
elsif (ref $spec and not builtin::blessed($spec) and builtin::reftype($spec) eq 'ARRAY') { } 1 2 3
elsif (not ref $spec) { } 2 0 2
Condition66%2/3
builtin::blessed($spec) and $spec->isa("Number::Tolerant")
!ll&&!rl&&r
301
MC/DC50%1/2
builtin::blessed($spec) and $spec->isa("Number::Tolerant")
builtin::blessed($spec) $spec->isa("Number::Tolerant")
Truth table66%2/3
A and B
A= builtin::blessed($spec)B= $spec->isa("Number::Tolerant")
ABresult
0X0
100
111
64 1 $want = $spec;
65 ▶ } elsif (ref $spec and not(blessed $spec) and reftype $spec eq 'ARRAY') {
Condition66%4/6
ref $spec and not builtin::blessed($spec) and builtin::reftype($spec) eq "ARRAY"
!ll&&!rl&&r
201
ref $spec and not builtin::blessed($spec)
!ll&&!rl&&r
201
MC/DC33%1/3
ref $spec and not builtin::blessed($spec) and builtin::reftype($spec) eq "ARRAY"
ref $spec not builtin::blessed($spec) builtin::reftype($spec) eq "ARRAY"
Truth table50%2/4
A and not(B) and C
A= ref $specB= not builtin::blessed($spec)C= builtin::reftype($spec) eq "ARRAY"
ABCresult
0XX0
10X0
1100
1111
66 1 $want = tolerance(@$spec);
67 } elsif (! ref $spec) {
68 2 $want = Number::Tolerant->from_string($spec);
69 }
70  
71 4 ▶ Carp::croak("couldn't build a tolerance from $spec") unless defined $want;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $want 0 4 4
72  
73 4 ▶ return 1 if $Test->ok($have == $want, $desc);
Branch: 1 branch
BranchTrueFalseTotal
if $Test->ok($have == $want, $desc) 1 3 4
74  
75 # XXX: make this work -- rjbs, 2010-11-29
76 3 my $cmp = $have <=> $want;
77 3 ▶ my $why = $cmp == -1 ? "is below"
Branch: 2 branches
BranchTrueFalseTotal
$cmp == -1 ? : 3 0 3
$cmp == 1 ? : 0 0 0
78 : $cmp == 1 ? "is above"
79 : "is outside";
80  
81 3 $Test->diag("given value $why acceptable tolerances");
82 3 $Test->diag(sprintf "%8s: %s\n%8s: %s\n", have => $have, want => $want);
83  
84 3 return 0;
85 }
86  
87 1 1;
88  
89 __END__
« Number/Tolerant.pm ↑ Summary Number/Tolerant/Type/offset.pm »