blib/lib/TAP/Parser/IteratorFactory.pm

filter: Statementstmt 55.5% 90 / 162 Branchbran 39.7% 27 / 68 Conditioncond 32.3% 11 / 34 MC/DCmcdc 18.1% 4 / 22 Subroutinesub 70.8% 17 / 24 Podpod 100% 10 / 10 totaltotal 49.6% 159 / 320 CC 53 SCAR 22.8
CC
53
Cov
48%
detect_source
17.7
detect_source
17.7
_load_handler
17.4
CRAP 9.8
« blib/lib/TAP/Formatter/Console/Session.pm ↑ Summary blib/lib/TAP/Formatter/Session.pm »
1 package TAP::Parser::IteratorFactory;
2  
3 37 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 37 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 37 ▶use Carp qw( confess );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 37 ▶use File::Basename qw( fileparse );
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 37 ▶use base 'TAP::Object';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 37 ▶use constant handlers => [];
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 =head1 NAME
14  
15 TAP::Parser::IteratorFactory - Figures out which SourceHandler objects to use for a given Source
16  
17 =head1 VERSION
18  
19 Version 3.52
20  
21 =cut
22  
23 37 our $VERSION = '3.52';
24  
25 =head1 SYNOPSIS
26  
27 use TAP::Parser::IteratorFactory;
28 my $factory = TAP::Parser::IteratorFactory->new({ %config });
29 my $iterator = $factory->make_iterator( $filename );
30  
31 =head1 DESCRIPTION
32  
33 This is a factory class that takes a L<TAP::Parser::Source> and runs it through all the
34 registered L<TAP::Parser::SourceHandler>s to see which one should handle the source.
35  
36 If you're a plugin author, you'll be interested in how to L</register_handler>s,
37 how L</detect_source> works.
38  
39 =head1 METHODS
40  
41 =head2 Class Methods
42  
43 =head3 C<new>
44  
45 Creates a new factory class:
46  
47 my $sf = TAP::Parser::IteratorFactory->new( $config );
48  
49 C<$config> is optional. If given, sets L</config> and calls L</load_handlers>.
50  
51 =cut
52  
53 304 ▶sub _initialize {
Subroutine _initialize: called CC=2 SCAR=9.2
54 0 ▶ my ( $self, $config ) = @_;
Subroutine _initialize: not called CC=2 SCAR=9.2
55 0 ▶ $self->config( $config || {} )->load_handlers;
Condition50%2/4
$config || {}
l!l
5299
$config || {}
l!l
00
MC/DC100%1/1
$config || {}
$config
MC/DC0%0/1
$config || {}
$config
Truth table100%2/2
A
A= $config
Aresult
11
00
Truth table0%0/2
A
A= $config
Aresult
11
00
56 0 return $self;
57 }
58  
59 =head3 C<register_handler>
60  
61 Registers a new L<TAP::Parser::SourceHandler> with this factory.
62  
63 __PACKAGE__->register_handler( $handler_class );
64  
65 =head3 C<handlers>
66  
67 List of handlers that have been registered.
68  
69 =cut
70  
71 174 ▶sub register_handler {
Subroutine register_handler: called CC=4 SCAR=20.9
Pod documented
72 0 ▶ my ( $class, $dclass ) = @_;
Subroutine register_handler: not called CC=4 SCAR=20.9
Pod documented
73  
74 0 ▶ confess("$dclass must implement can_handle & make_iterator methods!")
Branch: 2 branches
BranchTrueFalseTotal
unless UNIVERSAL::can($dclass, "can_handle") and UNIVERSAL::can($dclass, "make_iterator") 0 174 174
unless UNIVERSAL::can($dclass, "can_handle") and UNIVERSAL::can($dclass, "make_iterator") 0 0 0
Condition16%1/6
UNIVERSAL::can($dclass, "can_handle") and UNIVERSAL::can($dclass, "make_iterator")
!ll&&!rl&&r
00174
UNIVERSAL::can($dclass, "can_handle") and UNIVERSAL::can($dclass, "make_iterator")
!ll&&!rl&&r
000
MC/DC0%0/2
UNIVERSAL::can($dclass, "can_handle") and UNIVERSAL::can($dclass, "make_iterator")
UNIVERSAL::can($dclass, "can_handle") UNIVERSAL::can($dclass, "make_iterator")
MC/DC0%0/2
UNIVERSAL::can($dclass, "can_handle") and UNIVERSAL::can($dclass, "make_iterator")
UNIVERSAL::can($dclass, "can_handle") UNIVERSAL::can($dclass, "make_iterator")
Truth table33%1/3
A and B
A= UNIVERSAL::can($dclass, "can_handle")B= UNIVERSAL::can($dclass, "make_iterator")
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= UNIVERSAL::can($dclass, "can_handle")B= UNIVERSAL::can($dclass, "make_iterator")
ABresult
0X0
100
111
75 unless UNIVERSAL::can( $dclass, 'can_handle' )
76 && UNIVERSAL::can( $dclass, 'make_iterator' );
77  
78 0 my $handlers = $class->handlers;
79 0 push @{$handlers}, $dclass
80 0 ▶ unless grep { $_ eq $dclass } @{$handlers};
Branch: 2 branches
BranchTrueFalseTotal
unless grep {$_ eq $dclass;} @{$handlers;} 174 0 174
unless grep {$_ eq $dclass;} @{$handlers;} 0 0 0
81  
82 0 return $class;
83 }
84  
85 ##############################################################################
86  
87 =head2 Instance Methods
88  
89 =head3 C<config>
90  
91 my $cfg = $sf->config;
92 $sf->config({ Perl => { %config } });
93  
94 Chaining getter/setter for the configuration of the available source handlers.
95 This is a hashref keyed on handler class whose values contain config to be passed
96 onto the handlers during detection & creation. Class names may be fully qualified
97 or abbreviated, eg:
98  
99 # these are equivalent
100 $sf->config({ 'TAP::Parser::SourceHandler::Perl' => { %config } });
101 $sf->config({ 'Perl' => { %config } });
102  
103 =cut
104  
105 914 ▶sub config {
Subroutine config: called CC=3 SCAR=14.2
Pod documented
106 0 ▶ my $self = shift;
Subroutine config: not called CC=3 SCAR=14.2
Pod documented
107 0 ▶ return $self->{config} unless @_;
Branch: 2 branches
BranchTrueFalseTotal
unless @_ 608 306 914
unless @_ 0 0 0
108 0 ▶ unless ( 'HASH' eq ref $_[0] ) {
Branch: 2 branches
BranchTrueFalseTotal
unless ("HASH" eq ref $_[0]) 1 305 306
unless ("HASH" eq ref $_[0]) 0 0 0
109 0 $self->_croak('Argument to &config must be a hash reference');
110 }
111 0 $self->{config} = shift;
112 0 return $self;
113 }
114  
115 313 ▶sub _last_handler {
Subroutine _last_handler: called CC=2 SCAR=9.2
116 0 ▶ my $self = shift;
Subroutine _last_handler: not called CC=2 SCAR=9.2
117 0 ▶ return $self->{last_handler} unless @_;
Branch: 2 branches
BranchTrueFalseTotal
unless @_ 12 301 313
unless @_ 0 0 0
118 0 $self->{last_handler} = shift;
119 0 return $self;
120 }
121  
122 313 ▶sub _testing {
Subroutine _testing: called CC=2 SCAR=9.2
123 0 ▶ my $self = shift;
Subroutine _testing: not called CC=2 SCAR=9.2
124 0 ▶ return $self->{testing} unless @_;
Branch: 2 branches
BranchTrueFalseTotal
unless @_ 301 12 313
unless @_ 0 0 0
125 0 $self->{testing} = shift;
126 0 return $self;
127 }
128  
129 ##############################################################################
130  
131 =head3 C<load_handlers>
132  
133 $sf->load_handlers;
134  
135 Loads the handler classes defined in L</config>. For example, given a config:
136  
137 $sf->config({
138 MySourceHandler => { some => 'config' },
139 });
140  
141 C<load_handlers> will attempt to load the C<MySourceHandler> class by looking in
142 C<@INC> for it in this order:
143  
144 TAP::Parser::SourceHandler::MySourceHandler
145 MySourceHandler
146  
147 C<croak>s on error.
148  
149 =cut
150  
151 304 ▶sub load_handlers {
Subroutine load_handlers: called CC=2 SCAR=9.2
Pod documented
152 0 ▶ my ($self) = @_;
Subroutine load_handlers: not called CC=2 SCAR=9.2
Pod documented
153 0 for my $handler ( keys %{ $self->config } ) {
154 0 my $sclass = $self->_load_handler($handler);
155  
156 # TODO: store which class we loaded anywhere?
157 }
158 0 return $self;
159 }
160  
161 5 ▶sub _load_handler {
Subroutine _load_handler: called CC=7 SCAR=28.6
162 0 ▶ my ( $self, $handler ) = @_;
Subroutine _load_handler: not called CC=7 SCAR=28.6
163  
164 0 my @errors;
165 0 for my $dclass ( "TAP::Parser::SourceHandler::$handler", $handler ) {
166 0 ▶ return $dclass
Branch: 2 branches
BranchTrueFalseTotal
if UNIVERSAL::can($dclass, "can_handle") and UNIVERSAL::can($dclass, "make_iterator") 3 5 8
if UNIVERSAL::can($dclass, "can_handle") and UNIVERSAL::can($dclass, "make_iterator") 0 0 0
Condition33%2/6
UNIVERSAL::can($dclass, "can_handle") and UNIVERSAL::can($dclass, "make_iterator")
!ll&&!rl&&r
503
UNIVERSAL::can($dclass, "can_handle") and UNIVERSAL::can($dclass, "make_iterator")
!ll&&!rl&&r
000
MC/DC50%1/2
UNIVERSAL::can($dclass, "can_handle") and UNIVERSAL::can($dclass, "make_iterator")
UNIVERSAL::can($dclass, "can_handle") UNIVERSAL::can($dclass, "make_iterator")
MC/DC0%0/2
UNIVERSAL::can($dclass, "can_handle") and UNIVERSAL::can($dclass, "make_iterator")
UNIVERSAL::can($dclass, "can_handle") UNIVERSAL::can($dclass, "make_iterator")
Truth table66%2/3
A and B
A= UNIVERSAL::can($dclass, "can_handle")B= UNIVERSAL::can($dclass, "make_iterator")
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= UNIVERSAL::can($dclass, "can_handle")B= UNIVERSAL::can($dclass, "make_iterator")
ABresult
0X0
100
111
167 if UNIVERSAL::can( $dclass, 'can_handle' )
168 && UNIVERSAL::can( $dclass, 'make_iterator' );
169  
170 0 ▶ eval "use $dclass";
Subroutine BEGIN: called CC=1 SCAR=0.5
Subroutine BEGIN: called CC=1 SCAR=0.5
171 0 ▶ if ( my $e = $@ ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $e = $@) 3 2 5
if (my $e = $@) 0 0 0
172 0 push @errors, $e;
173 0 next;
174 }
175  
176 0 ▶ return $dclass
Branch: 2 branches
BranchTrueFalseTotal
if UNIVERSAL::can($dclass, "can_handle") and UNIVERSAL::can($dclass, "make_iterator") 2 0 2
if UNIVERSAL::can($dclass, "can_handle") and UNIVERSAL::can($dclass, "make_iterator") 0 0 0
Condition16%1/6
UNIVERSAL::can($dclass, "can_handle") and UNIVERSAL::can($dclass, "make_iterator")
!ll&&!rl&&r
002
UNIVERSAL::can($dclass, "can_handle") and UNIVERSAL::can($dclass, "make_iterator")
!ll&&!rl&&r
000
MC/DC0%0/2
UNIVERSAL::can($dclass, "can_handle") and UNIVERSAL::can($dclass, "make_iterator")
UNIVERSAL::can($dclass, "can_handle") UNIVERSAL::can($dclass, "make_iterator")
MC/DC0%0/2
UNIVERSAL::can($dclass, "can_handle") and UNIVERSAL::can($dclass, "make_iterator")
UNIVERSAL::can($dclass, "can_handle") UNIVERSAL::can($dclass, "make_iterator")
Truth table33%1/3
A and B
A= UNIVERSAL::can($dclass, "can_handle")B= UNIVERSAL::can($dclass, "make_iterator")
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= UNIVERSAL::can($dclass, "can_handle")B= UNIVERSAL::can($dclass, "make_iterator")
ABresult
0X0
100
111
177 if UNIVERSAL::can( $dclass, 'can_handle' )
178 && UNIVERSAL::can( $dclass, 'make_iterator' );
179 0 push @errors,
180 "handler '$dclass' does not implement can_handle & make_iterator";
181 }
182  
183 $self->_croak(
184 0 "Cannot load handler '$handler': " . join( "\n", @errors ) );
185 }
186  
187 ##############################################################################
188  
189 =head3 C<make_iterator>
190  
191 my $iterator = $src_factory->make_iterator( $source );
192  
193 Given a L<TAP::Parser::Source>, finds the most suitable L<TAP::Parser::SourceHandler>
194 to use to create a L<TAP::Parser::Iterator> (see L</detect_source>). Dies on error.
195  
196 =cut
197  
198 304 ▶sub make_iterator {
Subroutine make_iterator: called CC=5 SCAR=23.0
Pod documented
199 0 ▶ my ( $self, $source ) = @_;
Pod documented
200  
201 0 ▶ $self->_croak('no raw source defined!') unless defined $source->raw;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $source->raw 0 304 304
unless defined $source->raw 0 0 0
202  
203 0 $source->config( $self->config )->assemble_meta;
204  
205 # is the raw source already an object?
206 return $source->raw
207 if ( $source->meta->{is_object}
208 0 ▶ && UNIVERSAL::isa( $source->raw, 'TAP::Parser::SourceHandler' ) );
Branch: 2 branches
BranchTrueFalseTotal
if $source->meta->{"is_object"} and UNIVERSAL::isa($source->raw, "TAP::Parser::SourceHandler") 0 304 304
if $source->meta->{"is_object"} and UNIVERSAL::isa($source->raw, "TAP::Parser::SourceHandler") 0 0 0
Condition33%2/6
$source->meta->{"is_object"} and UNIVERSAL::isa($source->raw, "TAP::Parser::SourceHandler")
!ll&&!rl&&r
30310
$source->meta->{"is_object"} and UNIVERSAL::isa($source->raw, "TAP::Parser::SourceHandler")
!ll&&!rl&&r
000
MC/DC0%0/2
$source->meta->{"is_object"} and UNIVERSAL::isa($source->raw, "TAP::Parser::SourceHandler")
$source->meta->{"is_object"} UNIVERSAL::isa($source->raw, "TAP::Parser::SourceHandler")
MC/DC0%0/2
$source->meta->{"is_object"} and UNIVERSAL::isa($source->raw, "TAP::Parser::SourceHandler")
$source->meta->{"is_object"} UNIVERSAL::isa($source->raw, "TAP::Parser::SourceHandler")
Truth table66%2/3
A and B
A= $source->meta->{"is_object"}B= UNIVERSAL::isa($source->raw, "TAP::Parser::SourceHandler")
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $source->meta->{"is_object"}B= UNIVERSAL::isa($source->raw, "TAP::Parser::SourceHandler")
ABresult
0X0
100
111
209  
210 # figure out what kind of source it is
211 0 my $sd_class = $self->detect_source($source);
212 0 $self->_last_handler($sd_class);
213  
214 0 ▶ return if $self->_testing;
Branch: 2 branches
BranchTrueFalseTotal
if $self->_testing 11 290 301
if $self->_testing 0 0 0
215  
216 # create it
217 0 my $iterator = $sd_class->make_iterator($source);
218  
219 0 return $iterator;
220 }
221  
222 =head3 C<detect_source>
223  
224 Given a L<TAP::Parser::Source>, detects what kind of source it is and
225 returns I<one> L<TAP::Parser::SourceHandler> (the most confident one). Dies
226 on error.
227  
228 The detection algorithm works something like this:
229  
230 for (@registered_handlers) {
231 # ask them how confident they are about handling this source
232 $confidence{$handler} = $handler->can_handle( $source )
233 }
234 # choose the most confident handler
235  
236 Ties are handled by choosing the first handler.
237  
238 =cut
239  
240 304 ▶sub detect_source {
Subroutine detect_source: called CC=8 SCAR=28.7
Pod documented
241 0 ▶ my ( $self, $source ) = @_;
Pod documented
242  
243 0 ▶ confess('no raw source ref defined!') unless defined $source->raw;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $source->raw 0 304 304
unless defined $source->raw 0 0 0
244  
245 # find a list of handlers that can handle this source:
246 0 my %confidence_for;
247 0 for my $handler ( @{ $self->handlers } ) {
248 0 my $confidence = $handler->can_handle($source);
249 # warn "handler: $handler: $confidence\n";
250 0 ▶ $confidence_for{$handler} = $confidence if $confidence;
Branch: 2 branches
BranchTrueFalseTotal
if $confidence 314 1216 1530
if $confidence 0 0 0
251 }
252  
253 0 ▶ if ( !%confidence_for ) {
Branch: 2 branches
BranchTrueFalseTotal
unless (%confidence_for) 2 302 304
unless (%confidence_for) 0 0 0
254 # error: can't detect source
255 0 my $raw_source_short = substr( ${ $source->raw }, 0, 50 );
256 0 confess("Cannot detect source of '$raw_source_short'!");
257 0 return;
258 }
259  
260 # if multiple handlers can handle it, choose the most confident one
261 my @handlers =
262 0 sort { $confidence_for{$b} <=> $confidence_for{$a} }
263 keys %confidence_for;
264  
265 # Check for a tie.
266 0 ▶ if( @handlers > 1 &&
Branch: 2 branches
BranchTrueFalseTotal
if (@handlers > 1 and $confidence_for{$handlers[0]} == $confidence_for{$handlers[1]}) 1 301 302
if (@handlers > 1 and $confidence_for{$handlers[0]} == $confidence_for{$handlers[1]}) 0 0 0
Condition50%3/6
@handlers > 1 and $confidence_for{$handlers[0]} == $confidence_for{$handlers[1]}
!ll&&!rl&&r
290111
@handlers > 1 and $confidence_for{$handlers[0]} == $confidence_for{$handlers[1]}
!ll&&!rl&&r
000
MC/DC100%2/2
@handlers > 1 and $confidence_for{$handlers[0]} == $confidence_for{$handlers[1]}
@handlers > 1 $confidence_for{$handlers[0]} == $confidence_for{$handlers[1]}
MC/DC0%0/2
@handlers > 1 and $confidence_for{$handlers[0]} == $confidence_for{$handlers[1]}
@handlers > 1 $confidence_for{$handlers[0]} == $confidence_for{$handlers[1]}
Truth table100%3/3
A and B
A= @handlers > 1B= $confidence_for{$handlers[0]} == $confidence_for{$handlers[1]}
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= @handlers > 1B= $confidence_for{$handlers[0]} == $confidence_for{$handlers[1]}
ABresult
0X0
100
111
267 $confidence_for{$handlers[0]} == $confidence_for{$handlers[1]}
268 ) {
269 1 my $filename = $source->meta->{file}{basename};
270 1 die("There is a tie between $handlers[0] and $handlers[1].\n".
271 "Both voted $confidence_for{$handlers[0]} on $filename.\n");
272 }
273  
274 # this is really useful for debugging handlers:
275 301 ▶ if ( $ENV{TAP_HARNESS_SOURCE_FACTORY_VOTES} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ENV{"TAP_HARNESS_SOURCE_FACTORY_VOTES"}) 0 301 301
if ($ENV{"TAP_HARNESS_SOURCE_FACTORY_VOTES"}) 0 0 0
276 warn(
277 "votes: ",
278 0 join( ', ', map {"$_: $confidence_for{$_}"} @handlers ),
279 "\n"
280 );
281 }
282  
283 # return 1st
284 301 return $handlers[0];
285 }
286  
287 37 1;
288  
289 __END__
« blib/lib/TAP/Formatter/Console/Session.pm ↑ Summary blib/lib/TAP/Formatter/Session.pm »