This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Update Test-Harness to CPAN version 3.32
[perl5.git] / cpan / Test-Harness / lib / TAP / Parser / SourceHandler.pm
1 package TAP::Parser::SourceHandler;
2
3 use strict;
4 use warnings;
5
6 use TAP::Parser::Iterator ();
7 use base 'TAP::Object';
8
9 =head1 NAME
10
11 TAP::Parser::SourceHandler - Base class for different TAP source handlers
12
13 =head1 VERSION
14
15 Version 3.32
16
17 =cut
18
19 our $VERSION = '3.32';
20
21 =head1 SYNOPSIS
22
23   # abstract class - don't use directly!
24   # see TAP::Parser::IteratorFactory for general usage
25
26   # must be sub-classed for use
27   package MySourceHandler;
28   use base 'TAP::Parser::SourceHandler';
29   sub can_handle    { return $confidence_level }
30   sub make_iterator { return $iterator }
31
32   # see example below for more details
33
34 =head1 DESCRIPTION
35
36 This is an abstract base class for L<TAP::Parser::Source> handlers / handlers.
37
38 A C<TAP::Parser::SourceHandler> does whatever is necessary to produce & capture
39 a stream of TAP from the I<raw> source, and package it up in a
40 L<TAP::Parser::Iterator> for the parser to consume.
41
42 C<SourceHandlers> must implement the I<source detection & handling> interface
43 used by L<TAP::Parser::IteratorFactory>.  At 2 methods, the interface is pretty
44 simple: L</can_handle> and L</make_source>.
45
46 Unless you're writing a new L<TAP::Parser::SourceHandler>, a plugin, or
47 subclassing L<TAP::Parser>, you probably won't need to use this module directly.
48
49 =head1 METHODS
50
51 =head2 Class Methods
52
53 =head3 C<can_handle>
54
55 I<Abstract method>.
56
57   my $vote = $class->can_handle( $source );
58
59 C<$source> is a L<TAP::Parser::Source>.
60
61 Returns a number between C<0> & C<1> reflecting how confidently the raw source
62 can be handled.  For example, C<0> means the source cannot handle it, C<0.5>
63 means it may be able to, and C<1> means it definitely can.  See
64 L<TAP::Parser::IteratorFactory/detect_source> for details on how this is used.
65
66 =cut
67
68 sub can_handle {
69     my ( $class, $args ) = @_;
70     $class->_croak(
71         "Abstract method 'can_handle' not implemented for $class!");
72     return;
73 }
74
75 =head3 C<make_iterator>
76
77 I<Abstract method>.
78
79   my $iterator = $class->make_iterator( $source );
80
81 C<$source> is a L<TAP::Parser::Source>.
82
83 Returns a new L<TAP::Parser::Iterator> object for use by the L<TAP::Parser>.
84 C<croak>s on error.
85
86 =cut
87
88 sub make_iterator {
89     my ( $class, $args ) = @_;
90     $class->_croak(
91         "Abstract method 'make_iterator' not implemented for $class!");
92     return;
93 }
94 1;
95
96 __END__
97
98 =head1 SUBCLASSING
99
100 Please see L<TAP::Parser/SUBCLASSING> for a subclassing overview, and any
101 of the subclasses that ship with this module as an example.  What follows is
102 a quick overview.
103
104 Start by familiarizing yourself with L<TAP::Parser::Source> and
105 L<TAP::Parser::IteratorFactory>.  L<TAP::Parser::SourceHandler::RawTAP> is
106 the easiest sub-class to use as an example.
107
108 It's important to point out that if you want your subclass to be automatically
109 used by L<TAP::Parser> you'll have to and make sure it gets loaded somehow.
110 If you're using L<prove> you can write an L<App::Prove> plugin.  If you're
111 using L<TAP::Parser> or L<TAP::Harness> directly (e.g. through a custom script,
112 L<ExtUtils::MakeMaker>, or L<Module::Build>) you can use the C<config> option
113 which will cause L<TAP::Parser::IteratorFactory/load_sources> to load your
114 subclass).
115
116 Don't forget to register your class with
117 L<TAP::Parser::IteratorFactory/register_handler>.
118
119 =head2 Example
120
121   package MySourceHandler;
122
123   use strict;
124
125   use MySourceHandler; # see TAP::Parser::SourceHandler
126   use TAP::Parser::IteratorFactory;
127
128   use base 'TAP::Parser::SourceHandler';
129
130   TAP::Parser::IteratorFactory->register_handler( __PACKAGE__ );
131
132   sub can_handle {
133       my ( $class, $src ) = @_;
134       my $meta   = $src->meta;
135       my $config = $src->config_for( $class );
136
137       if ($config->{accept_all}) {
138           return 1.0;
139       } elsif (my $file = $meta->{file}) {
140           return 0.0 unless $file->{exists};
141           return 1.0 if $file->{lc_ext} eq '.tap';
142           return 0.9 if $file->{shebang} && $file->{shebang} =~ /^#!.+tap/;
143           return 0.5 if $file->{text};
144           return 0.1 if $file->{binary};
145       } elsif ($meta->{scalar}) {
146           return 0.8 if $$raw_source_ref =~ /\d\.\.\d/;
147           return 0.6 if $meta->{has_newlines};
148       } elsif ($meta->{array}) {
149           return 0.8 if $meta->{size} < 5;
150           return 0.6 if $raw_source_ref->[0] =~ /foo/;
151           return 0.5;
152       } elsif ($meta->{hash}) {
153           return 0.6 if $raw_source_ref->{foo};
154           return 0.2;
155       }
156
157       return 0;
158   }
159
160   sub make_iterator {
161       my ($class, $source) = @_;
162       # this is where you manipulate the source and
163       # capture the stream of TAP in an iterator
164       # either pick a TAP::Parser::Iterator::* or write your own...
165       my $iterator = TAP::Parser::Iterator::Array->new([ 'foo', 'bar' ]);
166       return $iterator;
167   }
168
169   1;
170
171 =head1 AUTHORS
172
173 TAPx Developers.
174
175 Source detection stuff added by Steve Purkis
176
177 =head1 SEE ALSO
178
179 L<TAP::Object>,
180 L<TAP::Parser>,
181 L<TAP::Parser::Source>,
182 L<TAP::Parser::Iterator>,
183 L<TAP::Parser::IteratorFactory>,
184 L<TAP::Parser::SourceHandler::Executable>,
185 L<TAP::Parser::SourceHandler::Perl>,
186 L<TAP::Parser::SourceHandler::File>,
187 L<TAP::Parser::SourceHandler::Handle>,
188 L<TAP::Parser::SourceHandler::RawTAP>
189
190 =cut
191