1b39c5158Smillertpackage Test::Builder::Module; 2b39c5158Smillert 3b39c5158Smillertuse strict; 4b39c5158Smillert 59f11ffb7Safresh1use Test::Builder; 6b39c5158Smillert 7b39c5158Smillertrequire Exporter; 8b39c5158Smillertour @ISA = qw(Exporter); 9b39c5158Smillert 10*3d61058aSafresh1our $VERSION = '1.302199'; 11b39c5158Smillert 12b39c5158Smillert 13b39c5158Smillert=head1 NAME 14b39c5158Smillert 15b39c5158SmillertTest::Builder::Module - Base class for test modules 16b39c5158Smillert 17b39c5158Smillert=head1 SYNOPSIS 18b39c5158Smillert 19b39c5158Smillert # Emulates Test::Simple 20b39c5158Smillert package Your::Module; 21b39c5158Smillert 22b39c5158Smillert my $CLASS = __PACKAGE__; 23b39c5158Smillert 249f11ffb7Safresh1 use parent 'Test::Builder::Module'; 25b39c5158Smillert @EXPORT = qw(ok); 26b39c5158Smillert 27b39c5158Smillert sub ok ($;$) { 28b39c5158Smillert my $tb = $CLASS->builder; 29b39c5158Smillert return $tb->ok(@_); 30b39c5158Smillert } 31b39c5158Smillert 32b39c5158Smillert 1; 33b39c5158Smillert 34b39c5158Smillert 35b39c5158Smillert=head1 DESCRIPTION 36b39c5158Smillert 37b8851fccSafresh1This is a superclass for L<Test::Builder>-based modules. It provides a 38b39c5158Smillerthandful of common functionality and a method of getting at the underlying 39b8851fccSafresh1L<Test::Builder> object. 40b39c5158Smillert 41b39c5158Smillert 42b39c5158Smillert=head2 Importing 43b39c5158Smillert 44b8851fccSafresh1Test::Builder::Module is a subclass of L<Exporter> which means your 45b39c5158Smillertmodule is also a subclass of Exporter. @EXPORT, @EXPORT_OK, etc... 46b39c5158Smillertall act normally. 47b39c5158Smillert 48b8851fccSafresh1A few methods are provided to do the C<< use Your::Module tests => 23 >> part 49b39c5158Smillertfor you. 50b39c5158Smillert 51b39c5158Smillert=head3 import 52b39c5158Smillert 53b8851fccSafresh1Test::Builder::Module provides an C<import()> method which acts in the 54b8851fccSafresh1same basic way as L<Test::More>'s, setting the plan and controlling 55b39c5158Smillertexporting of functions and variables. This allows your module to set 56b8851fccSafresh1the plan independent of L<Test::More>. 57b39c5158Smillert 58b8851fccSafresh1All arguments passed to C<import()> are passed onto 59b39c5158SmillertC<< Your::Module->builder->plan() >> with the exception of 606eddea87SjasperC<< import =>[qw(things to import)] >>. 61b39c5158Smillert 62b39c5158Smillert use Your::Module import => [qw(this that)], tests => 23; 63b39c5158Smillert 64b8851fccSafresh1says to import the functions C<this()> and C<that()> as well as set the plan 65b39c5158Smillertto be 23 tests. 66b39c5158Smillert 67b8851fccSafresh1C<import()> also sets the C<exported_to()> attribute of your builder to be 68b8851fccSafresh1the caller of the C<import()> function. 69b39c5158Smillert 70b8851fccSafresh1Additional behaviors can be added to your C<import()> method by overriding 71b8851fccSafresh1C<import_extra()>. 72b39c5158Smillert 73b39c5158Smillert=cut 74b39c5158Smillert 75b39c5158Smillertsub import { 76b39c5158Smillert my($class) = shift; 77b39c5158Smillert 789f11ffb7Safresh1 Test2::API::test2_load() unless Test2::API::test2_in_preload(); 799f11ffb7Safresh1 80b39c5158Smillert # Don't run all this when loading ourself. 81b39c5158Smillert return 1 if $class eq 'Test::Builder::Module'; 82b39c5158Smillert 83b39c5158Smillert my $test = $class->builder; 84b39c5158Smillert 85b39c5158Smillert my $caller = caller; 86b39c5158Smillert 87b39c5158Smillert $test->exported_to($caller); 88b39c5158Smillert 89b39c5158Smillert $class->import_extra( \@_ ); 90b39c5158Smillert my(@imports) = $class->_strip_imports( \@_ ); 91b39c5158Smillert 92b39c5158Smillert $test->plan(@_); 93b39c5158Smillert 949f11ffb7Safresh1 local $Exporter::ExportLevel = $Exporter::ExportLevel + 1; 959f11ffb7Safresh1 $class->Exporter::import(@imports); 96b39c5158Smillert} 97b39c5158Smillert 98b39c5158Smillertsub _strip_imports { 99b39c5158Smillert my $class = shift; 100b39c5158Smillert my $list = shift; 101b39c5158Smillert 102b39c5158Smillert my @imports = (); 103b39c5158Smillert my @other = (); 104b39c5158Smillert my $idx = 0; 105b39c5158Smillert while( $idx <= $#{$list} ) { 106b39c5158Smillert my $item = $list->[$idx]; 107b39c5158Smillert 108b39c5158Smillert if( defined $item and $item eq 'import' ) { 109b39c5158Smillert push @imports, @{ $list->[ $idx + 1 ] }; 110b39c5158Smillert $idx++; 111b39c5158Smillert } 112b39c5158Smillert else { 113b39c5158Smillert push @other, $item; 114b39c5158Smillert } 115b39c5158Smillert 116b39c5158Smillert $idx++; 117b39c5158Smillert } 118b39c5158Smillert 119b39c5158Smillert @$list = @other; 120b39c5158Smillert 121b39c5158Smillert return @imports; 122b39c5158Smillert} 123b39c5158Smillert 124b39c5158Smillert=head3 import_extra 125b39c5158Smillert 126b39c5158Smillert Your::Module->import_extra(\@import_args); 127b39c5158Smillert 128b8851fccSafresh1C<import_extra()> is called by C<import()>. It provides an opportunity for you 129b39c5158Smillertto add behaviors to your module based on its import list. 130b39c5158Smillert 131b8851fccSafresh1Any extra arguments which shouldn't be passed on to C<plan()> should be 132b39c5158Smillertstripped off by this method. 133b39c5158Smillert 134b8851fccSafresh1See L<Test::More> for an example of its use. 135b39c5158Smillert 136b39c5158SmillertB<NOTE> This mechanism is I<VERY ALPHA AND LIKELY TO CHANGE> as it 137b39c5158Smillertfeels like a bit of an ugly hack in its current form. 138b39c5158Smillert 139b39c5158Smillert=cut 140b39c5158Smillert 141b39c5158Smillertsub import_extra { } 142b39c5158Smillert 143b39c5158Smillert=head2 Builder 144b39c5158Smillert 145b39c5158SmillertTest::Builder::Module provides some methods of getting at the underlying 146b39c5158SmillertTest::Builder object. 147b39c5158Smillert 148b39c5158Smillert=head3 builder 149b39c5158Smillert 150b39c5158Smillert my $builder = Your::Class->builder; 151b39c5158Smillert 152b8851fccSafresh1This method returns the L<Test::Builder> object associated with Your::Class. 153b39c5158SmillertIt is not a constructor so you can call it as often as you like. 154b39c5158Smillert 155b8851fccSafresh1This is the preferred way to get the L<Test::Builder> object. You should 156b39c5158SmillertI<not> get it via C<< Test::Builder->new >> as was previously 157b39c5158Smillertrecommended. 158b39c5158Smillert 159b8851fccSafresh1The object returned by C<builder()> may change at runtime so you should 160b8851fccSafresh1call C<builder()> inside each function rather than store it in a global. 161b39c5158Smillert 162b39c5158Smillert sub ok { 163b39c5158Smillert my $builder = Your::Class->builder; 164b39c5158Smillert 165b39c5158Smillert return $builder->ok(@_); 166b39c5158Smillert } 167b39c5158Smillert 168b39c5158Smillert 169b39c5158Smillert=cut 170b39c5158Smillert 171b39c5158Smillertsub builder { 172b39c5158Smillert return Test::Builder->new; 173b39c5158Smillert} 174b39c5158Smillert 17556d68f1eSafresh1=head1 SEE ALSO 17656d68f1eSafresh1 17756d68f1eSafresh1L<< Test2::Manual::Tooling::TestBuilder >> describes the improved 17856d68f1eSafresh1options for writing testing modules provided by L<< Test2 >>. 17956d68f1eSafresh1 18056d68f1eSafresh1=cut 18156d68f1eSafresh1 182b39c5158Smillert1; 183