1 package Test::SubCalls;
7 Test::SubCalls - Track the number of times subs are called
13 # Start tracking calls to a named sub
14 sub_track( 'Foo::foo' );
19 # Test that some sub deep in the codebase was called
20 # a specific number of times.
21 sub_calls( 'Foo::foo', 5 );
22 sub_calls( 'Foo::foo', 5, 'Use a custom test message' );
24 # Reset the counts for one or all subs
25 sub_reset( 'Foo::foo' );
30 There are a number of different situations (like testing caching code)
31 where you want to want to do a number of tests, and then verify that
32 some underlying subroutine deep within the code was called a specific
35 This module provides a number of functions for doing testing in this way
36 in association with your normal L<Test::More> (or similar) test scripts.
40 In the nature of test modules, all functions are exported by default.
46 use File::Spec 0.80 ();
47 use Test::More 0.42 ();
48 use Hook::LexWrap 0.20 ();
52 use vars qw{$VERSION @ISA @EXPORT};
56 @EXPORT = qw{sub_track sub_calls sub_reset sub_reset_all};
59 my $Test = Test::Builder->new;
67 #####################################################################
68 # Test::SubCalls Functions
72 =head2 sub_track $subname
74 The C<sub_track> function creates a new call tracker for a named function.
76 The sub to track must be provided by name, references to the function
77 itself are insufficient.
79 Returns true if added, or dies on error.
84 # Check the sub name is valid
88 unless ( defined *{"$subname"}{CODE} ) {
89 die "Test::SubCalls::sub_track : The sub '$subname' does not exist";
91 if ( defined $CALLS{$subname} ) {
92 die "Test::SubCalls::sub_track : Cannot add duplicate tracker for '$subname'";
96 # Initialise the count
99 # Lexwrap the subroutine
102 pre => sub { $CALLS{$subname}++ },
110 =head2 sub_calls $subname, $expected_calls [, $message ]
112 The C<sub_calls> function is the primary (and only) testing function
113 provided by C<Test::SubCalls>. A single call will represent one test in
116 It takes the subroutine name as originally provided to C<sub_track>,
117 the expected number of times the subroutine should have been called,
118 and an optional test message.
120 If no message is provided, a default message will be provided for you.
122 Test is ok if the number of times the sub has been called matches the
123 expected number, or not ok if not.
128 # Check the sub name is valid
130 unless ( defined $CALLS{$subname} ) {
131 die "Test::SubCalls::sub_calls : Cannot test untracked sub '$subname'";
136 unless ( $count =~ /^(?:0|[1-9]\d*)\z/s ) {
137 die "Test::SubCalls::sub_calls : Expected count '$count' is not an integer";
140 # Get the message, applying default if needed
141 my $message = shift || "$subname was called $count times";
142 $Test->is_num( $CALLS{$subname}, $count, $message );
147 =head2 sub_reset $subname
149 To prevent repeat users from having to take before and after counts when
150 they start testing from after zero, the C<sub_reset> function has been
151 provided to reset a sub call counter to zero.
153 Returns true or dies if the sub name is invalid or not currently tracked.
158 # Check the sub name is valid
160 unless ( defined $CALLS{$subname} ) {
161 die "Test::SubCalls::sub_reset : Cannot reset untracked sub '$subname'";
164 $CALLS{$subname} = 0;
173 Provided mainly as a convenience, the C<sub_reset_all> function will reset
174 all the counters currently defined.
181 foreach my $subname ( keys %CALLS ) {
182 $CALLS{$subname} = 0;
193 Bugs should be submitted via the CPAN bug tracker, located at
195 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test-SubCalls>
197 For other issues, or commercial enhancement or support, contact the author.
201 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
205 L<http://ali.as/>, L<Test::Builder>, L<Test::More>, L<Hook::LexWrap>
209 Copyright 2005 - 2009 Adam Kennedy.
211 This program is free software; you can redistribute
212 it and/or modify it under the same terms as Perl itself.
214 The full text of the license can be found in the
215 LICENSE file included with this module.