File: 28_class_load.t

package info (click to toggle)
libmodule-extractuse-perl 0.344-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 868 kB
  • sloc: perl: 11,270; makefile: 17
file content (67 lines) | stat: -rw-r--r-- 3,308 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/perl -w
use strict;
use Test::More;
use Test::Deep;
use Test::NoWarnings;
use Module::ExtractUse;

my @tests=
  (
#1
   ['load_class("Some::Module");',[qw(Some::Module)],undef,[qw(Some::Module)]],
   ['load_class("Some::Module", { -version => 1.23 });',[qw(Some::Module)],undef,[qw(Some::Module)]],
   ['Class::Load::load_class("Some::Module");',[qw(Some::Module)],undef,[qw(Some::Module)]],
   ['Class::Load::load_class("Some::Module", { -version => 1.23 });',[qw(Some::Module)],undef,[qw(Some::Module)]],

   ['try_load_class("Some::Module");',[qw(Some::Module)],undef,[qw(Some::Module)]],
   ['try_load_class("Some::Module", { -version => 1.23, other => "string" });',[qw(Some::Module)],undef,[qw(Some::Module)]],
   ['Class::Load::try_load_class("Some::Module");',[qw(Some::Module)],undef,[qw(Some::Module)]],
   ['Class::Load::try_load_class("Some::Module", { -version => 1.23 });',[qw(Some::Module)],undef,[qw(Some::Module)]],

   ['load_optional_class("Some::Module");',[qw(Some::Module)],undef,[qw(Some::Module)]],
   ['load_optional_class("Some::Module", { -version => 1.23 });',[qw(Some::Module)],undef,[qw(Some::Module)]],
   ['Class::Load::load_optional_class("Some::Module");',[qw(Some::Module)],undef,[qw(Some::Module)]],
   ['Class::Load::load_optional_class("Some::Module", { -version => 1.23 });',[qw(Some::Module)],undef,[qw(Some::Module)]],

   ['load_first_existing_class("Some::Module");', [qw(Some::Module)],undef,[qw(Some::Module)]],
   ['load_first_existing_class("Some::Module", "Other::Module", "Third::Module::Too");', [qw(Some::Module Other::Module Third::Module::Too)],undef,[qw(Some::Module Other::Module Third::Module::Too)]],
   ['load_first_existing_class("Some::Module", { -version => 1.23 });', [qw(Some::Module)],undef,[qw(Some::Module)]],
   ['load_first_existing_class("Some::Module", { -version => 1.23 }, "Other::Module", "Third::Module::Too", { -version => 4.56 } );', [qw(Some::Module Other::Module Third::Module::Too)],undef,[qw(Some::Module Other::Module Third::Module::Too)]],

    ['$foo->load_class("Some::Module")', undef, undef, undef],
    ['Other::Namespace::load_class("Some::Module")', undef, undef, undef],
    ['$foo->try_load_class("Some::Module")', undef, undef, undef],
    ['Other::Namespace::try_load_class("Some::Module")', undef, undef, undef],
    ['$foo->load_optional_class("Some::Module")', undef, undef, undef],
    ['Other::Namespace::load_optional_class("Some::Module")', undef, undef, undef],
    ['$foo->load_first_existing_class("Some::Module")', undef, undef, undef],
    ['Other::Namespace::load_first_existing_class("Some::Module")', undef, undef, undef],
);


plan tests => (scalar @tests)*3+1;


foreach my $t (@tests) {
    my ($code, @expected)=@$t;
    my $p=Module::ExtractUse->new;

    my @used = (
        $p->extract_use(\$code)->arrayref || undef,
        $p->extract_use(\$code)->arrayref_in_eval || undef,
        $p->extract_use(\$code)->arrayref_out_of_eval || undef,
    );

    for(my $i = 0; $i < @used; ++$i) {
        if (ref($expected[$i]) eq 'ARRAY') {
            cmp_bag($used[$i]||[],$expected[$i],$i.": ".$code);
        } elsif (!defined $expected[$i]) {
            is(undef,$used[$i],$i.": ".$code);
        } else {
            is($used[$i],$expected[$i],$i.": ".$code);
        }
    }
}