TAP::Parser::ResultFactory - 建立 TAP::Parser 輸出物件的工廠
use TAP::Parser::ResultFactory;
my $token = {...};
my $factory = TAP::Parser::ResultFactory->new;
my $result = $factory->make_result( $token );
版本 3.44
這是一個簡單的工廠類別,它會傳回一個 TAP::Parser::Result 子類別,代表 TAP 中目前測試資料的片段(通常是一行)。它主要由 TAP::Parser::Grammar 使用。除非您要建立子類別,否則您可能不需要直接使用這個模組。
new
建立一個新的工廠類別。注意:目前您不需要實例化一個工廠就能使用它。
make_result
傳回一個適當類別的實例,用於傳入的測試代碼。
my $result = TAP::Parser::ResultFactory->make_result($token);
也可以作為一個實例方法被呼叫。
class_for
接收一個參數:$type
。傳回此 $type
的類別,或使用錯誤訊息croak
。
register_type
接收兩個參數:$type
、$class
這讓您可以用您自己的自訂類型覆寫現有的類型,或註冊一個全新的類型,例如
# create a custom result type:
package MyResult;
use strict;
use base 'TAP::Parser::Result';
# register with the factory:
TAP::Parser::ResultFactory->register_type( 'my_type' => __PACKAGE__ );
# use it:
my $r = TAP::Parser::ResultFactory->( { type => 'my_type' } );
您的自訂類型應該會自動被 TAP::Parser 接收。
請參閱 TAP::Parser 中的「子類別化」 以取得子類別化的概觀。
在建立您自己的 ResultFactory
時,有幾件事需要記住
工廠本身從未被實例化(這可能會在未來改變)。這表示 _initialize
從未被呼叫。
TAP::Parser::Result->new
從未被呼叫,$tokens 被重新祝福。這將會在未來的版本中改變!
TAP::Parser::Result 子類別會直接在 TAP::Parser::ResultFactory 中註冊它們自己
package MyFooResult;
TAP::Parser::ResultFactory->register_type( foo => __PACKAGE__ );
當然,由您決定是否要忽略它們。
package MyResultFactory;
use strict;
use MyResult;
use base 'TAP::Parser::ResultFactory';
# force all results to be 'MyResult'
sub class_for {
return 'MyResult';
}
1;