Change 28404 broke the construct s/foo/<<BAR/e. So, try to be more
[p5sagit/p5-mst-13.2.git] / lib / version.t
CommitLineData
129318bd 1#! /usr/local/perl -w
a7ad731c 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 4
5#########################
6
34ba6322 7use Test::More qw(no_plan);
c8a14fb6 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 16}
17
137d6fc0 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 25no warnings 'redefine';
26*::qv = sub { return bless version::qv(shift), __PACKAGE__; };
137d6fc0 27
e0218a61 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 34isa_ok( $testobj, "version::Empty" );
35ok( $testobj->numify == 1.002003, "Numified correctly" );
9137345a 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 40ok( $verobj > $testobj, "Comparison vs parent class" );
41ok( $verobj gt $testobj, "Comparison vs parent class" );
42BaseTests("version::Empty");
43
e0218a61 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 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 66sub BaseTests {
67
c8a14fb6 68 my ($CLASS, $no_qv) = @_;
137d6fc0 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
c8a14fb6 74 diag "tests with bare numbers" if $Verbose;
137d6fc0 75 $version = $CLASS->new(5.005_03);
9137345a 76 is ( "$version" , "5.005030" , '5.005_03 eq 5.5.30' );
137d6fc0 77 $version = $CLASS->new(1.23);
13f8f398 78 is ( "$version" , "1.230" , '1.23 eq "1.230"' );
137d6fc0 79
80 # Test quoted number processing
c8a14fb6 81 diag "tests with quoted numbers" if $Verbose;
137d6fc0 82 $version = $CLASS->new("5.005_03");
c76df65e 83 is ( "$version" , "5.005_030" , '"5.005_03" eq "5.005_030"' );
137d6fc0 84 $version = $CLASS->new("v1.23");
9137345a 85 is ( "$version" , "v1.23.0" , '"v1.23" eq "v1.23.0"' );
137d6fc0 86
87 # Test stringify operator
c8a14fb6 88 diag "tests with stringify" if $Verbose;
137d6fc0 89 $version = $CLASS->new("5.005");
b9381830 90 is ( "$version" , "5.005" , '5.005 eq "5.005"' );
137d6fc0 91 $version = $CLASS->new("5.006.001");
9137345a 92 is ( "$version" , "v5.6.1" , '5.006.001 eq v5.6.1' );
137d6fc0 93 $version = $CLASS->new("1.2.3_4");
9137345a 94 is ( "$version" , "v1.2.3_4" , 'alpha version 1.2.3_4 eq v1.2.3_4' );
137d6fc0 95
96 # test illegal formats
c8a14fb6 97 diag "test illegal formats" if $Verbose;
137d6fc0 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
34ba6322 106 eval {my $version = $CLASS->new("1_2")};
107 like($@, qr/alpha without decimal/,
108 "Invalid version format (alpha without decimal)");
109
cd57dc11 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");
13f8f398 128 ok ("$version" eq "99.000", '$version eq "99.000"');
137d6fc0 129 ok ($version->numify == 99.0, '$version->numify == 99.0');
9137345a 130 ok ($version->normal eq "v99.0.0", '$version->normal eq v99.0.0');
137d6fc0 131
132 $version = $CLASS->new("something");
cd57dc11 133 like($warning, qr/$warnregex/,
134 "Version string contains invalid data; ignoring");
137d6fc0 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
cb5772bb 144 isa_ok ( $version, $CLASS );
137d6fc0 145
146 # Test comparison operators with self
c8a14fb6 147 diag "tests with self" if $Verbose;
137d6fc0 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";
c8a14fb6 155 diag "tests with non-objects" if $Verbose;
137d6fc0 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");
c8a14fb6 166 diag "tests with objects" if $Verbose;
137d6fc0 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";
c8a14fb6 176 diag "numeric tests with non-objects" if $Verbose;
137d6fc0 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);
c8a14fb6 184 diag "numeric tests with objects" if $Verbose;
137d6fc0 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
c8a14fb6 190 diag "numeric tests with numbers" if $Verbose;
137d6fc0 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
c8a14fb6 197 diag "Tests with extended decimal versions" if $Verbose;
137d6fc0 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";
c8a14fb6 209 diag "tests with alpha-style non-objects" if $Verbose;
137d6fc0 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");
c8a14fb6 215 diag "numeric tests with alpha-style non-objects"
216 if $Verbose;
137d6fc0 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");
c8a14fb6 224 diag "tests with alpha-style objects" if $Verbose;
137d6fc0 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");
c8a14fb6 232 diag "tests with alpha-style objects" if $Verbose;
137d6fc0 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
13f8f398 237 $version = $CLASS->new("1.2.3.4");
238 $new_version = $CLASS->new("1.2.3_4");
c8a14fb6 239 diag "tests with alpha-style objects with same subversion"
240 if $Verbose;
137d6fc0 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
c8a14fb6 245 diag "test implicit [in]equality" if $Verbose;
13f8f398 246 $version = $CLASS->new("v1.2.3");
247 $new_version = $CLASS->new("1.2.3.0");
137d6fc0 248 ok ( $version == $new_version, '$version == $new_version' );
13f8f398 249 $new_version = $CLASS->new("1.2.3_0");
137d6fc0 250 ok ( $version == $new_version, '$version == $new_version' );
13f8f398 251 $new_version = $CLASS->new("1.2.3.1");
137d6fc0 252 ok ( $version < $new_version, '$version < $new_version' );
13f8f398 253 $new_version = $CLASS->new("1.2.3_1");
137d6fc0 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
c8a14fb6 259 diag "forbidden operations" if $Verbose;
137d6fc0 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" );
265
c8a14fb6 266SKIP: {
267 skip "version require'd instead of use'd, cannot test qv", 3
268 if defined $no_qv;
137d6fc0 269 # test the qv() sub
c8a14fb6 270 diag "testing qv" if $Verbose;
137d6fc0 271 $version = qv("1.2");
c8a14fb6 272 cmp_ok ( $version, "eq", "v1.2.0", 'qv("1.2") eq "1.2.0"' );
137d6fc0 273 $version = qv(1.2);
c8a14fb6 274 cmp_ok ( $version, "eq", "v1.2.0", 'qv(1.2) eq "1.2.0"' );
43eaf59d 275 isa_ok( qv('5.008'), $CLASS );
c8a14fb6 276}
137d6fc0 277
13f8f398 278 # test creation from existing version object
c8a14fb6 279 diag "create new from existing version" if $Verbose;
cb5772bb 280 ok (eval {$new_version = $CLASS->new($version)},
13f8f398 281 "new from existing object");
9137345a 282 ok ($new_version == $version, "class->new($version) identical");
283 $new_version = $version->new();
c8a14fb6 284 isa_ok ($new_version, $CLASS );
285 is ($new_version, "0.000", "version->new() doesn't clone");
9137345a 286 $new_version = $version->new("1.2.3");
287 is ($new_version, "v1.2.3" , '$version->new("1.2.3") works too');
13f8f398 288
137d6fc0 289 # test the CVS revision mode
c8a14fb6 290 diag "testing CVS Revision" if $Verbose;
cb5772bb 291 $version = new $CLASS qw$Revision: 1.2$;
137d6fc0 292 ok ( $version eq "1.2.0", 'qw$Revision: 1.2$ eq 1.2.0' );
cb5772bb 293 $version = new $CLASS qw$Revision: 1.2.3.4$;
d2397f31 294 ok ( $version eq "1.2.3.4", 'qw$Revision: 1.2.3.4$ eq 1.2.3.4' );
137d6fc0 295
9137345a 296 # test the CPAN style reduced significant digit form
c8a14fb6 297 diag "testing CPAN-style versions" if $Verbose;
9137345a 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
137d6fc0 303 # test reformed UNIVERSAL::VERSION
c8a14fb6 304 diag "Replacement UNIVERSAL::VERSION tests" if $Verbose;
137d6fc0 305
306 # we know this file is here since we require it ourselves
13f8f398 307 $version = $Test::More::VERSION;
137d6fc0 308 eval "use Test::More $version";
b9381830 309 unlike($@, qr/Test::More version $version/,
137d6fc0 310 'Replacement eval works with exact version');
311
c8a14fb6 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;
13f8f398 318 eval "use Test::More $version";
b9381830 319 like($@, qr/Test::More version $version/,
137d6fc0 320 'Replacement eval works with incremented version');
321
b9381830 322 $version =~ s/\.0$//; #convert to string and remove trailing '.0'
137d6fc0 323 chop($version); # shorten by 1 digit, should still succeed
324 eval "use Test::More $version";
b9381830 325 unlike($@, qr/Test::More version $version/,
137d6fc0 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";
b9381830 330 like($@, qr/Test::More version $version/,
137d6fc0 331 'Replacement eval works with incremented digit');
332
c8a14fb6 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';
340 }
341 else {
342 $error_regex = 'xxx defines neither package nor VERSION';
343 }
344
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 is ($versiona, undef, "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: {
c8a14fb6 382 skip 'Cannot test bare v-strings with Perl < 5.8.1', 4
137d6fc0 383 if $] < 5.008_001;
c8a14fb6 384 diag "Tests with v-strings" if $Verbose;
137d6fc0 385 $version = $CLASS->new(1.2.3);
9137345a 386 ok("$version" eq "v1.2.3", '"$version" eq 1.2.3');
137d6fc0 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');
c8a14fb6 391 skip "version require'd instead of use'd, cannot test qv", 1
392 if defined $no_qv;
137d6fc0 393 $version = qv(1.2.3);
9137345a 394 ok("$version" eq "v1.2.3", 'v-string initialized qv()');
137d6fc0 395 }
e0218a61 396
c8a14fb6 397 diag "Tests with real-world (malformed) data" if $Verbose;
e0218a61 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
92dcf8ce 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
c8a14fb6 434SKIP: {
435
436 # dummy up a legal module for testing RT#19017
437 open F, ">www.pm" or die "Cannot open www.pm: $!\n";
438 print F <<"EOF";
439package www;
440use version; \$VERSION = qv('0.0.4');
4411;
442EOF
443 close F;
444
445 eval "use lib '.'; use www 0.000008;";
446 like ($@, qr/^www version 0.000008 \(v0.0.8\) required/,
447 "Make sure very small versions don't freak");
448 eval "use lib '.'; use www 1;";
449 like ($@, qr/^www version 1.000 \(v1.0.0\) required/,
450 "Comparing vs. version with no decimal");
451 eval "use lib '.'; use www 1.;";
452 like ($@, qr/^www version 1.000 \(v1.0.0\) required/,
453 "Comparing ");
454
455 skip 'Cannot "use" extended versions with Perl < 5.6.2', 1
456 if $] < 5.006_002;
457 eval "use lib '.'; use www 0.0.8;";
458 like ($@, qr/^www version 0.000008 \(v0.0.8\) required/,
459 "Make sure very small versions don't freak");
460
461 unlink 'www.pm';
462 }
92dcf8ce 463
464 open F, ">vvv.pm" or die "Cannot open vvv.pm: $!\n";
465 print F <<"EOF";
466package vvv;
467use base qw(version);
4681;
469EOF
470 close F;
471 # need to eliminate any other qv()'s
472 undef *main::qv;
473 ok(!defined(&{"main\::qv"}), "make sure we cleared qv() properly");
474 eval "use lib '.'; use vvv;";
475 ok(defined(&{"main\::qv"}), "make sure we exported qv() properly");
476 isa_ok( qv(1.2), "vvv");
477 unlink 'vvv.pm';
137d6fc0 478}
cb5772bb 479
4801;