Pcuke/Expectation.pm

filter: Statementstmt 100% 42 / 42 Branchbran 100% 6 / 6 Conditioncond 91.6% 11 / 12 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 100% 11 / 11 Podpod 100% 4 / 4 totaltotal 97.5% 78 / 80 CC 9 SCAR 12.6
CC
9
Cov
98%
_message
7.0
_result
3.0
BEGIN
1.0
CRAP 3.5
« Pcuke.pm ↑ Summary Pcuke/Executor.pm »
1 package Test::Pcuke::Expectation;
2 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 Test::Pcuke::Expectation - expectation on an object
10  
11 Takes an object, checks if some expectation about is correct
12  
13 use Test::Pcuke::Expectation;
14  
15 my $foo = Test::Pcuke::Expectation->new($object);
16 $foo->equals(5);
17 ...
18  
19 =cut
20  
21 2 my %descriptions = (
22 equals => '%OBJECT% is %NOT%equal to %VALUE%',
23 );
24  
25 =head1 METHODS
26  
27 =head2 new
28  
29 =cut
30  
31 7 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
32 7 my ($class, $object, $options) = @_;
33
34 my %instance_data = (
35 _object => $object,
36 _throw => $options->{throw}
37 7 );
38
39 7 return bless \%instance_data, $class;
40 }
41  
42 =head2 is_defined
43  
44 Checks if the object is defined
45  
46 =cut
47  
48 2 ▶sub is_defined {
Subroutine is_defined: called CC=1 SCAR=0.0
Pod documented
49 2 my ($self) = @_;
50 2 return $self->_result( defined $self->_object );
51 }
52  
53 =head2 is_a $class
54  
55 Checks if the object isa $class
56  
57 =cut
58  
59 2 ▶sub is_a {
Subroutine is_a: called CC=1 SCAR=0.0
Pod documented
60 2 my ($self, $class) = @_;
61 2 my $result = eval { $self->_object->isa($class) };
62 2 return $self->_result($result, $class);
63 }
64  
65 =head2 equals $subject
66  
67 Checks if the object eq $subject
68  
69 =cut
70  
71 4 ▶sub equals {
Subroutine equals: called CC=1 SCAR=0.0
Pod documented
72 4 my ($self, $value) = @_;
73 4 $self->_result($self->_object eq $value, $value);
74 };
75  
76  
77 8 ▶sub _message {
Subroutine _message: called CC=7 SCAR=19.5
78 8 my ($self, $result, $value) = @_;
79 8 my ($message, $var);
80
81 8 my ($package, $filename, $line,
82 $subroutine, $hasargs, $wantarray,
83 $evaltext, $is_require, $hints,
84 $bitmask, $hinthash) = caller 2;
85
86 8 $subroutine =~ s/^.*?::(\w+)$/$1/;
87
88 8 ▶ $message = ($descriptions{$subroutine} || "TODO: description for $subroutine");
Condition100%2/2
$descriptions{$subroutine} || "TODO: description for $subroutine"
l!l
44
MC/DC100%1/1
$descriptions{$subroutine} || "TODO: description for $subroutine"
$descriptions{$subroutine}
Truth table100%2/2
A
A= $descriptions{$subroutine}
Aresult
11
00
89
90 8 ▶ $var = ref $self->_object || $self->_object || 'undef';
Condition100%5/5
ref $self->_object || $self->_object || 'undef'
l!l
71
ref $self->_object or $self->_object
l!l&&r!l&&!r
251
MC/DC100%2/2
ref $self->_object || $self->_object || 'undef'
ref $self->_object $self->_object
Truth table100%3/3
A or B
A= ref $self->_objectB= $self->_object
ABresult
1X1
011
000
91 8 $message =~ s/%OBJECT%/$var/g;
92
93 8 ▶ $var = $result ? q{} : 'not ';
Branch: 1 branch
BranchTrueFalseTotal
$result ? : 4 4 8
94 8 $message =~ s/%NOT%/$var/g;
95
96 8 ▶ $var = ref $value || $value || q{};
Condition80%4/5
ref $value || $value || ''
l!l
62
ref $value or $value
l!l&&r!l&&!r
062
MC/DC50%1/2
ref $value || $value || ''
ref $value $value
Truth table66%2/3
A or B
A= ref $valueB= $value
ABresult
1X1
011
000
97 8 $message =~ s/%VALUE%/$var/g;
98
99 8 return $message;
100 }
101  
102 8 ▶sub _result {
Subroutine _result: called CC=3 SCAR=11.0
103 8 my ($self, $result, $value) = @_;
104 8 my $message = $self->_message($result, $value);
105
106 8 ▶ if ( my $exception = $self->_exception ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $exception = $self->_exception) 2 6 8
107 2 ▶ die $exception->new($message)
Branch: 1 branch
BranchTrueFalseTotal
unless $result 1 1 2
108 unless $result;
109 }
110
111 7 return $result;
112 }
113  
114 22 ▶sub _object { return $_[0]->{_object} }
Subroutine _object: called CC=1 SCAR=0.0
115  
116 8 ▶sub _exception {
Subroutine _exception: called CC=1 SCAR=0.0
117 8 my ($self) = @_;
118 8 return $self->{_throw};
119 }
120  
121 #--------------------------------------
122 2 1; # End of Test::Pcuke::Expectation
123 __END__
« Pcuke.pm ↑ Summary Pcuke/Executor.pm »