File Coverage

blib/lib/Test/Module/Runnable.pm
Criterion Covered Total %
statement 16 16 100.0
branch n/a
condition n/a
subroutine 10 10 100.0
pod 6 6 100.0
total 32 32 100.0


line stmt bran cond sub pod time code
1             # Module test framework
2             # Copyright (c) 2015-2019, Duncan Ross Palmer (2E0EOL) and others,
3             # All rights reserved.
4             #
5             # Redistribution and use in source and binary forms, with or without
6             # modification, are permitted provided that the following conditions are met:
7             #
8             # * Redistributions of source code must retain the above copyright notice,
9             # this list of conditions and the following disclaimer.
10             #
11             # * Redistributions in binary form must reproduce the above copyright
12             # notice, this list of conditions and the following disclaimer in the
13             # documentation and/or other materials provided with the distribution.
14             #
15             # * Neither the name of the Daybo Logic nor the names of its contributors
16             # may be used to endorse or promote products derived from this software
17             # without specific prior written permission.
18             #
19             # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20             # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21             # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22             # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23             # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24             # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25             # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26             # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27             # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28             # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29             # POSSIBILITY OF SUCH DAMAGE.
30              
31             =head1 NAME
32              
33             Test::Module::Runnable - A runnable framework on Moose for running tests
34              
35             =head1 SYNOPSIS
36              
37             package YourTestSuite;
38             use Moose;
39             use Test::More 0.96;
40              
41             extends 'Test::Module::Runnable';
42              
43             sub helper { } # Not called
44              
45             sub testExample { } # Automagically called due to 'test' prefix.
46              
47             package main;
48              
49             my $tester = new YourTestSuite;
50             return $tester->run;
51              
52             B<Deprecated> alternative:
53              
54             my $tester = new YourTestSuite;
55             plan tests => $tester->testCount;
56             foreach my $name ($tester->testMethods) {
57             subtest $name => $tester->$name;
58             }
59              
60             =head1 DESCRIPTION
61              
62             A test framework based on Moose introspection to automagically
63             call all methods matching a user-defined pattern. Supports per-test
64             setup and tear-down routines and easy early L<Test::Builder/BAIL_OUT> using
65             L<Test::More>.
66              
67             =cut
68              
69             package Test::Module::Runnable;
70 4     4   1566624 use Moose;
  4         499890  
  4         30  
71 4     4   30792 use Test::More 0.96;
  4         190659  
  4         31  
72 4     4   1603 use POSIX qw/EXIT_SUCCESS/;
  4         6759  
  4         31  
73              
74             BEGIN {
75 4     4   2390 our $VERSION = '0.4.2';
76             }
77              
78             extends 'Test::Module::Runnable::Base';
79              
80             =head1 ATTRIBUTES
81              
82             =over
83              
84             =item C<sut>
85              
86             System under test - a generic slot for an object you are testing, which
87             could be re-initialized under the C<setUp> routine, but this entry may be
88             ignored.
89              
90             =item C<mocker>
91              
92             This slot can be used during L</setUpBeforeClass> to set up a C<Test::MockModule>
93             for the L</sut> class being tested. If set, C<< mocker->unmock_all() >> will be
94             called automagically, just after each test method is executed.
95             This will allow different methods to to be mocked, which are not directly relevant
96             to the test method being executed.
97              
98             By default, this slot is C<undef>
99              
100             =item C<pattern>
101              
102             The pattern which defines which user-methods are considered tests.
103             Defaults to C<^test>.
104             Methods matching this pattern will be returned from L</methodNames>
105              
106             =item C<logger>
107              
108             A generic slot for a loggger, to be initialized with your logging framework,
109             or a mock logging system.
110              
111             This slot is not touched by this package, but might be passed on to
112             your L</sut>, or you may wish to clear it between tests by sub-classing
113             this package.
114              
115             =back
116              
117             =head1 METHODS
118              
119             =over
120              
121             =item C<methodNames>
122              
123             Returns a list of all names of test methods which should be called by C</subtest>,
124             ie. all method names beginning with 'test', or the user-defined L</pattern>.
125              
126             If you use L</run>, this is handled automagically.
127              
128             =item C<debug>
129              
130             Call C<Test::Builder::diag> with a user-defined message,
131             if and only if the C<TEST_VERBOSE> environment variable is set.
132              
133             =item C<mock($class, $method, $return)>
134              
135             This mocks the given method on the specified class, with the specified
136             return value, described below. Additionally, stores internally a log of all
137             method calls, and their arguments. Note that the first argument is not
138             saved, i.e. the object on which the method was called, as this is rarely useful
139             in a unit test comparison.
140              
141             The return value, C<$return>, may be specified in one of two ways:
142              
143             =over
144              
145             =item A C<CODE> reference
146              
147             In which case the code reference is simply called
148             each time, with all arguments as passed to the mocked function, and the
149             return value passed as-is to the caller. Note that care is taken that
150             if the mocked method is called in array context, the code reference is
151             called in array context, and likewise for scalar context.
152              
153             =item An C<ARRAY> reference
154              
155             In which case, a value is shifted from the front
156             of the array. If the value removed is itself a C<CODE> ref the code
157             reference is called, and its return value returned, as described above,
158             otherwise the value is returned as-is.
159              
160             Note that you cannot return a list by adding it to an array, so if you need to
161             use the array form, and also return a list, you will need to add a C<CODE> reference into the array:
162              
163             $self->mock($class, $method, [
164             1, # first call returns scalar '1'
165             [2,3,4], # second call returns array reference
166             sub { return (5,6,7) }, # third call returns a list
167             ]);
168              
169             =back
170              
171             If no value is specified, or if the specified array is exhaused, then either
172             C<undef> or an empty array is returned, depending on context.
173              
174             Calls including arguments and return values are passed to the L</debug>
175             method.
176              
177             =item unmock([class], [$method])
178              
179             Clears all mock objects.
180              
181             If no arguments are specified L<Test::Module::Runnable::Base/clearMocks> is called.
182              
183             Is a class is specified, only that class is cleared.
184              
185             If a method is specified too, only that method of that mocked class is cleared
186             (not methods by the same name under other classes).
187              
188             It is not legal to unmock a method in many or unspecified classes,
189             doing so will invoke C<die()>.
190              
191             The reference to the the tester is returned.
192              
193             =item C<mockCalls($class, $method)>
194              
195             Return a reference to an array of the calls made to the specified mocked function. Each entry in the arrayref
196             is an arrayref of the arguments to that call, B<excluding> the object reference itself (i.e. C<$self>).
197              
198             =item C<mockCallsWithObject($class, $method)>
199              
200             Return a reference to an array of the calls made to the specified mocked function. Each entry in the arrayref
201             is an arrayref of the arguments to that call, B<including> the object reference itself (i.e. C<$self>).
202              
203             This method is strongly encouraged in preference to L</mockCalls($class,
204             $method)> if your test constructs multiple instances of the same class,
205             so that you know that the right method calls were actually made on the
206             right object.
207              
208             Normal usage:
209              
210             cmp_deeply($self->mockCallsWithObject($class, $method), [
211             [ shallow($instance1), $arg1, $arg2 ],
212             [ shallow($instance2), $otherArg1, $otherArg2 ],
213             ...
214             ], 'correct method calls');
215              
216             =item C<unique>
217              
218             Returns a unique, integer ID, which is predictable.
219              
220             An optional C<$domain> can be specified, which is a discrete sequence,
221             isolated from anhy other domain. If not specified, a default domain is used.
222             The actual name for this domain is opaque, and is specified by
223             L<Test::Module::Runnable::Base/__unique_default_domain>.
224              
225             A special domain; C<rand> can be used for random numbers which will not repeat.
226              
227             =item C<methodCount>
228              
229             Returns the number of tests to pass to C<plan>
230             If you use L</run>, this is handled automagically.
231              
232             =item C<clearMocks>
233              
234             Forcibly clear all mock objects, if required e.g. in C<tearDown>.
235              
236             =back
237              
238             =head1 PROTECTED METHODS
239              
240             =over
241              
242             =item C<_mockdump>
243              
244             Helper method for dumping arguments and return values from C<mock> function.
245              
246             =back
247              
248             =head1 USER DEFINED METHODS
249              
250             =over
251              
252             =item C<setUpBeforeClass>
253              
254             If you need to initialize your test suite before any tests run,
255             this hook is your opportunity. If the setup fails, you should
256             return C<EXIT_FAILURE>. you must return C<EXIT_SUCCESS> in order
257             for tests to proceed.
258              
259             Don't write code here! Override the method in your test class.
260              
261             The default action is to do nothing.
262              
263             =cut
264              
265             sub setUpBeforeClass {
266 2     2 1 8 return EXIT_SUCCESS;
267             }
268              
269             =item C<tearDownAfterClass>
270              
271             If you need to finalize any cleanup for your test suite, after all
272             tests have completed running, this hook is your opportunity. If the
273             cleanup fails, you should return C<EXIT_FAILURE>. If cleanup succeeds,
274             you should return C<EXIT_SUCCESS>. You can also perform final sanity checking
275             here, because retuning C<EXIT_FAILURE> causes the suite to call
276             L<Test::Builder/BAIL_OUT>.
277              
278             Don't write code here! Override the method in your test class.
279              
280             The default action is to do nothing.
281              
282             =cut
283              
284             sub tearDownAfterClass {
285 3     3 1 8 return EXIT_SUCCESS;
286             }
287              
288             =item C<setUp>
289              
290             If you need to perform per-test setup, ie. before individual test methods
291             run, you should override this hook. You must return C<EXIT_SUCCESS> from
292             the hook, otherwise the entire test suite will be aborted via L<Test::Builder/BAIL_OUT>.
293              
294             Don't write code here! Override the method in your test class.
295              
296             The default action is to do nothing.
297              
298             =cut
299              
300             sub setUp {
301 1     1 1 2 return EXIT_SUCCESS;
302             }
303              
304             =item C<tearDown>
305              
306             If you need to perform per-test cleanup, ie. after individual test methods
307             run, you should override this hook. You must return C<EXIT_SUCCESS> from
308             the hook, otherwise the entire test suite will be aborted via L<Test::Builder/BAIL_OUT>.
309              
310             Don't write code here! Override the method in your test class.
311              
312             The default action is to do nothing.
313              
314             =cut
315              
316             sub tearDown {
317 3     3 1 7 return EXIT_SUCCESS;
318             }
319              
320             =item C<modeName>
321              
322             If set, this routine will be called from the internal
323             L<Test::Module::Runnable::Base/__generateMethodName>
324             method, which is used to generate the method name displyed to the user. This
325             name should represent the mode of testing currently in use, for example.
326             you may be re-running all the tests to test a different database driver.
327              
328             If C<undef> or an empty string is returned, the result is ignored, as if you
329             had not defined this method.
330              
331             SEE ALSO L</modeSwitch>
332              
333             This is a dummy method which just returns C<undef>.
334             User test classes can override this.
335              
336             =cut
337              
338             sub modeName {
339 35     35 1 77 return;
340             }
341              
342             =item C<modeSwitch>
343              
344             If set, this routine will be called between test runs.
345             This is typically used by setting an C<n> value of at least C<2>.
346             Every time the test suite finishes, this routine is called, and
347             you can replace a L</sut> or set a flag so that all tests can then
348             run with an underlying assumption shared between the tests inverted,
349             for example, with a different database driver.
350              
351             The return value from your registered C<modeSwitch CODE> reference
352             should be zero to indicate success. Your routine will be passed the
353             current C<n> iteration, starting with zero.
354              
355             This is the default action for switching the mode of the test between
356             iterations is to report success but do nothing.
357             Testers which are subclasses may override this method.
358              
359             =cut
360              
361             sub modeSwitch {
362 18     18 1 44 return EXIT_SUCCESS;
363             }
364              
365             =item C<run>
366              
367             Executes all of the tests, in a random order
368             An optional override may be passed with the tests parameter.
369              
370             * tests
371             An ARRAY ref which contains the inclusive list of all tests
372             to run. If not passed, all tests are run. If an empty list
373             is passed, no tests are run. If a test does not exist, C<confess>
374             is called.
375              
376             * n
377             Number of times to iterate through the tests.
378             Defaults to 1. Setting to a higher level is useful if you want to
379             prove that the random ordering of tests does not break, but you do
380             not want to type 'make test' many times.
381              
382             Returns:
383             The return value is always C<EXIT_SUCCESS>, which you can pass straight
384             to C<exit>
385              
386             =back
387              
388             =head1 AUTHOR
389              
390             Duncan Ross Palmer, 2E0EOL L<mailto:palmer@overchat.org>
391              
392             =head1 LICENCE
393              
394             Daybo Logic Shared Library
395             Copyright (c) 2015-2019, Duncan Ross Palmer (2E0EOL), Daybo Logic
396             All rights reserved.
397              
398             Redistribution and use in source and binary forms, with or without
399             modification, are permitted provided that the following conditions are met:
400              
401             * Redistributions of source code must retain the above copyright notice,
402             this list of conditions and the following disclaimer.
403              
404             * Redistributions in binary form must reproduce the above copyright
405             notice, this list of conditions and the following disclaimer in the
406             documentation and/or other materials provided with the distribution.
407              
408             * Neither the name of the Daybo Logic nor the names of its contributors
409             may be used to endorse or promote products derived from this software
410             without specific prior written permission.
411              
412             THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
413             AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
414             IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
415             ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
416             LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
417             CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
418             SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
419             INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
420             CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
421             ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
422             POSSIBILITY OF SUCH DAMAGE.
423              
424             =head1 AVAILABILITY
425              
426             L<https://metacpan.org/release/Test-Module-Runnable>
427             L<https://hg.sr.ht/~m6kvm/libtest-module-runnable-perl>
428             L<http://www.daybologic.co.uk/software.php?content=libtest-module-runnable-perl>
429              
430             =head1 CAVEATS
431              
432             None known.
433              
434             =cut
435              
436             1;