Commit | Line | Data |
7a4e305a |
1 | #!/usr/bin/perl -w |
2 | # -*- mode: cperl; tab-width: 8; indent-tabs-mode: nil; basic-offset: 2 -*- |
3 | # vim:ts=8:sw=2:et:sta:sts=2 |
4 | |
5 | use strict; |
eed8b6fa |
6 | use warnings; |
c8b69ebe |
7 | use Test::More; |
f33c0a6c |
8 | use IO::File; |
e6454d7f |
9 | use File::Spec; |
10 | use File::Temp; |
11 | use File::Basename; |
12 | use Cwd (); |
13 | use File::Path; |
7a4e305a |
14 | |
975132cb |
15 | my $undef; |
16 | |
7a4e305a |
17 | # parse various module $VERSION lines |
4284ed53 |
18 | # format: expected version => code snippet |
7a4e305a |
19 | my @modules = ( |
975132cb |
20 | $undef => <<'---', # no $VERSION line |
21 | package Simple; |
22 | --- |
23 | $undef => <<'---', # undefined $VERSION |
24 | package Simple; |
25 | our $VERSION; |
26 | --- |
7a4e305a |
27 | '1.23' => <<'---', # declared & defined on same line with 'our' |
28 | package Simple; |
29 | our $VERSION = '1.23'; |
30 | --- |
31 | '1.23' => <<'---', # declared & defined on separate lines with 'our' |
32 | package Simple; |
33 | our $VERSION; |
34 | $VERSION = '1.23'; |
35 | --- |
ad924b83 |
36 | '1.23' => <<'---', # commented & defined on same line |
37 | package Simple; |
38 | our $VERSION = '1.23'; # our $VERSION = '4.56'; |
39 | --- |
40 | '1.23' => <<'---', # commented & defined on separate lines |
41 | package Simple; |
42 | # our $VERSION = '4.56'; |
43 | our $VERSION = '1.23'; |
44 | --- |
7a4e305a |
45 | '1.23' => <<'---', # use vars |
46 | package Simple; |
47 | use vars qw( $VERSION ); |
48 | $VERSION = '1.23'; |
49 | --- |
50 | '1.23' => <<'---', # choose the right default package based on package/file name |
51 | package Simple::_private; |
52 | $VERSION = '0'; |
53 | package Simple; |
54 | $VERSION = '1.23'; # this should be chosen for version |
55 | --- |
56 | '1.23' => <<'---', # just read the first $VERSION line |
57 | package Simple; |
58 | $VERSION = '1.23'; # we should see this line |
59 | $VERSION = eval $VERSION; # and ignore this one |
60 | --- |
61 | '1.23' => <<'---', # just read the first $VERSION line in reopened package (1) |
62 | package Simple; |
63 | $VERSION = '1.23'; |
64 | package Error::Simple; |
65 | $VERSION = '2.34'; |
66 | package Simple; |
67 | --- |
68 | '1.23' => <<'---', # just read the first $VERSION line in reopened package (2) |
69 | package Simple; |
70 | package Error::Simple; |
71 | $VERSION = '2.34'; |
72 | package Simple; |
73 | $VERSION = '1.23'; |
74 | --- |
75 | '1.23' => <<'---', # mentions another module's $VERSION |
76 | package Simple; |
77 | $VERSION = '1.23'; |
78 | if ( $Other::VERSION ) { |
79 | # whatever |
80 | } |
81 | --- |
82 | '1.23' => <<'---', # mentions another module's $VERSION in a different package |
83 | package Simple; |
84 | $VERSION = '1.23'; |
85 | package Simple2; |
86 | if ( $Simple::VERSION ) { |
87 | # whatever |
88 | } |
89 | --- |
90 | '1.23' => <<'---', # $VERSION checked only in assignments, not regexp ops |
91 | package Simple; |
92 | $VERSION = '1.23'; |
93 | if ( $VERSION =~ /1\.23/ ) { |
94 | # whatever |
95 | } |
96 | --- |
97 | '1.23' => <<'---', # $VERSION checked only in assignments, not relational ops |
98 | package Simple; |
99 | $VERSION = '1.23'; |
100 | if ( $VERSION == 3.45 ) { |
101 | # whatever |
102 | } |
103 | --- |
104 | '1.23' => <<'---', # $VERSION checked only in assignments, not relational ops |
105 | package Simple; |
106 | $VERSION = '1.23'; |
107 | package Simple2; |
108 | if ( $Simple::VERSION == 3.45 ) { |
109 | # whatever |
110 | } |
111 | --- |
112 | '1.23' => <<'---', # Fully qualified $VERSION declared in package |
113 | package Simple; |
114 | $Simple::VERSION = 1.23; |
115 | --- |
116 | '1.23' => <<'---', # Differentiate fully qualified $VERSION in a package |
117 | package Simple; |
118 | $Simple2::VERSION = '999'; |
119 | $Simple::VERSION = 1.23; |
120 | --- |
121 | '1.23' => <<'---', # Differentiate fully qualified $VERSION and unqualified |
122 | package Simple; |
123 | $Simple2::VERSION = '999'; |
124 | $VERSION = 1.23; |
125 | --- |
126 | '1.23' => <<'---', # $VERSION declared as package variable from within 'main' package |
127 | $Simple::VERSION = '1.23'; |
128 | { |
129 | package Simple; |
130 | $x = $y, $cats = $dogs; |
131 | } |
132 | --- |
133 | '1.23' => <<'---', # $VERSION wrapped in parens - space inside |
134 | package Simple; |
135 | ( $VERSION ) = '1.23'; |
136 | --- |
137 | '1.23' => <<'---', # $VERSION wrapped in parens - no space inside |
138 | package Simple; |
139 | ($VERSION) = '1.23'; |
140 | --- |
141 | '1.23' => <<'---', # $VERSION follows a spurious 'package' in a quoted construct |
142 | package Simple; |
143 | __PACKAGE__->mk_accessors(qw( |
144 | program socket proc |
145 | package filename line codeline subroutine finished)); |
146 | |
147 | our $VERSION = "1.23"; |
148 | --- |
149 | '1.23' => <<'---', # $VERSION using version.pm |
150 | package Simple; |
151 | use version; our $VERSION = version->new('1.23'); |
152 | --- |
153 | '1.23' => <<'---', # $VERSION using version.pm and qv() |
154 | package Simple; |
155 | use version; our $VERSION = qv('1.230'); |
156 | --- |
157 | '1.23' => <<'---', # Two version assignments, should ignore second one |
158 | $Simple::VERSION = '1.230'; |
159 | $Simple::VERSION = eval $Simple::VERSION; |
160 | --- |
161 | '1.23' => <<'---', # declared & defined on same line with 'our' |
162 | package Simple; |
163 | our $VERSION = '1.23_00_00'; |
164 | --- |
165 | '1.23' => <<'---', # package NAME VERSION |
166 | package Simple 1.23; |
167 | --- |
168 | '1.23_01' => <<'---', # package NAME VERSION |
169 | package Simple 1.23_01; |
170 | --- |
171 | 'v1.2.3' => <<'---', # package NAME VERSION |
172 | package Simple v1.2.3; |
173 | --- |
174 | 'v1.2_3' => <<'---', # package NAME VERSION |
175 | package Simple v1.2_3; |
176 | --- |
92ad06ed |
177 | '1.23' => <<'---', # trailing crud |
178 | package Simple; |
179 | our $VERSION; |
180 | $VERSION = '1.23-alpha'; |
181 | --- |
182 | '1.23' => <<'---', # trailing crud |
183 | package Simple; |
184 | our $VERSION; |
185 | $VERSION = '1.23b'; |
186 | --- |
187 | '1.234' => <<'---', # multi_underscore |
188 | package Simple; |
189 | our $VERSION; |
190 | $VERSION = '1.2_3_4'; |
191 | --- |
192 | '0' => <<'---', # non-numeric |
193 | package Simple; |
194 | our $VERSION; |
195 | $VERSION = 'onetwothree'; |
196 | --- |
710f253f |
197 | $undef => <<'---', # package NAME BLOCK, undef $VERSION |
198 | package Simple { |
199 | our $VERSION; |
200 | } |
201 | --- |
202 | '1.23' => <<'---', # package NAME BLOCK, with $VERSION |
203 | package Simple { |
204 | our $VERSION = '1.23'; |
205 | } |
206 | --- |
207 | '1.23' => <<'---', # package NAME VERSION BLOCK |
208 | package Simple 1.23 { |
209 | 1; |
210 | } |
211 | --- |
212 | 'v1.2.3_4' => <<'---', # package NAME VERSION BLOCK |
213 | package Simple v1.2.3_4 { |
214 | 1; |
215 | } |
216 | --- |
72a35bb9 |
217 | '0' => <<'---', # set from separately-initialised variable |
218 | package Simple; |
219 | our $CVSVERSION = '$Revision: 1.7 $'; |
220 | our ($VERSION) = ($CVSVERSION =~ /(\d+\.\d+)/); |
221 | } |
222 | --- |
7a4e305a |
223 | ); |
7a4e305a |
224 | |
4284ed53 |
225 | # format: expected package name => code snippet |
8e4bef01 |
226 | my @pkg_names = ( |
227 | [ 'Simple' ] => <<'---', # package NAME |
228 | package Simple; |
229 | --- |
230 | [ 'Simple::Edward' ] => <<'---', # package NAME::SUBNAME |
231 | package Simple::Edward; |
232 | --- |
233 | [ 'Simple::Edward::' ] => <<'---', # package NAME::SUBNAME:: |
234 | package Simple::Edward::; |
235 | --- |
236 | [ "Simple'Edward" ] => <<'---', # package NAME'SUBNAME |
237 | package Simple'Edward; |
238 | --- |
239 | [ "Simple'Edward::" ] => <<'---', # package NAME'SUBNAME:: |
240 | package Simple'Edward::; |
241 | --- |
242 | [ 'Simple::::Edward' ] => <<'---', # package NAME::::SUBNAME |
243 | package Simple::::Edward; |
244 | --- |
245 | [ '::Simple::Edward' ] => <<'---', # package ::NAME::SUBNAME |
246 | package ::Simple::Edward; |
247 | --- |
248 | [ 'main' ] => <<'---', # package NAME:SUBNAME (fail) |
249 | package Simple:Edward; |
250 | --- |
251 | [ 'main' ] => <<'---', # package NAME' (fail) |
252 | package Simple'; |
253 | --- |
254 | [ 'main' ] => <<'---', # package NAME::SUBNAME' (fail) |
255 | package Simple::Edward'; |
256 | --- |
257 | [ 'main' ] => <<'---', # package NAME''SUBNAME (fail) |
258 | package Simple''Edward; |
259 | --- |
260 | [ 'main' ] => <<'---', # package NAME-SUBNAME (fail) |
261 | package Simple-Edward; |
262 | --- |
263 | ); |
8e4bef01 |
264 | |
4284ed53 |
265 | # 2 tests per each pair of @modules, @pkg_names entry |
266 | plan tests => 63 + ( @modules ) + ( @pkg_names ); |
7a4e305a |
267 | |
268 | require_ok('Module::Metadata'); |
269 | |
4284ed53 |
270 | { |
271 | # class method C<find_module_by_name> |
272 | my $module = Module::Metadata->find_module_by_name( |
273 | 'Module::Metadata' ); |
274 | ok( -e $module, 'find_module_by_name() succeeds' ); |
275 | } |
1abfcc9a |
276 | |
277 | ######################### |
278 | |
e6454d7f |
279 | BEGIN { |
280 | my $cwd = File::Spec->rel2abs(Cwd::cwd); |
281 | sub original_cwd { return $cwd } |
282 | } |
7a4e305a |
283 | |
e6454d7f |
284 | # Setup a temp directory |
285 | sub tmpdir { |
286 | my (@args) = @_; |
287 | my $dir = $ENV{PERL_CORE} ? original_cwd : File::Spec->tmpdir; |
288 | return File::Temp::tempdir('MMD-XXXXXXXX', CLEANUP => 0, DIR => $dir, @args); |
289 | } |
7a4e305a |
290 | |
e6454d7f |
291 | my $tmp; |
292 | BEGIN { $tmp = tmpdir; diag "using temp dir $tmp"; } |
293 | |
294 | END { |
295 | die "tests failed; leaving temp dir $tmp behind" |
296 | if $ENV{AUTHOR_TESTING} and not Test::Builder->new->is_passing; |
297 | diag "removing temp dir $tmp"; |
298 | chdir original_cwd; |
299 | File::Path::remove_tree($tmp); |
300 | } |
301 | |
302 | # generates a new distribution: |
303 | # files => { relative filename => $content ... } |
304 | # returns the name of the distribution (not including version), |
305 | # and the absolute path name to the dist. |
306 | { |
307 | my $test_num = 0; |
308 | sub new_dist { |
309 | my %opts = @_; |
310 | |
311 | my $distname = 'Simple' . $test_num++; |
312 | my $distdir = File::Spec->catdir($tmp, $distname); |
313 | note "using dist $distname in $distdir"; |
314 | |
315 | File::Path::mkpath($distdir) or die "failed to create '$distdir'"; |
316 | |
317 | foreach my $rel_filename (keys %{$opts{files}}) |
318 | { |
319 | my $abs_filename = File::Spec->catfile($distdir, $rel_filename); |
320 | my $dirname = File::Basename::dirname($abs_filename); |
321 | unless (-d $dirname) { |
322 | File::Path::mkpath($dirname) or die "Can't create '$dirname'"; |
323 | } |
324 | |
325 | note "creating $abs_filename"; |
326 | my $fh = IO::File->new(">$abs_filename") or die "Can't write '$abs_filename'\n"; |
327 | print $fh $opts{files}{$rel_filename}; |
328 | close $fh; |
329 | } |
330 | |
331 | chdir $distdir; |
332 | return ($distname, $distdir); |
333 | } |
334 | } |
7a4e305a |
335 | |
8afbb7e5 |
336 | { |
337 | # fail on invalid module name |
338 | my $pm_info = Module::Metadata->new_from_module( |
339 | 'Foo::Bar', inc => [] ); |
340 | ok( !defined( $pm_info ), 'fail if can\'t find module by module name' ); |
341 | } |
7a4e305a |
342 | |
8afbb7e5 |
343 | { |
344 | # fail on invalid filename |
345 | my $file = File::Spec->catfile( 'Foo', 'Bar.pm' ); |
346 | my $pm_info = Module::Metadata->new_from_file( $file, inc => [] ); |
347 | ok( !defined( $pm_info ), 'fail if can\'t find module by file name' ); |
348 | } |
7a4e305a |
349 | |
8afbb7e5 |
350 | { |
e6454d7f |
351 | my $file = File::Spec->catfile('lib', 'Simple.pm'); |
352 | my ($dist_name, $dist_dir) = new_dist(files => { $file => "package Simple;\n" }); |
353 | |
8afbb7e5 |
354 | # construct from module filename |
8afbb7e5 |
355 | my $pm_info = Module::Metadata->new_from_file( $file ); |
356 | ok( defined( $pm_info ), 'new_from_file() succeeds' ); |
357 | |
358 | # construct from filehandle |
359 | my $handle = IO::File->new($file); |
360 | $pm_info = Module::Metadata->new_from_handle( $handle, $file ); |
361 | ok( defined( $pm_info ), 'new_from_handle() succeeds' ); |
362 | $pm_info = Module::Metadata->new_from_handle( $handle ); |
363 | is( $pm_info, undef, "new_from_handle() without filename returns undef" ); |
364 | close($handle); |
365 | } |
f33c0a6c |
366 | |
8afbb7e5 |
367 | { |
368 | # construct from module name, using custom include path |
369 | my $pm_info = Module::Metadata->new_from_module( |
e6454d7f |
370 | 'Simple', inc => [ 'lib', @INC ] ); |
8afbb7e5 |
371 | ok( defined( $pm_info ), 'new_from_module() succeeds' ); |
372 | } |
7a4e305a |
373 | |
374 | |
4284ed53 |
375 | # iterate through @modules pairwise |
376 | my $test_case = 0; |
377 | while (++$test_case and my ($expected_version, $code) = splice @modules, 0, 2 ) { |
7a4e305a |
378 | SKIP: { |
379 | skip( "No our() support until perl 5.6", 2 ) |
4284ed53 |
380 | if $] < 5.006 && $code =~ /\bour\b/; |
7a4e305a |
381 | skip( "No package NAME VERSION support until perl 5.11.1", 2 ) |
4284ed53 |
382 | if $] < 5.011001 && $code =~ /package\s+[\w\:\']+\s+v?[0-9._]+/; |
7a4e305a |
383 | |
e6454d7f |
384 | my $file = File::Spec->catfile('lib', 'Simple.pm'); |
385 | my ($dist_name, $dist_dir) = new_dist(files => { $file => $code }); |
7a4e305a |
386 | |
387 | my $warnings = ''; |
388 | local $SIG{__WARN__} = sub { $warnings .= $_ for @_ }; |
389 | my $pm_info = Module::Metadata->new_from_file( $file ); |
390 | |
391 | # Test::Builder will prematurely numify objects, so use this form |
392 | my $errs; |
975132cb |
393 | my $got = $pm_info->version; |
7a872696 |
394 | |
395 | is( |
396 | $got, |
397 | $expected_version, |
398 | "case $test_case: correct module version (" |
399 | . (defined $expected_version? "'$expected_version'" : 'undef') |
400 | . ')' |
401 | ) |
402 | or $errs++; |
403 | |
4284ed53 |
404 | is( $warnings, '', "case $test_case: no warnings from parsing" ) or $errs++; |
405 | diag "Got: '$got'\nModule contents:\n$code" if $errs; |
7a4e305a |
406 | } |
407 | } |
408 | |
4284ed53 |
409 | $test_case = 0; |
410 | while (++$test_case and my ($expected_name, $code) = splice @pkg_names, 0, 2) { |
e6454d7f |
411 | my $file = File::Spec->catfile('lib', 'Simple.pm'); |
412 | my ($dist_name, $dist_dir) = new_dist(files => { $file => $code }); |
8e4bef01 |
413 | |
414 | my $warnings = ''; |
415 | local $SIG{__WARN__} = sub { $warnings .= $_ for @_ }; |
416 | my $pm_info = Module::Metadata->new_from_file( $file ); |
417 | |
418 | # Test::Builder will prematurely numify objects, so use this form |
419 | my $errs; |
420 | my @got = $pm_info->packages_inside(); |
4284ed53 |
421 | is_deeply( \@got, $expected_name, |
422 | "case $test_case: correct package names (expected '" . join(', ', @$expected_name) . "')" ) |
8e4bef01 |
423 | or $errs++; |
4284ed53 |
424 | is( $warnings, '', "case $test_case: no warnings from parsing" ) or $errs++; |
425 | diag "Got: '" . join(', ', @got) . "'\nModule contents:\n$code" if $errs; |
8e4bef01 |
426 | } |
427 | |
8afbb7e5 |
428 | { |
429 | # Find each package only once |
e6454d7f |
430 | my $file = File::Spec->catfile('lib', 'Simple.pm'); |
431 | my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } ); |
7a4e305a |
432 | package Simple; |
433 | $VERSION = '1.23'; |
434 | package Error::Simple; |
435 | $VERSION = '2.34'; |
436 | package Simple; |
437 | --- |
438 | |
8afbb7e5 |
439 | my $pm_info = Module::Metadata->new_from_file( $file ); |
7a4e305a |
440 | |
8afbb7e5 |
441 | my @packages = $pm_info->packages_inside; |
442 | is( @packages, 2, 'record only one occurence of each package' ); |
443 | } |
7a4e305a |
444 | |
8afbb7e5 |
445 | { |
446 | # Module 'Simple.pm' does not contain package 'Simple'; |
447 | # constructor should not complain, no default module name or version |
e6454d7f |
448 | my $file = File::Spec->catfile('lib', 'Simple.pm'); |
449 | my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } ); |
7a4e305a |
450 | package Simple::Not; |
451 | $VERSION = '1.23'; |
452 | --- |
453 | |
8afbb7e5 |
454 | my $pm_info = Module::Metadata->new_from_file( $file ); |
7a4e305a |
455 | |
8afbb7e5 |
456 | is( $pm_info->name, undef, 'no default package' ); |
457 | is( $pm_info->version, undef, 'no version w/o default package' ); |
458 | } |
7a4e305a |
459 | |
8afbb7e5 |
460 | { |
461 | # Module 'Simple.pm' contains an alpha version |
462 | # constructor should report first $VERSION found |
e6454d7f |
463 | my $file = File::Spec->catfile('lib', 'Simple.pm'); |
464 | my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } ); |
7a4e305a |
465 | package Simple; |
466 | $VERSION = '1.23_01'; |
467 | $VERSION = eval $VERSION; |
468 | --- |
469 | |
8afbb7e5 |
470 | my $pm_info = Module::Metadata->new_from_file( $file ); |
7a4e305a |
471 | |
8afbb7e5 |
472 | is( $pm_info->version, '1.23_01', 'alpha version reported'); |
7a4e305a |
473 | |
8afbb7e5 |
474 | # NOTE the following test has be done this way because Test::Builder is |
475 | # too smart for our own good and tries to see if the version object is a |
476 | # dual-var, which breaks with alpha versions: |
477 | # Argument "1.23_0100" isn't numeric in addition (+) at |
478 | # /usr/lib/perl5/5.8.7/Test/Builder.pm line 505. |
7a4e305a |
479 | |
8afbb7e5 |
480 | ok( $pm_info->version > 1.23, 'alpha version greater than non'); |
481 | } |
7a4e305a |
482 | |
7a4e305a |
483 | # parse $VERSION lines scripts for package main |
484 | my @scripts = ( |
485 | <<'---', # package main declared |
486 | #!perl -w |
487 | package main; |
488 | $VERSION = '0.01'; |
489 | --- |
490 | <<'---', # on first non-comment line, non declared package main |
491 | #!perl -w |
492 | $VERSION = '0.01'; |
493 | --- |
494 | <<'---', # after non-comment line |
495 | #!perl -w |
496 | use strict; |
497 | $VERSION = '0.01'; |
498 | --- |
499 | <<'---', # 1st declared package |
500 | #!perl -w |
501 | package main; |
502 | $VERSION = '0.01'; |
503 | package _private; |
504 | $VERSION = '999'; |
505 | --- |
506 | <<'---', # 2nd declared package |
507 | #!perl -w |
508 | package _private; |
509 | $VERSION = '999'; |
510 | package main; |
511 | $VERSION = '0.01'; |
512 | --- |
513 | <<'---', # split package |
514 | #!perl -w |
515 | package main; |
516 | package _private; |
517 | $VERSION = '999'; |
518 | package main; |
519 | $VERSION = '0.01'; |
520 | --- |
521 | <<'---', # define 'main' version from other package |
522 | package _private; |
523 | $::VERSION = 0.01; |
524 | $VERSION = '999'; |
525 | --- |
526 | <<'---', # define 'main' version from other package |
527 | package _private; |
528 | $VERSION = '999'; |
529 | $::VERSION = 0.01; |
530 | --- |
531 | ); |
532 | |
533 | my ( $i, $n ) = ( 1, scalar( @scripts ) ); |
534 | foreach my $script ( @scripts ) { |
e6454d7f |
535 | my $file = File::Spec->catfile('bin', 'simple.plx'); |
536 | my ($dist_name, $dist_dir) = new_dist(files => { $file => $script } ); |
537 | my $pm_info = Module::Metadata->new_from_file( $file ); |
7a4e305a |
538 | |
539 | is( $pm_info->version, '0.01', "correct script version ($i of $n)" ); |
540 | $i++; |
541 | } |
542 | |
8afbb7e5 |
543 | { |
544 | # examine properties of a module: name, pod, etc |
e6454d7f |
545 | my $file = File::Spec->catfile('lib', 'Simple.pm'); |
546 | my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } ); |
7a4e305a |
547 | package Simple; |
548 | $VERSION = '0.01'; |
549 | package Simple::Ex; |
550 | $VERSION = '0.02'; |
388bf282 |
551 | |
7a4e305a |
552 | =head1 NAME |
553 | |
554 | Simple - It's easy. |
555 | |
556 | =head1 AUTHOR |
557 | |
558 | Simple Simon |
559 | |
81ce8c82 |
560 | You can find me on the IRC channel |
561 | #simon on irc.perl.org. |
562 | |
7a4e305a |
563 | =cut |
564 | --- |
7a4e305a |
565 | |
8afbb7e5 |
566 | my $pm_info = Module::Metadata->new_from_module( |
e6454d7f |
567 | 'Simple', inc => [ 'lib', @INC ] ); |
7a4e305a |
568 | |
8afbb7e5 |
569 | is( $pm_info->name, 'Simple', 'found default package' ); |
570 | is( $pm_info->version, '0.01', 'version for default package' ); |
7a4e305a |
571 | |
8afbb7e5 |
572 | # got correct version for secondary package |
573 | is( $pm_info->version( 'Simple::Ex' ), '0.02', |
574 | 'version for secondary package' ); |
7a4e305a |
575 | |
8afbb7e5 |
576 | my $filename = $pm_info->filename; |
577 | ok( defined( $filename ) && -e $filename, |
578 | 'filename() returns valid path to module file' ); |
7a4e305a |
579 | |
8afbb7e5 |
580 | my @packages = $pm_info->packages_inside; |
581 | is( @packages, 2, 'found correct number of packages' ); |
582 | is( $packages[0], 'Simple', 'packages stored in order found' ); |
7a4e305a |
583 | |
8afbb7e5 |
584 | # we can detect presence of pod regardless of whether we are collecting it |
585 | ok( $pm_info->contains_pod, 'contains_pod() succeeds' ); |
7a4e305a |
586 | |
8afbb7e5 |
587 | my @pod = $pm_info->pod_inside; |
588 | is_deeply( \@pod, [qw(NAME AUTHOR)], 'found all pod sections' ); |
7a4e305a |
589 | |
8afbb7e5 |
590 | is( $pm_info->pod('NONE') , undef, |
591 | 'return undef() if pod section not present' ); |
7a4e305a |
592 | |
8afbb7e5 |
593 | is( $pm_info->pod('NAME'), undef, |
594 | 'return undef() if pod section not collected' ); |
7a4e305a |
595 | |
596 | |
8afbb7e5 |
597 | # collect_pod |
598 | $pm_info = Module::Metadata->new_from_module( |
e6454d7f |
599 | 'Simple', inc => [ 'lib', @INC ], collect_pod => 1 ); |
7a4e305a |
600 | |
617f8754 |
601 | my %pod; |
602 | for my $section (qw(NAME AUTHOR)) { |
603 | my $content = $pm_info->pod( $section ); |
604 | if ( $content ) { |
605 | $content =~ s/^\s+//; |
606 | $content =~ s/\s+$//; |
607 | } |
608 | $pod{$section} = $content; |
609 | } |
81ce8c82 |
610 | my %expected = ( |
611 | NAME => q|Simple - It's easy.|, |
612 | AUTHOR => <<'EXPECTED' |
613 | Simple Simon |
614 | |
615 | You can find me on the IRC channel |
616 | #simon on irc.perl.org. |
617 | EXPECTED |
618 | ); |
619 | for my $text (values %expected) { |
620 | $text =~ s/^\s+//; |
621 | $text =~ s/\s+$//; |
622 | } |
623 | is( $pod{NAME}, $expected{NAME}, 'collected NAME pod section' ); |
624 | is( $pod{AUTHOR}, $expected{AUTHOR}, 'collected AUTHOR pod section' ); |
7a4e305a |
625 | } |
7a4e305a |
626 | |
627 | { |
a4aafbc2 |
628 | # test things that look like POD, but aren't |
e6454d7f |
629 | my $file = File::Spec->catfile('lib', 'Simple.pm'); |
630 | my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } ); |
a4aafbc2 |
631 | package Simple; |
a4aafbc2 |
632 | |
cfedad89 |
633 | =YES THIS STARTS POD |
634 | |
635 | our $VERSION = '999'; |
a4aafbc2 |
636 | |
a4aafbc2 |
637 | =cute |
a4aafbc2 |
638 | |
cfedad89 |
639 | our $VERSION = '666'; |
640 | |
641 | =cut |
642 | |
643 | *foo |
644 | =*no_this_does_not_start_pod; |
645 | |
646 | our $VERSION = '1.23'; |
a4aafbc2 |
647 | |
648 | --- |
8afbb7e5 |
649 | my $pm_info = Module::Metadata->new_from_file('lib/Simple.pm'); |
a4aafbc2 |
650 | is( $pm_info->name, 'Simple', 'found default package' ); |
651 | is( $pm_info->version, '1.23', 'version for default package' ); |
652 | } |
653 | |
654 | { |
7a4e305a |
655 | # Make sure processing stops after __DATA__ |
e6454d7f |
656 | my $file = File::Spec->catfile('lib', 'Simple.pm'); |
657 | my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } ); |
7a4e305a |
658 | package Simple; |
659 | $VERSION = '0.01'; |
660 | __DATA__ |
661 | *UNIVERSAL::VERSION = sub { |
662 | foo(); |
663 | }; |
664 | --- |
7a4e305a |
665 | |
8afbb7e5 |
666 | my $pm_info = Module::Metadata->new_from_file('lib/Simple.pm'); |
7a4e305a |
667 | is( $pm_info->name, 'Simple', 'found default package' ); |
668 | is( $pm_info->version, '0.01', 'version for default package' ); |
669 | my @packages = $pm_info->packages_inside; |
670 | is_deeply(\@packages, ['Simple'], 'packages inside'); |
671 | } |
672 | |
673 | { |
674 | # Make sure we handle version.pm $VERSIONs well |
e6454d7f |
675 | my $file = File::Spec->catfile('lib', 'Simple.pm'); |
676 | my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } ); |
7a4e305a |
677 | package Simple; |
678 | $VERSION = version->new('0.60.' . (qw$Revision: 128 $)[1]); |
679 | package Simple::Simon; |
680 | $VERSION = version->new('0.61.' . (qw$Revision: 129 $)[1]); |
681 | --- |
7a4e305a |
682 | |
8afbb7e5 |
683 | my $pm_info = Module::Metadata->new_from_file('lib/Simple.pm'); |
7a4e305a |
684 | is( $pm_info->name, 'Simple', 'found default package' ); |
685 | is( $pm_info->version, '0.60.128', 'version for default package' ); |
686 | my @packages = $pm_info->packages_inside; |
687 | is_deeply([sort @packages], ['Simple', 'Simple::Simon'], 'packages inside'); |
688 | is( $pm_info->version('Simple::Simon'), '0.61.129', 'version for embedded package' ); |
689 | } |
690 | |
388bf282 |
691 | # check that package_versions_from_directory works |
692 | |
8afbb7e5 |
693 | { |
e6454d7f |
694 | my $file = File::Spec->catfile('lib', 'Simple.pm'); |
695 | my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } ); |
388bf282 |
696 | package Simple; |
697 | $VERSION = '0.01'; |
698 | package Simple::Ex; |
699 | $VERSION = '0.02'; |
700 | { |
701 | package main; # should ignore this |
702 | } |
703 | { |
704 | package DB; # should ignore this |
705 | } |
706 | { |
707 | package Simple::_private; # should ignore this |
708 | } |
709 | |
710 | =head1 NAME |
711 | |
712 | Simple - It's easy. |
713 | |
714 | =head1 AUTHOR |
715 | |
716 | Simple Simon |
717 | |
718 | =cut |
719 | --- |
388bf282 |
720 | |
8afbb7e5 |
721 | my $exp_pvfd = { |
722 | 'Simple' => { |
723 | 'file' => 'Simple.pm', |
724 | 'version' => '0.01' |
725 | }, |
726 | 'Simple::Ex' => { |
727 | 'file' => 'Simple.pm', |
728 | 'version' => '0.02' |
729 | } |
730 | }; |
388bf282 |
731 | |
8afbb7e5 |
732 | my $got_pvfd = Module::Metadata->package_versions_from_directory('lib'); |
388bf282 |
733 | |
8afbb7e5 |
734 | is_deeply( $got_pvfd, $exp_pvfd, "package_version_from_directory()" ) |
735 | or diag explain $got_pvfd; |
ca33f3bd |
736 | |
737 | { |
c06d0187 |
738 | my $got_provides = Module::Metadata->provides(dir => 'lib', version => 2); |
ca33f3bd |
739 | my $exp_provides = { |
740 | 'Simple' => { |
741 | 'file' => 'lib/Simple.pm', |
742 | 'version' => '0.01' |
743 | }, |
744 | 'Simple::Ex' => { |
745 | 'file' => 'lib/Simple.pm', |
746 | 'version' => '0.02' |
747 | } |
748 | }; |
749 | |
750 | is_deeply( $got_provides, $exp_provides, "provides()" ) |
751 | or diag explain $got_provides; |
752 | } |
753 | |
754 | { |
c06d0187 |
755 | my $got_provides = Module::Metadata->provides(dir => 'lib', prefix => 'other', version => 1.4); |
ca33f3bd |
756 | my $exp_provides = { |
757 | 'Simple' => { |
758 | 'file' => 'other/Simple.pm', |
759 | 'version' => '0.01' |
760 | }, |
761 | 'Simple::Ex' => { |
762 | 'file' => 'other/Simple.pm', |
763 | 'version' => '0.02' |
764 | } |
765 | }; |
766 | |
767 | is_deeply( $got_provides, $exp_provides, "provides()" ) |
768 | or diag explain $got_provides; |
769 | } |
8afbb7e5 |
770 | } |
3c614865 |
771 | |
772 | # Check package_versions_from_directory with regard to case-sensitivity |
773 | { |
e6454d7f |
774 | my $file = File::Spec->catfile('lib', 'Simple.pm'); |
775 | my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } ); |
3c614865 |
776 | package simple; |
777 | $VERSION = '0.01'; |
778 | --- |
3c614865 |
779 | |
8afbb7e5 |
780 | my $pm_info = Module::Metadata->new_from_file('lib/Simple.pm'); |
3c614865 |
781 | is( $pm_info->name, undef, 'no default package' ); |
782 | is( $pm_info->version, undef, 'version for default package' ); |
783 | is( $pm_info->version('simple'), '0.01', 'version for lower-case package' ); |
784 | is( $pm_info->version('Simple'), undef, 'version for capitalized package' ); |
22553051 |
785 | ok( $pm_info->is_indexable(), 'an indexable package is found' ); |
786 | ok( $pm_info->is_indexable('simple'), 'the simple package is indexable' ); |
787 | ok( !$pm_info->is_indexable('Simple'), 'the Simple package would not be indexed' ); |
8afbb7e5 |
788 | } |
3c614865 |
789 | |
8afbb7e5 |
790 | { |
e6454d7f |
791 | my $file = File::Spec->catfile('lib', 'Simple.pm'); |
792 | my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } ); |
3c614865 |
793 | package simple; |
794 | $VERSION = '0.01'; |
795 | package Simple; |
796 | $VERSION = '0.02'; |
797 | package SiMpLe; |
798 | $VERSION = '0.03'; |
799 | --- |
3c614865 |
800 | |
8afbb7e5 |
801 | my $pm_info = Module::Metadata->new_from_file('lib/Simple.pm'); |
3c614865 |
802 | is( $pm_info->name, 'Simple', 'found default package' ); |
803 | is( $pm_info->version, '0.02', 'version for default package' ); |
804 | is( $pm_info->version('simple'), '0.01', 'version for lower-case package' ); |
805 | is( $pm_info->version('Simple'), '0.02', 'version for capitalized package' ); |
806 | is( $pm_info->version('SiMpLe'), '0.03', 'version for mixed-case package' ); |
22553051 |
807 | ok( $pm_info->is_indexable('simple'), 'the simple package is indexable' ); |
808 | ok( $pm_info->is_indexable('Simple'), 'the Simple package is indexable' ); |
8afbb7e5 |
809 | } |
22553051 |
810 | |
8afbb7e5 |
811 | { |
e6454d7f |
812 | my $file = File::Spec->catfile('lib', 'Simple.pm'); |
813 | my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } ); |
22553051 |
814 | package ## hide from PAUSE |
815 | simple; |
816 | $VERSION = '0.01'; |
817 | --- |
818 | |
8afbb7e5 |
819 | my $pm_info = Module::Metadata->new_from_file('lib/Simple.pm'); |
22553051 |
820 | is( $pm_info->name, undef, 'no package names found' ); |
821 | ok( !$pm_info->is_indexable('simple'), 'the simple package would not be indexed' ); |
822 | ok( !$pm_info->is_indexable('Simple'), 'the Simple package would not be indexed' ); |
823 | ok( !$pm_info->is_indexable(), 'no indexable package is found' ); |
3c614865 |
824 | } |