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 );
$source
是 TAP::Parser::Source。
傳回介於 0
和 1
之間的數字,反映原始來源可以處理的信心程度。例如,0
表示來源無法處理,0.5
表示可能可以處理,而 1
表示肯定可以處理。有關如何使用此功能的詳細資訊,請參閱 "TAP::Parser::IteratorFactory 中的 "detect_source"。
make_iterator
抽象方法.
my $iterator = $class->make_iterator( $source );
$source
是 TAP::Parser::Source。
傳回新的 TAP::Parser::Iterator 物件,供 TAP::Parser 使用。發生錯誤時會 croak
。
有關子類別化的概觀,以及此模組附帶的任何子類別範例,請參閱 "TAP::Parser 中的 "SUBCLASSING"。以下是快速概觀。
首先熟悉 TAP::Parser::Source 和 TAP::Parser::IteratorFactory。 TAP::Parser::SourceHandler::RawTAP 是最容易用作範例的子類別。
重要的是要注意,如果您希望子類別自動由 TAP::Parser 使用,則必須確保以某種方式載入它。如果您使用 prove,則可以撰寫 App::Prove 外掛程式。如果您直接使用 TAP::Parser 或 TAP::Harness(例如透過自訂腳本、ExtUtils::MakeMaker 或 Module::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::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