This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[patch] simplify optree test support
[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
8dd04980
SP
322 TODO: {
323 local $TODO = "Test fails with Test::More versions ending in _0X";
324 $version =~ s/\.0$//; #convert to string and remove trailing '.0'
325 chop($version); # shorten by 1 digit, should still succeed
326 eval "use Test::More $version";
327 unlike($@, qr/Test::More version $version/,
317f7c8a 328 'Replacement eval works with single digit');
8dd04980
SP
329 }
330
317f7c8a
RGS
331 $version += 0.1; # this would fail with old UNIVERSAL::VERSION
332 eval "use Test::More $version";
333 like($@, qr/Test::More version $version/,
334 'Replacement eval works with incremented digit');
335
336 { # dummy up some variously broken modules for testing
337 open F, ">xxx.pm" or die "Cannot open xxx.pm: $!\n";
338 print F "1;\n";
339 close F;
340 my $error_regex;
341 if ( $] < 5.008 ) {
342 $error_regex = 'xxx does not define \$xxx::VERSION';
c8a14fb6 343 }
317f7c8a
RGS
344 else {
345 $error_regex = 'xxx defines neither package nor VERSION';
c8a14fb6
RGS
346 }
347
317f7c8a
RGS
348 eval "use lib '.'; use xxx 3;";
349 like ($@, qr/$error_regex/,
350 'Replacement handles modules without package or VERSION');
351 eval "use lib '.'; use xxx; $version = xxx->VERSION";
352 unlike ($@, qr/$error_regex/,
353 'Replacement handles modules without package or VERSION');
354 ok (defined($version), "Called as class method");
355 unlink 'xxx.pm';
356 }
357
358 { # dummy up some variously broken modules for testing
359 open F, ">yyy.pm" or die "Cannot open yyy.pm: $!\n";
360 print F "package yyy;\n#look ma no VERSION\n1;\n";
361 close F;
362 eval "use lib '.'; use yyy 3;";
363 like ($@, qr/^yyy does not define \$yyy::VERSION/,
364 'Replacement handles modules without VERSION');
365 eval "use lib '.'; use yyy; print yyy->VERSION";
366 unlike ($@, qr/^yyy does not define \$yyy::VERSION/,
367 'Replacement handles modules without VERSION');
368 unlink 'yyy.pm';
369 }
370
371 { # dummy up some variously broken modules for testing
372 open F, ">zzz.pm" or die "Cannot open zzz.pm: $!\n";
373 print F "package zzz;\n\@VERSION = ();\n1;\n";
374 close F;
375 eval "use lib '.'; use zzz 3;";
376 like ($@, qr/^zzz does not define \$zzz::VERSION/,
377 'Replacement handles modules without VERSION');
378 eval "use lib '.'; use zzz; print zzz->VERSION";
379 unlike ($@, qr/^zzz does not define \$zzz::VERSION/,
380 'Replacement handles modules without VERSION');
381 unlink 'zzz.pm';
382 }
383
137d6fc0 384SKIP: {
317f7c8a
RGS
385 skip 'Cannot test bare v-strings with Perl < 5.8.1', 4
386 if $] < 5.008_001;
387 diag "Tests with v-strings" if $Verbose;
388 $version = $CLASS->new(1.2.3);
389 ok("$version" eq "v1.2.3", '"$version" eq 1.2.3');
390 $version = $CLASS->new(1.0.0);
391 $new_version = $CLASS->new(1);
392 ok($version == $new_version, '$version == $new_version');
393 ok($version eq $new_version, '$version eq $new_version');
394 skip "version require'd instead of use'd, cannot test qv", 1
395 if defined $no_qv;
396 $version = qv(1.2.3);
397 ok("$version" eq "v1.2.3", 'v-string initialized qv()');
398 }
399
400 diag "Tests with real-world (malformed) data" if $Verbose;
401
402 # trailing zero testing (reported by Andreas Koenig).
403 $version = $CLASS->new("1");
404 ok($version->numify eq "1.000", "trailing zeros preserved");
405 $version = $CLASS->new("1.0");
406 ok($version->numify eq "1.000", "trailing zeros preserved");
407 $version = $CLASS->new("1.0.0");
408 ok($version->numify eq "1.000000", "trailing zeros preserved");
409 $version = $CLASS->new("1.0.0.0");
410 ok($version->numify eq "1.000000000", "trailing zeros preserved");
411
412 # leading zero testing (reported by Andreas Koenig).
413 $version = $CLASS->new(".7");
414 ok($version->numify eq "0.700", "leading zero inferred");
415
416 # leading space testing (reported by Andreas Koenig).
417 $version = $CLASS->new(" 1.7");
418 ok($version->numify eq "1.700", "leading space ignored");
419
420 # RT 19517 - deal with undef and 'undef' initialization
421 ok($version ne 'undef', "Undef version comparison #1");
422 ok($version ne undef, "Undef version comparison #2");
423 $version = $CLASS->new('undef');
424 unlike($warning, qr/^Version string 'undef' contains invalid data/,
425 "Version string 'undef'");
426
427 $version = $CLASS->new(undef);
428 like($warning, qr/^Use of uninitialized value/,
429 "Version string 'undef'");
430 ok($version eq 'undef', "Undef version comparison #3");
431 ok($version eq undef, "Undef version comparison #4");
432 eval "\$version = \$CLASS->new()"; # no parameter at all
433 unlike($@, qr/^Bizarre copy of CODE/, "No initializer at all");
434 ok($version eq 'undef', "Undef version comparison #5");
435 ok($version eq undef, "Undef version comparison #6");
436
437 $version = $CLASS->new(0.000001);
438 unlike($warning, qr/^Version string '1e-06' contains invalid data/,
439 "Very small version objects");
e0218a61 440
317f7c8a
RGS
441SKIP: {
442 # dummy up a legal module for testing RT#19017
443 open F, ">www.pm" or die "Cannot open www.pm: $!\n";
444 print F <<"EOF";
c8a14fb6
RGS
445package www;
446use version; \$VERSION = qv('0.0.4');
4471;
448EOF
317f7c8a
RGS
449 close F;
450
451 eval "use lib '.'; use www 0.000008;";
452 like ($@, qr/^www version 0.000008 \(v0.0.8\) required/,
453 "Make sure very small versions don't freak");
454 eval "use lib '.'; use www 1;";
455 like ($@, qr/^www version 1.000 \(v1.0.0\) required/,
456 "Comparing vs. version with no decimal");
457 eval "use lib '.'; use www 1.;";
458 like ($@, qr/^www version 1.000 \(v1.0.0\) required/,
459 "Comparing vs. version with decimal only");
460
461 skip 'Cannot "use" extended versions with Perl < 5.6.2', 1
462 if $] < 5.006_002;
463 eval "use lib '.'; use www 0.0.8;";
464 like ($@, qr/^www version 0.000008 \(v0.0.8\) required/,
465 "Make sure very small versions don't freak");
466
467 eval "use lib '.'; use www 0.0.4;";
468 unlike($@, qr/^www version 0.000004 \(v0.0.4\) required/,
469 'Succeed - required == VERSION');
470 cmp_ok ( "www"->VERSION, 'eq', '0.000004', 'No undef warnings' );
471
472 unlink 'www.pm';
473 }
474
475 open F, ">vvv.pm" or die "Cannot open vvv.pm: $!\n";
476 print F <<"EOF";
92dcf8ce
JP
477package vvv;
478use base qw(version);
4791;
480EOF
317f7c8a
RGS
481 close F;
482 # need to eliminate any other qv()'s
483 undef *main::qv;
484 ok(!defined(&{"main\::qv"}), "make sure we cleared qv() properly");
485 eval "use lib '.'; use vvv;";
486 ok(defined(&{"main\::qv"}), "make sure we exported qv() properly");
487 isa_ok( qv(1.2), "vvv");
488 unlink 'vvv.pm';
137d6fc0 489}
cb5772bb
RGS
490
4911;