line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Test::Expectation::Base; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
11
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
66
|
|
4
|
2
|
|
|
2
|
|
10
|
use warnings; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
45
|
|
5
|
2
|
|
|
2
|
|
10
|
use Carp; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
140
|
|
6
|
2
|
|
|
2
|
|
1958
|
use Data::Dumper; |
|
2
|
|
|
|
|
19809
|
|
|
2
|
|
|
|
|
151
|
|
7
|
2
|
|
|
2
|
|
1366
|
use Sub::Override; |
|
2
|
|
|
|
|
1722
|
|
|
2
|
|
|
|
|
2612
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
sub new { |
10
|
6
|
|
|
6
|
0
|
9
|
my ($class, $expectedClass, $expectedMethod) = @_; |
11
|
|
|
|
|
|
|
|
12
|
6
|
100
|
|
|
|
14
|
$expectedClass = ref($expectedClass) if (ref($expectedClass)); |
13
|
|
|
|
|
|
|
|
14
|
6
|
|
|
|
|
35
|
my $methodString = "${expectedClass}::${expectedMethod}"; |
15
|
|
|
|
|
|
|
|
16
|
6
|
|
|
|
|
42
|
my $self = { |
17
|
|
|
|
|
|
|
-met => 0, |
18
|
|
|
|
|
|
|
-method => $methodString, |
19
|
|
|
|
|
|
|
-class => $expectedClass, |
20
|
|
|
|
|
|
|
-failure => $methodString . " not called", |
21
|
|
|
|
|
|
|
-returnValues => [] |
22
|
|
|
|
|
|
|
}; |
23
|
|
|
|
|
|
|
|
24
|
6
|
|
|
|
|
13
|
$self->{-expectationsSet} = {}; |
25
|
|
|
|
|
|
|
|
26
|
6
|
|
|
|
|
11
|
bless($self, $class); |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
$self->_setReplacement(sub { |
29
|
4
|
|
|
4
|
|
19
|
$self->met(); |
30
|
4
|
|
|
|
|
13
|
$self->_doReturn |
31
|
6
|
|
|
|
|
32
|
}); |
32
|
|
|
|
|
|
|
|
33
|
6
|
|
|
|
|
191
|
return $self; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub _doReturn { |
37
|
5
|
|
|
5
|
|
7
|
my $self = shift; |
38
|
|
|
|
|
|
|
|
39
|
5
|
100
|
|
|
|
9
|
if (wantarray) { |
40
|
1
|
|
|
|
|
2
|
return @{$self->{-returnValues}}; |
|
1
|
|
|
|
|
6
|
|
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
else { |
43
|
4
|
|
|
|
|
18
|
return $self->{-returnValues}->[0]; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
# if an expectation is being set against one of these classes, then something |
48
|
|
|
|
|
|
|
# has probably gone wrong. |
49
|
|
|
|
|
|
|
sub expects { |
50
|
0
|
|
|
0
|
0
|
0
|
croak('Cannot set multiple expectations against a single method') |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
*does_not_expect = *expects; |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub _setReplacement { |
55
|
7
|
|
|
7
|
|
8
|
my ($self, $code) = @_; |
56
|
|
|
|
|
|
|
|
57
|
7
|
|
|
|
|
8
|
eval { |
58
|
7
|
|
|
|
|
17
|
$self->_restore(); |
59
|
7
|
|
|
|
|
48
|
$self->{-replacement} = Sub::Override->new( |
60
|
|
|
|
|
|
|
$self->{-method} => $code |
61
|
|
|
|
|
|
|
); |
62
|
|
|
|
|
|
|
}; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub with { |
66
|
1
|
|
|
1
|
0
|
2
|
my ($self, @expectedParams) = @_; |
67
|
|
|
|
|
|
|
|
68
|
1
|
50
|
|
|
|
6
|
croak('Cannot define "with" more than once against a single expectation') |
69
|
|
|
|
|
|
|
if ($self->{-expectationsSet}->{-with}) |
70
|
|
|
|
|
|
|
; |
71
|
|
|
|
|
|
|
|
72
|
1
|
|
|
|
|
4
|
$self->{-expectationsSet}->{-with} = 1; |
73
|
|
|
|
|
|
|
|
74
|
1
|
|
|
|
|
6
|
$self->{-failure} = $self->{-failure} . " with '@expectedParams'"; |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
$self->_setReplacement(sub { |
77
|
1
|
|
|
1
|
|
7
|
my (@params) = @_; |
78
|
|
|
|
|
|
|
|
79
|
1
|
50
|
33
|
|
|
9
|
shift(@params) if (ref($params[0]) && (ref($params[0]) eq $self->{-class})); |
80
|
|
|
|
|
|
|
|
81
|
1
|
|
|
|
|
4
|
$self->{-failure} .= ", got '@params'"; |
82
|
|
|
|
|
|
|
|
83
|
1
|
50
|
|
|
|
6
|
$self->met() if (Dumper(@params) eq Dumper(@expectedParams)); |
84
|
|
|
|
|
|
|
|
85
|
1
|
|
|
|
|
11
|
$self->_doReturn; |
86
|
1
|
|
|
|
|
6
|
}); |
87
|
|
|
|
|
|
|
|
88
|
1
|
|
|
|
|
29
|
return $self; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
# this isn't camel-cased so it's external interface is consistent |
92
|
|
|
|
|
|
|
sub to_return { |
93
|
3
|
|
|
3
|
0
|
13
|
my ($self, @returnValues) = @_; |
94
|
|
|
|
|
|
|
|
95
|
3
|
50
|
|
|
|
10
|
croak('Cannot set more that one return expectation') |
96
|
|
|
|
|
|
|
if ($self->{-expectationsSet}->{-return}) |
97
|
|
|
|
|
|
|
; |
98
|
|
|
|
|
|
|
|
99
|
3
|
|
|
|
|
8
|
$self->{-expectationsSet}->{-return} = 1; |
100
|
|
|
|
|
|
|
|
101
|
3
|
|
|
|
|
4
|
@{$self->{-returnValues}} = @returnValues; |
|
3
|
|
|
|
|
7
|
|
102
|
|
|
|
|
|
|
|
103
|
3
|
|
|
|
|
6
|
return $self |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
sub to_raise { |
107
|
0
|
|
|
0
|
0
|
0
|
my ($self, $exception) = @_; |
108
|
|
|
|
|
|
|
|
109
|
0
|
0
|
|
|
|
0
|
croak('Cannot expect more than one exception') |
110
|
|
|
|
|
|
|
if ($self->{-expectationsSet}->{-exception}) |
111
|
|
|
|
|
|
|
; |
112
|
|
|
|
|
|
|
|
113
|
0
|
|
|
|
|
0
|
$self->{-expectationsSet}->{-exception} = 1; |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
$self->_setReplacement(sub { |
116
|
0
|
|
|
0
|
|
0
|
$self->met(); |
117
|
0
|
|
|
|
|
0
|
die($exception . "\n"); |
118
|
0
|
|
|
|
|
0
|
}); |
119
|
|
|
|
|
|
|
|
120
|
0
|
|
|
|
|
0
|
return $self; |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
sub met { |
124
|
5
|
|
|
5
|
0
|
139
|
shift->{-met} = 1; |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
sub isMet { |
128
|
5
|
|
|
5
|
0
|
15
|
shift->{-met}; |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
sub class { |
132
|
0
|
|
|
0
|
0
|
0
|
shift->{-class}; |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
sub failure { |
136
|
0
|
|
|
0
|
0
|
0
|
shift->{-failure} |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
sub _restore { |
140
|
7
|
|
|
7
|
|
7
|
my $self = shift; |
141
|
7
|
100
|
|
|
|
30
|
$self->{-replacement}->restore() if $self->{-replacement}; |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
sub DESTROY { |
145
|
0
|
|
|
0
|
|
|
shift->_restore(); |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
1; |
149
|
|
|
|
|
|
|
|