X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Fmetadata.t;h=31112912e674ec5fa6405f471191c61aff3dff01;hb=2235a0d7a71bb7d5edda0901a5f5d8bb40158e72;hp=ed343513122104c2d3f6402f766bec99775a00e5;hpb=388bf28253bd1f10013ca7a83820b5a9d8a54598;p=p5sagit%2FModule-Metadata.git diff --git a/t/metadata.t b/t/metadata.t index ed34351..3111291 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,17 +211,65 @@ 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 => 40 + 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'); -# class method C -my $module = Module::Metadata->find_module_by_name( - 'Module::Metadata' ); -ok( -e $module, 'find_module_by_name() succeeds' ); +{ + # class method C + my $module = Module::Metadata->find_module_by_name( + 'Module::Metadata' ); + ok( -e $module, 'find_module_by_name() succeeds' ); +} ######################### @@ -222,36 +281,39 @@ $dist->regen; $dist->chdir_in; +{ + # fail on invalid module name + my $pm_info = Module::Metadata->new_from_module( + 'Foo::Bar', inc => [] ); + ok( !defined( $pm_info ), 'fail if can\'t find module by module name' ); +} -# fail on invalid module name -my $pm_info = Module::Metadata->new_from_module( - 'Foo::Bar', inc => [] ); -ok( !defined( $pm_info ), 'fail if can\'t find module by module name' ); - - -# fail on invalid filename -my $file = File::Spec->catfile( 'Foo', 'Bar.pm' ); -$pm_info = Module::Metadata->new_from_file( $file, inc => [] ); -ok( !defined( $pm_info ), 'fail if can\'t find module by file name' ); - - -# construct from module filename -$file = File::Spec->catfile( 'lib', split( /::/, $dist->name ) ) . '.pm'; -$pm_info = Module::Metadata->new_from_file( $file ); -ok( defined( $pm_info ), 'new_from_file() succeeds' ); - -# construct from filehandle -my $handle = IO::File->new($file); -$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" ); - +{ + # fail on invalid filename + my $file = File::Spec->catfile( 'Foo', 'Bar.pm' ); + my $pm_info = Module::Metadata->new_from_file( $file, inc => [] ); + ok( !defined( $pm_info ), 'fail if can\'t find module by file name' ); +} -# construct from module name, using custom include path -$pm_info = Module::Metadata->new_from_module( - $dist->name, inc => [ 'lib', @INC ] ); -ok( defined( $pm_info ), 'new_from_module() succeeds' ); +{ + # construct from module filename + my $file = File::Spec->catfile( 'lib', split( /::/, $dist->name ) ) . '.pm'; + my $pm_info = Module::Metadata->new_from_file( $file ); + ok( defined( $pm_info ), 'new_from_file() succeeds' ); + + # construct from filehandle + my $handle = IO::File->new($file); + $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( + $dist->name, inc => [ 'lib', @INC ] ); + ok( defined( $pm_info ), 'new_from_module() succeeds' ); +} foreach my $module ( sort keys %modules ) { @@ -267,6 +329,7 @@ foreach my $module ( sort keys %modules ) { my $warnings = ''; local $SIG{__WARN__} = sub { $warnings .= $_ for @_ }; + my $file = File::Spec->catfile( 'lib', split( /::/, $dist->name ) ) . '.pm'; my $pm_info = Module::Metadata->new_from_file( $file ); # Test::Builder will prematurely numify objects, so use this form @@ -289,8 +352,33 @@ foreach my $module ( sort keys %modules ) { # revert to pristine state $dist->regen( clean => 1 ); -# Find each package only once -$dist->change_file( 'lib/Simple.pm', <<'---' ); +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 $file = File::Spec->catfile( 'lib', split( /::/, $dist->name ) ) . '.pm'; + 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; $VERSION = '1.23'; package Error::Simple; @@ -298,47 +386,54 @@ $VERSION = '2.34'; package Simple; --- -$dist->regen; - -$pm_info = Module::Metadata->new_from_file( $file ); + $dist->regen; -my @packages = $pm_info->packages_inside; -is( @packages, 2, 'record only one occurence of each package' ); + my $file = File::Spec->catfile( 'lib', split( /::/, $dist->name ) ) . '.pm'; + my $pm_info = Module::Metadata->new_from_file( $file ); + my @packages = $pm_info->packages_inside; + is( @packages, 2, 'record only one occurence of each package' ); +} -# Module 'Simple.pm' does not contain package 'Simple'; -# constructor should not complain, no default module name or version -$dist->change_file( 'lib/Simple.pm', <<'---' ); +{ + # Module 'Simple.pm' does not contain package 'Simple'; + # constructor should not complain, no default module name or version + $dist->change_file( 'lib/Simple.pm', <<'---' ); package Simple::Not; $VERSION = '1.23'; --- -$dist->regen; -$pm_info = Module::Metadata->new_from_file( $file ); + $dist->regen; + my $file = File::Spec->catfile( 'lib', split( /::/, $dist->name ) ) . '.pm'; + my $pm_info = Module::Metadata->new_from_file( $file ); -is( $pm_info->name, undef, 'no default package' ); -is( $pm_info->version, undef, 'no version w/o default package' ); + is( $pm_info->name, undef, 'no default package' ); + is( $pm_info->version, undef, 'no version w/o default package' ); +} -# Module 'Simple.pm' contains an alpha version -# constructor should report first $VERSION found -$dist->change_file( 'lib/Simple.pm', <<'---' ); +{ + # Module 'Simple.pm' contains an alpha version + # constructor should report first $VERSION found + $dist->change_file( 'lib/Simple.pm', <<'---' ); package Simple; $VERSION = '1.23_01'; $VERSION = eval $VERSION; --- -$dist->regen; -$pm_info = Module::Metadata->new_from_file( $file ); + $dist->regen; + my $file = File::Spec->catfile( 'lib', split( /::/, $dist->name ) ) . '.pm'; + my $pm_info = Module::Metadata->new_from_file( $file ); -is( $pm_info->version, '1.23_01', 'alpha version reported'); + is( $pm_info->version, '1.23_01', 'alpha version reported'); -# NOTE the following test has be done this way because Test::Builder is -# too smart for our own good and tries to see if the version object is a -# dual-var, which breaks with alpha versions: -# Argument "1.23_0100" isn't numeric in addition (+) at -# /usr/lib/perl5/5.8.7/Test/Builder.pm line 505. + # NOTE the following test has be done this way because Test::Builder is + # too smart for our own good and tries to see if the version object is a + # dual-var, which breaks with alpha versions: + # Argument "1.23_0100" isn't numeric in addition (+) at + # /usr/lib/perl5/5.8.7/Test/Builder.pm line 505. -ok( $pm_info->version > 1.23, 'alpha version greater than non'); + ok( $pm_info->version > 1.23, 'alpha version greater than non'); +} # revert to pristine state $dist->regen( clean => 1 ); @@ -397,16 +492,16 @@ my ( $i, $n ) = ( 1, scalar( @scripts ) ); foreach my $script ( @scripts ) { $dist->change_file( 'bin/simple.plx', $script ); $dist->regen; - $pm_info = Module::Metadata->new_from_file( + my $pm_info = Module::Metadata->new_from_file( File::Spec->catfile( 'bin', 'simple.plx' ) ); is( $pm_info->version, '0.01', "correct script version ($i of $n)" ); $i++; } - -# examine properties of a module: name, pod, etc -$dist->change_file( 'lib/Simple.pm', <<'---' ); +{ + # examine properties of a module: name, pod, etc + $dist->change_file( 'lib/Simple.pm', <<'---' ); package Simple; $VERSION = '0.01'; package Simple::Ex; @@ -420,52 +515,100 @@ Simple - It's easy. Simple Simon +You can find me on the IRC channel +#simon on irc.perl.org. + =cut --- -$dist->regen; + $dist->regen; -$pm_info = Module::Metadata->new_from_module( + my $pm_info = Module::Metadata->new_from_module( $dist->name, inc => [ 'lib', @INC ] ); -is( $pm_info->name, 'Simple', 'found default package' ); -is( $pm_info->version, '0.01', 'version for default package' ); + is( $pm_info->name, 'Simple', 'found default package' ); + is( $pm_info->version, '0.01', 'version for default package' ); -# got correct version for secondary package -is( $pm_info->version( 'Simple::Ex' ), '0.02', - 'version for secondary package' ); + # got correct version for secondary package + is( $pm_info->version( 'Simple::Ex' ), '0.02', + 'version for secondary package' ); -my $filename = $pm_info->filename; -ok( defined( $filename ) && -e $filename, - 'filename() returns valid path to module file' ); + my $filename = $pm_info->filename; + ok( defined( $filename ) && -e $filename, + 'filename() returns valid path to module file' ); -@packages = $pm_info->packages_inside; -is( @packages, 2, 'found correct number of packages' ); -is( $packages[0], 'Simple', 'packages stored in order found' ); + my @packages = $pm_info->packages_inside; + is( @packages, 2, 'found correct number of packages' ); + is( $packages[0], 'Simple', 'packages stored in order found' ); -# we can detect presence of pod regardless of whether we are collecting it -ok( $pm_info->contains_pod, 'contains_pod() succeeds' ); + # we can detect presence of pod regardless of whether we are collecting it + ok( $pm_info->contains_pod, 'contains_pod() succeeds' ); -my @pod = $pm_info->pod_inside; -is_deeply( \@pod, [qw(NAME AUTHOR)], 'found all pod sections' ); + my @pod = $pm_info->pod_inside; + is_deeply( \@pod, [qw(NAME AUTHOR)], 'found all pod sections' ); -is( $pm_info->pod('NONE') , undef, - 'return undef() if pod section not present' ); + is( $pm_info->pod('NONE') , undef, + 'return undef() if pod section not present' ); -is( $pm_info->pod('NAME'), undef, - 'return undef() if pod section not collected' ); + is( $pm_info->pod('NAME'), undef, + 'return undef() if pod section not collected' ); -# collect_pod -$pm_info = Module::Metadata->new_from_module( - $dist->name, inc => [ 'lib', @INC ], collect_pod => 1 ); + # collect_pod + $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; + my $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__ @@ -479,7 +622,7 @@ __DATA__ --- $dist->regen; - $pm_info = Module::Metadata->new_from_file('lib/Simple.pm'); + my $pm_info = Module::Metadata->new_from_file('lib/Simple.pm'); is( $pm_info->name, 'Simple', 'found default package' ); is( $pm_info->version, '0.01', 'version for default package' ); my @packages = $pm_info->packages_inside; @@ -496,7 +639,7 @@ $VERSION = version->new('0.61.' . (qw$Revision: 129 $)[1]); --- $dist->regen; - $pm_info = Module::Metadata->new_from_file('lib/Simple.pm'); + my $pm_info = Module::Metadata->new_from_file('lib/Simple.pm'); is( $pm_info->name, 'Simple', 'found default package' ); is( $pm_info->version, '0.60.128', 'version for default package' ); my @packages = $pm_info->packages_inside; @@ -506,7 +649,8 @@ $VERSION = version->new('0.61.' . (qw$Revision: 129 $)[1]); # check that package_versions_from_directory works -$dist->change_file( 'lib/Simple.pm', <<'---' ); +{ + $dist->change_file( 'lib/Simple.pm', <<'---' ); package Simple; $VERSION = '0.01'; package Simple::Ex; @@ -531,20 +675,110 @@ Simple Simon =cut --- -$dist->regen; + $dist->regen; -my $exp_pvfd = { - 'Simple' => { - 'file' => 'Simple.pm', - 'version' => '0.01' - }, - 'Simple::Ex' => { - 'file' => 'Simple.pm', - 'version' => '0.02' - } -}; + my $exp_pvfd = { + 'Simple' => { + 'file' => 'Simple.pm', + 'version' => '0.01' + }, + 'Simple::Ex' => { + 'file' => 'Simple.pm', + 'version' => '0.02' + } + }; + + my $got_pvfd = Module::Metadata->package_versions_from_directory('lib'); -my $got_pvfd = Module::Metadata->package_versions_from_directory('lib'); + is_deeply( $got_pvfd, $exp_pvfd, "package_version_from_directory()" ) + or diag explain $got_pvfd; -is_deeply( $got_pvfd, $exp_pvfd, "package_version_from_directory" ) - or diag explain $got_pvfd; +{ + my $got_provides = Module::Metadata->provides(dir => 'lib', version => 2); + my $exp_provides = { + 'Simple' => { + 'file' => 'lib/Simple.pm', + 'version' => '0.01' + }, + 'Simple::Ex' => { + 'file' => 'lib/Simple.pm', + 'version' => '0.02' + } + }; + + is_deeply( $got_provides, $exp_provides, "provides()" ) + or diag explain $got_provides; +} + +{ + my $got_provides = Module::Metadata->provides(dir => 'lib', prefix => 'other', version => 1.4); + my $exp_provides = { + 'Simple' => { + 'file' => 'other/Simple.pm', + 'version' => '0.01' + }, + 'Simple::Ex' => { + 'file' => 'other/Simple.pm', + 'version' => '0.02' + } + }; + + 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; + + my $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; + + my $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; + + my $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' ); +}