~ubuntu-branches/ubuntu/raring/libmodule-info-perl/raring

« back to all changes in this revision

Viewing changes to t/Module-Info.t

  • Committer: Bazaar Package Importer
  • Author(s): Jay Bonci
  • Date: 2003-10-06 10:51:04 UTC
  • Revision ID: james.westby@ubuntu.com-20031006105104-1b67d55zyyay6jvo
Tags: upstream-0.24
ImportĀ upstreamĀ versionĀ 0.24

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/perl -w
 
2
 
 
3
use lib qw(t/lib);
 
4
use Test::More tests => 58;
 
5
use Config;
 
6
 
 
7
my $Mod_Info_VERSION = '0.24';
 
8
 
 
9
my @old5lib = defined $ENV{PERL5LIB} ? ($ENV{PERL5LIB}) : ();
 
10
$ENV{PERL5LIB} = join $Config{path_sep}, 'blib/lib', @old5lib;
 
11
 
 
12
use_ok('Module::Info');
 
13
my @expected_subs = qw(
 
14
                       new_from_file
 
15
                       new_from_module
 
16
                       new_from_loaded
 
17
                       all_installed
 
18
                       _find_all_installed
 
19
                       name               
 
20
                       version            
 
21
                       inc_dir            
 
22
                       file               
 
23
                       is_core            
 
24
                       packages_inside    
 
25
                       package_versions
 
26
                       modules_required
 
27
                       modules_used       
 
28
                       _file2mod          
 
29
                       subroutines        
 
30
                       superclasses
 
31
                       die_on_compilation_error
 
32
                       _is_macos_classic
 
33
                       _is_win95
 
34
                       _call_B
 
35
                       _call_perl
 
36
                       _get_extra_arguments
 
37
                       subroutines_called
 
38
                       dynamic_method_calls
 
39
                      );
 
40
 
 
41
can_ok('Module::Info', @expected_subs);
 
42
 
 
43
my $mod_info = Module::Info->new_from_file('lib/Module/Info.pm');
 
44
isa_ok($mod_info, 'Module::Info', 'new_from_file');
 
45
 
 
46
ok( !$mod_info->name,                       '    has no name' );
 
47
$mod_info->name('Module::Info');
 
48
ok( $mod_info->name,                        '    name set' );
 
49
is( $mod_info->version, $Mod_Info_VERSION,  '    version()' );
 
50
ok( !$mod_info->inc_dir,                    '    has no inc_dir' );
 
51
is( $mod_info->file, File::Spec->rel2abs('lib/Module/Info.pm'),
 
52
                                            '    file()');
 
53
ok( !$mod_info->is_core,                    '    not a core module' );
 
54
 
 
55
SKIP: {
 
56
    skip "Only works on 5.6.1 and up.", 8 unless $] >= 5.006001;
 
57
 
 
58
    @expected_subs = map "Module::Info::$_", @expected_subs;
 
59
 
 
60
    my @packages = $mod_info->packages_inside;
 
61
    is( @packages, 1,                   'Found a single package inside' );
 
62
    is( $packages[0], 'Module::Info',   '  and its what we want' );
 
63
 
 
64
    my %versions = $mod_info->package_versions;
 
65
    is( keys %versions, 1,                '1 package with package_versions()');
 
66
    is( $versions{Module::Info}, $Mod_Info_VERSION, 'version is correct');
 
67
 
 
68
    my %subs = $mod_info->subroutines;
 
69
    is( keys %subs, @expected_subs,    'Found all the subroutines' );
 
70
    is_deeply( [sort keys %subs], 
 
71
               [sort @expected_subs],  '   names' );
 
72
    
 
73
    my @mods = $mod_info->modules_used;
 
74
    is( @mods, 6,           'Found all modules used' );
 
75
    is_deeply( [sort @mods], [sort qw(strict File::Spec Config 
 
76
                                      Carp IPC::Open3 vars)],
 
77
                            '    the right ones' );
 
78
}
 
79
 
 
80
 
 
81
$mod_info = Module::Info->new_from_module('Module::Info');
 
82
isa_ok($mod_info, 'Module::Info', 'new_from_module');
 
83
 
 
84
is( $mod_info->name, 'Module::Info',        '    name()' );
 
85
is( $mod_info->version, $Mod_Info_VERSION,  '    version()' );
 
86
is( $mod_info->inc_dir, File::Spec->rel2abs('blib/lib'),
 
87
                                            '    inc_dir' );
 
88
is( $mod_info->file, File::Spec->rel2abs('blib/lib/Module/Info.pm'),
 
89
                                            '    file()');
 
90
ok( !$mod_info->is_core,                    '    not a core module' );
 
91
 
 
92
 
 
93
# Grab the core version of Text::Soundex and hope it hasn't been
 
94
# deleted.
 
95
@core_inc = map { File::Spec->canonpath($_) }
 
96
  ($Config{installarchlib}, $Config{installprivlib});
 
97
$mod_info = Module::Info->new_from_module('Text::Soundex', @core_inc);
 
98
is( $mod_info->name, 'Text::Soundex',       '    name()' );
 
99
 
 
100
# dunno what the version will be, 5.004's had none.
 
101
 
 
102
ok( grep($mod_info->inc_dir eq $_, @core_inc),       '    inc_dir()' );
 
103
is( $mod_info->file, 
 
104
    File::Spec->catfile( $mod_info->inc_dir, 'Text', 'Soundex.pm' ),
 
105
                                            '    file()');
 
106
ok( $mod_info->is_core,                     '    core module' );
 
107
 
 
108
 
 
109
$mod_info = Module::Info->new_from_loaded('Module::Info');
 
110
isa_ok($mod_info, 'Module::Info', 'new_from_module');
 
111
 
 
112
is( $mod_info->name, 'Module::Info',        '    name()' );
 
113
is( $mod_info->version, $Mod_Info_VERSION,  '    version()' );
 
114
is( $mod_info->inc_dir, File::Spec->rel2abs('blib/lib'),
 
115
                                            '    inc_dir' );
 
116
is( $mod_info->file, File::Spec->rel2abs('blib/lib/Module/Info.pm'),
 
117
                                            '    file()');
 
118
ok( !$mod_info->is_core,                    '    not a core module' );
 
119
 
 
120
 
 
121
@modules = Module::Info->all_installed('Module::Info');
 
122
ok( @modules,       'all_installed() returned something' );
 
123
ok( !(grep { !defined $_ || !$_->isa('Module::Info') } @modules),
 
124
                    "  they're all Module::Info objects"
 
125
  );
 
126
 
 
127
# I have no idea how many I'm going to get, so I'll only play with the 
 
128
# first one.  It's the current one.
 
129
$mod_info = $modules[0];
 
130
isa_ok($mod_info, 'Module::Info', 'all_installed');
 
131
 
 
132
is( $mod_info->name, 'Module::Info',        '    name()' );
 
133
is( $mod_info->version, $Mod_Info_VERSION,  '    version()' );
 
134
ok( !$mod_info->is_core,                    '    not a core module' );
 
135
 
 
136
 
 
137
# Ensure that code refs in @INC are skipped.
 
138
my @mods = Module::Info->all_installed('Module::Info', (@INC, sub { die }));
 
139
ok( @modules,       'all_installed() returned something' );
 
140
ok( !(grep { !defined $_ || !$_->isa('Module::Info') } @modules),
 
141
                    "  they're all Module::Info objects"
 
142
  );
 
143
 
 
144
 
 
145
SKIP: {
 
146
    skip "Only works on 5.6.1 and up.", 17 unless $] >= 5.006001;
 
147
 
 
148
    my $module = Module::Info->new_from_file('t/lib/Foo.pm');
 
149
    my @packages = $module->packages_inside;
 
150
    is( @packages, 2,       'Found two packages inside' );
 
151
    ok( eq_set(\@packages, [qw(Foo Bar)]),   "  they're right" );
 
152
 
 
153
    my %versions = $module->package_versions;
 
154
    is( keys %versions, 2,                '2 packages with package_versions()');
 
155
    is( $versions{Foo}, '7.254',          'version is correct');
 
156
    is( $versions{Bar}, undef,            'no version present');
 
157
 
 
158
    my %subs = $module->subroutines;
 
159
    is( keys %subs, 2,                          'Found two subroutine' );
 
160
    ok( exists $subs{'Foo::wibble'},            '   its right' );
 
161
 
 
162
    my($start, $end) = @{$subs{'Foo::wibble'}}{qw(start end)};
 
163
    print "# start $start, end $end\n";
 
164
    is( $start, 21,           '   start line' );
 
165
    is( $end,   22,           '   end line'   );
 
166
 
 
167
    my @mods = $module->modules_used;
 
168
    is( @mods, 7,           'modules_used' );
 
169
    is_deeply( [sort @mods], 
 
170
               [sort qw(strict vars Carp Exporter t/lib/Bar.pm t/lib/Foo.pm lib)] );
 
171
 
 
172
    $module->name('Foo');
 
173
    my @isa = $module->superclasses;
 
174
    is( @isa, 3,            'isa' );
 
175
    is_deeply( [sort @isa], [sort qw(This That What::Ever)] );
 
176
 
 
177
    my @calls = $module->subroutines_called;
 
178
 
 
179
    my $startline = 25;
 
180
    my @expected_calls = ({
 
181
                           line     => $startline,
 
182
                           class    => undef,
 
183
                           type     => 'function',
 
184
                           name     => 'wibble'
 
185
                          },
 
186
                          {
 
187
                           line     => $startline + 1,
 
188
                           class    => undef,
 
189
                           type     => 'symbolic function',
 
190
                           name     => undef,
 
191
                          },
 
192
                          {
 
193
                           line     => $startline + 2,
 
194
                           class    => 'Foo',
 
195
                           type     => 'class method',
 
196
                           name     => 'wibble',
 
197
                          },
 
198
                          {
 
199
                           line     => $startline + 3,
 
200
                           class    => undef,
 
201
                           type     => 'object method',
 
202
                           name     => 'wibble',
 
203
                          },
 
204
                          {
 
205
                           line     => $startline + 5,
 
206
                           class    => undef,
 
207
                           type     => 'object method',
 
208
                           name     => 'wibble',
 
209
                          },
 
210
                          {
 
211
                           line     => $startline + 7,
 
212
                           class    => 'Foo',
 
213
                           type     => 'dynamic class method',
 
214
                           name     => undef,
 
215
                          },
 
216
                          {
 
217
                           line     => $startline + 8,
 
218
                           class    => undef,
 
219
                           type     => 'dynamic object method',
 
220
                           name     => undef,
 
221
                          },
 
222
                          {
 
223
                           line     => $startline + 9,
 
224
                           class    => undef,
 
225
                           type     => 'dynamic object method',
 
226
                           name     => undef,
 
227
                          },
 
228
                          {
 
229
                           line     => $startline + 10,
 
230
                           class    => 'Foo',
 
231
                           type     => 'dynamic class method',
 
232
                           name     => undef,
 
233
                          },
 
234
                          {
 
235
                           line     => $startline + 14,
 
236
                           class    => undef,
 
237
                           type     => 'object method',
 
238
                           name     => 'wibble'
 
239
                          },
 
240
                          {
 
241
                           line     => $startline + 27,
 
242
                           class    => undef,
 
243
                           type     => 'function',
 
244
                           name     => 'croak'
 
245
                          },
 
246
                         );
 
247
    is_deeply(\@calls, \@expected_calls, 'subroutines_called');
 
248
    is_deeply([$module->dynamic_method_calls],
 
249
              [grep $_->{type} =~ /dynamic/, @expected_calls]);
 
250
 
 
251
    $module = Module::Info->new_from_file('t/lib/Bar.pm');
 
252
    @mods   = $module->modules_used;
 
253
    is( @mods, 3, 'modules_used with complex BEGIN block' );
 
254
    is_deeply( sort @mods,
 
255
               (sort qw(Cwd Carp strict)) );
 
256
}