1 package TAP::Parser::SourceHandler;
6 use TAP::Parser::Iterator ();
7 use parent 'TAP::Object';
11 TAP::Parser::SourceHandler - Base class for different TAP source handlers
19 our $VERSION = '3.29';
23 # abstract class - don't use directly!
24 # see TAP::Parser::IteratorFactory for general usage
26 # must be sub-classed for use
27 package MySourceHandler;
28 use parent 'TAP::Parser::SourceHandler';
29 sub can_handle { return $confidence_level }
30 sub make_iterator { return $iterator }
32 # see example below for more details
36 This is an abstract base class for L<TAP::Parser::Source> handlers / handlers.
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.
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>.
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.
57 my $vote = $class->can_handle( $source );
59 C<$source> is a L<TAP::Parser::Source>.
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.
69 my ( $class, $args ) = @_;
71 "Abstract method 'can_handle' not implemented for $class!");
75 =head3 C<make_iterator>
79 my $iterator = $class->make_iterator( $source );
81 C<$source> is a L<TAP::Parser::Source>.
83 Returns a new L<TAP::Parser::Iterator> object for use by the L<TAP::Parser>.
89 my ( $class, $args ) = @_;
91 "Abstract method 'make_iterator' not implemented for $class!");
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
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.
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
116 Don't forget to register your class with
117 L<TAP::Parser::IteratorFactory/register_handler>.
121 package MySourceHandler;
125 use MySourceHandler; # see TAP::Parser::SourceHandler
126 use TAP::Parser::IteratorFactory;
128 use parent 'TAP::Parser::SourceHandler';
130 TAP::Parser::IteratorFactory->register_handler( __PACKAGE__ );
133 my ( $class, $src ) = @_;
134 my $meta = $src->meta;
135 my $config = $src->config_for( $class );
137 if ($config->{accept_all}) {
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/;
152 } elsif ($meta->{hash}) {
153 return 0.6 if $raw_source_ref->{foo};
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' ]);
175 Source detection stuff added by Steve Purkis
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>