| 1 | package Test::Builder::Module; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | |||
| 5 | use Test::Builder; | ||
| 6 | |||
| 7 | require Exporter; | ||
| 8 | our @ISA = qw(Exporter); | ||
| 9 | |||
| 10 | our $VERSION = '1.302073'; | ||
| 11 | |||
| 12 | |||
| 13 | =head1 NAME | ||
| 14 | |||
| 15 | Test::Builder::Module - Base class for test modules | ||
| 16 | |||
| 17 | =head1 SYNOPSIS | ||
| 18 | |||
| 19 | # Emulates Test::Simple | ||
| 20 | package Your::Module; | ||
| 21 | |||
| 22 | my $CLASS = __PACKAGE__; | ||
| 23 | |||
| 24 | use parent 'Test::Builder::Module'; | ||
| 25 | @EXPORT = qw(ok); | ||
| 26 | |||
| 27 | sub ok ($;$) { | ||
| 28 | my $tb = $CLASS->builder; | ||
| 29 | return $tb->ok(@_); | ||
| 30 | } | ||
| 31 | |||
| 32 | 1; | ||
| 33 | |||
| 34 | |||
| 35 | =head1 DESCRIPTION | ||
| 36 | |||
| 37 | This is a superclass for L<Test::Builder>-based modules. It provides a | ||
| 38 | handful of common functionality and a method of getting at the underlying | ||
| 39 | L<Test::Builder> object. | ||
| 40 | |||
| 41 | |||
| 42 | =head2 Importing | ||
| 43 | |||
| 44 | Test::Builder::Module is a subclass of L<Exporter> which means your | ||
| 45 | module is also a subclass of Exporter. @EXPORT, @EXPORT_OK, etc... | ||
| 46 | all act normally. | ||
| 47 | |||
| 48 | A few methods are provided to do the C<< use Your::Module tests => 23 >> part | ||
| 49 | for you. | ||
| 50 | |||
| 51 | =head3 import | ||
| 52 | |||
| 53 | Test::Builder::Module provides an C<import()> method which acts in the | ||
| 54 | same basic way as L<Test::More>'s, setting the plan and controlling | ||
| 55 | exporting of functions and variables. This allows your module to set | ||
| 56 | the plan independent of L<Test::More>. | ||
| 57 | |||
| 58 | All arguments passed to C<import()> are passed onto | ||
| 59 | C<< Your::Module->builder->plan() >> with the exception of | ||
| 60 | C<< import =>[qw(things to import)] >>. | ||
| 61 | |||
| 62 | use Your::Module import => [qw(this that)], tests => 23; | ||
| 63 | |||
| 64 | says to import the functions C<this()> and C<that()> as well as set the plan | ||
| 65 | to be 23 tests. | ||
| 66 | |||
| 67 | C<import()> also sets the C<exported_to()> attribute of your builder to be | ||
| 68 | the caller of the C<import()> function. | ||
| 69 | |||
| 70 | Additional behaviors can be added to your C<import()> method by overriding | ||
| 71 | C<import_extra()>. | ||
| 72 | |||
| 73 | =cut | ||
| 74 | |||
| 75 | sub import { | ||
| 76 | my($class) = shift; | ||
| 77 | |||
| 78 | # Don't run all this when loading ourself. | ||
| 79 | return 1 if $class eq 'Test::Builder::Module'; | ||
| 80 | |||
| 81 | my $test = $class->builder; | ||
| 82 | |||
| 83 | my $caller = caller; | ||
| 84 | |||
| 85 | $test->exported_to($caller); | ||
| 86 | |||
| 87 | $class->import_extra( \@_ ); | ||
| 88 | my(@imports) = $class->_strip_imports( \@_ ); | ||
| 89 | |||
| 90 | $test->plan(@_); | ||
| 91 | |||
| 92 | local $Exporter::ExportLevel = $Exporter::ExportLevel + 1; | ||
| 93 | $class->Exporter::import(@imports); | ||
| 94 | } | ||
| 95 | |||
| 96 | sub _strip_imports { | ||
| 97 | my $class = shift; | ||
| 98 | my $list = shift; | ||
| 99 | |||
| 100 | my @imports = (); | ||
| 101 | my @other = (); | ||
| 102 | my $idx = 0; | ||
| 103 | while( $idx <= $#{$list} ) { | ||
| 104 | my $item = $list->[$idx]; | ||
| 105 | |||
| 106 | if( defined $item and $item eq 'import' ) { | ||
| 107 | push @imports, @{ $list->[ $idx + 1 ] }; | ||
| 108 | $idx++; | ||
| 109 | } | ||
| 110 | else { | ||
| 111 | push @other, $item; | ||
| 112 | } | ||
| 113 | |||
| 114 | $idx++; | ||
| 115 | } | ||
| 116 | |||
| 117 | @$list = @other; | ||
| 118 | |||
| 119 | return @imports; | ||
| 120 | } | ||
| 121 | |||
| 122 | =head3 import_extra | ||
| 123 | |||
| 124 | Your::Module->import_extra(\@import_args); | ||
| 125 | |||
| 126 | C<import_extra()> is called by C<import()>. It provides an opportunity for you | ||
| 127 | to add behaviors to your module based on its import list. | ||
| 128 | |||
| 129 | Any extra arguments which shouldn't be passed on to C<plan()> should be | ||
| 130 | stripped off by this method. | ||
| 131 | |||
| 132 | See L<Test::More> for an example of its use. | ||
| 133 | |||
| 134 | B<NOTE> This mechanism is I<VERY ALPHA AND LIKELY TO CHANGE> as it | ||
| 135 | feels like a bit of an ugly hack in its current form. | ||
| 136 | |||
| 137 | =cut | ||
| 138 | |||
| 139 | sub import_extra { } | ||
| 140 | |||
| 141 | =head2 Builder | ||
| 142 | |||
| 143 | Test::Builder::Module provides some methods of getting at the underlying | ||
| 144 | Test::Builder object. | ||
| 145 | |||
| 146 | =head3 builder | ||
| 147 | |||
| 148 | my $builder = Your::Class->builder; | ||
| 149 | |||
| 150 | This method returns the L<Test::Builder> object associated with Your::Class. | ||
| 151 | It is not a constructor so you can call it as often as you like. | ||
| 152 | |||
| 153 | This is the preferred way to get the L<Test::Builder> object. You should | ||
| 154 | I<not> get it via C<< Test::Builder->new >> as was previously | ||
| 155 | recommended. | ||
| 156 | |||
| 157 | The object returned by C<builder()> may change at runtime so you should | ||
| 158 | call C<builder()> inside each function rather than store it in a global. | ||
| 159 | |||
| 160 | sub ok { | ||
| 161 | my $builder = Your::Class->builder; | ||
| 162 | |||
| 163 | return $builder->ok(@_); | ||
| 164 | } | ||
| 165 | |||
| 166 | |||
| 167 | =cut | ||
| 168 | |||
| 169 | sub builder { | ||
| 170 | return Test::Builder->new; | ||
| 171 | } | ||
| 172 | |||
| 173 | 1; |