1 package Test::Builder::Module;
12 # 5.004's Exporter doesn't have export_to_level.
13 my $_export_to_level = sub {
16 (undef) = shift; # redundant arg
17 my $callpkg = caller($level);
18 $pkg->export( $callpkg, @_ );
23 Test::Builder::Module - Base class for test modules
27 # Emulates Test::Simple
30 my $CLASS = __PACKAGE__;
32 use base 'Test::Builder::Module';
36 my $tb = $CLASS->builder;
45 This is a superclass for Test::Builder-based modules. It provides a
46 handful of common functionality and a method of getting at the underlying
52 Test::Builder::Module is a subclass of Exporter which means your
53 module is also a subclass of Exporter. @EXPORT, @EXPORT_OK, etc...
56 A few methods are provided to do the C<use Your::Module tests => 23> part
61 Test::Builder::Module provides an import() method which acts in the
62 same basic way as Test::More's, setting the plan and controling
63 exporting of functions and variables. This allows your module to set
64 the plan independent of Test::More.
66 All arguments passed to import() are passed onto
67 C<< Your::Module->builder->plan() >> with the exception of
68 C<import =>[qw(things to import)]>.
70 use Your::Module import => [qw(this that)], tests => 23;
72 says to import the functions this() and that() as well as set the plan
75 import() also sets the exported_to() attribute of your builder to be
76 the caller of the import() function.
78 Additional behaviors can be added to your import() method by overriding
86 my $test = $class->builder;
90 $test->exported_to($caller);
92 $class->import_extra( \@_ );
93 my (@imports) = $class->_strip_imports( \@_ );
97 $class->$_export_to_level( 1, $class, @imports );
107 while ( $idx <= $#{$list} ) {
108 my $item = $list->[$idx];
110 if ( defined $item and $item eq 'import' ) {
111 push @imports, @{ $list->[ $idx + 1 ] };
128 Your::Module->import_extra(\@import_args);
130 import_extra() is called by import(). It provides an opportunity for you
131 to add behaviors to your module based on its import list.
133 Any extra arguments which shouldn't be passed on to plan() should be
134 stripped off by this method.
136 See Test::More for an example of its use.
138 B<NOTE> This mechanism is I<VERY ALPHA AND LIKELY TO CHANGE> as it
139 feels like a bit of an ugly hack in its current form.
147 Test::Builder::Module provides some methods of getting at the underlying
148 Test::Builder object.
152 my $builder = Your::Class->builder;
154 This method returns the Test::Builder object associated with Your::Class.
155 It is not a constructor so you can call it as often as you like.
157 This is the preferred way to get the Test::Builder object. You should
158 I<not> get it via C<< Test::Builder->new >> as was previously
161 The object returned by builder() may change at runtime so you should
162 call builder() inside each function rather than store it in a global.
165 my $builder = Your::Class->builder;
167 return $builder->ok(@_);
174 return Test::Builder->new;