1 package TAP::Parser::SourceHandler;
4 use vars qw($VERSION @ISA);
7 use TAP::Parser::Iterator ();
9 @ISA = qw(TAP::Object);
13 TAP::Parser::SourceHandler - Base class for different TAP source handlers
25 # abstract class - don't use directly!
26 # see TAP::Parser::IteratorFactory for general usage
28 # must be sub-classed for use
29 package MySourceHandler;
30 use base qw( TAP::Parser::SourceHandler );
31 sub can_handle { return $confidence_level }
32 sub make_iterator { return $iterator }
34 # see example below for more details
38 This is an abstract base class for L<TAP::Parser::Source> handlers / handlers.
40 A C<TAP::Parser::SourceHandler> does whatever is necessary to produce & capture
41 a stream of TAP from the I<raw> source, and package it up in a
42 L<TAP::Parser::Iterator> for the parser to consume.
44 C<SourceHandlers> must implement the I<source detection & handling> interface
45 used by L<TAP::Parser::IteratorFactory>. At 2 methods, the interface is pretty
46 simple: L</can_handle> and L</make_source>.
48 Unless you're writing a new L<TAP::Parser::SourceHandler>, a plugin, or
49 subclassing L<TAP::Parser>, you probably won't need to use this module directly.
59 my $vote = $class->can_handle( $source );
61 C<$source> is a L<TAP::Parser::Source>.
63 Returns a number between C<0> & C<1> reflecting how confidently the raw source
64 can be handled. For example, C<0> means the source cannot handle it, C<0.5>
65 means it may be able to, and C<1> means it definitely can. See
66 L<TAP::Parser::IteratorFactory/detect_source> for details on how this is used.
71 my ( $class, $args ) = @_;
73 "Abstract method 'can_handle' not implemented for $class!");
77 =head3 C<make_iterator>
81 my $iterator = $class->make_iterator( $source );
83 C<$source> is a L<TAP::Parser::Source>.
85 Returns a new L<TAP::Parser::Iterator> object for use by the L<TAP::Parser>.
91 my ( $class, $args ) = @_;
93 "Abstract method 'make_iterator' not implemented for $class!");
102 Please see L<TAP::Parser/SUBCLASSING> for a subclassing overview, and any
103 of the subclasses that ship with this module as an example. What follows is
106 Start by familiarizing yourself with L<TAP::Parser::Source> and
107 L<TAP::Parser::IteratorFactory>. L<TAP::Parser::SourceHandler::RawTAP> is
108 the easiest sub-class to use an an example.
110 It's important to point out that if you want your subclass to be automatically
111 used by L<TAP::Parser> you'll have to and make sure it gets loaded somehow.
112 If you're using L<prove> you can write an L<App::Prove> plugin. If you're
113 using L<TAP::Parser> or L<TAP::Harness> directly (e.g. through a custom script,
114 L<ExtUtils::MakeMaker>, or L<Module::Build>) you can use the C<config> option
115 which will cause L<TAP::Parser::IteratorFactory/load_sources> to load your
118 Don't forget to register your class with
119 L<TAP::Parser::IteratorFactory/register_handler>.
123 package MySourceHandler;
126 use vars '@ISA'; # compat with older perls
128 use MySourceHandler; # see TAP::Parser::SourceHandler
129 use TAP::Parser::IteratorFactory;
131 @ISA = qw( TAP::Parser::SourceHandler );
133 TAP::Parser::IteratorFactory->register_handler( __PACKAGE__ );
136 my ( $class, $src ) = @_;
137 my $meta = $src->meta;
138 my $config = $src->config_for( $class );
140 if ($config->{accept_all}) {
142 } elsif (my $file = $meta->{file}) {
143 return 0.0 unless $file->{exists};
144 return 1.0 if $file->{lc_ext} eq '.tap';
145 return 0.9 if $file->{shebang} && $file->{shebang} =~ /^#!.+tap/;
146 return 0.5 if $file->{text};
147 return 0.1 if $file->{binary};
148 } elsif ($meta->{scalar}) {
149 return 0.8 if $$raw_source_ref =~ /\d\.\.\d/;
150 return 0.6 if $meta->{has_newlines};
151 } elsif ($meta->{array}) {
152 return 0.8 if $meta->{size} < 5;
153 return 0.6 if $raw_source_ref->[0] =~ /foo/;
155 } elsif ($meta->{hash}) {
156 return 0.6 if $raw_source_ref->{foo};
164 my ($class, $source) = @_;
165 # this is where you manipulate the source and
166 # capture the stream of TAP in an iterator
167 # either pick a TAP::Parser::Iterator::* or write your own...
168 my $iterator = TAP::Parser::Iterator::Array->new([ 'foo', 'bar' ]);
178 Source detection stuff added by Steve Purkis
184 L<TAP::Parser::Source>,
185 L<TAP::Parser::Iterator>,
186 L<TAP::Parser::IteratorFactory>,
187 L<TAP::Parser::SourceHandler::Executable>,
188 L<TAP::Parser::SourceHandler::Perl>,
189 L<TAP::Parser::SourceHandler::File>,
190 L<TAP::Parser::SourceHandler::Handle>,
191 L<TAP::Parser::SourceHandler::RawTAP>