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