xref: /openbsd-src/gnu/usr.bin/perl/cpan/ExtUtils-MakeMaker/t/parse_version.t (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1#!/usr/bin/perl -w
2
3BEGIN {
4    unshift @INC, 't/lib';
5}
6chdir 't';
7
8use Test::More;
9use ExtUtils::MakeMaker;
10use File::Temp qw[tempfile];
11
12my $Has_Version = eval 'require version; "version"->import; 1';
13
14my %versions = (q[$VERSION = '1.00']            => '1.00',
15                q[*VERSION = \'1.01']           => '1.01',
16                q[($VERSION) = q$Revision: 32208 $ =~ /(\d+)/g;] => 32208,
17                q[$FOO::VERSION = '1.10';]      => '1.10',
18                q[*FOO::VERSION = \'1.11';]     => '1.11',
19                '$VERSION = 0.02'               => 0.02,
20                '$VERSION = 0.0'                => 0.0,
21                '$VERSION = -1.0'               => -1.0,
22                '$VERSION = undef'              => 'undef',
23                '$wibble  = 1.0'                => 'undef',
24                q[my $VERSION = '1.01']         => 'undef',
25                q[local $VERISON = '1.02']      => 'undef',
26                q[local $FOO::VERSION = '1.30'] => 'undef',
27                q[if( $Foo::VERSION >= 3.00 ) {]=> 'undef',
28                q[our $VERSION = '1.23';]       => '1.23',
29
30                '$Something::VERSION == 1.0'    => 'undef',
31                '$Something::VERSION <= 1.0'    => 'undef',
32                '$Something::VERSION >= 1.0'    => 'undef',
33                '$Something::VERSION != 1.0'    => 'undef',
34
35                qq[\$Something::VERSION == 1.0\n\$VERSION = 2.3\n]                     => '2.3',
36                qq[\$Something::VERSION == 1.0\n\$VERSION = 2.3\n\$VERSION = 4.5\n]    => '2.3',
37
38                '$VERSION = sprintf("%d.%03d", q$Revision: 3.74 $ =~ /(\d+)\.(\d+)/);' => '3.074',
39                '$VERSION = substr(q$Revision: 2.8 $, 10) + 2 . "";'                   => '4.8',
40                'elsif ( $Something::VERSION >= 1.99 )' => 'undef',
41
42               );
43
44if( $Has_Version ) {
45    $versions{q[use version; $VERSION = qv("1.2.3");]} = qv("1.2.3");
46    $versions{q[$VERSION = qv("1.2.3")]}               = qv("1.2.3");
47}
48
49if( $] >= 5.011001 ) {
50    $versions{'package Foo 1.23;'         } = '1.23';
51    $versions{'package Foo::Bar 1.23;'    } = '1.23';
52    $versions{'package Foo v1.2.3;'       } = 'v1.2.3';
53    $versions{'package Foo::Bar v1.2.3;'  } = 'v1.2.3';
54    $versions{' package Foo::Bar 1.23 ;'  } = '1.23';
55    $versions{"package Foo'Bar 1.23;"     } = '1.23';
56    $versions{"package Foo::Bar 1.2.3;"   } = '1.2.3';
57    $versions{'package Foo 1.230;'        } = '1.230';
58    $versions{'package Foo 1.23_01;'      } = '1.23_01';
59    $versions{'package Foo v1.23_01;'     } = 'v1.23_01';
60    $versions{q["package Foo 1.23"]}        = 'undef';
61    $versions{<<'END'}                      = '1.23';
62package Foo 1.23;
63our $VERSION = 2.34;
64END
65
66    $versions{<<'END'}                      = '2.34';
67our $VERSION = 2.34;
68package Foo 1.23;
69END
70
71    $versions{<<'END'}                      = '2.34';
72package Foo::100;
73our $VERSION = 2.34;
74END
75}
76
77plan tests => (3 * keys %versions) + 4;
78
79for my $code ( sort keys %versions ) {
80    my $expect = $versions{$code};
81    (my $label = $code) =~ s/\n/\\n/g;
82    my $warnings = "";
83    local $SIG{__WARN__} = sub { $warnings .= "@_\n"; };
84    is( parse_version_string($code), $expect, $label );
85    is($warnings, '', "$label does not cause warnings");
86}
87
88
89sub parse_version_string {
90    my $code = shift;
91
92    my ($fh,$file) = tempfile( DIR => '.', UNLINK => 1 );
93    print $fh "$code\n";
94    close $fh;
95
96    $_ = 'foo';
97    my $version = MM->parse_version( $file );
98    is( $_, 'foo', '$_ not leaked by parse_version' );
99
100    return $version;
101}
102
103
104# This is a specific test to see if a version subroutine in the $VERSION
105# declaration confuses later calls to the version class.
106# [rt.cpan.org 30747]
107SKIP: {
108    skip "need version.pm", 4 unless $Has_Version;
109    is parse_version_string(q[ $VERSION = '1.00'; sub version { $VERSION } ]),
110       '1.00', "eval 'sub version {...} in version string";
111    is parse_version_string(q[ use version; $VERSION = version->new("1.2.3") ]),
112       qv("1.2.3"), "version.pm not confused by version sub";
113}
114