Fix provides() when passed a list of files
[p5sagit/Module-Metadata.git] / t / metadata.t
1 #!/usr/bin/perl -w
2 # -*- mode: cperl; tab-width: 8; indent-tabs-mode: nil; basic-offset: 2 -*-
3 # vim:ts=8:sw=2:et:sta:sts=2
4
5 use strict;
6 use warnings;
7 use Test::More;
8 use IO::File;
9 use File::Spec;
10 use File::Temp;
11 use File::Basename;
12 use Cwd ();
13
14 my $undef;
15
16 # parse various module $VERSION lines
17 # these will be reversed later to create %modules
18 my @modules = (
19   $undef => <<'---', # no $VERSION line
20 package Simple;
21 ---
22   $undef => <<'---', # undefined $VERSION
23 package Simple;
24 our $VERSION;
25 ---
26   '1.23' => <<'---', # declared & defined on same line with 'our'
27 package Simple;
28 our $VERSION = '1.23';
29 ---
30   '1.23' => <<'---', # declared & defined on separate lines with 'our'
31 package Simple;
32 our $VERSION;
33 $VERSION = '1.23';
34 ---
35   '1.23' => <<'---', # commented & defined on same line
36 package Simple;
37 our $VERSION = '1.23'; # our $VERSION = '4.56';
38 ---
39   '1.23' => <<'---', # commented & defined on separate lines
40 package Simple;
41 # our $VERSION = '4.56';
42 our $VERSION = '1.23';
43 ---
44   '1.23' => <<'---', # use vars
45 package Simple;
46 use vars qw( $VERSION );
47 $VERSION = '1.23';
48 ---
49   '1.23' => <<'---', # choose the right default package based on package/file name
50 package Simple::_private;
51 $VERSION = '0';
52 package Simple;
53 $VERSION = '1.23'; # this should be chosen for version
54 ---
55   '1.23' => <<'---', # just read the first $VERSION line
56 package Simple;
57 $VERSION = '1.23'; # we should see this line
58 $VERSION = eval $VERSION; # and ignore this one
59 ---
60   '1.23' => <<'---', # just read the first $VERSION line in reopened package (1)
61 package Simple;
62 $VERSION = '1.23';
63 package Error::Simple;
64 $VERSION = '2.34';
65 package Simple;
66 ---
67   '1.23' => <<'---', # just read the first $VERSION line in reopened package (2)
68 package Simple;
69 package Error::Simple;
70 $VERSION = '2.34';
71 package Simple;
72 $VERSION = '1.23';
73 ---
74   '1.23' => <<'---', # mentions another module's $VERSION
75 package Simple;
76 $VERSION = '1.23';
77 if ( $Other::VERSION ) {
78     # whatever
79 }
80 ---
81   '1.23' => <<'---', # mentions another module's $VERSION in a different package
82 package Simple;
83 $VERSION = '1.23';
84 package Simple2;
85 if ( $Simple::VERSION ) {
86     # whatever
87 }
88 ---
89   '1.23' => <<'---', # $VERSION checked only in assignments, not regexp ops
90 package Simple;
91 $VERSION = '1.23';
92 if ( $VERSION =~ /1\.23/ ) {
93     # whatever
94 }
95 ---
96   '1.23' => <<'---', # $VERSION checked only in assignments, not relational ops
97 package Simple;
98 $VERSION = '1.23';
99 if ( $VERSION == 3.45 ) {
100     # whatever
101 }
102 ---
103   '1.23' => <<'---', # $VERSION checked only in assignments, not relational ops
104 package Simple;
105 $VERSION = '1.23';
106 package Simple2;
107 if ( $Simple::VERSION == 3.45 ) {
108     # whatever
109 }
110 ---
111   '1.23' => <<'---', # Fully qualified $VERSION declared in package
112 package Simple;
113 $Simple::VERSION = 1.23;
114 ---
115   '1.23' => <<'---', # Differentiate fully qualified $VERSION in a package
116 package Simple;
117 $Simple2::VERSION = '999';
118 $Simple::VERSION = 1.23;
119 ---
120   '1.23' => <<'---', # Differentiate fully qualified $VERSION and unqualified
121 package Simple;
122 $Simple2::VERSION = '999';
123 $VERSION = 1.23;
124 ---
125   '1.23' => <<'---', # $VERSION declared as package variable from within 'main' package
126 $Simple::VERSION = '1.23';
127 {
128   package Simple;
129   $x = $y, $cats = $dogs;
130 }
131 ---
132   '1.23' => <<'---', # $VERSION wrapped in parens - space inside
133 package Simple;
134 ( $VERSION ) = '1.23';
135 ---
136   '1.23' => <<'---', # $VERSION wrapped in parens - no space inside
137 package Simple;
138 ($VERSION) = '1.23';
139 ---
140   '1.23' => <<'---', # $VERSION follows a spurious 'package' in a quoted construct
141 package Simple;
142 __PACKAGE__->mk_accessors(qw(
143     program socket proc
144     package filename line codeline subroutine finished));
145
146 our $VERSION = "1.23";
147 ---
148   '1.23' => <<'---', # $VERSION using version.pm
149   package Simple;
150   use version; our $VERSION = version->new('1.23');
151 ---
152   '1.23' => <<'---', # $VERSION using version.pm and qv()
153   package Simple;
154   use version; our $VERSION = qv('1.230');
155 ---
156   '1.23' => <<'---', # Two version assignments, should ignore second one
157   $Simple::VERSION = '1.230';
158   $Simple::VERSION = eval $Simple::VERSION;
159 ---
160   '1.23' => <<'---', # declared & defined on same line with 'our'
161 package Simple;
162 our $VERSION = '1.23_00_00';
163 ---
164   '1.23' => <<'---', # package NAME VERSION
165   package Simple 1.23;
166 ---
167   '1.23_01' => <<'---', # package NAME VERSION
168   package Simple 1.23_01;
169 ---
170   'v1.2.3' => <<'---', # package NAME VERSION
171   package Simple v1.2.3;
172 ---
173   'v1.2_3' => <<'---', # package NAME VERSION
174   package Simple v1.2_3;
175 ---
176   '1.23' => <<'---', # trailing crud
177   package Simple;
178   our $VERSION;
179   $VERSION = '1.23-alpha';
180 ---
181   '1.23' => <<'---', # trailing crud
182   package Simple;
183   our $VERSION;
184   $VERSION = '1.23b';
185 ---
186   '1.234' => <<'---', # multi_underscore
187   package Simple;
188   our $VERSION;
189   $VERSION = '1.2_3_4';
190 ---
191   '0' => <<'---', # non-numeric
192   package Simple;
193   our $VERSION;
194   $VERSION = 'onetwothree';
195 ---
196   $undef => <<'---', # package NAME BLOCK, undef $VERSION
197 package Simple {
198   our $VERSION;
199 }
200 ---
201   '1.23' => <<'---', # package NAME BLOCK, with $VERSION
202 package Simple {
203   our $VERSION = '1.23';
204 }
205 ---
206   '1.23' => <<'---', # package NAME VERSION BLOCK
207 package Simple 1.23 {
208   1;
209 }
210 ---
211   'v1.2.3_4' => <<'---', # package NAME VERSION BLOCK
212 package Simple v1.2.3_4 {
213   1;
214 }
215 ---
216   '0' => <<'---', # set from separately-initialised variable
217 package Simple;
218   our $CVSVERSION   = '$Revision: 1.7 $';
219   our ($VERSION)    = ($CVSVERSION =~ /(\d+\.\d+)/);
220 }
221 ---
222 );
223 my %modules = reverse @modules;
224
225 my @pkg_names = (
226   [ 'Simple' ] => <<'---', # package NAME
227 package Simple;
228 ---
229   [ 'Simple::Edward' ] => <<'---', # package NAME::SUBNAME
230 package Simple::Edward;
231 ---
232   [ 'Simple::Edward::' ] => <<'---', # package NAME::SUBNAME::
233 package Simple::Edward::;
234 ---
235   [ "Simple'Edward" ] => <<'---', # package NAME'SUBNAME
236 package Simple'Edward;
237 ---
238   [ "Simple'Edward::" ] => <<'---', # package NAME'SUBNAME::
239 package Simple'Edward::;
240 ---
241   [ 'Simple::::Edward' ] => <<'---', # package NAME::::SUBNAME
242 package Simple::::Edward;
243 ---
244   [ '::Simple::Edward' ] => <<'---', # package ::NAME::SUBNAME
245 package ::Simple::Edward;
246 ---
247   [ 'main' ] => <<'---', # package NAME:SUBNAME (fail)
248 package Simple:Edward;
249 ---
250   [ 'main' ] => <<'---', # package NAME' (fail)
251 package Simple';
252 ---
253   [ 'main' ] => <<'---', # package NAME::SUBNAME' (fail)
254 package Simple::Edward';
255 ---
256   [ 'main' ] => <<'---', # package NAME''SUBNAME (fail)
257 package Simple''Edward;
258 ---
259   [ 'main' ] => <<'---', # package NAME-SUBNAME (fail)
260 package Simple-Edward;
261 ---
262 );
263 my %pkg_names = reverse @pkg_names;
264
265 plan tests => 64 + (2 * keys( %modules )) + (2 * keys( %pkg_names ));
266
267 require_ok('Module::Metadata');
268
269 {
270   # class method C<find_module_by_name>
271   my $module = Module::Metadata->find_module_by_name(
272                  'Module::Metadata' );
273   ok( -e $module, 'find_module_by_name() succeeds' );
274 }
275
276 #########################
277
278 BEGIN {
279   my $cwd = File::Spec->rel2abs(Cwd::cwd);
280   sub original_cwd { return $cwd }
281 }
282
283 # Setup a temp directory
284 sub tmpdir {
285   my (@args) = @_;
286   my $dir = $ENV{PERL_CORE} ? original_cwd : File::Spec->tmpdir;
287   return File::Temp::tempdir('MMD-XXXXXXXX', CLEANUP => 0, DIR => $dir, @args);
288 }
289
290 my $tmp;
291 BEGIN { $tmp = tmpdir; diag "using temp dir $tmp"; }
292
293 END {
294   die "tests failed; leaving temp dir $tmp behind"
295     if $ENV{AUTHOR_TESTING} and not Test::Builder->new->is_passing;
296   diag "removing temp dir $tmp";
297   chdir original_cwd;
298   File::Path::remove_tree($tmp);
299 }
300
301 # generates a new distribution:
302 # files => { relative filename => $content ... }
303 # returns the name of the distribution (not including version),
304 # and the absolute path name to the dist.
305 {
306   my $test_num = 0;
307   sub new_dist {
308     my %opts = @_;
309
310     my $distname = 'Simple' . $test_num++;
311     my $distdir = File::Spec->catdir($tmp, $distname);
312     note "using dist $distname in $distdir";
313
314     File::Path::mkpath($distdir) or die "failed to create '$distdir'";
315
316     foreach my $rel_filename (keys %{$opts{files}})
317     {
318       my $abs_filename = File::Spec->catfile($distdir, $rel_filename);
319       my $dirname = File::Basename::dirname($abs_filename);
320       unless (-d $dirname) {
321         File::Path::mkpath($dirname) or die "Can't create '$dirname'";
322       }
323
324       note "creating $abs_filename";
325       my $fh = IO::File->new(">$abs_filename") or die "Can't write '$abs_filename'\n";
326       print $fh $opts{files}{$rel_filename};
327       close $fh;
328     }
329
330     chdir $distdir;
331     return ($distname, $distdir);
332   }
333 }
334
335 {
336   # fail on invalid module name
337   my $pm_info = Module::Metadata->new_from_module(
338                   'Foo::Bar', inc => [] );
339   ok( !defined( $pm_info ), 'fail if can\'t find module by module name' );
340 }
341
342 {
343   # fail on invalid filename
344   my $file = File::Spec->catfile( 'Foo', 'Bar.pm' );
345   my $pm_info = Module::Metadata->new_from_file( $file, inc => [] );
346   ok( !defined( $pm_info ), 'fail if can\'t find module by file name' );
347 }
348
349 {
350   my $file = File::Spec->catfile('lib', 'Simple.pm');
351   my ($dist_name, $dist_dir) = new_dist(files => { $file => "package Simple;\n" });
352
353   # construct from module filename
354   my $pm_info = Module::Metadata->new_from_file( $file );
355   ok( defined( $pm_info ), 'new_from_file() succeeds' );
356
357   # construct from filehandle
358   my $handle = IO::File->new($file);
359   $pm_info = Module::Metadata->new_from_handle( $handle, $file );
360   ok( defined( $pm_info ), 'new_from_handle() succeeds' );
361   $pm_info = Module::Metadata->new_from_handle( $handle );
362   is( $pm_info, undef, "new_from_handle() without filename returns undef" );
363   close($handle);
364
365   # construct from module name, using custom include path
366   $pm_info = Module::Metadata->new_from_module(
367                'Simple', inc => [ 'lib', @INC ] );
368   ok( defined( $pm_info ), 'new_from_module() succeeds' );
369 }
370
371
372 foreach my $module ( sort keys %modules ) {
373     my $expected = $modules{$module};
374  SKIP: {
375     skip( "No our() support until perl 5.6", 2 )
376         if $] < 5.006 && $module =~ /\bour\b/;
377     skip( "No package NAME VERSION support until perl 5.11.1", 2 )
378         if $] < 5.011001 && $module =~ /package\s+[\w\:\']+\s+v?[0-9._]+/;
379
380     my $file = File::Spec->catfile('lib', 'Simple.pm');
381     my ($dist_name, $dist_dir) = new_dist(files => { $file => $module });
382
383     my $warnings = '';
384     local $SIG{__WARN__} = sub { $warnings .= $_ for @_ };
385     my $pm_info = Module::Metadata->new_from_file( $file );
386
387     # Test::Builder will prematurely numify objects, so use this form
388     my $errs;
389     my $got = $pm_info->version;
390     if ( defined $expected ) {
391         ok( $got eq $expected,
392             "correct module version (expected '$expected')" )
393             or $errs++;
394     } else {
395         ok( !defined($got),
396             "correct module version (expected undef)" )
397             or $errs++;
398     }
399     is( $warnings, '', 'no warnings from parsing' ) or $errs++;
400     diag "Got: '$got'\nModule contents:\n$module" if $errs;
401   }
402 }
403
404 foreach my $pkg_contents ( sort keys %pkg_names ) {
405   my $expected = $pkg_names{$pkg_contents};
406
407   my $file = File::Spec->catfile('lib', 'Simple.pm');
408   my ($dist_name, $dist_dir) = new_dist(files => { $file => $pkg_contents });
409
410   my $warnings = '';
411   local $SIG{__WARN__} = sub { $warnings .= $_ for @_ };
412   my $pm_info = Module::Metadata->new_from_file( $file );
413
414   # Test::Builder will prematurely numify objects, so use this form
415   my $errs;
416   my @got = $pm_info->packages_inside();
417   is_deeply( \@got, $expected,
418              "correct package names (expected '" . join(', ', @$expected) . "')" )
419           or $errs++;
420   is( $warnings, '', 'no warnings from parsing' ) or $errs++;
421   diag "Got: '" . join(', ', @got) . "'\nModule contents:\n$pkg_contents" if $errs;
422 }
423
424 {
425   # Find each package only once
426   my $file = File::Spec->catfile('lib', 'Simple.pm');
427   my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
428 package Simple;
429 $VERSION = '1.23';
430 package Error::Simple;
431 $VERSION = '2.34';
432 package Simple;
433 ---
434
435   my $pm_info = Module::Metadata->new_from_file( $file );
436
437   my @packages = $pm_info->packages_inside;
438   is( @packages, 2, 'record only one occurence of each package' );
439 }
440
441 {
442   # Module 'Simple.pm' does not contain package 'Simple';
443   # constructor should not complain, no default module name or version
444   my $file = File::Spec->catfile('lib', 'Simple.pm');
445   my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
446 package Simple::Not;
447 $VERSION = '1.23';
448 ---
449
450   my $pm_info = Module::Metadata->new_from_file( $file );
451
452   is( $pm_info->name, undef, 'no default package' );
453   is( $pm_info->version, undef, 'no version w/o default package' );
454 }
455
456 {
457   # Module 'Simple.pm' contains an alpha version
458   # constructor should report first $VERSION found
459   my $file = File::Spec->catfile('lib', 'Simple.pm');
460   my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
461 package Simple;
462 $VERSION = '1.23_01';
463 $VERSION = eval $VERSION;
464 ---
465
466   my $pm_info = Module::Metadata->new_from_file( $file );
467
468   is( $pm_info->version, '1.23_01', 'alpha version reported');
469
470   # NOTE the following test has be done this way because Test::Builder is
471   # too smart for our own good and tries to see if the version object is a
472   # dual-var, which breaks with alpha versions:
473   #    Argument "1.23_0100" isn't numeric in addition (+) at
474   #    /usr/lib/perl5/5.8.7/Test/Builder.pm line 505.
475
476   ok( $pm_info->version > 1.23, 'alpha version greater than non');
477 }
478
479 # parse $VERSION lines scripts for package main
480 my @scripts = (
481   <<'---', # package main declared
482 #!perl -w
483 package main;
484 $VERSION = '0.01';
485 ---
486   <<'---', # on first non-comment line, non declared package main
487 #!perl -w
488 $VERSION = '0.01';
489 ---
490   <<'---', # after non-comment line
491 #!perl -w
492 use strict;
493 $VERSION = '0.01';
494 ---
495   <<'---', # 1st declared package
496 #!perl -w
497 package main;
498 $VERSION = '0.01';
499 package _private;
500 $VERSION = '999';
501 ---
502   <<'---', # 2nd declared package
503 #!perl -w
504 package _private;
505 $VERSION = '999';
506 package main;
507 $VERSION = '0.01';
508 ---
509   <<'---', # split package
510 #!perl -w
511 package main;
512 package _private;
513 $VERSION = '999';
514 package main;
515 $VERSION = '0.01';
516 ---
517   <<'---', # define 'main' version from other package
518 package _private;
519 $::VERSION = 0.01;
520 $VERSION = '999';
521 ---
522   <<'---', # define 'main' version from other package
523 package _private;
524 $VERSION = '999';
525 $::VERSION = 0.01;
526 ---
527 );
528
529 my ( $i, $n ) = ( 1, scalar( @scripts ) );
530 foreach my $script ( @scripts ) {
531   my $file = File::Spec->catfile('bin', 'simple.plx');
532   my ($dist_name, $dist_dir) = new_dist(files => { $file => $script } );
533   my $pm_info = Module::Metadata->new_from_file( $file );
534
535   is( $pm_info->version, '0.01', "correct script version ($i of $n)" );
536   $i++;
537 }
538
539 {
540   # examine properties of a module: name, pod, etc
541   my $file = File::Spec->catfile('lib', 'Simple.pm');
542   my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
543 package Simple;
544 $VERSION = '0.01';
545 package Simple::Ex;
546 $VERSION = '0.02';
547
548 =head1 NAME
549
550 Simple - It's easy.
551
552 =head1 AUTHOR
553
554 Simple Simon
555
556 You can find me on the IRC channel
557 #simon on irc.perl.org.
558
559 =cut
560 ---
561
562   my $pm_info = Module::Metadata->new_from_module(
563              'Simple', inc => [ 'lib', @INC ] );
564
565   is( $pm_info->name, 'Simple', 'found default package' );
566   is( $pm_info->version, '0.01', 'version for default package' );
567
568   # got correct version for secondary package
569   is( $pm_info->version( 'Simple::Ex' ), '0.02',
570       'version for secondary package' );
571
572   my $filename = $pm_info->filename;
573   ok( defined( $filename ) && -e $filename,
574       'filename() returns valid path to module file' );
575
576   my @packages = $pm_info->packages_inside;
577   is( @packages, 2, 'found correct number of packages' );
578   is( $packages[0], 'Simple', 'packages stored in order found' );
579
580   # we can detect presence of pod regardless of whether we are collecting it
581   ok( $pm_info->contains_pod, 'contains_pod() succeeds' );
582
583   my @pod = $pm_info->pod_inside;
584   is_deeply( \@pod, [qw(NAME AUTHOR)], 'found all pod sections' );
585
586   is( $pm_info->pod('NONE') , undef,
587       'return undef() if pod section not present' );
588
589   is( $pm_info->pod('NAME'), undef,
590       'return undef() if pod section not collected' );
591
592
593   # collect_pod
594   $pm_info = Module::Metadata->new_from_module(
595                'Simple', inc => [ 'lib', @INC ], collect_pod => 1 );
596
597   my %pod;
598   for my $section (qw(NAME AUTHOR)) {
599     my $content = $pm_info->pod( $section );
600     if ( $content ) {
601       $content =~ s/^\s+//;
602       $content =~ s/\s+$//;
603     }
604     $pod{$section} = $content;
605   }
606   my %expected = (
607     NAME   => q|Simple - It's easy.|,
608     AUTHOR => <<'EXPECTED'
609 Simple Simon
610
611 You can find me on the IRC channel
612 #simon on irc.perl.org.
613 EXPECTED
614   );
615   for my $text (values %expected) {
616     $text =~ s/^\s+//;
617     $text =~ s/\s+$//;
618   }
619   is( $pod{NAME},   $expected{NAME},   'collected NAME pod section' );
620   is( $pod{AUTHOR}, $expected{AUTHOR}, 'collected AUTHOR pod section' );
621 }
622
623 {
624   # test things that look like POD, but aren't
625   my $file = File::Spec->catfile('lib', 'Simple.pm');
626   my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
627 package Simple;
628
629 =YES THIS STARTS POD
630
631 our $VERSION = '999';
632
633 =cute
634
635 our $VERSION = '666';
636
637 =cut
638
639 *foo
640 =*no_this_does_not_start_pod;
641
642 our $VERSION = '1.23';
643
644 ---
645   my $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
646   is( $pm_info->name, 'Simple', 'found default package' );
647   is( $pm_info->version, '1.23', 'version for default package' );
648 }
649
650 {
651   # Make sure processing stops after __DATA__
652   my $file = File::Spec->catfile('lib', 'Simple.pm');
653   my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
654 package Simple;
655 $VERSION = '0.01';
656 __DATA__
657 *UNIVERSAL::VERSION = sub {
658   foo();
659 };
660 ---
661
662   my $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
663   is( $pm_info->name, 'Simple', 'found default package' );
664   is( $pm_info->version, '0.01', 'version for default package' );
665   my @packages = $pm_info->packages_inside;
666   is_deeply(\@packages, ['Simple'], 'packages inside');
667 }
668
669 {
670   # Make sure we handle version.pm $VERSIONs well
671   my $file = File::Spec->catfile('lib', 'Simple.pm');
672   my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
673 package Simple;
674 $VERSION = version->new('0.60.' . (qw$Revision: 128 $)[1]);
675 package Simple::Simon;
676 $VERSION = version->new('0.61.' . (qw$Revision: 129 $)[1]);
677 ---
678
679   my $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
680   is( $pm_info->name, 'Simple', 'found default package' );
681   is( $pm_info->version, '0.60.128', 'version for default package' );
682   my @packages = $pm_info->packages_inside;
683   is_deeply([sort @packages], ['Simple', 'Simple::Simon'], 'packages inside');
684   is( $pm_info->version('Simple::Simon'), '0.61.129', 'version for embedded package' );
685 }
686
687 # check that package_versions_from_directory works
688
689 {
690   my $file = File::Spec->catfile('lib', 'Simple.pm');
691   my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
692 package Simple;
693 $VERSION = '0.01';
694 package Simple::Ex;
695 $VERSION = '0.02';
696 {
697   package main; # should ignore this
698 }
699 {
700   package DB; # should ignore this
701 }
702 {
703   package Simple::_private; # should ignore this
704 }
705
706 =head1 NAME
707
708 Simple - It's easy.
709
710 =head1 AUTHOR
711
712 Simple Simon
713
714 =cut
715 ---
716
717   my $exp_pvfd = {
718     'Simple' => {
719       'file' => 'Simple.pm',
720       'version' => '0.01'
721     },
722     'Simple::Ex' => {
723       'file' => 'Simple.pm',
724       'version' => '0.02'
725     }
726   };
727
728   my $got_pvfd = Module::Metadata->package_versions_from_directory('lib');
729
730   is_deeply( $got_pvfd, $exp_pvfd, "package_version_from_directory()" )
731     or diag explain $got_pvfd;
732
733 {
734   my $got_provides = Module::Metadata->provides(dir => 'lib', version => 2);
735   my $exp_provides = {
736     'Simple' => {
737       'file' => 'lib/Simple.pm',
738       'version' => '0.01'
739     },
740     'Simple::Ex' => {
741       'file' => 'lib/Simple.pm',
742       'version' => '0.02'
743     }
744   };
745
746   is_deeply( $got_provides, $exp_provides, "provides(dir => ...)" )
747     or diag explain $got_provides;
748
749   $got_provides = Module::Metadata->provides(files => ['lib/Simple.pm'], version => 2);
750   is_deeply( $got_provides, $exp_provides, "provides(files => ...)" )
751     or diag explain $got_provides;
752 }
753
754 {
755   my $got_provides = Module::Metadata->provides(dir => 'lib', prefix => 'other', version => 1.4);
756   my $exp_provides = {
757     'Simple' => {
758       'file' => 'other/Simple.pm',
759       'version' => '0.01'
760     },
761     'Simple::Ex' => {
762       'file' => 'other/Simple.pm',
763       'version' => '0.02'
764     }
765   };
766
767   is_deeply( $got_provides, $exp_provides, "provides(dir => ..., prefix => ...)" )
768     or diag explain $got_provides;
769 }
770 }
771
772 # Check package_versions_from_directory with regard to case-sensitivity
773 {
774   my $file = File::Spec->catfile('lib', 'Simple.pm');
775   my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
776 package simple;
777 $VERSION = '0.01';
778 ---
779
780   my $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
781   is( $pm_info->name, undef, 'no default package' );
782   is( $pm_info->version, undef, 'version for default package' );
783   is( $pm_info->version('simple'), '0.01', 'version for lower-case package' );
784   is( $pm_info->version('Simple'), undef, 'version for capitalized package' );
785   ok( $pm_info->is_indexable(), 'an indexable package is found' );
786   ok( $pm_info->is_indexable('simple'), 'the simple package is indexable' );
787   ok( !$pm_info->is_indexable('Simple'), 'the Simple package would not be indexed' );
788 }
789
790 {
791   my $file = File::Spec->catfile('lib', 'Simple.pm');
792   my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
793 package simple;
794 $VERSION = '0.01';
795 package Simple;
796 $VERSION = '0.02';
797 package SiMpLe;
798 $VERSION = '0.03';
799 ---
800
801   my $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
802   is( $pm_info->name, 'Simple', 'found default package' );
803   is( $pm_info->version, '0.02', 'version for default package' );
804   is( $pm_info->version('simple'), '0.01', 'version for lower-case package' );
805   is( $pm_info->version('Simple'), '0.02', 'version for capitalized package' );
806   is( $pm_info->version('SiMpLe'), '0.03', 'version for mixed-case package' );
807   ok( $pm_info->is_indexable('simple'), 'the simple package is indexable' );
808   ok( $pm_info->is_indexable('Simple'), 'the Simple package is indexable' );
809 }
810
811 {
812   my $file = File::Spec->catfile('lib', 'Simple.pm');
813   my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
814 package ## hide from PAUSE
815    simple;
816 $VERSION = '0.01';
817 ---
818
819   my $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
820   is( $pm_info->name, undef, 'no package names found' );
821   ok( !$pm_info->is_indexable('simple'), 'the simple package would not be indexed' );
822   ok( !$pm_info->is_indexable('Simple'), 'the Simple package would not be indexed' );
823   ok( !$pm_info->is_indexable(), 'no indexable package is found' );
824 }