/usr/share/perl5/Test/API.pm is in libtest-api-perl 0.005-1.
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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 | use 5.006;
use strict;
use warnings;
package Test::API;
# ABSTRACT: Test a list of subroutines provided by a module
our $VERSION = '0.005'; # VERSION
use Symbol ();
use Test::Builder::Module 0.86;
our @ISA = qw/Test::Builder::Module/;
our @EXPORT = qw/public_ok import_ok class_api_ok/;
#--------------------------------------------------------------------------#
sub import_ok ($;@) { ## no critic
my $package = shift;
my %spec = @_;
for my $key (qw/export export_ok/) {
$spec{$key} ||= [];
$spec{$key} = [ $spec{$key} ] unless ref $spec{$key} eq 'ARRAY';
}
my $tb = _builder();
my @errors;
my %flagged;
my $label = "importing from $package";
return 0 unless _check_loaded( $package, $label );
# test export
{
my $test_pkg = *{ Symbol::gensym() }{NAME};
eval "package $test_pkg; use $package;"; ## no critic
my ( $ok, $missing, $extra ) = _public_ok( $test_pkg, @{ $spec{export} } );
if ( !$ok ) {
push @errors, "not exported: @$missing" if @$missing;
@flagged{@$missing} = (1) x @$missing if @$missing;
push @errors, "unexpectedly exported: @$extra" if @$extra;
@flagged{@$extra} = (1) x @$extra if @$extra;
}
}
# test export_ok
my @exportable;
for my $fcn ( _public_fcns($package) ) {
next if $flagged{$fcn}; # already complaining about this so skip
next if grep { $fcn eq $_ } @{ $spec{export} }; # exported by default
my $pkg_name = *{ Symbol::gensym() }{NAME};
eval "package $pkg_name; use $package '$fcn';"; ## no critic
my ( $ok, $missing, $extra ) = _public_ok( $pkg_name, $fcn );
if ($ok) {
push @exportable, $fcn;
}
}
my ( $missing, $extra ) = _difference( $spec{export_ok}, \@exportable, );
push @errors, "not optionally exportable: @$missing" if @$missing;
push @errors, "extra optionally exportable: @$extra" if @$extra;
# notify of results
$tb->ok( !@errors, "importing from $package" );
$tb->diag($_) for @errors;
return !@errors;
}
#--------------------------------------------------------------------------#
sub public_ok ($;@) { ## no critic
my ( $package, @expected ) = @_;
my $tb = _builder();
my $label = "public API for $package";
return 0 unless _check_loaded( $package, $label );
my ( $ok, $missing, $extra ) = _public_ok( $package, @expected );
$tb->ok( $ok, $label );
if ( !$ok ) {
$tb->diag("missing: @$missing") if @$missing;
$tb->diag("extra: @$extra") if @$extra;
}
return $ok;
}
#--------------------------------------------------------------------------#
sub class_api_ok ($;@) { ## no critic
my ( $package, @expected ) = @_;
my $tb = _builder();
my $label = "public API for class $package";
return 0 unless _check_loaded( $package, $label );
my ( $ok, $missing, $extra ) = _public_ok( $package, @expected );
# Call ->can to check if missing methods might be provided
# by parent classes...
if ( !$ok ) {
@$missing = grep { not $package->can($_) } @$missing;
$ok = not( scalar(@$missing) + scalar(@$extra) );
}
$tb->ok( $ok, $label );
if ( !$ok ) {
$tb->diag("missing: @$missing") if @$missing;
$tb->diag("extra: @$extra") if @$extra;
}
return $ok;
}
#--------------------------------------------------------------------------#
sub _builder {
return __PACKAGE__->builder;
}
#--------------------------------------------------------------------------#
sub _check_loaded {
my ( $package, $label ) = @_;
( my $path = $package ) =~ s{::}{/}g;
$path .= ".pm";
if ( $INC{$path} ) {
return 1;
}
else {
my $tb = _builder();
local $Test::Builder::Level = $Test::Builder::Level + 1;
$tb->ok( 0, $label );
$tb->diag("Module '$package' not loaded");
return;
}
}
#--------------------------------------------------------------------------#
sub _difference {
my ( $array1, $array2 ) = @_;
my ( %only1, %only2 );
@only1{@$array1} = (1) x @$array1;
delete @only1{@$array2};
@only2{@$array2} = (1) x @$array2;
delete @only2{@$array1};
return ( [ sort keys %only1 ], [ sort keys %only2 ] );
}
#--------------------------------------------------------------------------#
# list adapted from Pod::Coverage
my %private = map { ; $_ => 1 } qw(
import unimport bootstrap
AUTOLOAD BUILD BUILDARGS CLONE CLONE_SKIP DESTROY DEMOLISH meta
TIESCALAR TIEARRAY TIEHASH TIEHANDLE
FETCH STORE UNTIE FETCHSIZE STORESIZE POP PUSH SHIFT UNSHIFT SPLICE
DELETE EXISTS EXTEND CLEAR FIRSTKEY NEXTKEY PRINT PRINTF WRITE
READLINE GETC READ CLOSE BINMODE OPEN EOF FILENO SEEK TELL SCALAR
MODIFY_REF_ATTRIBUTES MODIFY_SCALAR_ATTRIBUTES MODIFY_ARRAY_ATTRIBUTES
MODIFY_HASH_ATTRIBUTES MODIFY_CODE_ATTRIBUTES MODIFY_GLOB_ATTRIBUTES
MODIFY_FORMAT_ATTRIBUTES MODIFY_IO_ATTRIBUTES
FETCH_REF_ATTRIBUTES FETCH_SCALAR_ATTRIBUTES FETCH_ARRAY_ATTRIBUTES
FETCH_HASH_ATTRIBUTES FETCH_CODE_ATTRIBUTES FETCH_GLOB_ATTRIBUTES
FETCH_FORMAT_ATTRIBUTES FETCH_IO_ATTRIBUTES
);
sub _public_fcns {
my ($package) = @_;
no strict qw(refs);
return grep { substr( $_, 0, 1 ) ne '_' && !$private{$_} && $_ !~ /^\(/ }
map { ( my $f = $_ ) =~ s/^\*$package\:://; $f }
grep { defined( *$_{CODE} ) } values( %{"$package\::"} );
}
#--------------------------------------------------------------------------#
sub _public_ok ($;@) { ## no critic
my ( $package, @expected ) = @_;
my @fcns = _public_fcns($package);
my ( $missing, $extra ) = _difference( \@expected, \@fcns );
return ( !@$missing && !@$extra, $missing, $extra );
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
Test::API - Test a list of subroutines provided by a module
=head1 VERSION
version 0.005
=head1 SYNOPSIS
use Test::More tests => 3;
use Test::API;
require_ok( 'My::Package' );
public_ok ( 'My::Package', @names );
import_ok ( 'My::Package',
export => [ 'foo', 'bar' ],
export_ok => [ 'baz', 'bam' ],
);
class_api_ok( 'My::Class', @methods );
=head1 DESCRIPTION
This simple test module checks the subroutines provided by a module. This is
useful for confirming a planned API in testing and ensuring that other
functions aren't unintentionally included via import.
=head1 USAGE
Note: Subroutines starting with an underscore are ignored, as are a number
of other methods not intended to be called directly by end-users.
import unimport bootstrap
AUTOLOAD BUILD BUILDARGS CLONE CLONE_SKIP DESTROY DEMOLISH
TIESCALAR TIEARRAY TIEHASH TIEHANDLE
FETCH STORE UNTIE FETCHSIZE STORESIZE POP PUSH SHIFT UNSHIFT SPLICE
DELETE EXISTS EXTEND CLEAR FIRSTKEY NEXTKEY PRINT PRINTF WRITE
READLINE GETC READ CLOSE BINMODE OPEN EOF FILENO SEEK TELL SCALAR
MODIFY_REF_ATTRIBUTES MODIFY_SCALAR_ATTRIBUTES MODIFY_ARRAY_ATTRIBUTES
MODIFY_HASH_ATTRIBUTES MODIFY_CODE_ATTRIBUTES MODIFY_GLOB_ATTRIBUTES
MODIFY_FORMAT_ATTRIBUTES MODIFY_IO_ATTRIBUTES
FETCH_REF_ATTRIBUTES FETCH_SCALAR_ATTRIBUTES FETCH_ARRAY_ATTRIBUTES
FETCH_HASH_ATTRIBUTES FETCH_CODE_ATTRIBUTES FETCH_GLOB_ATTRIBUTES
FETCH_FORMAT_ATTRIBUTES FETCH_IO_ATTRIBUTES
Therefore, do not include any of these in a list of expected subroutines.
=head2 public_ok
public_ok( $package, @names );
This function checks that all of the C<@names> provided are available within the
C<$package> namespace and that *only* these subroutines are available. This
means that subroutines imported from other modules will cause this test to fail
unless they are explicitly included in C<@names>.
=head2 class_api_ok
class_api_ok( $class, @names );
A variation of C<public_ok> for object-oriented modules. Allows superclasses
to fill in "missing" subroutines, but "extra" methods provided by superclasses
will not cause the test to fail.
=head2 import_ok
import_ok ( $package, %spec );
This function checks that C<$package> correctly exports an expected list of
subroutines and *only* these subroutines. The C<%spec> generally follows
the style used by [Exporter], but in lower case:
%spec = (
export => [ 'foo', 'bar' ], # exported automatically
export_ok => [ 'baz', 'bam' ], # optional exports
);
For C<export_ok>, the test will check for public functions not listed in
C<export> or C<export_ok> that can be imported and will fail if any are found.
=head1 SEE ALSO
=over 4
=item *
L<Test::ClassAPI> -- more geared towards class trees with inheritance
=back
=for :stopwords cpan testmatrix url annocpan anno bugtracker rt cpants kwalitee diff irc mailto metadata placeholders metacpan
=head1 SUPPORT
=head2 Bugs / Feature Requests
Please report any bugs or feature requests through the issue tracker
at L<https://github.com/dagolden/Test-API/issues>.
You will be notified automatically of any progress on your issue.
=head2 Source Code
This is open source software. The code repository is available for
public review and contribution under the terms of the license.
L<https://github.com/dagolden/Test-API>
git clone https://github.com/dagolden/Test-API.git
=head1 AUTHOR
David Golden <dagolden@cpan.org>
=head1 CONTRIBUTOR
Toby Inkster <mail@tobyinkster.co.uk>
=head1 COPYRIGHT AND LICENSE
This software is Copyright (c) 2014 by David Golden.
This is free software, licensed under:
The Apache License, Version 2.0, January 2004
=cut
|