line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
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> 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
|
8
|
|
|
8
|
|
16882
|
use strict; |
|
8
|
|
|
|
|
9
|
|
|
8
|
|
|
|
|
215
|
|
16
|
8
|
|
|
8
|
|
26
|
use warnings; |
|
8
|
|
|
|
|
10
|
|
|
8
|
|
|
|
|
3155
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub new { |
19
|
98
|
|
|
98
|
0
|
2255
|
my $class = shift; |
20
|
98
|
|
|
|
|
139
|
my $self = bless { |
21
|
|
|
|
|
|
|
}, $class; |
22
|
98
|
|
|
|
|
192
|
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
|
|
|
|
|
|
|
sub thenReturn { |
37
|
58
|
|
|
58
|
1
|
61
|
my $self = shift; |
38
|
58
|
|
|
|
|
59
|
my ($Value) = @_; |
39
|
58
|
50
|
|
|
|
102
|
die('Return value undefined. Use "thenReturnUndef" if you need to return undef.') unless($Value); |
40
|
58
|
|
|
|
|
130
|
$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
|
|
|
|
|
|
|
sub thenReturnArray { |
55
|
4
|
|
|
4
|
1
|
799
|
my $self = shift; |
56
|
4
|
|
|
|
|
7
|
my ($Value) = @_; |
57
|
4
|
100
|
|
|
|
23
|
die('NoAnArrayRef') unless(ref($Value) eq 'ARRAY'); |
58
|
2
|
|
|
|
|
5
|
$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
|
|
|
|
|
|
|
sub thenReturnHash { |
73
|
4
|
|
|
4
|
1
|
713
|
my $self = shift; |
74
|
4
|
|
|
|
|
10
|
my ($Value) = @_; |
75
|
4
|
100
|
|
|
|
29
|
die('NoAHashRef') unless(ref($Value) eq 'HASH'); |
76
|
2
|
|
|
|
|
7
|
$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
|
|
|
|
|
|
|
sub thenReturnUndef { |
91
|
8
|
|
|
8
|
1
|
11
|
my $self = shift; |
92
|
8
|
|
|
|
|
17
|
$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
|
|
|
|
|
|
|
sub thenThrowError { |
106
|
3
|
|
|
3
|
1
|
331
|
my $self = shift; |
107
|
3
|
|
|
|
|
4
|
my ($ErrorCode) = @_; |
108
|
3
|
100
|
|
|
|
15
|
die('NoErrorCode') unless($ErrorCode); |
109
|
2
|
|
|
|
|
5
|
$self->{'ErrorType'} = $ErrorCode; |
110
|
2
|
|
|
|
|
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
|
|
|
|
|
|
|
sub thenCall{ |
125
|
31
|
|
|
31
|
1
|
1009
|
my $self = shift; |
126
|
31
|
|
|
|
|
39
|
my ($FunctionPointer) = @_; |
127
|
31
|
100
|
|
|
|
94
|
die('NoAnCodeRef') unless(ref($FunctionPointer) eq 'CODE'); |
128
|
28
|
|
|
|
|
51
|
$self->{'FunctionPointer'} = $FunctionPointer; |
129
|
28
|
|
|
|
|
42
|
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
|
|
|
|
|
|
|
sub call { |
146
|
95
|
|
|
95
|
1
|
143
|
my $self = shift; |
147
|
95
|
|
|
|
|
113
|
my @Params = @_; |
148
|
95
|
100
|
|
|
|
365
|
if($self->{'ErrorType'}){ |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
149
|
2
|
|
|
|
|
20
|
die($self->{'ErrorType'}); |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
}elsif($self->{'ArrayValue'}){ |
152
|
2
|
|
|
|
|
2
|
return @{$self->{'ArrayValue'}}; |
|
2
|
|
|
|
|
9
|
|
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
}elsif($self->{'HashValue'}){ |
155
|
2
|
|
|
|
|
2
|
return %{$self->{'HashValue'}}; |
|
2
|
|
|
|
|
14
|
|
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
}elsif($self->{'UndefValue'}){ |
158
|
8
|
|
|
|
|
26
|
return; |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
}elsif($self->{'FunctionPointer'}){ |
161
|
28
|
|
|
|
|
66
|
return $self->{'FunctionPointer'}->(@Params); |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
}elsif($self->{'Value'}){ |
164
|
52
|
|
|
|
|
305
|
return $self->{'Value'}; |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
}else{ |
167
|
1
|
|
|
|
|
8
|
die('NoReturnValue'); |
168
|
|
|
|
|
|
|
} |
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
1; |
171
|
|
|
|
|
|
|
__END__ |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=head1 LICENSE |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
Copyright (C) 2017 ePages GmbH |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
178
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=head1 AUTHOR |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
Christian Breitkreutz E<lt>christianbreitkreutz@gmx.deE<gt> |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=cut |
185
|
|
|
|
|
|
|
|