1 package Test::Builder::Module;
8 our @ISA = qw(Exporter);
10 our $VERSION = '0.94';
11 $VERSION = eval $VERSION; ## no critic (BuiltinFunctions::ProhibitStringyEval)
16 Test::Builder::Module - Base class for test modules
20 # Emulates Test::Simple
23 my $CLASS = __PACKAGE__;
25 use base 'Test::Builder::Module';
29 my $tb = $CLASS->builder;
38 This is a superclass for Test::Builder-based modules. It provides a
39 handful of common functionality and a method of getting at the underlying
45 Test::Builder::Module is a subclass of Exporter which means your
46 module is also a subclass of Exporter. @EXPORT, @EXPORT_OK, etc...
49 A few methods are provided to do the C<use Your::Module tests => 23> part
54 Test::Builder::Module provides an import() method which acts in the
55 same basic way as Test::More's, setting the plan and controling
56 exporting of functions and variables. This allows your module to set
57 the plan independent of Test::More.
59 All arguments passed to import() are passed onto
60 C<< Your::Module->builder->plan() >> with the exception of
61 C<import =>[qw(things to import)]>.
63 use Your::Module import => [qw(this that)], tests => 23;
65 says to import the functions this() and that() as well as set the plan
68 import() also sets the exported_to() attribute of your builder to be
69 the caller of the import() function.
71 Additional behaviors can be added to your import() method by overriding
79 # Don't run all this when loading ourself.
80 return 1 if $class eq 'Test::Builder::Module';
82 my $test = $class->builder;
86 $test->exported_to($caller);
88 $class->import_extra( \@_ );
89 my(@imports) = $class->_strip_imports( \@_ );
93 $class->export_to_level( 1, $class, @imports );
103 while( $idx <= $#{$list} ) {
104 my $item = $list->[$idx];
106 if( defined $item and $item eq 'import' ) {
107 push @imports, @{ $list->[ $idx + 1 ] };
124 Your::Module->import_extra(\@import_args);
126 import_extra() is called by import(). It provides an opportunity for you
127 to add behaviors to your module based on its import list.
129 Any extra arguments which shouldn't be passed on to plan() should be
130 stripped off by this method.
132 See Test::More for an example of its use.
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.
143 Test::Builder::Module provides some methods of getting at the underlying
144 Test::Builder object.
148 my $builder = Your::Class->builder;
150 This method returns the Test::Builder object associated with Your::Class.
151 It is not a constructor so you can call it as often as you like.
153 This is the preferred way to get the Test::Builder object. You should
154 I<not> get it via C<< Test::Builder->new >> as was previously
157 The object returned by builder() may change at runtime so you should
158 call builder() inside each function rather than store it in a global.
161 my $builder = Your::Class->builder;
163 return $builder->ok(@_);
170 return Test::Builder->new;