keep test cases in lists, rather than a hash, for consistent ordering, and providing...
[p5sagit/Module-Metadata.git] / t / metadata.t
index 4a64b54..9f6f478 100644 (file)
@@ -3,14 +3,16 @@
 # 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;
 
 my $undef;
 
 # parse various module $VERSION lines
-# these will be reversed later to create %modules
+# format: expected version => code snippet
 my @modules = (
   $undef => <<'---', # no $VERSION line
 package Simple;
@@ -209,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 => 52 + 2 * keys( %modules );
+# format: expected package name => code snippet
+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;
+---
+);
+
+# 2 tests per each pair of @modules, @pkg_names entry
+plan tests => 63 + ( @modules ) + ( @pkg_names );
 
 require_ok('Module::Metadata');
 
-# class method C<find_module_by_name>
-my $module = Module::Metadata->find_module_by_name(
-               'Module::Metadata' );
-ok( -e $module, 'find_module_by_name() succeeds' );
+{
+    # class method C<find_module_by_name>
+    my $module = Module::Metadata->find_module_by_name(
+                   'Module::Metadata' );
+    ok( -e $module, 'find_module_by_name() succeeds' );
+}
 
 #########################
 
@@ -234,7 +284,7 @@ $dist->chdir_in;
 
 # fail on invalid module name
 my $pm_info = Module::Metadata->new_from_module(
-               'Foo::Bar', inc => [] );
+                'Foo::Bar', inc => [] );
 ok( !defined( $pm_info ), 'fail if can\'t find module by module name' );
 
 
@@ -259,19 +309,20 @@ close($handle);
 
 # construct from module name, using custom include path
 $pm_info = Module::Metadata->new_from_module(
-            $dist->name, inc => [ 'lib', @INC ] );
+             $dist->name, inc => [ 'lib', @INC ] );
 ok( defined( $pm_info ), 'new_from_module() succeeds' );
 
 
-foreach my $module ( sort keys %modules ) {
-    my $expected = $modules{$module};
+# iterate through @modules pairwise
+my $test_case = 0;
+while (++$test_case and my ($expected_version, $code) = splice @modules, 0, 2 ) {
  SKIP: {
     skip( "No our() support until perl 5.6", 2 )
-        if $] < 5.006 && $module =~ /\bour\b/;
+        if $] < 5.006 && $code =~ /\bour\b/;
     skip( "No package NAME VERSION support until perl 5.11.1", 2 )
-        if $] < 5.011001 && $module =~ /package\s+[\w\:\']+\s+v?[0-9._]+/;
+        if $] < 5.011001 && $code =~ /package\s+[\w\:\']+\s+v?[0-9._]+/;
 
-    $dist->change_file( 'lib/Simple.pm', $module );
+    $dist->change_file( 'lib/Simple.pm', $code );
     $dist->regen;
 
     my $warnings = '';
@@ -281,23 +332,45 @@ foreach my $module ( sort keys %modules ) {
     # Test::Builder will prematurely numify objects, so use this form
     my $errs;
     my $got = $pm_info->version;
-    if ( defined $expected ) {
-        ok( $got eq $expected,
-            "correct module version (expected '$expected')" )
+    if ( defined $expected_version ) {
+        ok( $got eq $expected_version,
+            "case $test_case: correct module version (expected '$expected_version')" )
             or $errs++;
     } else {
         ok( !defined($got),
-            "correct module version (expected undef)" )
+            "case $test_case: correct module version (expected undef)" )
             or $errs++;
     }
-    is( $warnings, '', 'no warnings from parsing' ) or $errs++;
-    diag "Got: '$got'\nModule contents:\n$module" if $errs;
+    is( $warnings, '', "case $test_case: no warnings from parsing" ) or $errs++;
+    diag "Got: '$got'\nModule contents:\n$code" if $errs;
   }
 }
 
 # revert to pristine state
 $dist->regen( clean => 1 );
 
+$test_case = 0;
+while (++$test_case and my ($expected_name, $code) = splice @pkg_names, 0, 2) {
+    $dist->change_file( 'lib/Simple.pm', $code);
+    $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_name,
+               "case $test_case: correct package names (expected '" . join(', ', @$expected_name) . "')" )
+            or $errs++;
+    is( $warnings, '', "case $test_case: no warnings from parsing" ) or $errs++;
+    diag "Got: '" . join(', ', @got) . "'\nModule contents:\n$code" if $errs;
+}
+
+# revert to pristine state
+$dist->regen( clean => 1 );
+
 # Find each package only once
 $dist->change_file( 'lib/Simple.pm', <<'---' );
 package Simple;
@@ -407,7 +480,7 @@ foreach my $script ( @scripts ) {
   $dist->change_file( 'bin/simple.plx', $script );
   $dist->regen;
   $pm_info = Module::Metadata->new_from_file(
-              File::Spec->catfile( 'bin', 'simple.plx' ) );
+               File::Spec->catfile( 'bin', 'simple.plx' ) );
 
   is( $pm_info->version, '0.01', "correct script version ($i of $n)" );
   $i++;
@@ -429,6 +502,9 @@ Simple - It's easy.
 
 Simple Simon
 
+You can find me on the IRC channel
+#simon on irc.perl.org.
+
 =cut
 ---
 $dist->regen;
@@ -478,8 +554,48 @@ $pm_info = Module::Metadata->new_from_module(
     }
     $pod{$section} = $content;
   }
-  is( $pod{NAME}, q|Simple - It's easy.|, 'collected NAME pod section' );
-  is( $pod{AUTHOR}, q|Simple Simon|, 'collected AUTHOR pod section' );
+  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' );
+}
+
+{
+  # 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' );
 }
 
 {
@@ -611,6 +727,9 @@ $VERSION = '0.01';
   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;
@@ -628,4 +747,20 @@ $VERSION = '0.03';
   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' );
 }