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 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
|
|
|
20
|
|
60319
|
use strict; |
|
20
|
|
|
|
|
53
|
|
|
20
|
|
|
|
|
506
|
|
16
|
20
|
|
|
20
|
|
92
|
use warnings; |
|
20
|
|
|
|
|
38
|
|
|
20
|
|
|
|
|
515
|
|
17
|
20
|
|
|
20
|
|
1152
|
use Test::Mockify::Tools qw (Error); |
|
20
|
|
|
|
|
50
|
|
|
20
|
|
|
|
|
9681
|
|
18
|
|
|
|
|
|
|
sub new { |
19
|
159
|
|
|
159
|
0
|
3287
|
my $class = shift; |
20
|
159
|
|
|
|
|
288
|
my $self = bless { |
21
|
|
|
|
|
|
|
}, $class; |
22
|
159
|
|
|
|
|
386
|
return $self; |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
=pod |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
=head2 thenReturn |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
The C method set the return value of C. |
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
|
114
|
|
|
114
|
1
|
240
|
my $self = shift; |
38
|
114
|
|
|
|
|
207
|
my ($Value) = @_; |
39
|
114
|
100
|
|
|
|
261
|
Error('Return value undefined. Use "thenReturnUndef" if you need to return undef.') unless(defined $Value); |
40
|
113
|
|
|
|
|
298
|
$self->{'Value'} = $Value; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
=pod |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
=head2 thenReturnArray |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
The C method sets the return value of C 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
|
951
|
my $self = shift; |
56
|
4
|
|
|
|
|
8
|
my ($Value) = @_; |
57
|
4
|
100
|
|
|
|
32
|
Error('NoAnArrayRef') unless(ref($Value) eq 'ARRAY'); |
58
|
2
|
|
|
|
|
8
|
$self->{'ArrayValue'} = $Value; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
=pod |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=head2 thenReturnHash |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
The C method sets the return value of C 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
|
984
|
my $self = shift; |
74
|
4
|
|
|
|
|
10
|
my ($Value) = @_; |
75
|
4
|
100
|
|
|
|
16
|
Error('NoAHashRef') unless(ref($Value) eq 'HASH'); |
76
|
2
|
|
|
|
|
7
|
$self->{'HashValue'} = $Value; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
=pod |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=head2 thenReturnUndef |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
The C method sets the return value of C 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
|
9
|
|
|
9
|
1
|
441
|
my $self = shift; |
92
|
9
|
|
|
|
|
23
|
$self->{'UndefValue'} = 1; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
=pod |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=head2 thenThrowError |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
The C method sets the return value of C 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
|
391
|
my $self = shift; |
107
|
3
|
|
|
|
|
8
|
my ($ErrorCode) = @_; |
108
|
3
|
100
|
|
|
|
9
|
Error('NoErrorCode') unless($ErrorCode); |
109
|
2
|
|
|
|
|
7
|
$self->{'ErrorType'} = $ErrorCode; |
110
|
2
|
|
|
|
|
4
|
return; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
=pod |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=head2 thenCall |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
The C method change the C 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
|
39
|
|
|
39
|
1
|
1444
|
my $self = shift; |
126
|
39
|
|
|
|
|
61
|
my ($FunctionPointer) = @_; |
127
|
39
|
100
|
|
|
|
120
|
Error('NoAnCodeRef') unless(ref($FunctionPointer) eq 'CODE'); |
128
|
36
|
|
|
|
|
97
|
$self->{'FunctionPointer'} = $FunctionPointer; |
129
|
36
|
|
|
|
|
59
|
return; |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
=pod |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=head2 call |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
The C method will return the return value which was set with one of the setter methods likeC. |
136
|
|
|
|
|
|
|
In case of C 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
|
169
|
|
|
169
|
1
|
348
|
my $self = shift; |
147
|
169
|
|
|
|
|
317
|
my @Params = @_; |
148
|
169
|
100
|
|
|
|
782
|
if($self->{'ErrorType'}){ |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
149
|
2
|
|
|
|
|
8
|
Error($self->{'ErrorType'}); |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
}elsif($self->{'ArrayValue'}){ |
152
|
2
|
|
|
|
|
4
|
return @{$self->{'ArrayValue'}}; |
|
2
|
|
|
|
|
9
|
|
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
}elsif($self->{'HashValue'}){ |
155
|
2
|
|
|
|
|
5
|
return %{$self->{'HashValue'}}; |
|
2
|
|
|
|
|
10
|
|
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
}elsif($self->{'UndefValue'}){ |
158
|
8
|
|
|
|
|
28
|
return; |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
}elsif($self->{'FunctionPointer'}){ |
161
|
38
|
|
|
|
|
102
|
return $self->{'FunctionPointer'}->(@Params); |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
}elsif(defined $self->{'Value'}){ |
164
|
116
|
|
|
|
|
507
|
return $self->{'Value'}; |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
}else{ |
167
|
1
|
|
|
|
|
5
|
Error('NoReturnValue'); |
168
|
|
|
|
|
|
|
} |
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
1; |
171
|
|
|
|
|
|
|
__END__ |