Perl 5 version 16.1 documentation

TAP::Parser::SourceHandler

NAME

TAP::Parser::SourceHandler - Base class for different TAP source handlers

VERSION

Version 3.23

SYNOPSIS

  1. # abstract class - don't use directly!
  2. # see TAP::Parser::IteratorFactory for general usage
  3. # must be sub-classed for use
  4. package MySourceHandler;
  5. use base qw( TAP::Parser::SourceHandler );
  6. sub can_handle { return $confidence_level }
  7. sub make_iterator { return $iterator }
  8. # see example below for more details

DESCRIPTION

This is an abstract base class for TAP::Parser::Source handlers / handlers.

A TAP::Parser::SourceHandler does whatever is necessary to produce & capture a stream of TAP from the raw source, and package it up in a TAP::Parser::Iterator for the parser to consume.

SourceHandlers must implement the source detection & handling interface used by TAP::Parser::IteratorFactory. At 2 methods, the interface is pretty simple: can_handle and make_source.

Unless you're writing a new TAP::Parser::SourceHandler, a plugin, or subclassing TAP::Parser, you probably won't need to use this module directly.

METHODS

Class Methods

can_handle

Abstract method.

  1. my $vote = $class->can_handle( $source );

$source is a TAP::Parser::Source.

Returns a number between 0 & 1 reflecting how confidently the raw source can be handled. For example, 0 means the source cannot handle it, 0.5 means it may be able to, and 1 means it definitely can. See detect_source in TAP::Parser::IteratorFactory for details on how this is used.

make_iterator

Abstract method.

  1. my $iterator = $class->make_iterator( $source );

$source is a TAP::Parser::Source.

Returns a new TAP::Parser::Iterator object for use by the TAP::Parser. croak s on error.

SUBCLASSING

Please see SUBCLASSING in TAP::Parser for a subclassing overview, and any of the subclasses that ship with this module as an example. What follows is a quick overview.

Start by familiarizing yourself with TAP::Parser::Source and TAP::Parser::IteratorFactory. TAP::Parser::SourceHandler::RawTAP is the easiest sub-class to use an an example.

It's important to point out that if you want your subclass to be automatically used by TAP::Parser you'll have to and make sure it gets loaded somehow. If you're using prove you can write an App::Prove plugin. If you're using TAP::Parser or TAP::Harness directly (e.g. through a custom script, ExtUtils::MakeMaker, or Module::Build) you can use the config option which will cause load_sources in TAP::Parser::IteratorFactory to load your subclass).

Don't forget to register your class with register_handler in TAP::Parser::IteratorFactory.

Example

  1. package MySourceHandler;
  2. use strict;
  3. use vars '@ISA'; # compat with older perls
  4. use MySourceHandler; # see TAP::Parser::SourceHandler
  5. use TAP::Parser::IteratorFactory;
  6. @ISA = qw( TAP::Parser::SourceHandler );
  7. TAP::Parser::IteratorFactory->register_handler( __PACKAGE__ );
  8. sub can_handle {
  9. my ( $class, $src ) = @_;
  10. my $meta = $src->meta;
  11. my $config = $src->config_for( $class );
  12. if ($config->{accept_all}) {
  13. return 1.0;
  14. } elsif (my $file = $meta->{file}) {
  15. return 0.0 unless $file->{exists};
  16. return 1.0 if $file->{lc_ext} eq '.tap';
  17. return 0.9 if $file->{shebang} && $file->{shebang} =~ /^#!.+tap/;
  18. return 0.5 if $file->{text};
  19. return 0.1 if $file->{binary};
  20. } elsif ($meta->{scalar}) {
  21. return 0.8 if $$raw_source_ref =~ /\d\.\.\d/;
  22. return 0.6 if $meta->{has_newlines};
  23. } elsif ($meta->{array}) {
  24. return 0.8 if $meta->{size} < 5;
  25. return 0.6 if $raw_source_ref->[0] =~ /foo/;
  26. return 0.5;
  27. } elsif ($meta->{hash}) {
  28. return 0.6 if $raw_source_ref->{foo};
  29. return 0.2;
  30. }
  31. return 0;
  32. }
  33. sub make_iterator {
  34. my ($class, $source) = @_;
  35. # this is where you manipulate the source and
  36. # capture the stream of TAP in an iterator
  37. # either pick a TAP::Parser::Iterator::* or write your own...
  38. my $iterator = TAP::Parser::Iterator::Array->new([ 'foo', 'bar' ]);
  39. return $iterator;
  40. }
  41. 1;

AUTHORS

TAPx Developers.

Source detection stuff added by Steve Purkis

SEE ALSO

TAP::Object, TAP::Parser, TAP::Parser::Source, TAP::Parser::Iterator, TAP::Parser::IteratorFactory, TAP::Parser::SourceHandler::Executable, TAP::Parser::SourceHandler::Perl, TAP::Parser::SourceHandler::File, TAP::Parser::SourceHandler::Handle, TAP::Parser::SourceHandler::RawTAP