This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Upgrade to version.pm 0.67
[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);
c8a14fb6
RGS
8require Test::Harness;
9no warnings 'once';
10*Verbose = \$Test::Harness::Verbose;
a7ad731c 11
137d6fc0 12diag "Tests with base class" unless $ENV{PERL_CORE};
a7ad731c 13
5eb567df 14BEGIN {
43eaf59d 15 use_ok("version", 0.50); # If we made it this far, we are ok.
5eb567df
RGS
16}
17
137d6fc0
JP
18BaseTests("version");
19
20diag "Tests with empty derived class" unless $ENV{PERL_CORE};
21
22package version::Empty;
43eaf59d 23use base version;
137d6fc0 24$VERSION = 0.01;
43eaf59d
SP
25no warnings 'redefine';
26*::qv = sub { return bless version::qv(shift), __PACKAGE__; };
137d6fc0 27
e0218a61
JP
28package version::Bad;
29use base version;
30sub new { my($self,$n)=@_; bless \$n, $self }
31
137d6fc0 32package main;
e0218a61 33my $testobj = version::Empty->new(1.002_003);
137d6fc0
JP
34isa_ok( $testobj, "version::Empty" );
35ok( $testobj->numify == 1.002003, "Numified correctly" );
9137345a
JP
36ok( $testobj->stringify eq "1.002003", "Stringified correctly" );
37ok( $testobj->normal eq "v1.2.3", "Normalified correctly" );
137d6fc0 38
e0218a61 39my $verobj = version->new("1.2.4");
137d6fc0
JP
40ok( $verobj > $testobj, "Comparison vs parent class" );
41ok( $verobj gt $testobj, "Comparison vs parent class" );
42BaseTests("version::Empty");
43
e0218a61
JP
44diag "tests with bad subclass" unless $ENV{PERL_CORE};
45$testobj = version::Bad->new(1.002_003);
46isa_ok( $testobj, "version::Bad" );
47eval { my $string = $testobj->numify };
48like($@, qr/Invalid version object/,
49 "Bad subclass numify");
50eval { my $string = $testobj->normal };
51like($@, qr/Invalid version object/,
52 "Bad subclass normal");
53eval { my $string = $testobj->stringify };
54like($@, qr/Invalid version object/,
55 "Bad subclass stringify");
56eval { my $test = $testobj > 1.0 };
57like($@, qr/Invalid version object/,
58 "Bad subclass vcmp");
59
c0b17f21
JP
60# dummy up a redundant call to satify David Wheeler
61local $SIG{__WARN__} = sub { die $_[0] };
62eval 'use version;';
63unlike ($@, qr/^Subroutine main::qv redefined/,
64 "Only export qv once per package (to prevent redefined warnings).");
65
137d6fc0
JP
66sub BaseTests {
67
317f7c8a
RGS
68 my ($CLASS, $no_qv) = @_;
69
70 # Insert your test code below, the Test module is use()ed here so read
71 # its man page ( perldoc Test ) for help writing this test script.
72
73 # Test bare number processing
74 diag "tests with bare numbers" if $Verbose;
75 $version = $CLASS->new(5.005_03);
76 is ( "$version" , "5.005030" , '5.005_03 eq 5.5.30' );
77 $version = $CLASS->new(1.23);
78 is ( "$version" , "1.230" , '1.23 eq "1.230"' );
79
80 # Test quoted number processing
81 diag "tests with quoted numbers" if $Verbose;
82 $version = $CLASS->new("5.005_03");
83 is ( "$version" , "5.005_030" , '"5.005_03" eq "5.005_030"' );
84 $version = $CLASS->new("v1.23");
85 is ( "$version" , "v1.23.0" , '"v1.23" eq "v1.23.0"' );
86
87 # Test stringify operator
88 diag "tests with stringify" if $Verbose;
89 $version = $CLASS->new("5.005");
90 is ( "$version" , "5.005" , '5.005 eq "5.005"' );
91 $version = $CLASS->new("5.006.001");
92 is ( "$version" , "v5.6.1" , '5.006.001 eq v5.6.1' );
93 $version = $CLASS->new("1.2.3_4");
94 is ( "$version" , "v1.2.3_4" , 'alpha version 1.2.3_4 eq v1.2.3_4' );
95
96 # test illegal formats
97 diag "test illegal formats" if $Verbose;
98 eval {my $version = $CLASS->new("1.2_3_4")};
99 like($@, qr/multiple underscores/,
100 "Invalid version format (multiple underscores)");
101
102 eval {my $version = $CLASS->new("1.2_3.4")};
103 like($@, qr/underscores before decimal/,
104 "Invalid version format (underscores before decimal)");
105
106 eval {my $version = $CLASS->new("1_2")};
107 like($@, qr/alpha without decimal/,
108 "Invalid version format (alpha without decimal)");
109
110 # for this first test, just upgrade the warn() to die()
111 eval {
112 local $SIG{__WARN__} = sub { die $_[0] };
113 $version = $CLASS->new("1.2b3");
114 };
115 my $warnregex = "Version string '.+' contains invalid data; ".
116 "ignoring: '.+'";
117
118 like($@, qr/$warnregex/,
119 "Version string contains invalid data; ignoring");
120
121 # from here on out capture the warning and test independently
122 my $warning;
123 local $SIG{__WARN__} = sub { $warning = $_[0] };
124 $version = $CLASS->new("99 and 44/100 pure");
125
126 like($warning, qr/$warnregex/,
127 "Version string contains invalid data; ignoring");
128 ok ("$version" eq "99.000", '$version eq "99.000"');
129 ok ($version->numify == 99.0, '$version->numify == 99.0');
130 ok ($version->normal eq "v99.0.0", '$version->normal eq v99.0.0');
131
132 $version = $CLASS->new("something");
133 like($warning, qr/$warnregex/,
134 "Version string contains invalid data; ignoring");
135 ok (defined $version, 'defined $version');
136
137 # reset the test object to something reasonable
138 $version = $CLASS->new("1.2.3");
139
140 # Test boolean operator
141 ok ($version, 'boolean');
142
143 # Test class membership
144 isa_ok ( $version, $CLASS );
145
146 # Test comparison operators with self
147 diag "tests with self" if $Verbose;
148 ok ( $version eq $version, '$version eq $version' );
149 is ( $version cmp $version, 0, '$version cmp $version == 0' );
150 ok ( $version == $version, '$version == $version' );
151
152 # test first with non-object
153 $version = $CLASS->new("5.006.001");
154 $new_version = "5.8.0";
155 diag "tests with non-objects" if $Verbose;
156 ok ( $version ne $new_version, '$version ne $new_version' );
157 ok ( $version lt $new_version, '$version lt $new_version' );
158 ok ( $new_version gt $version, '$new_version gt $version' );
159 ok ( ref(\$new_version) eq 'SCALAR', 'no auto-upgrade');
160 $new_version = "$version";
161 ok ( $version eq $new_version, '$version eq $new_version' );
162 ok ( $new_version eq $version, '$new_version eq $version' );
163
164 # now test with existing object
165 $new_version = $CLASS->new("5.8.0");
166 diag "tests with objects" if $Verbose;
167 ok ( $version ne $new_version, '$version ne $new_version' );
168 ok ( $version lt $new_version, '$version lt $new_version' );
169 ok ( $new_version gt $version, '$new_version gt $version' );
170 $new_version = $CLASS->new("$version");
171 ok ( $version eq $new_version, '$version eq $new_version' );
172
173 # Test Numeric Comparison operators
174 # test first with non-object
175 $new_version = "5.8.0";
176 diag "numeric tests with non-objects" if $Verbose;
177 ok ( $version == $version, '$version == $version' );
178 ok ( $version < $new_version, '$version < $new_version' );
179 ok ( $new_version > $version, '$new_version > $version' );
180 ok ( $version != $new_version, '$version != $new_version' );
181
182 # now test with existing object
183 $new_version = $CLASS->new($new_version);
184 diag "numeric tests with objects" if $Verbose;
185 ok ( $version < $new_version, '$version < $new_version' );
186 ok ( $new_version > $version, '$new_version > $version' );
187 ok ( $version != $new_version, '$version != $new_version' );
188
189 # now test with actual numbers
190 diag "numeric tests with numbers" if $Verbose;
191 ok ( $version->numify() == 5.006001, '$version->numify() == 5.006001' );
192 ok ( $version->numify() <= 5.006001, '$version->numify() <= 5.006001' );
193 ok ( $version->numify() < 5.008, '$version->numify() < 5.008' );
194 #ok ( $version->numify() > v5.005_02, '$version->numify() > 5.005_02' );
195
196 # test with long decimals
197 diag "Tests with extended decimal versions" if $Verbose;
198 $version = $CLASS->new(1.002003);
199 ok ( $version eq "1.2.3", '$version eq "1.2.3"');
200 ok ( $version->numify == 1.002003, '$version->numify == 1.002003');
201 $version = $CLASS->new("2002.09.30.1");
202 ok ( $version eq "2002.9.30.1",'$version eq 2002.9.30.1');
203 ok ( $version->numify == 2002.009030001,
204 '$version->numify == 2002.009030001');
205
206 # now test with alpha version form with string
207 $version = $CLASS->new("1.2.3");
208 $new_version = "1.2.3_4";
209 diag "tests with alpha-style non-objects" if $Verbose;
210 ok ( $version lt $new_version, '$version lt $new_version' );
211 ok ( $new_version gt $version, '$new_version gt $version' );
212 ok ( $version ne $new_version, '$version ne $new_version' );
213
214 $version = $CLASS->new("1.2.4");
215 diag "numeric tests with alpha-style non-objects"
216 if $Verbose;
217 ok ( $version > $new_version, '$version > $new_version' );
218 ok ( $new_version < $version, '$new_version < $version' );
219 ok ( $version != $new_version, '$version != $new_version' );
220
221 # now test with alpha version form with object
222 $version = $CLASS->new("1.2.3");
223 $new_version = $CLASS->new("1.2.3_4");
224 diag "tests with alpha-style objects" if $Verbose;
225 ok ( $version < $new_version, '$version < $new_version' );
226 ok ( $new_version > $version, '$new_version > $version' );
227 ok ( $version != $new_version, '$version != $new_version' );
228 ok ( !$version->is_alpha, '!$version->is_alpha');
229 ok ( $new_version->is_alpha, '$new_version->is_alpha');
230
231 $version = $CLASS->new("1.2.4");
232 diag "tests with alpha-style objects" if $Verbose;
233 ok ( $version > $new_version, '$version > $new_version' );
234 ok ( $new_version < $version, '$new_version < $version' );
235 ok ( $version != $new_version, '$version != $new_version' );
236
237 $version = $CLASS->new("1.2.3.4");
238 $new_version = $CLASS->new("1.2.3_4");
239 diag "tests with alpha-style objects with same subversion"
240 if $Verbose;
241 ok ( $version > $new_version, '$version > $new_version' );
242 ok ( $new_version < $version, '$new_version < $version' );
243 ok ( $version != $new_version, '$version != $new_version' );
244
245 diag "test implicit [in]equality" if $Verbose;
246 $version = $CLASS->new("v1.2.3");
247 $new_version = $CLASS->new("1.2.3.0");
248 ok ( $version == $new_version, '$version == $new_version' );
249 $new_version = $CLASS->new("1.2.3_0");
250 ok ( $version == $new_version, '$version == $new_version' );
251 $new_version = $CLASS->new("1.2.3.1");
252 ok ( $version < $new_version, '$version < $new_version' );
253 $new_version = $CLASS->new("1.2.3_1");
254 ok ( $version < $new_version, '$version < $new_version' );
255 $new_version = $CLASS->new("1.1.999");
256 ok ( $version > $new_version, '$version > $new_version' );
257
258 # that which is not expressly permitted is forbidden
259 diag "forbidden operations" if $Verbose;
260 ok ( !eval { ++$version }, "noop ++" );
261 ok ( !eval { --$version }, "noop --" );
262 ok ( !eval { $version/1 }, "noop /" );
263 ok ( !eval { $version*3 }, "noop *" );
264 ok ( !eval { abs($version) }, "noop abs" );
137d6fc0 265
c8a14fb6 266SKIP: {
317f7c8a
RGS
267 skip "version require'd instead of use'd, cannot test qv", 3
268 if defined $no_qv;
269 # test the qv() sub
270 diag "testing qv" if $Verbose;
271 $version = qv("1.2");
272 cmp_ok ( $version, "eq", "v1.2.0", 'qv("1.2") eq "1.2.0"' );
273 $version = qv(1.2);
274 cmp_ok ( $version, "eq", "v1.2.0", 'qv(1.2) eq "1.2.0"' );
275 isa_ok( qv('5.008'), $CLASS );
c8a14fb6 276}
137d6fc0 277
317f7c8a
RGS
278 # test creation from existing version object
279 diag "create new from existing version" if $Verbose;
280 ok (eval {$new_version = $CLASS->new($version)},
281 "new from existing object");
282 ok ($new_version == $version, "class->new($version) identical");
283 $new_version = $version->new();
284 isa_ok ($new_version, $CLASS );
285 is ($new_version, "0.000", "version->new() doesn't clone");
286 $new_version = $version->new("1.2.3");
287 is ($new_version, "v1.2.3" , '$version->new("1.2.3") works too');
288
289 # test the CVS revision mode
290 diag "testing CVS Revision" if $Verbose;
291 $version = new $CLASS qw$Revision: 1.2$;
292 ok ( $version eq "1.2.0", 'qw$Revision: 1.2$ eq 1.2.0' );
293 $version = new $CLASS qw$Revision: 1.2.3.4$;
294 ok ( $version eq "1.2.3.4", 'qw$Revision: 1.2.3.4$ eq 1.2.3.4' );
295
296 # test the CPAN style reduced significant digit form
297 diag "testing CPAN-style versions" if $Verbose;
298 $version = $CLASS->new("1.23_01");
299 is ( "$version" , "1.23_0100", "CPAN-style alpha version" );
300 ok ( $version > 1.23, "1.23_01 > 1.23");
301 ok ( $version < 1.24, "1.23_01 < 1.24");
302
303 # test reformed UNIVERSAL::VERSION
304 diag "Replacement UNIVERSAL::VERSION tests" if $Verbose;
305
306 # we know this file is here since we require it ourselves
307 $version = $Test::More::VERSION;
308 eval "use Test::More $version";
309 unlike($@, qr/Test::More version $version/,
310 'Replacement eval works with exact version');
c8a14fb6 311
317f7c8a
RGS
312 # test as class method
313 $new_version = Test::More->VERSION;
314 cmp_ok($new_version,'cmp',$version, "Called as class method");
315
316 # this should fail even with old UNIVERSAL::VERSION
317 $version = $Test::More::VERSION+0.01;
318 eval "use Test::More $version";
319 like($@, qr/Test::More version $version/,
320 'Replacement eval works with incremented version');
321
322 $version =~ s/\.0$//; #convert to string and remove trailing '.0'
323 chop($version); # shorten by 1 digit, should still succeed
324 eval "use Test::More $version";
325 unlike($@, qr/Test::More version $version/,
326 'Replacement eval works with single digit');
327
328 $version += 0.1; # this would fail with old UNIVERSAL::VERSION
329 eval "use Test::More $version";
330 like($@, qr/Test::More version $version/,
331 'Replacement eval works with incremented digit');
332
333 { # dummy up some variously broken modules for testing
334 open F, ">xxx.pm" or die "Cannot open xxx.pm: $!\n";
335 print F "1;\n";
336 close F;
337 my $error_regex;
338 if ( $] < 5.008 ) {
339 $error_regex = 'xxx does not define \$xxx::VERSION';
c8a14fb6 340 }
317f7c8a
RGS
341 else {
342 $error_regex = 'xxx defines neither package nor VERSION';
c8a14fb6
RGS
343 }
344
317f7c8a
RGS
345 eval "use lib '.'; use xxx 3;";
346 like ($@, qr/$error_regex/,
347 'Replacement handles modules without package or VERSION');
348 eval "use lib '.'; use xxx; $version = xxx->VERSION";
349 unlike ($@, qr/$error_regex/,
350 'Replacement handles modules without package or VERSION');
351 ok (defined($version), "Called as class method");
352 unlink 'xxx.pm';
353 }
354
355 { # dummy up some variously broken modules for testing
356 open F, ">yyy.pm" or die "Cannot open yyy.pm: $!\n";
357 print F "package yyy;\n#look ma no VERSION\n1;\n";
358 close F;
359 eval "use lib '.'; use yyy 3;";
360 like ($@, qr/^yyy does not define \$yyy::VERSION/,
361 'Replacement handles modules without VERSION');
362 eval "use lib '.'; use yyy; print yyy->VERSION";
363 unlike ($@, qr/^yyy does not define \$yyy::VERSION/,
364 'Replacement handles modules without VERSION');
365 unlink 'yyy.pm';
366 }
367
368 { # dummy up some variously broken modules for testing
369 open F, ">zzz.pm" or die "Cannot open zzz.pm: $!\n";
370 print F "package zzz;\n\@VERSION = ();\n1;\n";
371 close F;
372 eval "use lib '.'; use zzz 3;";
373 like ($@, qr/^zzz does not define \$zzz::VERSION/,
374 'Replacement handles modules without VERSION');
375 eval "use lib '.'; use zzz; print zzz->VERSION";
376 unlike ($@, qr/^zzz does not define \$zzz::VERSION/,
377 'Replacement handles modules without VERSION');
378 unlink 'zzz.pm';
379 }
380
137d6fc0 381SKIP: {
317f7c8a
RGS
382 skip 'Cannot test bare v-strings with Perl < 5.8.1', 4
383 if $] < 5.008_001;
384 diag "Tests with v-strings" if $Verbose;
385 $version = $CLASS->new(1.2.3);
386 ok("$version" eq "v1.2.3", '"$version" eq 1.2.3');
387 $version = $CLASS->new(1.0.0);
388 $new_version = $CLASS->new(1);
389 ok($version == $new_version, '$version == $new_version');
390 ok($version eq $new_version, '$version eq $new_version');
391 skip "version require'd instead of use'd, cannot test qv", 1
392 if defined $no_qv;
393 $version = qv(1.2.3);
394 ok("$version" eq "v1.2.3", 'v-string initialized qv()');
395 }
396
397 diag "Tests with real-world (malformed) data" if $Verbose;
398
399 # trailing zero testing (reported by Andreas Koenig).
400 $version = $CLASS->new("1");
401 ok($version->numify eq "1.000", "trailing zeros preserved");
402 $version = $CLASS->new("1.0");
403 ok($version->numify eq "1.000", "trailing zeros preserved");
404 $version = $CLASS->new("1.0.0");
405 ok($version->numify eq "1.000000", "trailing zeros preserved");
406 $version = $CLASS->new("1.0.0.0");
407 ok($version->numify eq "1.000000000", "trailing zeros preserved");
408
409 # leading zero testing (reported by Andreas Koenig).
410 $version = $CLASS->new(".7");
411 ok($version->numify eq "0.700", "leading zero inferred");
412
413 # leading space testing (reported by Andreas Koenig).
414 $version = $CLASS->new(" 1.7");
415 ok($version->numify eq "1.700", "leading space ignored");
416
417 # RT 19517 - deal with undef and 'undef' initialization
418 ok($version ne 'undef', "Undef version comparison #1");
419 ok($version ne undef, "Undef version comparison #2");
420 $version = $CLASS->new('undef');
421 unlike($warning, qr/^Version string 'undef' contains invalid data/,
422 "Version string 'undef'");
423
424 $version = $CLASS->new(undef);
425 like($warning, qr/^Use of uninitialized value/,
426 "Version string 'undef'");
427 ok($version eq 'undef', "Undef version comparison #3");
428 ok($version eq undef, "Undef version comparison #4");
429 eval "\$version = \$CLASS->new()"; # no parameter at all
430 unlike($@, qr/^Bizarre copy of CODE/, "No initializer at all");
431 ok($version eq 'undef', "Undef version comparison #5");
432 ok($version eq undef, "Undef version comparison #6");
433
434 $version = $CLASS->new(0.000001);
435 unlike($warning, qr/^Version string '1e-06' contains invalid data/,
436 "Very small version objects");
e0218a61 437
317f7c8a
RGS
438SKIP: {
439 # dummy up a legal module for testing RT#19017
440 open F, ">www.pm" or die "Cannot open www.pm: $!\n";
441 print F <<"EOF";
c8a14fb6
RGS
442package www;
443use version; \$VERSION = qv('0.0.4');
4441;
445EOF
317f7c8a
RGS
446 close F;
447
448 eval "use lib '.'; use www 0.000008;";
449 like ($@, qr/^www version 0.000008 \(v0.0.8\) required/,
450 "Make sure very small versions don't freak");
451 eval "use lib '.'; use www 1;";
452 like ($@, qr/^www version 1.000 \(v1.0.0\) required/,
453 "Comparing vs. version with no decimal");
454 eval "use lib '.'; use www 1.;";
455 like ($@, qr/^www version 1.000 \(v1.0.0\) required/,
456 "Comparing vs. version with decimal only");
457
458 skip 'Cannot "use" extended versions with Perl < 5.6.2', 1
459 if $] < 5.006_002;
460 eval "use lib '.'; use www 0.0.8;";
461 like ($@, qr/^www version 0.000008 \(v0.0.8\) required/,
462 "Make sure very small versions don't freak");
463
464 eval "use lib '.'; use www 0.0.4;";
465 unlike($@, qr/^www version 0.000004 \(v0.0.4\) required/,
466 'Succeed - required == VERSION');
467 cmp_ok ( "www"->VERSION, 'eq', '0.000004', 'No undef warnings' );
468
469 unlink 'www.pm';
470 }
471
472 open F, ">vvv.pm" or die "Cannot open vvv.pm: $!\n";
473 print F <<"EOF";
92dcf8ce
JP
474package vvv;
475use base qw(version);
4761;
477EOF
317f7c8a
RGS
478 close F;
479 # need to eliminate any other qv()'s
480 undef *main::qv;
481 ok(!defined(&{"main\::qv"}), "make sure we cleared qv() properly");
482 eval "use lib '.'; use vvv;";
483 ok(defined(&{"main\::qv"}), "make sure we exported qv() properly");
484 isa_ok( qv(1.2), "vvv");
485 unlink 'vvv.pm';
137d6fc0 486}
cb5772bb
RGS
487
4881;