This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
s/superceded/superseded/g
[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'
a7ad731c
HS
4
5#########################
6
34ba6322 7use Test::More qw(no_plan);
a7ad731c 8
137d6fc0 9diag "Tests with base class" unless $ENV{PERL_CORE};
a7ad731c 10
5eb567df 11BEGIN {
43eaf59d 12 use_ok("version", 0.50); # If we made it this far, we are ok.
5eb567df
RGS
13}
14
137d6fc0
JP
15BaseTests("version");
16
17diag "Tests with empty derived class" unless $ENV{PERL_CORE};
18
19package version::Empty;
43eaf59d 20use base version;
137d6fc0 21$VERSION = 0.01;
43eaf59d
SP
22no warnings 'redefine';
23*::qv = sub { return bless version::qv(shift), __PACKAGE__; };
137d6fc0 24
e0218a61
JP
25package version::Bad;
26use base version;
27sub new { my($self,$n)=@_; bless \$n, $self }
28
137d6fc0 29package main;
e0218a61 30my $testobj = version::Empty->new(1.002_003);
137d6fc0
JP
31isa_ok( $testobj, "version::Empty" );
32ok( $testobj->numify == 1.002003, "Numified correctly" );
9137345a
JP
33ok( $testobj->stringify eq "1.002003", "Stringified correctly" );
34ok( $testobj->normal eq "v1.2.3", "Normalified correctly" );
137d6fc0 35
e0218a61 36my $verobj = version->new("1.2.4");
137d6fc0
JP
37ok( $verobj > $testobj, "Comparison vs parent class" );
38ok( $verobj gt $testobj, "Comparison vs parent class" );
39BaseTests("version::Empty");
40
e0218a61
JP
41diag "tests with bad subclass" unless $ENV{PERL_CORE};
42$testobj = version::Bad->new(1.002_003);
43isa_ok( $testobj, "version::Bad" );
44eval { my $string = $testobj->numify };
45like($@, qr/Invalid version object/,
46 "Bad subclass numify");
47eval { my $string = $testobj->normal };
48like($@, qr/Invalid version object/,
49 "Bad subclass normal");
50eval { my $string = $testobj->stringify };
51like($@, qr/Invalid version object/,
52 "Bad subclass stringify");
53eval { my $test = $testobj > 1.0 };
54like($@, qr/Invalid version object/,
55 "Bad subclass vcmp");
56
137d6fc0
JP
57sub BaseTests {
58
59 my $CLASS = shift;
60
61 # Insert your test code below, the Test module is use()ed here so read
62 # its man page ( perldoc Test ) for help writing this test script.
63
64 # Test bare number processing
65 diag "tests with bare numbers" unless $ENV{PERL_CORE};
66 $version = $CLASS->new(5.005_03);
9137345a 67 is ( "$version" , "5.005030" , '5.005_03 eq 5.5.30' );
137d6fc0 68 $version = $CLASS->new(1.23);
13f8f398 69 is ( "$version" , "1.230" , '1.23 eq "1.230"' );
137d6fc0
JP
70
71 # Test quoted number processing
72 diag "tests with quoted numbers" unless $ENV{PERL_CORE};
73 $version = $CLASS->new("5.005_03");
c76df65e 74 is ( "$version" , "5.005_030" , '"5.005_03" eq "5.005_030"' );
137d6fc0 75 $version = $CLASS->new("v1.23");
9137345a 76 is ( "$version" , "v1.23.0" , '"v1.23" eq "v1.23.0"' );
137d6fc0
JP
77
78 # Test stringify operator
79 diag "tests with stringify" unless $ENV{PERL_CORE};
80 $version = $CLASS->new("5.005");
b9381830 81 is ( "$version" , "5.005" , '5.005 eq "5.005"' );
137d6fc0 82 $version = $CLASS->new("5.006.001");
9137345a 83 is ( "$version" , "v5.6.1" , '5.006.001 eq v5.6.1' );
137d6fc0 84 $version = $CLASS->new("1.2.3_4");
9137345a 85 is ( "$version" , "v1.2.3_4" , 'alpha version 1.2.3_4 eq v1.2.3_4' );
137d6fc0
JP
86
87 # test illegal formats
88 diag "test illegal formats" unless $ENV{PERL_CORE};
89 eval {my $version = $CLASS->new("1.2_3_4")};
90 like($@, qr/multiple underscores/,
91 "Invalid version format (multiple underscores)");
92
93 eval {my $version = $CLASS->new("1.2_3.4")};
94 like($@, qr/underscores before decimal/,
95 "Invalid version format (underscores before decimal)");
96
34ba6322
SP
97 eval {my $version = $CLASS->new("1_2")};
98 like($@, qr/alpha without decimal/,
99 "Invalid version format (alpha without decimal)");
100
137d6fc0 101 $version = $CLASS->new("99 and 44/100 pure");
13f8f398 102 ok ("$version" eq "99.000", '$version eq "99.000"');
137d6fc0 103 ok ($version->numify == 99.0, '$version->numify == 99.0');
9137345a 104 ok ($version->normal eq "v99.0.0", '$version->normal eq v99.0.0');
137d6fc0
JP
105
106 $version = $CLASS->new("something");
107 ok (defined $version, 'defined $version');
108
109 # reset the test object to something reasonable
110 $version = $CLASS->new("1.2.3");
111
112 # Test boolean operator
113 ok ($version, 'boolean');
114
115 # Test class membership
cb5772bb 116 isa_ok ( $version, $CLASS );
137d6fc0
JP
117
118 # Test comparison operators with self
119 diag "tests with self" unless $ENV{PERL_CORE};
120 ok ( $version eq $version, '$version eq $version' );
121 is ( $version cmp $version, 0, '$version cmp $version == 0' );
122 ok ( $version == $version, '$version == $version' );
123
124 # test first with non-object
125 $version = $CLASS->new("5.006.001");
126 $new_version = "5.8.0";
127 diag "tests with non-objects" unless $ENV{PERL_CORE};
128 ok ( $version ne $new_version, '$version ne $new_version' );
129 ok ( $version lt $new_version, '$version lt $new_version' );
130 ok ( $new_version gt $version, '$new_version gt $version' );
131 ok ( ref(\$new_version) eq 'SCALAR', 'no auto-upgrade');
132 $new_version = "$version";
133 ok ( $version eq $new_version, '$version eq $new_version' );
134 ok ( $new_version eq $version, '$new_version eq $version' );
135
136 # now test with existing object
137 $new_version = $CLASS->new("5.8.0");
138 diag "tests with objects" unless $ENV{PERL_CORE};
139 ok ( $version ne $new_version, '$version ne $new_version' );
140 ok ( $version lt $new_version, '$version lt $new_version' );
141 ok ( $new_version gt $version, '$new_version gt $version' );
142 $new_version = $CLASS->new("$version");
143 ok ( $version eq $new_version, '$version eq $new_version' );
144
145 # Test Numeric Comparison operators
146 # test first with non-object
147 $new_version = "5.8.0";
148 diag "numeric tests with non-objects" unless $ENV{PERL_CORE};
149 ok ( $version == $version, '$version == $version' );
150 ok ( $version < $new_version, '$version < $new_version' );
151 ok ( $new_version > $version, '$new_version > $version' );
152 ok ( $version != $new_version, '$version != $new_version' );
153
154 # now test with existing object
155 $new_version = $CLASS->new($new_version);
156 diag "numeric tests with objects" unless $ENV{PERL_CORE};
157 ok ( $version < $new_version, '$version < $new_version' );
158 ok ( $new_version > $version, '$new_version > $version' );
159 ok ( $version != $new_version, '$version != $new_version' );
160
161 # now test with actual numbers
162 diag "numeric tests with numbers" unless $ENV{PERL_CORE};
163 ok ( $version->numify() == 5.006001, '$version->numify() == 5.006001' );
164 ok ( $version->numify() <= 5.006001, '$version->numify() <= 5.006001' );
165 ok ( $version->numify() < 5.008, '$version->numify() < 5.008' );
166 #ok ( $version->numify() > v5.005_02, '$version->numify() > 5.005_02' );
167
168 # test with long decimals
169 diag "Tests with extended decimal versions" unless $ENV{PERL_CORE};
170 $version = $CLASS->new(1.002003);
171 ok ( $version eq "1.2.3", '$version eq "1.2.3"');
172 ok ( $version->numify == 1.002003, '$version->numify == 1.002003');
173 $version = $CLASS->new("2002.09.30.1");
174 ok ( $version eq "2002.9.30.1",'$version eq 2002.9.30.1');
175 ok ( $version->numify == 2002.009030001,
176 '$version->numify == 2002.009030001');
177
178 # now test with alpha version form with string
179 $version = $CLASS->new("1.2.3");
180 $new_version = "1.2.3_4";
181 diag "tests with alpha-style non-objects" unless $ENV{PERL_CORE};
182 ok ( $version lt $new_version, '$version lt $new_version' );
183 ok ( $new_version gt $version, '$new_version gt $version' );
184 ok ( $version ne $new_version, '$version ne $new_version' );
185
186 $version = $CLASS->new("1.2.4");
187 diag "numeric tests with alpha-style non-objects" unless $ENV{PERL_CORE};
188 ok ( $version > $new_version, '$version > $new_version' );
189 ok ( $new_version < $version, '$new_version < $version' );
190 ok ( $version != $new_version, '$version != $new_version' );
191
192 # now test with alpha version form with object
193 $version = $CLASS->new("1.2.3");
194 $new_version = $CLASS->new("1.2.3_4");
195 diag "tests with alpha-style objects" unless $ENV{PERL_CORE};
196 ok ( $version < $new_version, '$version < $new_version' );
197 ok ( $new_version > $version, '$new_version > $version' );
198 ok ( $version != $new_version, '$version != $new_version' );
199 ok ( !$version->is_alpha, '!$version->is_alpha');
200 ok ( $new_version->is_alpha, '$new_version->is_alpha');
201
202 $version = $CLASS->new("1.2.4");
203 diag "tests with alpha-style objects" unless $ENV{PERL_CORE};
204 ok ( $version > $new_version, '$version > $new_version' );
205 ok ( $new_version < $version, '$new_version < $version' );
206 ok ( $version != $new_version, '$version != $new_version' );
207
13f8f398
JP
208 $version = $CLASS->new("1.2.3.4");
209 $new_version = $CLASS->new("1.2.3_4");
137d6fc0
JP
210 diag "tests with alpha-style objects with same subversion" unless $ENV{PERL_CORE};
211 ok ( $version > $new_version, '$version > $new_version' );
212 ok ( $new_version < $version, '$new_version < $version' );
213 ok ( $version != $new_version, '$version != $new_version' );
214
215 diag "test implicit [in]equality" unless $ENV{PERL_CORE};
13f8f398
JP
216 $version = $CLASS->new("v1.2.3");
217 $new_version = $CLASS->new("1.2.3.0");
137d6fc0 218 ok ( $version == $new_version, '$version == $new_version' );
13f8f398 219 $new_version = $CLASS->new("1.2.3_0");
137d6fc0 220 ok ( $version == $new_version, '$version == $new_version' );
13f8f398 221 $new_version = $CLASS->new("1.2.3.1");
137d6fc0 222 ok ( $version < $new_version, '$version < $new_version' );
13f8f398 223 $new_version = $CLASS->new("1.2.3_1");
137d6fc0
JP
224 ok ( $version < $new_version, '$version < $new_version' );
225 $new_version = $CLASS->new("1.1.999");
226 ok ( $version > $new_version, '$version > $new_version' );
227
228 # that which is not expressly permitted is forbidden
229 diag "forbidden operations" unless $ENV{PERL_CORE};
230 ok ( !eval { ++$version }, "noop ++" );
231 ok ( !eval { --$version }, "noop --" );
232 ok ( !eval { $version/1 }, "noop /" );
233 ok ( !eval { $version*3 }, "noop *" );
234 ok ( !eval { abs($version) }, "noop abs" );
235
236 # test the qv() sub
237 diag "testing qv" unless $ENV{PERL_CORE};
238 $version = qv("1.2");
239 ok ( $version eq "1.2.0", 'qv("1.2") eq "1.2.0"' );
240 $version = qv(1.2);
241 ok ( $version eq "1.2.0", 'qv(1.2) eq "1.2.0"' );
43eaf59d 242 isa_ok( qv('5.008'), $CLASS );
137d6fc0 243
13f8f398
JP
244 # test creation from existing version object
245 diag "create new from existing version" unless $ENV{PERL_CORE};
cb5772bb 246 ok (eval {$new_version = $CLASS->new($version)},
13f8f398 247 "new from existing object");
9137345a
JP
248 ok ($new_version == $version, "class->new($version) identical");
249 $new_version = $version->new();
250 ok ($new_version == $version, "$version->new() also identical");
251 $new_version = $version->new("1.2.3");
252 is ($new_version, "v1.2.3" , '$version->new("1.2.3") works too');
13f8f398 253
137d6fc0
JP
254 # test the CVS revision mode
255 diag "testing CVS Revision" unless $ENV{PERL_CORE};
cb5772bb 256 $version = new $CLASS qw$Revision: 1.2$;
137d6fc0 257 ok ( $version eq "1.2.0", 'qw$Revision: 1.2$ eq 1.2.0' );
cb5772bb 258 $version = new $CLASS qw$Revision: 1.2.3.4$;
d2397f31 259 ok ( $version eq "1.2.3.4", 'qw$Revision: 1.2.3.4$ eq 1.2.3.4' );
137d6fc0 260
9137345a
JP
261 # test the CPAN style reduced significant digit form
262 diag "testing CPAN-style versions" unless $ENV{PERL_CORE};
263 $version = $CLASS->new("1.23_01");
264 is ( "$version" , "1.23_0100", "CPAN-style alpha version" );
265 ok ( $version > 1.23, "1.23_01 > 1.23");
266 ok ( $version < 1.24, "1.23_01 < 1.24");
267
137d6fc0
JP
268 # test reformed UNIVERSAL::VERSION
269 diag "Replacement UNIVERSAL::VERSION tests" unless $ENV{PERL_CORE};
270
271 # we know this file is here since we require it ourselves
13f8f398 272 $version = $Test::More::VERSION;
137d6fc0 273 eval "use Test::More $version";
b9381830 274 unlike($@, qr/Test::More version $version/,
137d6fc0
JP
275 'Replacement eval works with exact version');
276
13f8f398
JP
277 $version = $Test::More::VERSION+0.01; # this should fail even with old UNIVERSAL::VERSION
278 eval "use Test::More $version";
b9381830 279 like($@, qr/Test::More version $version/,
137d6fc0
JP
280 'Replacement eval works with incremented version');
281
b9381830 282 $version =~ s/\.0$//; #convert to string and remove trailing '.0'
137d6fc0
JP
283 chop($version); # shorten by 1 digit, should still succeed
284 eval "use Test::More $version";
b9381830 285 unlike($@, qr/Test::More version $version/,
137d6fc0
JP
286 'Replacement eval works with single digit');
287
288 $version += 0.1; # this would fail with old UNIVERSAL::VERSION
289 eval "use Test::More $version";
b9381830 290 like($@, qr/Test::More version $version/,
137d6fc0
JP
291 'Replacement eval works with incremented digit');
292
293SKIP: {
b9381830 294 skip 'Cannot test v-strings with Perl < 5.8.1', 4
137d6fc0
JP
295 if $] < 5.008_001;
296 diag "Tests with v-strings" unless $ENV{PERL_CORE};
297 $version = $CLASS->new(1.2.3);
9137345a 298 ok("$version" eq "v1.2.3", '"$version" eq 1.2.3');
137d6fc0
JP
299 $version = $CLASS->new(1.0.0);
300 $new_version = $CLASS->new(1);
301 ok($version == $new_version, '$version == $new_version');
302 ok($version eq $new_version, '$version eq $new_version');
137d6fc0 303 $version = qv(1.2.3);
9137345a 304 ok("$version" eq "v1.2.3", 'v-string initialized qv()');
137d6fc0 305 }
e0218a61
JP
306
307 diag "Tests with real-world (malformed) data" unless $ENV{PERL_CORE};
308
309 # trailing zero testing (reported by Andreas Koenig).
310 $version = $CLASS->new("1");
311 ok($version->numify eq "1.000", "trailing zeros preserved");
312 $version = $CLASS->new("1.0");
313 ok($version->numify eq "1.000", "trailing zeros preserved");
314 $version = $CLASS->new("1.0.0");
315 ok($version->numify eq "1.000000", "trailing zeros preserved");
316 $version = $CLASS->new("1.0.0.0");
317 ok($version->numify eq "1.000000000", "trailing zeros preserved");
318
319 # leading zero testing (reported by Andreas Koenig).
320 $version = $CLASS->new(".7");
321 ok($version->numify eq "0.700", "leading zero inferred");
322
323 # leading space testing (reported by Andreas Koenig).
324 $version = $CLASS->new(" 1.7");
325 ok($version->numify eq "1.700", "leading space ignored");
326
137d6fc0 327}
cb5772bb
RGS
328
3291;