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