Mockify/ReturnValue.pm

filter: Statementstmt 100% 49 / 49 Branchbran 100% 22 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod 87.5% 7 / 8 totaltotal 98.8% 89 / 90 CC 12 SCAR 12.1
CC
12
Cov
100%
call
7.0
thenReturn
2.0
thenReturnArray
2.0
CRAP 3.4
« Mockify/Tools.pm ↑ Summary Mockify/Verify.pm »
1 =pod
2  
3 =head1 Name
4  
5 Test::Mockify::ReturnValue - To define return values
6  
7 =head1 DESCRIPTION
8  
9 Use L<Test::Modify::ReturnValue|Test::Modify::ReturnValue> to define different types of return values. See method description for more details.
10  
11 =head1 METHODS
12  
13 =cut
14 package Test::Mockify::ReturnValue;
15 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 20 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 20 ▶use Test::Mockify::Tools qw (Error);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 159 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
19 159 my $class = shift;
20 159 my $self = bless {
21 }, $class;
22 159 return $self;
23 }
24 =pod
25  
26 =head2 thenReturn
27  
28 The C<thenReturn> method set the return value of C<call>.
29  
30 my $ReturnValue = Test::Mockify::ReturnValue->new();
31 $ReturnValue->thenReturn('Hello World');
32 my $Result = $ReturnValue->call();
33 is($Result, 'Hello World');
34  
35 =cut
36 114 ▶sub thenReturn {
Subroutine thenReturn: called CC=2 SCAR=6.9
Pod documented
37 114 my $self = shift;
38 114 my ($Value) = @_;
39 114 ▶ Error('Return value undefined. Use "thenReturnUndef" if you need to return undef.') unless(defined $Value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Value 1 113 114
40 113 $self->{'Value'} = $Value;
41 }
42 =pod
43  
44 =head2 thenReturnArray
45  
46 The C<thenReturnArray> method sets the return value of C<call> in the way that it will return an Array.
47  
48 my $ReturnValue = Test::Mockify::ReturnValue->new();
49 $ReturnValue->thenReturnArray([1,23]);
50 my @Result = $ReturnValue->call();
51 is_deeply(\@Result, [1,23]);
52  
53 =cut
54 4 ▶sub thenReturnArray {
Subroutine thenReturnArray: called CC=2 SCAR=6.9
Pod documented
55 4 my $self = shift;
56 4 my ($Value) = @_;
57 4 ▶ Error('NoAnArrayRef') unless(ref($Value) eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $Value eq "ARRAY" 2 2 4
58 2 $self->{'ArrayValue'} = $Value;
59 }
60 =pod
61  
62 =head2 thenReturnHash
63  
64 The C<thenReturnArray> method sets the return value of C<call> in the way that it will return a Hash.
65  
66 my $ReturnValue = Test::Mockify::ReturnValue->new();
67 $ReturnValue->thenReturnHash({1 => 23});
68 my %Result = $ReturnValue->call();
69 is_deeply(\%Result, {1 => 23});
70  
71 =cut
72 4 ▶sub thenReturnHash {
Subroutine thenReturnHash: called CC=2 SCAR=6.9
Pod documented
73 4 my $self = shift;
74 4 my ($Value) = @_;
75 4 ▶ Error('NoAHashRef') unless(ref($Value) eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $Value eq "HASH" 2 2 4
76 2 $self->{'HashValue'} = $Value;
77 }
78 =pod
79  
80 =head2 thenReturnUndef
81  
82 The C<thenReturnArray> method sets the return value of C<call> in the way that it will return undef.
83  
84 my $ReturnValue = Test::Mockify::ReturnValue->new();
85 $ReturnValue->thenReturnUndef();
86 my $Result = $ReturnValue->call();
87 is($Result, undef);
88  
89 =cut
90 9 ▶sub thenReturnUndef {
Subroutine thenReturnUndef: called CC=1 SCAR=0.0
Pod documented
91 9 my $self = shift;
92 9 $self->{'UndefValue'} = 1;
93 }
94 =pod
95  
96 =head2 thenThrowError
97  
98 The C<thenReturnArray> method sets the return value of C<call> in the way that it will create an error.
99  
100 my $ReturnValue = Test::Mockify::ReturnValue->new();
101 $ReturnValue->thenThrowError('ErrorType');
102 throws_ok( sub { $ReturnValue->call() }, qr/ErrorType/, );
103  
104 =cut
105 3 ▶sub thenThrowError {
Subroutine thenThrowError: called CC=2 SCAR=6.9
Pod documented
106 3 my $self = shift;
107 3 my ($ErrorCode) = @_;
108 3 ▶ Error('NoErrorCode') unless($ErrorCode);
Branch: 1 branch
BranchTrueFalseTotal
unless $ErrorCode 1 2 3
109 2 $self->{'ErrorType'} = $ErrorCode;
110 2 return;
111 }
112 =pod
113  
114 =head2 thenCall
115  
116 The C<thenCall> method change the C<call> Function in a way that it will trigger the function and pass in the parameters.
117  
118 my $ReturnValue = Test::Mockify::ReturnValue->new();
119 $ReturnValue->thenCall(sub{return join('-', @_);});
120 my $Result = $ReturnValue->call('hello','world');
121 is($Result, 'hello-world');
122  
123 =cut
124 39 ▶sub thenCall{
Subroutine thenCall: called CC=2 SCAR=6.9
Pod documented
125 39 my $self = shift;
126 39 my ($FunctionPointer) = @_;
127 39 ▶ Error('NoAnCodeRef') unless(ref($FunctionPointer) eq 'CODE');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $FunctionPointer eq "CODE" 3 36 39
128 36 $self->{'FunctionPointer'} = $FunctionPointer;
129 36 return;
130 }
131 =pod
132  
133 =head2 call
134  
135 The C<call> method will return the return value which was set with one of the setter methods likeC<thenReturn>.
136 In case of C<thenCall> it will also forward the parameters.
137 It will throw an error if one of the setter methods was not called at least once.
138  
139 my $ReturnValue = Test::Mockify::ReturnValue->new();
140 $ReturnValue->thenReturn('Hello World');
141 my $Result = $ReturnValue->call();
142 is($Result, 'Hello World');
143  
144 =cut
145 169 ▶sub call {
Subroutine call: called CC=7 SCAR=19.5
Pod documented
146 169 my $self = shift;
147 169 my @Params = @_;
148 169 ▶ if($self->{'ErrorType'}){
Branch: 6 branches
BranchTrueFalseTotal
if ($self->{'ErrorType'}) { } 2 167 169
elsif ($self->{'ArrayValue'}) { } 2 165 167
elsif ($self->{'HashValue'}) { } 2 163 165
elsif ($self->{'UndefValue'}) { } 8 155 163
elsif ($self->{'FunctionPointer'}) { } 38 117 155
elsif (defined $self->{'Value'}) { } 116 1 117
149 2 Error($self->{'ErrorType'});
150  
151 }elsif($self->{'ArrayValue'}){
152 2 return @{$self->{'ArrayValue'}};
153  
154 }elsif($self->{'HashValue'}){
155 2 return %{$self->{'HashValue'}};
156  
157 }elsif($self->{'UndefValue'}){
158 8 return;
159  
160 }elsif($self->{'FunctionPointer'}){
161 38 return $self->{'FunctionPointer'}->(@Params);
162  
163 }elsif(defined $self->{'Value'}){
164 116 return $self->{'Value'};
165  
166 }else{
167 1 Error('NoReturnValue');
168 }
169 }
170 20 1;
171 __END__
« Mockify/Tools.pm ↑ Summary Mockify/Verify.pm »