1Test::Trap::Builder(3)User Contributed Perl DocumentationTest::Trap::Builder(3)
2
3
4
6 Test::Trap::Builder - Backend for building test traps
7
9 Version 0.2.1
10
12 package My::Test::Trap;
13
14 use Test::Trap::Builder;
15 my $B = Test::Trap::Builder->new;
16
17 $B->layer( $layer_name => \&layer_implementation );
18 $B->accessor( simple => [ $layer_name ] );
19
20 $B->multi_layer( $multi_name => @names );
21
22 $B->test( $test_name => 'trap, predicate, name', \&test_function );
23
25 Test::Trap neither traps nor tests everything you may want to trap or
26 test. So, Test::Trap::Builder provides methods to write your own trap
27 layers, accessors, and test callbacks -- preferably for use with your
28 own modules (trappers).
29
30 Note that layers are methods with mangled names (names are prefixed
31 with "layer:"), and so inherited like any other method, while accessors
32 are ordinary methods. Meanwhile, test callbacks are not referenced in
33 the symbol table by themselves, but only in combinations with
34 accessors, all methods of the form ACCESSOR_TEST.
35
37 Trappers should not inherit from Test::Trap::Builder, but may import a
38 few convenience methods for use in building the trap. Do not use them
39 as methods of Test::Trap::Builder -- they are intended to be methods of
40 trap objects. (If you inherit from another trapper, you need not, and
41 probably should not, import these yourself -- you should inherit these
42 methods like any other.)
43
44 Trappers may import any number of these methods, or all of them by way
45 of the ":methods" tag.
46
47 Layers should be implemented as methods, and while they need not call
48 any of these convenience methods in turn, that likely makes for more
49 readable code than any alternative. Likewise, test callbacks may use
50 convenience methods for more readable code.
51
52 Of course, certain convenience methods may also be useful in more
53 generic methods messing with trap or builder objects.
54
55 Prop [PACKAGE]
56 A method returning a reference to a hash, holding the PACKAGE's (by
57 default the caller's) tag-on properties for the (current) trap object.
58 Currently, Test::Trap::Builder defines the following properties:
59
60 layers
61 While the trap is springing, the queue of layers remaining.
62 Usually set by the "trap" method and consumed by the "Next" method.
63
64 teardown
65 While the trap is springing, the queue of teardown actions
66 remaining. Usually accumulated through the "Teardown" method and
67 invoked by the "trap" method.
68
69 code
70 The user code trapped. Usually set by the "trap" method and
71 invoked by the "Run" method.
72
73 exception
74 An internal exception. Usually set through the "Exception" method
75 and examined by the "trap" method.
76
77 on_test_failure
78 A callback invoked by the "TestFailure" method. Layers in
79 particular may want to set this.
80
81 test_accessor
82 The name and (optionally) the index of the accessor, the contents
83 of which we're currently testing. Best accessed through the
84 "TestAccessor" method, and usually set by the "test" and "accessor"
85 methods, but if you are writing your own tests or accessors
86 directly, you just might need to set it. Perhaps.
87
88 Be nice: Treat another module's tag-on properties as you would treat
89 another module's global variables. Don't use them except as
90 documented.
91
92 Example:
93
94 # in a layer, setting the callback for TestFailure:
95 $self->Prop('Test::Trap::Builder')->{on_test_failure} = \&mydiag;
96
97 DESTROY
98 This cleans up the tag-on properties when the trap object is destroyed.
99 Don't try to make a trapper that doesn't call this; it will get
100 confused.
101
102 If your trapper needs its own "DESTROY", make sure it calls this one as
103 well:
104
105 sub DESTROY {
106 my $self = shift;
107 # do your thing
108 $self->Test::Trap::Builder::DESTROY;
109 # and more things
110 }
111
112 Run
113 A terminating layer should call this method to run the user code.
114 Should only be called in a dynamic context in which layers are being
115 applied.
116
117 Next
118 Every non-terminating layer should call this method (or an equivalent)
119 to progress to the next layer. Should only be called in a dynamic
120 context in which layers are being applied. Note that this method need
121 not return, so any tear-down actions should probably be registered with
122 the Teardown method (see below).
123
124 Teardown SUBS
125 If your layer wants to clean up its setup, it may use this method to
126 register any number of tear-down actions, to be performed (in reverse
127 registration order) once the user code has been executed. Should only
128 be called in a dynamic context in which layers are being applied.
129
130 TestAccessor
131 Returns a string of the form "NAME(INDEX)", where NAME and INDEX are
132 the name of the accessor and the index (if any) being tested. Should
133 only be called in the dynamic context of test callbacks.
134
135 This is intended for diagnostics:
136
137 diag( sprintf 'Expected %s in %s; got %s',
138 $expected, $self->TestAccessor, dump($got),
139 );
140
141 TestFailure
142 Runs the "on_test_failure" tag-on property (if any) on the trap object.
143 If you are writing unregistered tests, you might want to include (some
144 variation of) this call:
145
146 $ok or $self->TestFailure;
147
148 Exception STRINGS
149 Layer implementations may run into exceptional situations, in which
150 they want the entire trap to fail. Unfortunately, another layer may be
151 trapping ordinary exceptions, so you need some kind of magic in order
152 to throw an untrappable exception. This is one convenient way.
153
154 Should only be called in a dynamic context in which layers are being
155 applied.
156
157 Note: The Exception method won't work if called from outside of the
158 regular control flow, like inside a DESTROY method or signal handler.
159 If anything like this happens, CORE::exit will be called with an exit
160 code of 8.
161
163 new
164 Returns a singleton object. Don't expect this module to work with a
165 different instance object of this class.
166
167 trap TRAPPER, GLOBREF, LAYERARRAYREF, CODE
168 Implements a trap for the TRAPPER module, applying the layers of
169 LAYERARRAYREF, trapping various outcomes of the user CODE, and storing
170 the trap object into the scalar slot of GLOBREF.
171
172 In most cases, the trapper should conveniently export a function
173 calling this method.
174
175 layer NAME, CODE
176 Registers a layer by NAME to the calling trapper. When the layer is
177 applied, the CODE will be invoked on the trap object being built, with
178 no arguments, and should call either the Next() or Run() method or
179 equivalent.
180
181 output_layer NAME, GLOBREF
182 Registers (by NAME and to the calling trapper) a layer for trapping
183 output on the file handle of the GLOBREF, using NAME also as the
184 attribute name.
185
186 output_layer_backend NAME, [CODE]
187 When called with two arguments, registers (by NAME and globally) a
188 backend for output trap layers. When called with a single argument,
189 looks up and returns the backend registered by NAME (or undef).
190
191 When a layer using this backend is applied, the CODE will be called on
192 the trap object, with the layer name and the output handle's fileno and
193 globref as arguments.
194
195 first_output_layer_backend SPEC
196 Where SPEC is empty, just returns.
197
198 Where SPEC is a string of comma-or-semicolon separated backend names,
199 runs through the names, returning the first implementation it finds.
200 Dies if no implementation is found by any of these names.
201
202 multi_layer NAME, LAYERS
203 Registers (by NAME) a layer that just pushes a number of other LAYERS
204 on the stack of layers. If any of the LAYERS is neither an anonymous
205 method nor the name of a layer registered to the caller or a trapper it
206 inherits from, an exception is raised.
207
208 layer_implementation TRAPPER, LAYERS
209 Returns the subroutines that implement the requested LAYERS. If any of
210 the LAYERS is neither an anonymous method nor the name of a layer
211 registered to or inherited by the TRAPPER, an exception is raised.
212
213 accessor NAMED_ARGS
214 Generates and registers any number of accessors according to the
215 NAMED_ARGS, and also generates the proper test methods for these
216 accessors (see below).
217
218 The following named arguments are recognized:
219
220 is_leaveby
221 If true, the tests methods will generate better diagnostics if the
222 trap was not left as specified. Also, a special did_ACCESSOR test
223 method will be generated (unless already present), simply passing
224 as long as the trap was left as specified.
225
226 is_array
227 If true, the simple accessor(s) will be smart about context and
228 arguments, returning an arrayref on no argument (in any context),
229 an array slice in list context (on any number of arguments), and
230 the element indexed by the first argument otherwise.
231
232 simple
233 Should be a reference to an array of accessor names. For each
234 name, an accessor (assuming hash based trap object with accessor
235 names as keys), will be generated and registered.
236
237 flexible
238 Should be a reference to a hash. For each pair, a name and an
239 implementation, an accessor is generated and registered.
240
241 test NAME, ARGSPEC, CODE
242 Registers a test callback by NAME and to the calling trapper.
243
244 Trappers inherit test callbacks like methods (though they are not
245 implemented as such; don't expect to find them in the symbol table).
246
247 Test methods of the form ACCESSOR_TEST will be made available (directly
248 or by inheritence) to every trapper that registers or inherits both the
249 accessor named ACCESSOR and the test named TEST.
250
251 (In more detail, the method will be generated in every trapper that
252 either (1) registers both the test and the accessor, or (2) registers
253 either and inherits the other.)
254
255 When the test method is called, any implicit leaveby condition will be
256 tested first, and if it passes (or there were none), the CODE is called
257 with arguments according to the words found in the ARGSPEC string:
258
259 trap
260 The trap object.
261
262 entirety
263 The ACCESSOR's return value when called without arguments.
264
265 element
266 The ACCESSOR's return value when called with index, if applicable
267 (i.e. for array accessors). Index is not applicable to scalar
268 accessors, so such are still called without index.
269
270 The index, when applicable, will be taken from the test method's
271 arguments.
272
273 predicate
274 What the ACCESSOR's return value should be tested against (taken
275 from the test method's arguments). (There may be any fixed number
276 of predicates.)
277
278 name
279 The test name (taken from the test method's arguments).
280
282 A complete example, implementing a timeout layer (depending on
283 Time::HiRes::ualarm being present), a simpletee layer (printing the
284 trapped stdout/stderr to the original file handles after the trap has
285 sprung), and a cmp_ok test method template:
286
287 package My::Test::Trap;
288 use base 'Test::Trap'; # for example
289 use Test::Trap::Builder;
290
291 my $B = Test::Trap::Builder->new;
292
293 # example (layer:timeout):
294 use Time::HiRes qw/ualarm/;
295 $B->layer( timeout => $_ ) for sub {
296 my $self = shift;
297 eval {
298 local $SIG{ALRM} = sub {
299 $self->{timeout} = 1; # simple truth
300 $SIG{ALRM} = sub {die};
301 die;
302 };
303 ualarm 1000, 1; # one second max, then die repeatedly!
304 $self->Next;
305 };
306 alarm 0;
307 if ($self->{timeout}) {
308 $self->{leaveby} = 'timeout';
309 delete $self->{$_} for qw/ die exit return /;
310 }
311 };
312 $B->accessor( is_leaveby => 1,
313 simple => ['timeout'],
314 );
315
316 # example (layer:simpletee):
317 $B->layer( simpletee => $_ ) for sub {
318 my $self = shift;
319 for (qw/ stdout stderr /) {
320 exists $self->{$_} or $self->Exception("Too late to tee $_");
321 }
322 $self->Teardown($_) for sub {
323 print STDOUT $self->{stdout} if exists $self->{stdout};
324 print STDERR $self->{stderr} if exists $self->{stderr};
325 };
326 $self->Next;
327 };
328 # no accessor for this layer
329
330 $B->multi_layer( flow => qw/ raw die exit timeout / );
331 $B->multi_layer( default => qw/ flow stdout stderr warn simpletee / );
332
333 $B->test_method( cmp_ok => 1, 2, \&Test::More::cmp_ok );
334
336 The interface of this module is likely to remain somewhat in flux for a
337 while yet.
338
339 The different implementations of output trap layers have their own
340 caveats; see Test::Trap::Builder::Tempfile,
341 Test::Trap::Builder::PerlIO, Test::Trap::Builder::SystemSafe.
342
343 Multiple inheritance is not (yet?) fully supported. If one parent has
344 registered a test callback "X" and another has registered an accessor
345 "Y", the test method "Y_X" will not be generated.
346
347 Threads? No idea. It might even work correctly.
348
350 Please report any bugs or feature requests directly to the author.
351
353 Eirik Berg Hanssen, "<ebhanssen@allverden.no>"
354
356 Copyright 2006-2008 Eirik Berg Hanssen, All Rights Reserved.
357
358 This program is free software; you can redistribute it and/or modify it
359 under the same terms as Perl itself.
360
361
362
363perl v5.12.3 2011-05-06 Test::Trap::Builder(3)