X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Fmetadata.t;h=38649bd41921c4a76b1b40d9575ce345a9fd6e7b;hb=c8b69ebec0fe977c8b6ae89a53ebd0319dc93988;hp=b93cefbe781e9cf068291c5cb49a0bef8cf8b06c;hpb=ca33f3bdaed855f973142d952c708d8792d56045;p=p5sagit%2FModule-Metadata.git diff --git a/t/metadata.t b/t/metadata.t index b93cefb..38649bd 100644 --- a/t/metadata.t +++ b/t/metadata.t @@ -3,7 +3,9 @@ # vim:ts=8:sw=2:et:sta:sts=2 use strict; +use warnings; use lib 't/lib'; +use Test::More; use IO::File; use MBTest; @@ -28,6 +30,15 @@ package Simple; our $VERSION; $VERSION = '1.23'; --- + '1.23' => <<'---', # commented & defined on same line +package Simple; +our $VERSION = '1.23'; # our $VERSION = '4.56'; +--- + '1.23' => <<'---', # commented & defined on separate lines +package Simple; +# our $VERSION = '4.56'; +our $VERSION = '1.23'; +--- '1.23' => <<'---', # use vars package Simple; use vars qw( $VERSION ); @@ -200,10 +211,56 @@ package Simple v1.2.3_4 { 1; } --- + '0' => <<'---', # set from separately-initialised variable +package Simple; + our $CVSVERSION = '$Revision: 1.7 $'; + our ($VERSION) = ($CVSVERSION =~ /(\d+\.\d+)/); +} +--- ); my %modules = reverse @modules; -plan tests => 42 + 2 * keys( %modules ); +my @pkg_names = ( + [ 'Simple' ] => <<'---', # package NAME +package Simple; +--- + [ 'Simple::Edward' ] => <<'---', # package NAME::SUBNAME +package Simple::Edward; +--- + [ 'Simple::Edward::' ] => <<'---', # package NAME::SUBNAME:: +package Simple::Edward::; +--- + [ "Simple'Edward" ] => <<'---', # package NAME'SUBNAME +package Simple'Edward; +--- + [ "Simple'Edward::" ] => <<'---', # package NAME'SUBNAME:: +package Simple'Edward::; +--- + [ 'Simple::::Edward' ] => <<'---', # package NAME::::SUBNAME +package Simple::::Edward; +--- + [ '::Simple::Edward' ] => <<'---', # package ::NAME::SUBNAME +package ::Simple::Edward; +--- + [ 'main' ] => <<'---', # package NAME:SUBNAME (fail) +package Simple:Edward; +--- + [ 'main' ] => <<'---', # package NAME' (fail) +package Simple'; +--- + [ 'main' ] => <<'---', # package NAME::SUBNAME' (fail) +package Simple::Edward'; +--- + [ 'main' ] => <<'---', # package NAME''SUBNAME (fail) +package Simple''Edward; +--- + [ 'main' ] => <<'---', # package NAME-SUBNAME (fail) +package Simple-Edward; +--- +); +my %pkg_names = reverse @pkg_names; + +plan tests => 63 + (2 * keys( %modules )) + (2 * keys( %pkg_names )); require_ok('Module::Metadata'); @@ -246,7 +303,7 @@ $pm_info = Module::Metadata->new_from_handle( $handle, $file ); ok( defined( $pm_info ), 'new_from_handle() succeeds' ); $pm_info = Module::Metadata->new_from_handle( $handle ); is( $pm_info, undef, "new_from_handle() without filename returns undef" ); - +close($handle); # construct from module name, using custom include path $pm_info = Module::Metadata->new_from_module( @@ -289,6 +346,29 @@ foreach my $module ( sort keys %modules ) { # revert to pristine state $dist->regen( clean => 1 ); +foreach my $pkg_name ( sort keys %pkg_names ) { + my $expected = $pkg_names{$pkg_name}; + + $dist->change_file( 'lib/Simple.pm', $pkg_name ); + $dist->regen; + + my $warnings = ''; + local $SIG{__WARN__} = sub { $warnings .= $_ for @_ }; + my $pm_info = Module::Metadata->new_from_file( $file ); + + # Test::Builder will prematurely numify objects, so use this form + my $errs; + my @got = $pm_info->packages_inside(); + is_deeply( \@got, $expected, + "correct package names (expected '" . join(', ', @$expected) . "')" ) + or $errs++; + is( $warnings, '', 'no warnings from parsing' ) or $errs++; + diag "Got: '" . join(', ', @got) . "'\nModule contents:\n$pkg_name" if $errs; +} + +# revert to pristine state +$dist->regen( clean => 1 ); + # Find each package only once $dist->change_file( 'lib/Simple.pm', <<'---' ); package Simple; @@ -420,6 +500,9 @@ Simple - It's easy. Simple Simon +You can find me on the IRC channel +#simon on irc.perl.org. + =cut --- $dist->regen; @@ -459,13 +542,59 @@ is( $pm_info->pod('NAME'), undef, $pm_info = Module::Metadata->new_from_module( $dist->name, inc => [ 'lib', @INC ], collect_pod => 1 ); -my $name = $pm_info->pod('NAME'); -if ( $name ) { - $name =~ s/^\s+//; - $name =~ s/\s+$//; +{ + my %pod; + for my $section (qw(NAME AUTHOR)) { + my $content = $pm_info->pod( $section ); + if ( $content ) { + $content =~ s/^\s+//; + $content =~ s/\s+$//; + } + $pod{$section} = $content; + } + my %expected = ( + NAME => q|Simple - It's easy.|, + AUTHOR => <<'EXPECTED' +Simple Simon + +You can find me on the IRC channel +#simon on irc.perl.org. +EXPECTED + ); + for my $text (values %expected) { + $text =~ s/^\s+//; + $text =~ s/\s+$//; + } + is( $pod{NAME}, $expected{NAME}, 'collected NAME pod section' ); + is( $pod{AUTHOR}, $expected{AUTHOR}, 'collected AUTHOR pod section' ); } -is( $name, q|Simple - It's easy.|, 'collected pod section' ); +{ + # test things that look like POD, but aren't +$dist->change_file( 'lib/Simple.pm', <<'---' ); +package Simple; + +=YES THIS STARTS POD + +our $VERSION = '999'; + +=cute + +our $VERSION = '666'; + +=cut + +*foo +=*no_this_does_not_start_pod; + +our $VERSION = '1.23'; + +--- + $dist->regen; + $pm_info = Module::Metadata->new_from_file('lib/Simple.pm'); + is( $pm_info->name, 'Simple', 'found default package' ); + is( $pm_info->version, '1.23', 'version for default package' ); +} { # Make sure processing stops after __DATA__ @@ -550,7 +679,7 @@ is_deeply( $got_pvfd, $exp_pvfd, "package_version_from_directory()" ) or diag explain $got_pvfd; { - my $got_provides = Module::Metadata->provides(dir => 'lib'); + my $got_provides = Module::Metadata->provides(dir => 'lib', version => 2); my $exp_provides = { 'Simple' => { 'file' => 'lib/Simple.pm', @@ -567,7 +696,7 @@ is_deeply( $got_pvfd, $exp_pvfd, "package_version_from_directory()" ) } { - my $got_provides = Module::Metadata->provides(dir => 'lib', prefix => 'other'); + my $got_provides = Module::Metadata->provides(dir => 'lib', prefix => 'other', version => 1.4); my $exp_provides = { 'Simple' => { 'file' => 'other/Simple.pm', @@ -582,3 +711,54 @@ is_deeply( $got_pvfd, $exp_pvfd, "package_version_from_directory()" ) is_deeply( $got_provides, $exp_provides, "provides()" ) or diag explain $got_provides; } + +# Check package_versions_from_directory with regard to case-sensitivity +{ + $dist->change_file( 'lib/Simple.pm', <<'---' ); +package simple; +$VERSION = '0.01'; +--- + $dist->regen; + + $pm_info = Module::Metadata->new_from_file('lib/Simple.pm'); + is( $pm_info->name, undef, 'no default package' ); + is( $pm_info->version, undef, 'version for default package' ); + is( $pm_info->version('simple'), '0.01', 'version for lower-case package' ); + is( $pm_info->version('Simple'), undef, 'version for capitalized package' ); + ok( $pm_info->is_indexable(), 'an indexable package is found' ); + ok( $pm_info->is_indexable('simple'), 'the simple package is indexable' ); + ok( !$pm_info->is_indexable('Simple'), 'the Simple package would not be indexed' ); + + $dist->change_file( 'lib/Simple.pm', <<'---' ); +package simple; +$VERSION = '0.01'; +package Simple; +$VERSION = '0.02'; +package SiMpLe; +$VERSION = '0.03'; +--- + $dist->regen; + + $pm_info = Module::Metadata->new_from_file('lib/Simple.pm'); + is( $pm_info->name, 'Simple', 'found default package' ); + is( $pm_info->version, '0.02', 'version for default package' ); + is( $pm_info->version('simple'), '0.01', 'version for lower-case package' ); + is( $pm_info->version('Simple'), '0.02', 'version for capitalized package' ); + is( $pm_info->version('SiMpLe'), '0.03', 'version for mixed-case package' ); + ok( $pm_info->is_indexable('simple'), 'the simple package is indexable' ); + ok( $pm_info->is_indexable('Simple'), 'the Simple package is indexable' ); + + $dist->change_file( 'lib/Simple.pm', <<'---' ); +package ## hide from PAUSE + simple; +$VERSION = '0.01'; +--- + + $dist->regen; + + $pm_info = Module::Metadata->new_from_file('lib/Simple.pm'); + is( $pm_info->name, undef, 'no package names found' ); + ok( !$pm_info->is_indexable('simple'), 'the simple package would not be indexed' ); + ok( !$pm_info->is_indexable('Simple'), 'the Simple package would not be indexed' ); + ok( !$pm_info->is_indexable(), 'no indexable package is found' ); +}