line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
package Test::IOC; |
3
|
|
|
|
|
|
|
|
4
|
1
|
|
|
1
|
|
62220
|
use strict; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
50
|
|
5
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
45
|
|
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
6
|
use base qw/Exporter/; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
137
|
|
8
|
|
|
|
|
|
|
|
9
|
1
|
|
|
1
|
|
7
|
use Test::Builder; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
26
|
|
10
|
|
|
|
|
|
|
|
11
|
1
|
|
|
1
|
|
545
|
use IOC::Registry; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
36
|
|
12
|
1
|
|
|
1
|
|
12
|
use Test::More; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
11
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our $VERSION = "0.01"; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
our @EXPORT = qw( |
17
|
|
|
|
|
|
|
locate_service search_service |
18
|
|
|
|
|
|
|
locate_container search_container |
19
|
|
|
|
|
|
|
service_isa service_is service_can service_is_deeply |
20
|
|
|
|
|
|
|
service_exists container_exists |
21
|
|
|
|
|
|
|
container_list_is service_list_is |
22
|
|
|
|
|
|
|
service_is_literal service_is_prototype service_is_singleton |
23
|
|
|
|
|
|
|
); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
my $t = Test::Builder->new; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
my $r = IOC::Registry->instance; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
# utility subs |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
our $err; |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub _try (&) { |
34
|
16
|
|
|
16
|
|
21
|
my $s = shift; |
35
|
16
|
|
|
|
|
25
|
local $@; |
36
|
16
|
|
|
|
|
25
|
my $r = eval { $s->( @_ ) }; |
|
16
|
|
|
|
|
32
|
|
37
|
16
|
|
|
|
|
28
|
$err = $@; |
38
|
16
|
|
|
|
|
78
|
$r; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub locate_service ($) { |
42
|
11
|
|
|
11
|
1
|
17
|
my $path = shift; |
43
|
11
|
|
|
11
|
|
47
|
_try { $r->locateService($path) }; |
|
11
|
|
|
|
|
34
|
|
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub search_for_service ($) { |
47
|
0
|
|
|
0
|
1
|
0
|
my $name = shift; |
48
|
0
|
|
|
|
|
0
|
$r->searchForService($name); |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
sub locate_container ($) { |
52
|
5
|
|
|
5
|
1
|
10
|
my $path = shift; |
53
|
5
|
|
|
5
|
|
23
|
_try { $r->locateContainer($path) } |
54
|
5
|
|
|
|
|
29
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub search_for_container ($) { |
57
|
0
|
|
|
0
|
1
|
0
|
my $name = shift; |
58
|
0
|
|
|
|
|
0
|
$r->searchForContainer($name); |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
# basic tests |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub service_exists ($;$) { |
64
|
3
|
|
|
3
|
1
|
548
|
my ( $path, $desc ) = @_; |
65
|
3
|
50
|
33
|
|
|
9
|
$t->ok( defined(locate_service($path)), $desc || "The service '$path' exists in the registry" ) || diag $err; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub container_exists ($;$) { |
69
|
1
|
|
|
1
|
1
|
8
|
my ( $path, $desc ) = @_; |
70
|
1
|
|
33
|
|
|
12
|
$t->ok( defined(locate_container($path)), $desc || "The container '$path' exists in the registry" ); |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub service_alias_ok ($$;$) { |
74
|
0
|
|
|
0
|
1
|
0
|
my ( $real, $alias, $desc ) = @_; |
75
|
0
|
|
0
|
|
|
0
|
$desc ||= "The service at '$real' is aliased to '$alias'"; |
76
|
|
|
|
|
|
|
|
77
|
0
|
|
|
|
|
0
|
return $t->is_eq( $real, $r->{service_aliases}{$alias}, $desc ); |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
# FIXME test it like this: |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
# my $real_s = locate_service($real); |
82
|
|
|
|
|
|
|
# my $alias_s = locate_service($alias); |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
# return $t->fail("The service '$real' does not exist in the registry") unless defined $real_s; |
85
|
|
|
|
|
|
|
# return $t->fail("The service '$alias' does not exist in the registry") unless defined $alias; |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
# compare true equality of IOC::Service objects or deep equality of the returned services |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub container_list_is ($$;$) { |
91
|
1
|
|
|
1
|
1
|
453
|
my ( $path, $spec, $desc ) = @_; |
92
|
1
|
|
|
|
|
2
|
local $" = ", "; |
93
|
1
|
|
33
|
|
|
5
|
$desc ||= "The containers at '$path' are @$spec"; |
94
|
|
|
|
|
|
|
|
95
|
1
|
|
|
|
|
3
|
my @got; |
96
|
|
|
|
|
|
|
|
97
|
1
|
50
|
|
|
|
5
|
if ( $path eq "/" ) { |
98
|
1
|
|
|
|
|
7
|
@got = $r->getRegisteredContainerList; |
99
|
|
|
|
|
|
|
} else { |
100
|
0
|
|
0
|
|
|
0
|
my $c = locate_container($path) || return $t->fail("Container '$path' does not exist"); |
101
|
0
|
|
|
|
|
0
|
@got = $c->getSubContainerList; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
1
|
|
|
|
|
8
|
@_ = ( [ sort @got ], [ sort @$spec ], $desc ); |
105
|
1
|
|
|
|
|
9
|
goto &is_deeply; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
sub service_list_is ($$;$) { |
109
|
1
|
|
|
1
|
1
|
597
|
my ( $path, $spec, $desc ) = @_; |
110
|
1
|
|
|
|
|
3
|
local $" = ", "; |
111
|
1
|
|
33
|
|
|
9
|
$desc ||= "The services at '$path' are @$spec"; |
112
|
|
|
|
|
|
|
|
113
|
1
|
50
|
|
|
|
4
|
if ( $path eq "/" ) { |
114
|
0
|
|
|
|
|
0
|
die "Services cannot be added to the registry"; |
115
|
|
|
|
|
|
|
} else { |
116
|
1
|
|
50
|
|
|
10
|
my $c = locate_container($path) || return $t->fail("Container '$path' does not exist"); |
117
|
|
|
|
|
|
|
|
118
|
1
|
|
|
|
|
8
|
@_ = ( [ sort $c->getServiceList ], [ sort @$spec ], $desc ); |
119
|
1
|
|
|
|
|
4
|
goto &is_deeply; |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
sub service_is_literal ($;$) { |
124
|
1
|
|
|
1
|
1
|
3
|
my ( $path, $desc ) = @_; |
125
|
1
|
|
33
|
|
|
15
|
$desc ||= "'$path' is a literal service"; |
126
|
1
|
|
|
|
|
2
|
local $@; |
127
|
1
|
|
|
|
|
2
|
$t->ok( eval { get_service_object($path)->isa("IOC::Service::Literal") }, $desc ); |
|
1
|
|
|
|
|
5
|
|
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
sub service_is_prototype ($;$) { |
131
|
1
|
|
|
1
|
1
|
3
|
my ( $path, $desc ) = @_; |
132
|
1
|
|
33
|
|
|
10
|
$desc ||= "'$path' is a prototype service"; |
133
|
1
|
|
|
|
|
1
|
local $@; |
134
|
1
|
|
|
|
|
3
|
$t->ok( eval { get_service_object($path)->isa("IOC::Service::Prototype") }, $desc ); |
|
1
|
|
|
|
|
5
|
|
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
sub service_is_singleton ($;$) { |
138
|
1
|
|
|
1
|
1
|
4
|
my ( $path, $desc ) = @_; |
139
|
1
|
|
33
|
|
|
10
|
$desc ||= "'$path' is a singleton service"; |
140
|
1
|
|
|
|
|
3
|
local $@; |
141
|
1
|
|
|
|
|
2
|
my $s = get_service_object($path); |
142
|
1
|
|
|
|
|
4
|
$t->ok( eval { |
143
|
1
|
50
|
33
|
|
|
27
|
$s->isa("IOC::Service") |
144
|
|
|
|
|
|
|
and |
145
|
|
|
|
|
|
|
!$s->isa("IOC::Service::Literal") |
146
|
|
|
|
|
|
|
and |
147
|
|
|
|
|
|
|
!$s->isa("IOC::Service::Prototype") |
148
|
|
|
|
|
|
|
}, $desc ); |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
sub get_service_object ($) { |
152
|
3
|
|
|
3
|
1
|
7
|
my $path = shift; |
153
|
3
|
|
|
|
|
21
|
$path =~ s{ / ([^/]+) $ }{}x; |
154
|
3
|
|
|
|
|
7
|
my $name = $1; |
155
|
3
|
|
50
|
|
|
9
|
my $c = locate_container($path) || return; |
156
|
3
|
|
|
|
|
37
|
$c->{services}{$name}; # FIXME yuck |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
# test + utility sub combination |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
my %tests = ( |
162
|
|
|
|
|
|
|
is => \&is, |
163
|
|
|
|
|
|
|
isa => \&isa_ok, |
164
|
|
|
|
|
|
|
can => \&can_ok, |
165
|
|
|
|
|
|
|
is_deeply => \&is_deeply, |
166
|
|
|
|
|
|
|
); |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
foreach my $test ( keys %tests ) { |
169
|
|
|
|
|
|
|
my $test_sub = $tests{$test}; |
170
|
|
|
|
|
|
|
|
171
|
1
|
|
|
1
|
|
3225
|
no strict 'refs'; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
77
|
|
172
|
|
|
|
|
|
|
*{ "service_$test" } = sub { |
173
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
168
|
|
174
|
8
|
|
|
8
|
|
292
|
my ( $path, @spec ) = @_; |
175
|
|
|
|
|
|
|
|
176
|
8
|
|
|
|
|
19
|
my $service = locate_service($path); |
177
|
|
|
|
|
|
|
|
178
|
8
|
50
|
|
|
|
35
|
if ( defined $service ) { |
179
|
8
|
|
|
|
|
21
|
@_ = ( $service, @spec ); |
180
|
8
|
|
|
|
|
34
|
goto $test_sub; |
181
|
|
|
|
|
|
|
} else { |
182
|
0
|
|
|
|
|
|
fail( "The service '$path' does not exist in the registry" ); |
183
|
|
|
|
|
|
|
} |
184
|
|
|
|
|
|
|
} |
185
|
|
|
|
|
|
|
} |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
__PACKAGE__; |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
__END__ |