Test/IOC.pm

filter: Statementstmt 89.3% 92 / 103 Branchbran 50.0% 4 / 8 Conditioncond 39.2% 11 / 28 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 88.0% 22 / 25 Podpod 100% 13 / 13 totaltotal 74.3% 142 / 191 CC 18 SCAR 10.1
CC
18
Cov
77%
service_alias_ok
6.0
container_list_is
4.8
service_is_singleton
4.7
CRAP 2.7
« IOC/Visitor/SearchForService.pm ↑ Summary IOC/Visitor/SearchForContainer.pm »
1  
2 package Test::IOC;
3  
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use base qw/Exporter/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use Test::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶use IOC::Registry;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Test::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 our $VERSION = "0.01";
15  
16 2 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 2 my $t = Test::Builder->new;
26  
27 2 my $r = IOC::Registry->instance;
28  
29 # utility subs
30  
31 2 our $err;
32  
33 16 ▶sub _try (&) {
Subroutine _try: called CC=1 SCAR=0.0
34 16 my $s = shift;
35 16 local $@;
36 16 my $r = eval { $s->( @_ ) };
37 16 $err = $@;
38 16 $r;
39 }
40  
41 11 ▶sub locate_service ($) {
Subroutine locate_service: called CC=1 SCAR=0.0
Pod documented
42 11 my $path = shift;
43 11 ▶ _try { $r->locateService($path) };
Subroutine __ANON__: called CC=1 SCAR=0.0
44 }
45  
46 0 ▶sub search_for_service ($) {
Subroutine search_for_service: not called CC=1 SCAR=6.9
Pod documented
47 0 my $name = shift;
48 0 $r->searchForService($name);
49 }
50  
51 5 ▶sub locate_container ($) {
Subroutine locate_container: called CC=1 SCAR=0.0
Pod documented
52 5 my $path = shift;
53 5 ▶ _try { $r->locateContainer($path) }
Subroutine __ANON__: called CC=1 SCAR=0.0
54 5 }
55  
56 0 ▶sub search_for_container ($) {
Subroutine search_for_container: not called CC=1 SCAR=6.9
Pod documented
57 0 my $name = shift;
58 0 $r->searchForContainer($name);
59 }
60  
61 # basic tests
62  
63 3 ▶sub service_exists ($;$) {
Subroutine service_exists: called CC=3 SCAR=12.0
Pod documented
64 3 my ( $path, $desc ) = @_;
65 3 ▶ $t->ok( defined(locate_service($path)), $desc || "The service '$path' exists in the registry" ) || diag $err;
Branch: 1 branch
BranchTrueFalseTotal
$t->ok(defined &locate_service($path), $desc || "The service '${path}' exists in the registry") or diag($Test::IOC::err) 3 0 3
Condition50%1/2
$desc || "The service '${path}' exists in the registry"
l!l
03
MC/DC0%0/1
$desc || "The service '${path}' exists in the registry"
$desc
Truth table50%1/2
A
A= $desc
Aresult
11
00
66 }
67  
68 1 ▶sub container_exists ($;$) {
Subroutine container_exists: called CC=2 SCAR=7.2
Pod documented
69 1 my ( $path, $desc ) = @_;
70 1 ▶ $t->ok( defined(locate_container($path)), $desc || "The container '$path' exists in the registry" );
Condition50%1/2
$desc || "The container '${path}' exists in the registry"
l!l
01
MC/DC0%0/1
$desc || "The container '${path}' exists in the registry"
$desc
Truth table50%1/2
A
A= $desc
Aresult
11
00
71 }
72  
73 0 ▶sub service_alias_ok ($$;$) {
Subroutine service_alias_ok: not called CC=2 SCAR=17.9
Pod documented
74 0 my ( $real, $alias, $desc ) = @_;
75 0 ▶ $desc ||= "The service at '$real' is aliased to '$alias'";
Condition0%0/2
$desc ||= "The service at '${real}' is aliased to '${alias}'"
l!l
00
MC/DC0%0/1
$desc ||= "The service at '${real}' is aliased to '${alias}'"
$desc
Truth table0%0/2
A
A= $desc
Aresult
11
00
76  
77 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 1 ▶sub container_list_is ($$;$) {
Subroutine container_list_is: called CC=4 SCAR=15.8
Pod documented
91 1 my ( $path, $spec, $desc ) = @_;
92 1 local $" = ", ";
93 1 ▶ $desc ||= "The containers at '$path' are @$spec";
Condition50%1/2
$desc ||= "The containers at '${path}' are @$spec"
l!l
10
MC/DC0%0/1
$desc ||= "The containers at '${path}' are @$spec"
$desc
Truth table50%1/2
A
A= $desc
Aresult
11
00
94  
95 1 my @got;
96  
97 1 ▶ if ( $path eq "/" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path eq '/') { } 1 0 1
98 1 @got = $r->getRegisteredContainerList;
99 } else {
100 0 ▶ my $c = locate_container($path) || return $t->fail("Container '$path' does not exist");
Condition0%0/2
&locate_container($path) || (return $t->fail("Container '${path}' does not exist"))
l!l
00
MC/DC0%0/1
&locate_container($path) || (return $t->fail("Container '${path}' does not exist"))
&locate_container($path)
Truth table0%0/2
A
A= &locate_container($path)
Aresult
11
00
101 0 @got = $c->getSubContainerList;
102 }
103  
104 1 @_ = ( [ sort @got ], [ sort @$spec ], $desc );
105 1 goto &is_deeply;
106 }
107  
108 1 ▶sub service_list_is ($$;$) {
Subroutine service_list_is: called CC=4 SCAR=14.8
Pod documented
109 1 my ( $path, $spec, $desc ) = @_;
110 1 local $" = ", ";
111 1 ▶ $desc ||= "The services at '$path' are @$spec";
Condition50%1/2
$desc ||= "The services at '${path}' are @$spec"
l!l
01
MC/DC0%0/1
$desc ||= "The services at '${path}' are @$spec"
$desc
Truth table50%1/2
A
A= $desc
Aresult
11
00
112  
113 1 ▶ if ( $path eq "/" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path eq '/') { } 0 1 1
114 0 die "Services cannot be added to the registry";
115 } else {
116 1 ▶ my $c = locate_container($path) || return $t->fail("Container '$path' does not exist");
Condition50%1/2
&locate_container($path) || (return $t->fail("Container '${path}' does not exist"))
l!l
10
MC/DC0%0/1
&locate_container($path) || (return $t->fail("Container '${path}' does not exist"))
&locate_container($path)
Truth table50%1/2
A
A= &locate_container($path)
Aresult
11
00
117  
118 1 @_ = ( [ sort $c->getServiceList ], [ sort @$spec ], $desc );
119 1 goto &is_deeply;
120 }
121 }
122  
123 1 ▶sub service_is_literal ($;$) {
Subroutine service_is_literal: called CC=2 SCAR=7.0
Pod documented
124 1 my ( $path, $desc ) = @_;
125 1 ▶ $desc ||= "'$path' is a literal service";
Condition50%1/2
$desc ||= "'${path}' is a literal service"
l!l
01
MC/DC0%0/1
$desc ||= "'${path}' is a literal service"
$desc
Truth table50%1/2
A
A= $desc
Aresult
11
00
126 1 local $@;
127 1 $t->ok( eval { get_service_object($path)->isa("IOC::Service::Literal") }, $desc );
128 }
129  
130 1 ▶sub service_is_prototype ($;$) {
Subroutine service_is_prototype: called CC=2 SCAR=7.0
Pod documented
131 1 my ( $path, $desc ) = @_;
132 1 ▶ $desc ||= "'$path' is a prototype service";
Condition50%1/2
$desc ||= "'${path}' is a prototype service"
l!l
01
MC/DC0%0/1
$desc ||= "'${path}' is a prototype service"
$desc
Truth table50%1/2
A
A= $desc
Aresult
11
00
133 1 local $@;
134 1 $t->ok( eval { get_service_object($path)->isa("IOC::Service::Prototype") }, $desc );
135 }
136  
137 1 ▶sub service_is_singleton ($;$) {
Subroutine service_is_singleton: called CC=4 SCAR=15.5
Pod documented
138 1 my ( $path, $desc ) = @_;
139 1 ▶ $desc ||= "'$path' is a singleton service";
Condition50%1/2
$desc ||= "'${path}' is a singleton service"
l!l
01
MC/DC0%0/1
$desc ||= "'${path}' is a singleton service"
$desc
Truth table50%1/2
A
A= $desc
Aresult
11
00
140 1 local $@;
141 1 my $s = get_service_object($path);
142 1 $t->ok( eval {
143 1 ▶ $s->isa("IOC::Service")
Condition33%2/6
$s->isa('IOC::Service') && !$s->isa('IOC::Service::Literal') && !$s->isa('IOC::Service::Prototype')
!ll&&!rl&&r
001
$s->isa("IOC::Service") and not $s->isa("IOC::Service::Literal")
!ll&&!rl&&r
001
MC/DC0%0/3
$s->isa('IOC::Service') && !$s->isa('IOC::Service::Literal') && !$s->isa('IOC::Service::Prototype')
$s->isa("IOC::Service") not $s->isa("IOC::Service::Literal") !$s->isa('IOC::Service::Prototype')
Truth table25%1/4
A and not(B) && not(C)
A= $s->isa("IOC::Service")B= not $s->isa("IOC::Service::Literal")C= !$s->isa('IOC::Service::Prototype')
ABCresult
0XX0
10X0
1100
1111
144 and
145 !$s->isa("IOC::Service::Literal")
146 and
147 !$s->isa("IOC::Service::Prototype")
148 }, $desc );
149 }
150  
151 3 ▶sub get_service_object ($) {
Subroutine get_service_object: called CC=2 SCAR=7.0
Pod documented
152 3 my $path = shift;
153 3 $path =~ s{ / ([^/]+) $ }{}x;
154 3 my $name = $1;
155 3 ▶ my $c = locate_container($path) || return;
Condition50%1/2
&locate_container($path) || (return)
l!l
30
MC/DC0%0/1
&locate_container($path) || (return)
&locate_container($path)
Truth table50%1/2
A
A= &locate_container($path)
Aresult
11
00
156 3 $c->{services}{$name}; # FIXME yuck
157 }
158  
159 # test + utility sub combination
160  
161 2 my %tests = (
162 is => \&is,
163 isa => \&isa_ok,
164 can => \&can_ok,
165 is_deeply => \&is_deeply,
166 );
167  
168 2 foreach my $test ( keys %tests ) {
169 8 my $test_sub = $tests{$test};
170  
171 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
172 8 ▶ *{ "service_$test" } = sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
173 2 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
174 8 my ( $path, @spec ) = @_;
175  
176 8 my $service = locate_service($path);
177  
178 8 ▶ if ( defined $service ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $service) { } 8 0 8
179 8 @_ = ( $service, @spec );
180 8 goto $test_sub;
181 } else {
182 0 fail( "The service '$path' does not exist in the registry" );
183 }
184 }
185 8 }
186  
187 2 __PACKAGE__;
188  
189 __END__
« IOC/Visitor/SearchForService.pm ↑ Summary IOC/Visitor/SearchForContainer.pm »