use strict;
use lib 't/lib';
+use IO::File;
use MBTest;
+my $undef;
+
# parse various module $VERSION lines
# these will be reversed later to create %modules
my @modules = (
+ $undef => <<'---', # no $VERSION line
+package Simple;
+---
+ $undef => <<'---', # undefined $VERSION
+package Simple;
+our $VERSION;
+---
'1.23' => <<'---', # declared & defined on same line with 'our'
package Simple;
our $VERSION = '1.23';
our $VERSION;
$VERSION = 'onetwothree';
---
+ $undef => <<'---', # package NAME BLOCK, undef $VERSION
+package Simple {
+ our $VERSION;
+}
+---
+ '1.23' => <<'---', # package NAME BLOCK, with $VERSION
+package Simple {
+ our $VERSION = '1.23';
+}
+---
+ '1.23' => <<'---', # package NAME VERSION BLOCK
+package Simple 1.23 {
+ 1;
+}
+---
+ 'v1.2.3_4' => <<'---', # package NAME VERSION BLOCK
+package Simple v1.2.3_4 {
+ 1;
+}
+---
);
my %modules = reverse @modules;
-plan tests => 37 + 2 * keys( %modules );
+plan tests => 42 + 2 * keys( %modules );
require_ok('Module::Metadata');
$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" );
+
+
# construct from module name, using custom include path
$pm_info = Module::Metadata->new_from_module(
$dist->name, inc => [ 'lib', @INC ] );
# Test::Builder will prematurely numify objects, so use this form
my $errs;
- ok( $pm_info->version eq $expected,
- "correct module version (expected '$expected')" )
- or $errs++;
+ my $got = $pm_info->version;
+ if ( defined $expected ) {
+ ok( $got eq $expected,
+ "correct module version (expected '$expected')" )
+ or $errs++;
+ } else {
+ ok( !defined($got),
+ "correct module version (expected undef)" )
+ or $errs++;
+ }
is( $warnings, '', 'no warnings from parsing' ) or $errs++;
- diag "Got: '@{[$pm_info->version]}'\nModule contents:\n$module" if $errs;
+ diag "Got: '$got'\nModule contents:\n$module" if $errs;
}
}
$VERSION = '0.01';
package Simple::Ex;
$VERSION = '0.02';
+
=head1 NAME
Simple - It's easy.
is( $pm_info->version('Simple::Simon'), '0.61.129', 'version for embedded package' );
}
+# check that package_versions_from_directory works
+
+$dist->change_file( 'lib/Simple.pm', <<'---' );
+package Simple;
+$VERSION = '0.01';
+package Simple::Ex;
+$VERSION = '0.02';
+{
+ package main; # should ignore this
+}
+{
+ package DB; # should ignore this
+}
+{
+ package Simple::_private; # should ignore this
+}
+
+=head1 NAME
+
+Simple - It's easy.
+
+=head1 AUTHOR
+
+Simple Simon
+
+=cut
+---
+$dist->regen;
+
+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');
+
+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;
+}