內容

名稱

TAP::Parser::SourceHandler - 不同 TAP 來源處理常式的基礎類別

版本

版本 3.44

摘要

# abstract class - don't use directly!
# see TAP::Parser::IteratorFactory for general usage

# must be sub-classed for use
package MySourceHandler;
use base 'TAP::Parser::SourceHandler';
sub can_handle    { return $confidence_level }
sub make_iterator { return $iterator }

# see example below for more details

說明

這是 TAP::Parser::Source 處理常式 / 處理常式的抽象基礎類別。

TAP::Parser::SourceHandler 會執行任何必要的工作,以從原始來源產生並擷取 TAP 串流,並將其封裝在 TAP::Parser::Iterator 中供剖析器使用。

SourceHandlers 必須實作 TAP::Parser::IteratorFactory 使用的來源偵測與處理介面。介面有 2 個方法,相當簡單:"can_handle""make_source"

除非您正在撰寫新的 TAP::Parser::SourceHandler、外掛程式,或建立 TAP::Parser 的子類別,否則您可能不需要直接使用這個模組。

方法

類別方法

can_handle

抽象方法.

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

$sourceTAP::Parser::Source

傳回介於 01 之間的數字,反映原始來源可以處理的信心程度。例如,0 表示來源無法處理,0.5 表示可能可以處理,而 1 表示肯定可以處理。有關如何使用此功能的詳細資訊,請參閱 "TAP::Parser::IteratorFactory 中的 "detect_source"

make_iterator

抽象方法.

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

$sourceTAP::Parser::Source

傳回新的 TAP::Parser::Iterator 物件,供 TAP::Parser 使用。發生錯誤時會 croak

子類別化

有關子類別化的概觀,以及此模組附帶的任何子類別範例,請參閱 "TAP::Parser 中的 "SUBCLASSING"。以下是快速概觀。

首先熟悉 TAP::Parser::SourceTAP::Parser::IteratorFactoryTAP::Parser::SourceHandler::RawTAP 是最容易用作範例的子類別。

重要的是要注意,如果您希望子類別自動由 TAP::Parser 使用,則必須確保以某種方式載入它。如果您使用 prove,則可以撰寫 App::Prove 外掛程式。如果您直接使用 TAP::ParserTAP::Harness(例如透過自訂腳本、ExtUtils::MakeMakerModule::Build),則可以使用 config 選項,這將導致 "TAP::Parser::IteratorFactory 中的 "load_sources" 載入您的子類別)。

別忘了使用 "TAP::Parser::IteratorFactory 中的 "register_handler" 註冊您的類別。

範例

package MySourceHandler;

use strict;

use MySourceHandler; # see TAP::Parser::SourceHandler
use TAP::Parser::IteratorFactory;

use base 'TAP::Parser::SourceHandler';

TAP::Parser::IteratorFactory->register_handler( __PACKAGE__ );

sub can_handle {
    my ( $class, $src ) = @_;
    my $meta   = $src->meta;
    my $config = $src->config_for( $class );

    if ($config->{accept_all}) {
        return 1.0;
    } elsif (my $file = $meta->{file}) {
        return 0.0 unless $file->{exists};
        return 1.0 if $file->{lc_ext} eq '.tap';
        return 0.9 if $file->{shebang} && $file->{shebang} =~ /^#!.+tap/;
        return 0.5 if $file->{text};
        return 0.1 if $file->{binary};
    } elsif ($meta->{scalar}) {
        return 0.8 if $$raw_source_ref =~ /\d\.\.\d/;
        return 0.6 if $meta->{has_newlines};
    } elsif ($meta->{array}) {
        return 0.8 if $meta->{size} < 5;
        return 0.6 if $raw_source_ref->[0] =~ /foo/;
        return 0.5;
    } elsif ($meta->{hash}) {
        return 0.6 if $raw_source_ref->{foo};
        return 0.2;
    }

    return 0;
}

sub make_iterator {
    my ($class, $source) = @_;
    # this is where you manipulate the source and
    # capture the stream of TAP in an iterator
    # either pick a TAP::Parser::Iterator::* or write your own...
    my $iterator = TAP::Parser::Iterator::Array->new([ 'foo', 'bar' ]);
    return $iterator;
}

1;

作者

TAPx 開發人員。

Steve Purkis 新增來源偵測功能

另請參閱

TAP::ObjectTAP::ParserTAP::Parser::SourceTAP::Parser::IteratorTAP::Parser::IteratorFactoryTAP::Parser::SourceHandler::ExecutableTAP::Parser::SourceHandler::PerlTAP::Parser::SourceHandler::FileTAP::Parser::SourceHandler::HandleTAP::Parser::SourceHandler::RawTAP