Track /etc/gitconfig
[msysgit/mtrensch.git] / lib / perl5 / 5.8.8 / Test / Builder / Module.pm
blobb3ccce63292ada99a662aa379cc112cda5fb795b
1 package Test::Builder::Module;
3 use Test::Builder;
5 require Exporter;
6 @ISA = qw(Exporter);
8 $VERSION = '0.02';
10 use strict;
12 # 5.004's Exporter doesn't have export_to_level.
13 my $_export_to_level = sub {
14 my $pkg = shift;
15 my $level = shift;
16 (undef) = shift; # redundant arg
17 my $callpkg = caller($level);
18 $pkg->export($callpkg, @_);
22 =head1 NAME
24 Test::Builder::Module - Base class for test modules
26 =head1 SYNOPSIS
28 # Emulates Test::Simple
29 package Your::Module;
31 my $CLASS = __PACKAGE__;
33 use base 'Test::Builder::Module';
34 @EXPORT = qw(ok);
36 sub ok ($;$) {
37 my $tb = $CLASS->builder;
38 return $tb->ok(@_);
44 =head1 DESCRIPTION
46 This is a superclass for Test::Builder-based modules. It provides a
47 handful of common functionality and a method of getting at the underlying
48 Test::Builder object.
51 =head2 Importing
53 Test::Builder::Module is a subclass of Exporter which means your
54 module is also a subclass of Exporter. @EXPORT, @EXPORT_OK, etc...
55 all act normally.
57 A few methods are provided to do the C<use Your::Module tests => 23> part
58 for you.
60 =head3 import
62 Test::Builder::Module provides an import() method which acts in the
63 same basic way as Test::More's, setting the plan and controling
64 exporting of functions and variables. This allows your module to set
65 the plan independent of Test::More.
67 All arguments passed to import() are passed onto
68 C<< Your::Module->builder->plan() >> with the exception of
69 C<import =>[qw(things to import)]>.
71 use Your::Module import => [qw(this that)], tests => 23;
73 says to import the functions this() and that() as well as set the plan
74 to be 23 tests.
76 import() also sets the exported_to() attribute of your builder to be
77 the caller of the import() function.
79 Additional behaviors can be added to your import() method by overriding
80 import_extra().
82 =cut
84 sub import {
85 my($class) = shift;
87 my $test = $class->builder;
89 my $caller = caller;
91 $test->exported_to($caller);
93 $class->import_extra(\@_);
94 my(@imports) = $class->_strip_imports(\@_);
96 $test->plan(@_);
98 $class->$_export_to_level(1, $class, @imports);
102 sub _strip_imports {
103 my $class = shift;
104 my $list = shift;
106 my @imports = ();
107 my @other = ();
108 my $idx = 0;
109 while( $idx <= $#{$list} ) {
110 my $item = $list->[$idx];
112 if( defined $item and $item eq 'import' ) {
113 push @imports, @{$list->[$idx+1]};
114 $idx++;
116 else {
117 push @other, $item;
120 $idx++;
123 @$list = @other;
125 return @imports;
129 =head3 import_extra
131 Your::Module->import_extra(\@import_args);
133 import_extra() is called by import(). It provides an opportunity for you
134 to add behaviors to your module based on its import list.
136 Any extra arguments which shouldn't be passed on to plan() should be
137 stripped off by this method.
139 See Test::More for an example of its use.
141 B<NOTE> This mechanism is I<VERY ALPHA AND LIKELY TO CHANGE> as it
142 feels like a bit of an ugly hack in its current form.
144 =cut
146 sub import_extra {}
149 =head2 Builder
151 Test::Builder::Module provides some methods of getting at the underlying
152 Test::Builder object.
154 =head3 builder
156 my $builder = Your::Class->builder;
158 This method returns the Test::Builder object associated with Your::Class.
159 It is not a constructor so you can call it as often as you like.
161 This is the preferred way to get the Test::Builder object. You should
162 I<not> get it via C<< Test::Builder->new >> as was previously
163 recommended.
165 The object returned by builder() may change at runtime so you should
166 call builder() inside each function rather than store it in a global.
168 sub ok {
169 my $builder = Your::Class->builder;
171 return $builder->ok(@_);
175 =cut
177 sub builder {
178 return Test::Builder->new;