File: Exports.pm

package info (click to toggle)
libtest-exports-perl 1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 104 kB
  • sloc: perl: 354; makefile: 2
file content (267 lines) | stat: -rw-r--r-- 6,297 bytes parent folder | download
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
package Test::Exports;

=head1 NAME

Test::Exports - Test that modules export the right symbols

=head1 SYNOPSIS

    use Test::More;
    use Test::Exports;
    
    require_ok "My::Module" or BAIL_OUT "can't load module";

    import_ok "My::Module", [],             "default import OK";
    is_import qw/foo bar/, "My::Module",    "imports subs";

    new_import_pkg;

    import_ok "My::Module", ["foo"],        "named import OK";
    is_import "foo", "My::Module",          "imports foo";
    cant_ok "bar",                          "doesn't import bar";

=head1 DESCRIPTION

This module provides simple test functions for testing other modules'
C<import> methods. Testing is currently limited to checking which subs
have been imported.

In order to keep different calls to C<< ->import >> separate,
Test::Exports performs these calls from a private package. The
symbol-testing functions then test whether or not symbols are present in
this private package, ensuring none of this interferes with your test
script itself.

=head1 FUNCTIONS

These are all exported by default, as is usual with testing modules.

=cut

use warnings;
use strict;

use B;

use parent "Test::Builder::Module";

our @EXPORT = qw/
    new_import_pkg 
    import_ok import_nok
    is_import cant_ok
/;

our $VERSION = "1";

my $CLASS = __PACKAGE__;

=head2 C<new_import_pkg>

Create a new package to perform imports into. This is useful when you
want to test C<< ->import >> with different arguments: otherwise you'd
need to find some way of going back and clearing up the imports from the
last call.

This returns the name of the new package (which will look like
C<Test::Exports::TestAAAAB>) in case you need it.

=cut

my $counter = "AAAAA";
my $PKG;

sub new_import_pkg { $counter++; $PKG = "$CLASS\::Test$counter" }
new_import_pkg;

=head2 C<import_ok $module, \@args, $name>

Call C<< $module->import >> from the current testing package, passing
C<@args>, and check the call succeeded. 'Success' means not throwing an
exception: C<use> doesn't care if C<import> returns false, so neither do
we.

C<@args> defaults to the empty list; C<$name> defaults to something
sensible.

=cut

sub import_ok {
    my ($mod, $args, $msg) = @_;
    my $tb  = $CLASS->builder;

    local $" = ", ";
    $args   ||= [];
    $msg    ||= "$mod->import(@$args) succeeds";

    my $code = "package $PKG; $mod->import(\@\$args); 1";

    #$tb->diag($code);

    my $eval = eval $code;

    $tb->ok($eval, $msg) or $tb->diag(<<DIAG);
$mod->import(@$args) failed:
$@
DIAG
}

=head2 C<import_nok $module, \@args, $name>

Call C<< $module->import(@args) >> and expect it to throw an exception.
Defaults as for L</import_ok>.

=cut

sub import_nok {
    my ($mod, $args, $msg) = @_;
    my $tb  = $CLASS->builder;

    local $" = ", ";
    $args   ||= [];
    $msg    ||= "$mod->import(@$args) fails";

    my $eval = eval "package $PKG; $mod->import(\@\$args); 1";

    $tb->ok(!$eval, $msg) or $tb->diag(<<DIAG);
$mod->import(@$args) succeeded where it should have failed.
DIAG
}

=head2 C<is_import @subs, $module, $name>

For each name in C<@subs>, check that the current testing package has a
sub by that name and that it is the same as the equinominal sub in the
C<$module> package.

Neither C<$module> nor C<$name> are optional.

=cut

sub is_import {
    my $msg  = pop;
    my $from = pop;
    my $tb = $CLASS->builder;

    my @nok;

    for (@_) {
        my $to = "$PKG\::$_";

        no strict 'refs';
        unless (defined &$to) {
            push @nok, <<DIAG;
  \&$to is not defined
DIAG
            next;
        }

        \&$to == \&{"$from\::$_"} or push @nok, <<DIAG;
  \&$to is not imported correctly
DIAG
    }

    my $ok = $tb->ok(!@nok, $msg) or $tb->diag(<<DIAG);
Expected subs to be imported from $from:
DIAG
    $tb->diag($_) for @nok;
    return $ok;
}

=head2 C<cant_ok @subs, $name>

For each sub in @subs, check that a sub of that name does not exist in
the current testing package. If one is found the diagnostic will
indicate where it was originally defined, to help track down the stray
export.

=cut

sub cant_ok {
    my $msg = pop;
    my $tb  = $CLASS->builder;

    my @nok;

    for (@_) {
        my $can = $PKG->can($_);
        $can and push @nok, $_;
    }

    my $ok = $tb->ok(!@nok, $msg);
    
    for (@nok) {
        my $from = B::svref_2object($PKG->can($_))->GV->STASH->NAME;
        $tb->diag(<<DIAG);
    \&$PKG\::$_ is imported from $from
DIAG
    }

    return $ok;
}

=head1 TODO

=head2 C<is_import>

Currently this just checks that C<\&Our::Pkg::sub == \&Your::Pkg::sub>,
which means

=over 4

=item *

it is impossible to test for exports which have been renamed, and

=item *

we can't be sure the sub originally came from Your::Pkg: it may have
been exported into both packages from somewhere else.

=back

It would be good to fix at least the former.

=head1 AUTHOR

Ben Morrow <ben@morrow.me.uk>

=head1 BUGS

Please report any bugs to <bug-Test-Exports@rt.cpan.org>.

=head1 COPYRIGHT

Copyright 2010 Ben Morrow.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

=over 4

=item *

Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

=item *

Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

=back

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

=cut

1;