This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Update Module-Metadata to CPAN version 1.000025
[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 0.82;
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   'v2.2.102.2' => <<'---', # our + bare v-string
225 package Simple;
226 our $VERSION     = v2.2.102.2;
227 ---
228   '0.0.9_1' => <<'---', # our + dev release
229 package Simple;
230 our $VERSION = "0.0.9_1";
231 ---
232   '1.12' => <<'---', # our + crazy string and substitution code
233 package Simple;
234 our $VERSION     = '1.12.B55J2qn'; our $WTF = $VERSION; $WTF =~ s/^\d+\.\d+\.//; # attempts to rationalize $WTF go here.
235 ---
236   '1.12' => <<'---', # our in braces, as in Dist::Zilla::Plugin::PkgVersion with use_our = 1
237 package Simple;
238 { our $VERSION = '1.12'; }
239 ---
240 );
241
242 # format: expected package name => code snippet
243 my @pkg_names = (
244   [ 'Simple' ] => <<'---', # package NAME
245 package Simple;
246 ---
247   [ 'Simple::Edward' ] => <<'---', # package NAME::SUBNAME
248 package Simple::Edward;
249 ---
250   [ 'Simple::Edward::' ] => <<'---', # package NAME::SUBNAME::
251 package Simple::Edward::;
252 ---
253   [ "Simple'Edward" ] => <<'---', # package NAME'SUBNAME
254 package Simple'Edward;
255 ---
256   [ "Simple'Edward::" ] => <<'---', # package NAME'SUBNAME::
257 package Simple'Edward::;
258 ---
259   [ 'Simple::::Edward' ] => <<'---', # package NAME::::SUBNAME
260 package Simple::::Edward;
261 ---
262   [ '::Simple::Edward' ] => <<'---', # package ::NAME::SUBNAME
263 package ::Simple::Edward;
264 ---
265   [ 'main' ] => <<'---', # package NAME:SUBNAME (fail)
266 package Simple:Edward;
267 ---
268   [ 'main' ] => <<'---', # package NAME' (fail)
269 package Simple';
270 ---
271   [ 'main' ] => <<'---', # package NAME::SUBNAME' (fail)
272 package Simple::Edward';
273 ---
274   [ 'main' ] => <<'---', # package NAME''SUBNAME (fail)
275 package Simple''Edward;
276 ---
277   [ 'main' ] => <<'---', # package NAME-SUBNAME (fail)
278 package Simple-Edward;
279 ---
280 );
281
282 # 2 tests per each pair of @modules (plus 1 for defined keys), 2 per pair of @pkg_names
283 plan tests => 63
284   + ( @modules + grep { defined $modules[2*$_] } 0..$#modules/2 )
285   + ( @pkg_names );
286
287 require_ok('Module::Metadata');
288
289 {
290     # class method C<find_module_by_name>
291     my $module = Module::Metadata->find_module_by_name(
292                    'Module::Metadata' );
293     ok( -e $module, 'find_module_by_name() succeeds' );
294 }
295
296 #########################
297
298 BEGIN {
299   my $cwd = File::Spec->rel2abs(Cwd::cwd);
300   sub original_cwd { return $cwd }
301 }
302
303 # Set up a temp directory
304 sub tmpdir {
305   my (@args) = @_;
306   my $dir = $ENV{PERL_CORE} ? original_cwd : File::Spec->tmpdir;
307   return File::Temp::tempdir('MMD-XXXXXXXX', CLEANUP => 0, DIR => $dir, @args);
308 }
309
310 my $tmp;
311 BEGIN { $tmp = tmpdir; diag "using temp dir $tmp"; }
312
313 END {
314   die "tests failed; leaving temp dir $tmp behind"
315     if $ENV{AUTHOR_TESTING} and not Test::Builder->new->is_passing;
316   diag "removing temp dir $tmp";
317   chdir original_cwd;
318   File::Path::rmtree($tmp);
319 }
320
321 # generates a new distribution:
322 # files => { relative filename => $content ... }
323 # returns the name of the distribution (not including version),
324 # and the absolute path name to the dist.
325 {
326   my $test_num = 0;
327   sub new_dist {
328     my %opts = @_;
329
330     my $distname = 'Simple' . $test_num++;
331     my $distdir = File::Spec->catdir($tmp, $distname);
332     note "using dist $distname in $distdir";
333
334     File::Path::mkpath($distdir) or die "failed to create '$distdir'";
335
336     foreach my $rel_filename (keys %{$opts{files}})
337     {
338       my $abs_filename = File::Spec->catfile($distdir, $rel_filename);
339       my $dirname = File::Basename::dirname($abs_filename);
340       unless (-d $dirname) {
341         File::Path::mkpath($dirname) or die "Can't create '$dirname'";
342       }
343
344       note "creating $abs_filename";
345       my $fh = IO::File->new(">$abs_filename") or die "Can't write '$abs_filename'\n";
346       print $fh $opts{files}{$rel_filename};
347       close $fh;
348     }
349
350     chdir $distdir;
351     return ($distname, $distdir);
352   }
353 }
354
355 {
356   # fail on invalid module name
357   my $pm_info = Module::Metadata->new_from_module(
358                   'Foo::Bar', inc => [] );
359   ok( !defined( $pm_info ), 'fail if can\'t find module by module name' );
360 }
361
362 {
363   # fail on invalid filename
364   my $file = File::Spec->catfile( 'Foo', 'Bar.pm' );
365   my $pm_info = Module::Metadata->new_from_file( $file, inc => [] );
366   ok( !defined( $pm_info ), 'fail if can\'t find module by file name' );
367 }
368
369 {
370   my $file = File::Spec->catfile('lib', 'Simple.pm');
371   my ($dist_name, $dist_dir) = new_dist(files => { $file => "package Simple;\n" });
372
373   # construct from module filename
374   my $pm_info = Module::Metadata->new_from_file( $file );
375   ok( defined( $pm_info ), 'new_from_file() succeeds' );
376
377   # construct from filehandle
378   my $handle = IO::File->new($file);
379   $pm_info = Module::Metadata->new_from_handle( $handle, $file );
380   ok( defined( $pm_info ), 'new_from_handle() succeeds' );
381   $pm_info = Module::Metadata->new_from_handle( $handle );
382   is( $pm_info, undef, "new_from_handle() without filename returns undef" );
383   close($handle);
384 }
385
386 {
387   # construct from module name, using custom include path
388   my $pm_info = Module::Metadata->new_from_module(
389                'Simple', inc => [ 'lib', @INC ] );
390   ok( defined( $pm_info ), 'new_from_module() succeeds' );
391 }
392
393
394 # iterate through @modules pairwise
395 my $test_case = 0;
396 while (++$test_case and my ($expected_version, $code) = splice @modules, 0, 2 ) {
397  SKIP: {
398     skip( "No our() support until perl 5.6", (defined $expected_version ? 3 : 2) )
399         if $] < 5.006 && $code =~ /\bour\b/;
400     skip( "No package NAME VERSION support until perl 5.11.1", (defined $expected_version ? 3 : 2) )
401         if $] < 5.011001 && $code =~ /package\s+[\w\:\']+\s+v?[0-9._]+/;
402
403     my $file = File::Spec->catfile('lib', 'Simple.pm');
404     my ($dist_name, $dist_dir) = new_dist(files => { $file => $code });
405
406     my $warnings = '';
407     local $SIG{__WARN__} = sub { $warnings .= $_ for @_ };
408     my $pm_info = Module::Metadata->new_from_file( $file );
409
410     my $errs;
411     my $got = $pm_info->version;
412
413     # note that in Test::More 0.94 and earlier, is() stringifies first before comparing;
414     # from 0.95_01 and later, it just lets the objects figure out how to handle 'eq'
415     # We want to ensure we preserve the original, as long as it's legal, so we
416     # explicitly check the stringified form.
417     isa_ok($got, 'version') if defined $expected_version;
418     is(
419       (defined $got ? "$got" : $got),
420       $expected_version,
421       "case $test_case: correct module version ("
422         . (defined $expected_version? "'$expected_version'" : 'undef')
423         . ')'
424     )
425     or $errs++;
426
427     is( $warnings, '', "case $test_case: no warnings from parsing" ) or $errs++;
428     diag Dumper({ got => $pm_info->version, module_contents => $code }) if $errs;
429   }
430 }
431
432 $test_case = 0;
433 while (++$test_case and my ($expected_name, $code) = splice @pkg_names, 0, 2) {
434     my $file = File::Spec->catfile('lib', 'Simple.pm');
435     my ($dist_name, $dist_dir) = new_dist(files => { $file => $code });
436
437     my $warnings = '';
438     local $SIG{__WARN__} = sub { $warnings .= $_ for @_ };
439     my $pm_info = Module::Metadata->new_from_file( $file );
440
441     # Test::Builder will prematurely numify objects, so use this form
442     my $errs;
443     my @got = $pm_info->packages_inside();
444     is_deeply( \@got, $expected_name,
445                "case $test_case: correct package names (expected '" . join(', ', @$expected_name) . "')" )
446             or $errs++;
447     is( $warnings, '', "case $test_case: no warnings from parsing" ) or $errs++;
448     diag "Got: '" . join(', ', @got) . "'\nModule contents:\n$code" if $errs;
449 }
450
451 {
452   # Find each package only once
453   my $file = File::Spec->catfile('lib', 'Simple.pm');
454   my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
455 package Simple;
456 $VERSION = '1.23';
457 package Error::Simple;
458 $VERSION = '2.34';
459 package Simple;
460 ---
461
462   my $pm_info = Module::Metadata->new_from_file( $file );
463
464   my @packages = $pm_info->packages_inside;
465   is( @packages, 2, 'record only one occurence of each package' );
466 }
467
468 {
469   # Module 'Simple.pm' does not contain package 'Simple';
470   # constructor should not complain, no default module name or version
471   my $file = File::Spec->catfile('lib', 'Simple.pm');
472   my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
473 package Simple::Not;
474 $VERSION = '1.23';
475 ---
476
477   my $pm_info = Module::Metadata->new_from_file( $file );
478
479   is( $pm_info->name, undef, 'no default package' );
480   is( $pm_info->version, undef, 'no version w/o default package' );
481 }
482
483 {
484   # Module 'Simple.pm' contains an alpha version
485   # constructor should report first $VERSION found
486   my $file = File::Spec->catfile('lib', 'Simple.pm');
487   my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
488 package Simple;
489 $VERSION = '1.23_01';
490 $VERSION = eval $VERSION;
491 ---
492
493   my $pm_info = Module::Metadata->new_from_file( $file );
494
495   is( $pm_info->version, '1.23_01', 'alpha version reported');
496
497   # NOTE the following test has be done this way because Test::Builder is
498   # too smart for our own good and tries to see if the version object is a
499   # dual-var, which breaks with alpha versions:
500   #    Argument "1.23_0100" isn't numeric in addition (+) at
501   #    /usr/lib/perl5/5.8.7/Test/Builder.pm line 505.
502
503   ok( $pm_info->version > 1.23, 'alpha version greater than non');
504 }
505
506 # parse $VERSION lines scripts for package main
507 my @scripts = (
508   <<'---', # package main declared
509 #!perl -w
510 package main;
511 $VERSION = '0.01';
512 ---
513   <<'---', # on first non-comment line, non declared package main
514 #!perl -w
515 $VERSION = '0.01';
516 ---
517   <<'---', # after non-comment line
518 #!perl -w
519 use strict;
520 $VERSION = '0.01';
521 ---
522   <<'---', # 1st declared package
523 #!perl -w
524 package main;
525 $VERSION = '0.01';
526 package _private;
527 $VERSION = '999';
528 ---
529   <<'---', # 2nd declared package
530 #!perl -w
531 package _private;
532 $VERSION = '999';
533 package main;
534 $VERSION = '0.01';
535 ---
536   <<'---', # split package
537 #!perl -w
538 package main;
539 package _private;
540 $VERSION = '999';
541 package main;
542 $VERSION = '0.01';
543 ---
544   <<'---', # define 'main' version from other package
545 package _private;
546 $::VERSION = 0.01;
547 $VERSION = '999';
548 ---
549   <<'---', # define 'main' version from other package
550 package _private;
551 $VERSION = '999';
552 $::VERSION = 0.01;
553 ---
554 );
555
556 my ( $i, $n ) = ( 1, scalar( @scripts ) );
557 foreach my $script ( @scripts ) {
558   my $file = File::Spec->catfile('bin', 'simple.plx');
559   my ($dist_name, $dist_dir) = new_dist(files => { $file => $script } );
560   my $pm_info = Module::Metadata->new_from_file( $file );
561
562   is( $pm_info->version, '0.01', "correct script version ($i of $n)" );
563   $i++;
564 }
565
566 {
567   # examine properties of a module: name, pod, etc
568   my $file = File::Spec->catfile('lib', 'Simple.pm');
569   my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
570 package Simple;
571 $VERSION = '0.01';
572 package Simple::Ex;
573 $VERSION = '0.02';
574
575 =head1 NAME
576
577 Simple - It's easy.
578
579 =head1 AUTHOR
580
581 Simple Simon
582
583 You can find me on the IRC channel
584 #simon on irc.perl.org.
585
586 =cut
587 ---
588
589   my $pm_info = Module::Metadata->new_from_module(
590              'Simple', inc => [ 'lib', @INC ] );
591
592   is( $pm_info->name, 'Simple', 'found default package' );
593   is( $pm_info->version, '0.01', 'version for default package' );
594
595   # got correct version for secondary package
596   is( $pm_info->version( 'Simple::Ex' ), '0.02',
597       'version for secondary package' );
598
599   my $filename = $pm_info->filename;
600   ok( defined( $filename ) && -e $filename,
601       'filename() returns valid path to module file' );
602
603   my @packages = $pm_info->packages_inside;
604   is( @packages, 2, 'found correct number of packages' );
605   is( $packages[0], 'Simple', 'packages stored in order found' );
606
607   # we can detect presence of pod regardless of whether we are collecting it
608   ok( $pm_info->contains_pod, 'contains_pod() succeeds' );
609
610   my @pod = $pm_info->pod_inside;
611   is_deeply( \@pod, [qw(NAME AUTHOR)], 'found all pod sections' );
612
613   is( $pm_info->pod('NONE') , undef,
614       'return undef() if pod section not present' );
615
616   is( $pm_info->pod('NAME'), undef,
617       'return undef() if pod section not collected' );
618
619
620   # collect_pod
621   $pm_info = Module::Metadata->new_from_module(
622                'Simple', inc => [ 'lib', @INC ], collect_pod => 1 );
623
624   my %pod;
625   for my $section (qw(NAME AUTHOR)) {
626     my $content = $pm_info->pod( $section );
627     if ( $content ) {
628       $content =~ s/^\s+//;
629       $content =~ s/\s+$//;
630     }
631     $pod{$section} = $content;
632   }
633   my %expected = (
634     NAME   => q|Simple - It's easy.|,
635     AUTHOR => <<'EXPECTED'
636 Simple Simon
637
638 You can find me on the IRC channel
639 #simon on irc.perl.org.
640 EXPECTED
641   );
642   for my $text (values %expected) {
643     $text =~ s/^\s+//;
644     $text =~ s/\s+$//;
645   }
646   is( $pod{NAME},   $expected{NAME},   'collected NAME pod section' );
647   is( $pod{AUTHOR}, $expected{AUTHOR}, 'collected AUTHOR pod section' );
648 }
649
650 {
651   # test things that look like POD, but aren't
652   my $file = File::Spec->catfile('lib', 'Simple.pm');
653   my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
654 package Simple;
655
656 =YES THIS STARTS POD
657
658 our $VERSION = '999';
659
660 =cute
661
662 our $VERSION = '666';
663
664 =cut
665
666 *foo
667 =*no_this_does_not_start_pod;
668
669 our $VERSION = '1.23';
670
671 ---
672   my $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
673   is( $pm_info->name, 'Simple', 'found default package' );
674   is( $pm_info->version, '1.23', 'version for default package' );
675 }
676
677 {
678   # Make sure processing stops after __DATA__
679   my $file = File::Spec->catfile('lib', 'Simple.pm');
680   my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
681 package Simple;
682 $VERSION = '0.01';
683 __DATA__
684 *UNIVERSAL::VERSION = sub {
685   foo();
686 };
687 ---
688
689   my $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
690   is( $pm_info->name, 'Simple', 'found default package' );
691   is( $pm_info->version, '0.01', 'version for default package' );
692   my @packages = $pm_info->packages_inside;
693   is_deeply(\@packages, ['Simple'], 'packages inside');
694 }
695
696 {
697   # Make sure we handle version.pm $VERSIONs well
698   my $file = File::Spec->catfile('lib', 'Simple.pm');
699   my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
700 package Simple;
701 $VERSION = version->new('0.60.' . (qw$Revision: 128 $)[1]);
702 package Simple::Simon;
703 $VERSION = version->new('0.61.' . (qw$Revision: 129 $)[1]);
704 ---
705
706   my $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
707   is( $pm_info->name, 'Simple', 'found default package' );
708   is( $pm_info->version, '0.60.128', 'version for default package' );
709   my @packages = $pm_info->packages_inside;
710   is_deeply([sort @packages], ['Simple', 'Simple::Simon'], 'packages inside');
711   is( $pm_info->version('Simple::Simon'), '0.61.129', 'version for embedded package' );
712 }
713
714 # check that package_versions_from_directory works
715
716 {
717   my $file = File::Spec->catfile('lib', 'Simple.pm');
718   my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
719 package Simple;
720 $VERSION = '0.01';
721 package Simple::Ex;
722 $VERSION = '0.02';
723 {
724   package main; # should ignore this
725 }
726 {
727   package DB; # should ignore this
728 }
729 {
730   package Simple::_private; # should ignore this
731 }
732
733 =head1 NAME
734
735 Simple - It's easy.
736
737 =head1 AUTHOR
738
739 Simple Simon
740
741 =cut
742 ---
743
744   my $exp_pvfd = {
745     'Simple' => {
746       'file' => 'Simple.pm',
747       'version' => '0.01'
748     },
749     'Simple::Ex' => {
750       'file' => 'Simple.pm',
751       'version' => '0.02'
752     }
753   };
754
755   my $got_pvfd = Module::Metadata->package_versions_from_directory('lib');
756
757   is_deeply( $got_pvfd, $exp_pvfd, "package_version_from_directory()" )
758     or diag explain $got_pvfd;
759
760 {
761   my $got_provides = Module::Metadata->provides(dir => 'lib', version => 2);
762   my $exp_provides = {
763     'Simple' => {
764       'file' => 'lib/Simple.pm',
765       'version' => '0.01'
766     },
767     'Simple::Ex' => {
768       'file' => 'lib/Simple.pm',
769       'version' => '0.02'
770     }
771   };
772
773   is_deeply( $got_provides, $exp_provides, "provides()" )
774     or diag explain $got_provides;
775 }
776
777 {
778   my $got_provides = Module::Metadata->provides(dir => 'lib', prefix => 'other', version => 1.4);
779   my $exp_provides = {
780     'Simple' => {
781       'file' => 'other/Simple.pm',
782       'version' => '0.01'
783     },
784     'Simple::Ex' => {
785       'file' => 'other/Simple.pm',
786       'version' => '0.02'
787     }
788   };
789
790   is_deeply( $got_provides, $exp_provides, "provides()" )
791     or diag explain $got_provides;
792 }
793 }
794
795 # Check package_versions_from_directory with regard to case-sensitivity
796 {
797   my $file = File::Spec->catfile('lib', 'Simple.pm');
798   my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
799 package simple;
800 $VERSION = '0.01';
801 ---
802
803   my $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
804   is( $pm_info->name, undef, 'no default package' );
805   is( $pm_info->version, undef, 'version for default package' );
806   is( $pm_info->version('simple'), '0.01', 'version for lower-case package' );
807   is( $pm_info->version('Simple'), undef, 'version for capitalized package' );
808   ok( $pm_info->is_indexable(), 'an indexable package is found' );
809   ok( $pm_info->is_indexable('simple'), 'the simple package is indexable' );
810   ok( !$pm_info->is_indexable('Simple'), 'the Simple package would not be indexed' );
811 }
812
813 {
814   my $file = File::Spec->catfile('lib', 'Simple.pm');
815   my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
816 package simple;
817 $VERSION = '0.01';
818 package Simple;
819 $VERSION = '0.02';
820 package SiMpLe;
821 $VERSION = '0.03';
822 ---
823
824   my $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
825   is( $pm_info->name, 'Simple', 'found default package' );
826   is( $pm_info->version, '0.02', 'version for default package' );
827   is( $pm_info->version('simple'), '0.01', 'version for lower-case package' );
828   is( $pm_info->version('Simple'), '0.02', 'version for capitalized package' );
829   is( $pm_info->version('SiMpLe'), '0.03', 'version for mixed-case package' );
830   ok( $pm_info->is_indexable('simple'), 'the simple package is indexable' );
831   ok( $pm_info->is_indexable('Simple'), 'the Simple package is indexable' );
832 }
833
834 {
835   my $file = File::Spec->catfile('lib', 'Simple.pm');
836   my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
837 package ## hide from PAUSE
838    simple;
839 $VERSION = '0.01';
840 ---
841
842   my $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
843   is( $pm_info->name, undef, 'no package names found' );
844   ok( !$pm_info->is_indexable('simple'), 'the simple package would not be indexed' );
845   ok( !$pm_info->is_indexable('Simple'), 'the Simple package would not be indexed' );
846   ok( !$pm_info->is_indexable(), 'no indexable package is found' );
847 }