This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Implement "my $_".
[perl5.git] / lib / version.t
CommitLineData
129318bd 1#! /usr/local/perl -w
a7ad731c
HS
2# Before `make install' is performed this script should be runnable with
3# `make test'. After `make install' it should work as `perl test.pl'
c8d69e4a 4# $Revision: 2.4 $
a7ad731c
HS
5
6#########################
7
c8d69e4a 8use Test::More tests => 73;
129318bd 9use_ok("version"); # If we made it this far, we are ok.
a7ad731c
HS
10
11my ($version, $new_version);
12#########################
13
14# Insert your test code below, the Test module is use()ed here so read
15# its man page ( perldoc Test ) for help writing this test script.
16
129318bd
JP
17# Test bare number processing
18diag "tests with bare numbers" unless $ENV{PERL_CORE};
19$version = new version 5.005_03;
20is ( "$version" , "5.5.30" , '5.005_03 eq 5.5.30' );
21$version = new version 1.23;
22is ( "$version" , "1.230" , '1.23 eq "1.230"' );
23
24# Test quoted number processing
25diag "tests with quoted numbers" unless $ENV{PERL_CORE};
26$version = new version "5.005_03";
27is ( "$version" , "5.5_3" , '"5.005_03" eq "5.5_3"' );
28$version = new version "v1.23";
29is ( "$version" , "1.23" , '"v1.23" eq "1.23"' );
30
a7ad731c
HS
31# Test stringify operator
32diag "tests with stringify" unless $ENV{PERL_CORE};
33$version = new version "5.005";
34is ( "$version" , "5.5" , '5.005 eq 5.5' );
a7ad731c
HS
35$version = new version "5.006.001";
36is ( "$version" , "5.6.1" , '5.006.001 eq 5.6.1' );
37$version = new version "1.2.3_4";
c8d69e4a 38is ( "$version" , "1.2.3_4" , 'alpha version 1.2.3_4 eq 1.2.3_4' );
a7ad731c
HS
39
40# test illegal formats
41diag "test illegal formats" unless $ENV{PERL_CORE};
42eval {my $version = new version "1.2_3_4";};
43like($@, qr/multiple underscores/,
44 "Invalid version format (multiple underscores)");
45
46eval {my $version = new version "1.2_3.4";};
47like($@, qr/underscores before decimal/,
48 "Invalid version format (underscores before decimal)");
49
46314c13
JP
50$version = new version "99 and 44/100 pure";
51ok ("$version" eq "99.0", '$version eq "99.0"');
52ok ($version->numify == 99.0, '$version->numify == 99.0');
53
54$version = new version "something";
129318bd
JP
55ok (defined $version, 'defined $version');
56
57# reset the test object to something reasonable
58$version = new version "1.2.3";
46314c13 59
a7ad731c
HS
60# Test boolean operator
61ok ($version, 'boolean');
62
63# Test ref operator
64ok (ref($version) eq 'version','ref operator');
65
66# Test comparison operators with self
67diag "tests with self" unless $ENV{PERL_CORE};
68ok ( $version eq $version, '$version eq $version' );
69is ( $version cmp $version, 0, '$version cmp $version == 0' );
70ok ( $version == $version, '$version == $version' );
71
72# test first with non-object
73$version = new version "5.006.001";
74$new_version = "5.8.0";
75diag "tests with non-objects" unless $ENV{PERL_CORE};
76ok ( $version ne $new_version, '$version ne $new_version' );
77ok ( $version lt $new_version, '$version lt $new_version' );
78ok ( $new_version gt $version, '$new_version gt $version' );
79ok ( ref(\$new_version) eq 'SCALAR', 'no auto-upgrade');
80$new_version = "$version";
81ok ( $version eq $new_version, '$version eq $new_version' );
82ok ( $new_version eq $version, '$new_version eq $version' );
83
84# now test with existing object
85$new_version = new version "5.8.0";
86diag "tests with objects" unless $ENV{PERL_CORE};
87ok ( $version ne $new_version, '$version ne $new_version' );
88ok ( $version lt $new_version, '$version lt $new_version' );
89ok ( $new_version gt $version, '$new_version gt $version' );
90$new_version = new version "$version";
91ok ( $version eq $new_version, '$version eq $new_version' );
92
93# Test Numeric Comparison operators
94# test first with non-object
95$new_version = "5.8.0";
96diag "numeric tests with non-objects" unless $ENV{PERL_CORE};
97ok ( $version == $version, '$version == $version' );
98ok ( $version < $new_version, '$version < $new_version' );
99ok ( $new_version > $version, '$new_version > $version' );
100ok ( $version != $new_version, '$version != $new_version' );
101
102# now test with existing object
103$new_version = new version $new_version;
104diag "numeric tests with objects" unless $ENV{PERL_CORE};
105ok ( $version < $new_version, '$version < $new_version' );
106ok ( $new_version > $version, '$new_version > $version' );
107ok ( $version != $new_version, '$version != $new_version' );
108
109# now test with actual numbers
110diag "numeric tests with numbers" unless $ENV{PERL_CORE};
111ok ( $version->numify() == 5.006001, '$version->numify() == 5.006001' );
112ok ( $version->numify() <= 5.006001, '$version->numify() <= 5.006001' );
113ok ( $version->numify() < 5.008, '$version->numify() < 5.008' );
114#ok ( $version->numify() > v5.005_02, '$version->numify() > 5.005_02' );
115
116# test with long decimals
117diag "Tests with extended decimal versions" unless $ENV{PERL_CORE};
118$version = new version 1.002003;
119ok ( $version eq "1.2.3", '$version eq "1.2.3"');
120ok ( $version->numify == 1.002003, '$version->numify == 1.002003');
121$version = new version "2002.09.30.1";
122ok ( $version eq "2002.9.30.1",'$version eq 2002.9.30.1');
123ok ( $version->numify == 2002.009030001,
124 '$version->numify == 2002.009030001');
125
c8d69e4a 126# now test with alpha version form with string
a7ad731c
HS
127$version = new version "1.2.3";
128$new_version = "1.2.3_4";
c8d69e4a 129diag "tests with alpha-style non-objects" unless $ENV{PERL_CORE};
a7ad731c
HS
130ok ( $version lt $new_version, '$version lt $new_version' );
131ok ( $new_version gt $version, '$new_version gt $version' );
132ok ( $version ne $new_version, '$version ne $new_version' );
133
134$version = new version "1.2.4";
c8d69e4a 135diag "numeric tests with alpha-style non-objects" unless $ENV{PERL_CORE};
a7ad731c
HS
136ok ( $version > $new_version, '$version > $new_version' );
137ok ( $new_version < $version, '$new_version < $version' );
138ok ( $version != $new_version, '$version != $new_version' );
139
c8d69e4a 140# now test with alpha version form with object
a7ad731c
HS
141$version = new version "1.2.3";
142$new_version = new version "1.2.3_4";
c8d69e4a 143diag "tests with alpha-style objects" unless $ENV{PERL_CORE};
a7ad731c
HS
144ok ( $version < $new_version, '$version < $new_version' );
145ok ( $new_version > $version, '$new_version > $version' );
146ok ( $version != $new_version, '$version != $new_version' );
c8d69e4a
JP
147ok ( !$version->is_alpha, '!$version->is_alpha');
148ok ( $new_version->is_alpha, '$new_version->is_alpha');
a7ad731c
HS
149
150$version = new version "1.2.4";
c8d69e4a 151diag "tests with alpha-style objects" unless $ENV{PERL_CORE};
a7ad731c
HS
152ok ( $version > $new_version, '$version > $new_version' );
153ok ( $new_version < $version, '$new_version < $version' );
154ok ( $version != $new_version, '$version != $new_version' );
155
156$version = new version "1.2.4";
157$new_version = new version "1.2_4";
c8d69e4a 158diag "tests with alpha-style objects with same subversion" unless $ENV{PERL_CORE};
a7ad731c
HS
159ok ( $version > $new_version, '$version > $new_version' );
160ok ( $new_version < $version, '$new_version < $version' );
161ok ( $version != $new_version, '$version != $new_version' );
162
129318bd
JP
163diag "test implicit [in]equality" unless $ENV{PERL_CORE};
164$version = new version "v1.2";
165$new_version = new version "1.2.0";
166ok ( $version == $new_version, '$version == $new_version' );
167$new_version = new version "1.2_0";
168ok ( $version == $new_version, '$version == $new_version' );
169$new_version = new version "1.2.1";
170ok ( $version < $new_version, '$version < $new_version' );
171$new_version = new version "1.2_1";
172ok ( $version < $new_version, '$version < $new_version' );
173$new_version = new version "1.1.999";
174ok ( $version > $new_version, '$version > $new_version' );
175
a7ad731c
HS
176# that which is not expressly permitted is forbidden
177diag "forbidden operations" unless $ENV{PERL_CORE};
178ok ( !eval { $version++ }, "noop ++" );
179ok ( !eval { $version-- }, "noop --" );
180ok ( !eval { $version/1 }, "noop /" );
181ok ( !eval { $version*3 }, "noop *" );
182ok ( !eval { abs($version) }, "noop abs" );
183
184# test reformed UNIVERSAL::VERSION
185diag "Replacement UNIVERSAL::VERSION tests" unless $ENV{PERL_CORE};
186
187# we know this file is here since we require it ourselves
129318bd 188$version = new version $Test::More::VERSION;
a7ad731c
HS
189eval "use Test::More $version";
190unlike($@, qr/Test::More version $version required/,
191 'Replacement eval works with exact version');
192
129318bd 193$version = new version $Test::More::VERSION+0.01; # this should fail even with old UNIVERSAL::VERSION
a7ad731c
HS
194eval "use Test::More $version";
195like($@, qr/Test::More version $version required/,
196 'Replacement eval works with incremented version');
197
198chop($version); # shorten by 1 digit, should still succeed
199eval "use Test::More $version";
200unlike($@, qr/Test::More version $version required/,
201 'Replacement eval works with single digit');
202
203$version += 0.1; # this would fail with old UNIVERSAL::VERSION
204eval "use Test::More $version";
205unlike($@, qr/Test::More version $version required/,
206 'Replacement eval works with incremented digit');
207