2 # -*- mode: cperl; tab-width: 8; indent-tabs-mode: nil; basic-offset: 2 -*-
3 # vim:ts=8:sw=2:et:sta:sts=2
10 # parse various module $VERSION lines
11 # these will be reversed later to create %modules
13 '1.23' => <<'---', # declared & defined on same line with 'our'
15 our $VERSION = '1.23';
17 '1.23' => <<'---', # declared & defined on separate lines with 'our'
22 '1.23' => <<'---', # use vars
24 use vars qw( $VERSION );
27 '1.23' => <<'---', # choose the right default package based on package/file name
28 package Simple::_private;
31 $VERSION = '1.23'; # this should be chosen for version
33 '1.23' => <<'---', # just read the first $VERSION line
35 $VERSION = '1.23'; # we should see this line
36 $VERSION = eval $VERSION; # and ignore this one
38 '1.23' => <<'---', # just read the first $VERSION line in reopened package (1)
41 package Error::Simple;
45 '1.23' => <<'---', # just read the first $VERSION line in reopened package (2)
47 package Error::Simple;
52 '1.23' => <<'---', # mentions another module's $VERSION
55 if ( $Other::VERSION ) {
59 '1.23' => <<'---', # mentions another module's $VERSION in a different package
63 if ( $Simple::VERSION ) {
67 '1.23' => <<'---', # $VERSION checked only in assignments, not regexp ops
70 if ( $VERSION =~ /1\.23/ ) {
74 '1.23' => <<'---', # $VERSION checked only in assignments, not relational ops
77 if ( $VERSION == 3.45 ) {
81 '1.23' => <<'---', # $VERSION checked only in assignments, not relational ops
85 if ( $Simple::VERSION == 3.45 ) {
89 '1.23' => <<'---', # Fully qualified $VERSION declared in package
91 $Simple::VERSION = 1.23;
93 '1.23' => <<'---', # Differentiate fully qualified $VERSION in a package
95 $Simple2::VERSION = '999';
96 $Simple::VERSION = 1.23;
98 '1.23' => <<'---', # Differentiate fully qualified $VERSION and unqualified
100 $Simple2::VERSION = '999';
103 '1.23' => <<'---', # $VERSION declared as package variable from within 'main' package
104 $Simple::VERSION = '1.23';
107 $x = $y, $cats = $dogs;
110 '1.23' => <<'---', # $VERSION wrapped in parens - space inside
112 ( $VERSION ) = '1.23';
114 '1.23' => <<'---', # $VERSION wrapped in parens - no space inside
118 '1.23' => <<'---', # $VERSION follows a spurious 'package' in a quoted construct
120 __PACKAGE__->mk_accessors(qw(
122 package filename line codeline subroutine finished));
124 our $VERSION = "1.23";
126 '1.23' => <<'---', # $VERSION using version.pm
128 use version; our $VERSION = version->new('1.23');
130 '1.23' => <<'---', # $VERSION using version.pm and qv()
132 use version; our $VERSION = qv('1.230');
134 '1.23' => <<'---', # Two version assignments, should ignore second one
135 $Simple::VERSION = '1.230';
136 $Simple::VERSION = eval $Simple::VERSION;
138 '1.23' => <<'---', # declared & defined on same line with 'our'
140 our $VERSION = '1.23_00_00';
142 '1.23' => <<'---', # package NAME VERSION
145 '1.23_01' => <<'---', # package NAME VERSION
146 package Simple 1.23_01;
148 'v1.2.3' => <<'---', # package NAME VERSION
149 package Simple v1.2.3;
151 'v1.2_3' => <<'---', # package NAME VERSION
152 package Simple v1.2_3;
154 '1.23' => <<'---', # trailing crud
157 $VERSION = '1.23-alpha';
159 '1.23' => <<'---', # trailing crud
164 '1.234' => <<'---', # multi_underscore
167 $VERSION = '1.2_3_4';
169 '0' => <<'---', # non-numeric
172 $VERSION = 'onetwothree';
175 my %modules = reverse @modules;
177 plan tests => 39 + 2 * keys( %modules );
179 require_ok('Module::Metadata');
181 # class method C<find_module_by_name>
182 my $module = Module::Metadata->find_module_by_name(
183 'Module::Metadata' );
184 ok( -e $module, 'find_module_by_name() succeeds' );
186 #########################
188 my $tmp = MBTest->tmpdir;
191 my $dist = DistGen->new( dir => $tmp );
197 # fail on invalid module name
198 my $pm_info = Module::Metadata->new_from_module(
199 'Foo::Bar', inc => [] );
200 ok( !defined( $pm_info ), 'fail if can\'t find module by module name' );
203 # fail on invalid filename
204 my $file = File::Spec->catfile( 'Foo', 'Bar.pm' );
205 $pm_info = Module::Metadata->new_from_file( $file, inc => [] );
206 ok( !defined( $pm_info ), 'fail if can\'t find module by file name' );
209 # construct from module filename
210 $file = File::Spec->catfile( 'lib', split( /::/, $dist->name ) ) . '.pm';
211 $pm_info = Module::Metadata->new_from_file( $file );
212 ok( defined( $pm_info ), 'new_from_file() succeeds' );
214 # construct from filehandle
215 my $handle = IO::File->new($file);
216 $pm_info = Module::Metadata->new_from_handle( $handle, $file );
217 ok( defined( $pm_info ), 'new_from_handle() succeeds' );
218 $pm_info = Module::Metadata->new_from_handle( $handle );
219 is( $pm_info, undef, "new_from_handle() without filename returns undef" );
222 # construct from module name, using custom include path
223 $pm_info = Module::Metadata->new_from_module(
224 $dist->name, inc => [ 'lib', @INC ] );
225 ok( defined( $pm_info ), 'new_from_module() succeeds' );
228 foreach my $module ( sort keys %modules ) {
229 my $expected = $modules{$module};
231 skip( "No our() support until perl 5.6", 2 )
232 if $] < 5.006 && $module =~ /\bour\b/;
233 skip( "No package NAME VERSION support until perl 5.11.1", 2 )
234 if $] < 5.011001 && $module =~ /package\s+[\w\:\']+\s+v?[0-9._]+/;
236 $dist->change_file( 'lib/Simple.pm', $module );
240 local $SIG{__WARN__} = sub { $warnings .= $_ for @_ };
241 my $pm_info = Module::Metadata->new_from_file( $file );
243 # Test::Builder will prematurely numify objects, so use this form
245 ok( $pm_info->version eq $expected,
246 "correct module version (expected '$expected')" )
248 is( $warnings, '', 'no warnings from parsing' ) or $errs++;
249 diag "Got: '@{[$pm_info->version]}'\nModule contents:\n$module" if $errs;
253 # revert to pristine state
254 $dist->regen( clean => 1 );
256 # Find each package only once
257 $dist->change_file( 'lib/Simple.pm', <<'---' );
260 package Error::Simple;
267 $pm_info = Module::Metadata->new_from_file( $file );
269 my @packages = $pm_info->packages_inside;
270 is( @packages, 2, 'record only one occurence of each package' );
273 # Module 'Simple.pm' does not contain package 'Simple';
274 # constructor should not complain, no default module name or version
275 $dist->change_file( 'lib/Simple.pm', <<'---' );
281 $pm_info = Module::Metadata->new_from_file( $file );
283 is( $pm_info->name, undef, 'no default package' );
284 is( $pm_info->version, undef, 'no version w/o default package' );
286 # Module 'Simple.pm' contains an alpha version
287 # constructor should report first $VERSION found
288 $dist->change_file( 'lib/Simple.pm', <<'---' );
290 $VERSION = '1.23_01';
291 $VERSION = eval $VERSION;
295 $pm_info = Module::Metadata->new_from_file( $file );
297 is( $pm_info->version, '1.23_01', 'alpha version reported');
299 # NOTE the following test has be done this way because Test::Builder is
300 # too smart for our own good and tries to see if the version object is a
301 # dual-var, which breaks with alpha versions:
302 # Argument "1.23_0100" isn't numeric in addition (+) at
303 # /usr/lib/perl5/5.8.7/Test/Builder.pm line 505.
305 ok( $pm_info->version > 1.23, 'alpha version greater than non');
307 # revert to pristine state
308 $dist->regen( clean => 1 );
310 # parse $VERSION lines scripts for package main
312 <<'---', # package main declared
317 <<'---', # on first non-comment line, non declared package main
321 <<'---', # after non-comment line
326 <<'---', # 1st declared package
333 <<'---', # 2nd declared package
340 <<'---', # split package
348 <<'---', # define 'main' version from other package
353 <<'---', # define 'main' version from other package
360 my ( $i, $n ) = ( 1, scalar( @scripts ) );
361 foreach my $script ( @scripts ) {
362 $dist->change_file( 'bin/simple.plx', $script );
364 $pm_info = Module::Metadata->new_from_file(
365 File::Spec->catfile( 'bin', 'simple.plx' ) );
367 is( $pm_info->version, '0.01', "correct script version ($i of $n)" );
372 # examine properties of a module: name, pod, etc
373 $dist->change_file( 'lib/Simple.pm', <<'---' );
390 $pm_info = Module::Metadata->new_from_module(
391 $dist->name, inc => [ 'lib', @INC ] );
393 is( $pm_info->name, 'Simple', 'found default package' );
394 is( $pm_info->version, '0.01', 'version for default package' );
396 # got correct version for secondary package
397 is( $pm_info->version( 'Simple::Ex' ), '0.02',
398 'version for secondary package' );
400 my $filename = $pm_info->filename;
401 ok( defined( $filename ) && -e $filename,
402 'filename() returns valid path to module file' );
404 @packages = $pm_info->packages_inside;
405 is( @packages, 2, 'found correct number of packages' );
406 is( $packages[0], 'Simple', 'packages stored in order found' );
408 # we can detect presence of pod regardless of whether we are collecting it
409 ok( $pm_info->contains_pod, 'contains_pod() succeeds' );
411 my @pod = $pm_info->pod_inside;
412 is_deeply( \@pod, [qw(NAME AUTHOR)], 'found all pod sections' );
414 is( $pm_info->pod('NONE') , undef,
415 'return undef() if pod section not present' );
417 is( $pm_info->pod('NAME'), undef,
418 'return undef() if pod section not collected' );
422 $pm_info = Module::Metadata->new_from_module(
423 $dist->name, inc => [ 'lib', @INC ], collect_pod => 1 );
425 my $name = $pm_info->pod('NAME');
430 is( $name, q|Simple - It's easy.|, 'collected pod section' );
434 # Make sure processing stops after __DATA__
435 $dist->change_file( 'lib/Simple.pm', <<'---' );
439 *UNIVERSAL::VERSION = sub {
445 $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
446 is( $pm_info->name, 'Simple', 'found default package' );
447 is( $pm_info->version, '0.01', 'version for default package' );
448 my @packages = $pm_info->packages_inside;
449 is_deeply(\@packages, ['Simple'], 'packages inside');
453 # Make sure we handle version.pm $VERSIONs well
454 $dist->change_file( 'lib/Simple.pm', <<'---' );
456 $VERSION = version->new('0.60.' . (qw$Revision: 128 $)[1]);
457 package Simple::Simon;
458 $VERSION = version->new('0.61.' . (qw$Revision: 129 $)[1]);
462 $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
463 is( $pm_info->name, 'Simple', 'found default package' );
464 is( $pm_info->version, '0.60.128', 'version for default package' );
465 my @packages = $pm_info->packages_inside;
466 is_deeply([sort @packages], ['Simple', 'Simple::Simon'], 'packages inside');
467 is( $pm_info->version('Simple::Simon'), '0.61.129', 'version for embedded package' );