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