/usr/share/perl5/Test/SubCalls.pm is in libtest-subcalls-perl 1.09-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | package Test::SubCalls;
=pod
=head1 NAME
Test::SubCalls - Track the number of times subs are called
=head1 SYNOPSIS
use Test::SubCalls;
# Start tracking calls to a named sub
sub_track( 'Foo::foo' );
# Run some test code
...
# Test that some sub deep in the codebase was called
# a specific number of times.
sub_calls( 'Foo::foo', 5 );
sub_calls( 'Foo::foo', 5, 'Use a custom test message' );
# Reset the counts for one or all subs
sub_reset( 'Foo::foo' );
sub_reset_all();
=head1 DESCRIPTION
There are a number of different situations (like testing caching code)
where you want to want to do a number of tests, and then verify that
some underlying subroutine deep within the code was called a specific
number of times.
This module provides a number of functions for doing testing in this way
in association with your normal L<Test::More> (or similar) test scripts.
=head1 FUNCTIONS
In the nature of test modules, all functions are exported by default.
=cut
use 5.006;
use strict;
use File::Spec 0.80 ();
use Test::More 0.42 ();
use Hook::LexWrap 0.20 ();
use Exporter ();
use Test::Builder ();
use vars qw{$VERSION @ISA @EXPORT};
BEGIN {
$VERSION = '1.09';
@ISA = 'Exporter';
@EXPORT = qw{sub_track sub_calls sub_reset sub_reset_all};
}
my $Test = Test::Builder->new;
my %CALLS = ();
#####################################################################
# Test::SubCalls Functions
=pod
=head2 sub_track $subname
The C<sub_track> function creates a new call tracker for a named function.
The sub to track must be provided by name, references to the function
itself are insufficient.
Returns true if added, or dies on error.
=cut
sub sub_track {
# Check the sub name is valid
my $subname = shift;
SCOPE: {
no strict 'refs';
unless ( defined *{"$subname"}{CODE} ) {
die "Test::SubCalls::sub_track : The sub '$subname' does not exist";
}
if ( defined $CALLS{$subname} ) {
die "Test::SubCalls::sub_track : Cannot add duplicate tracker for '$subname'";
}
}
# Initialise the count
$CALLS{$subname} = 0;
# Lexwrap the subroutine
Hook::LexWrap::wrap(
$subname,
pre => sub { $CALLS{$subname}++ },
);
1;
}
=pod
=head2 sub_calls $subname, $expected_calls [, $message ]
The C<sub_calls> function is the primary (and only) testing function
provided by C<Test::SubCalls>. A single call will represent one test in
your plan.
It takes the subroutine name as originally provided to C<sub_track>,
the expected number of times the subroutine should have been called,
and an optional test message.
If no message is provided, a default message will be provided for you.
Test is ok if the number of times the sub has been called matches the
expected number, or not ok if not.
=cut
sub sub_calls {
# Check the sub name is valid
my $subname = shift;
unless ( defined $CALLS{$subname} ) {
die "Test::SubCalls::sub_calls : Cannot test untracked sub '$subname'";
}
# Check the count
my $count = shift;
unless ( $count =~ /^(?:0|[1-9]\d*)\z/s ) {
die "Test::SubCalls::sub_calls : Expected count '$count' is not an integer";
}
# Get the message, applying default if needed
my $message = shift || "$subname was called $count times";
$Test->is_num( $CALLS{$subname}, $count, $message );
}
=pod
=head2 sub_reset $subname
To prevent repeat users from having to take before and after counts when
they start testing from after zero, the C<sub_reset> function has been
provided to reset a sub call counter to zero.
Returns true or dies if the sub name is invalid or not currently tracked.
=cut
sub sub_reset {
# Check the sub name is valid
my $subname = shift;
unless ( defined $CALLS{$subname} ) {
die "Test::SubCalls::sub_reset : Cannot reset untracked sub '$subname'";
}
$CALLS{$subname} = 0;
1;
}
=pod
=head2 sub_reset_all
Provided mainly as a convenience, the C<sub_reset_all> function will reset
all the counters currently defined.
Returns true.
=cut
sub sub_reset_all {
foreach my $subname ( keys %CALLS ) {
$CALLS{$subname} = 0;
}
1;
}
1;
=pod
=head1 SUPPORT
Bugs should be submitted via the CPAN bug tracker, located at
L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test-SubCalls>
For other issues, or commercial enhancement or support, contact the author.
=head1 AUTHOR
Adam Kennedy E<lt>adamk@cpan.orgE<gt>
=head1 SEE ALSO
L<http://ali.as/>, L<Test::Builder>, L<Test::More>, L<Hook::LexWrap>
=head1 COPYRIGHT
Copyright 2005 - 2009 Adam Kennedy.
This program is free software; you can redistribute
it and/or modify it under the same terms as Perl itself.
The full text of the license can be found in the
LICENSE file included with this module.
=cut
|