Fix a syntax error in test
[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
137d6fc0 60sub BaseTests {
61
c8a14fb6 62 my ($CLASS, $no_qv) = @_;
137d6fc0 63
64 # Insert your test code below, the Test module is use()ed here so read
65 # its man page ( perldoc Test ) for help writing this test script.
66
67 # Test bare number processing
c8a14fb6 68 diag "tests with bare numbers" if $Verbose;
137d6fc0 69 $version = $CLASS->new(5.005_03);
9137345a 70 is ( "$version" , "5.005030" , '5.005_03 eq 5.5.30' );
137d6fc0 71 $version = $CLASS->new(1.23);
13f8f398 72 is ( "$version" , "1.230" , '1.23 eq "1.230"' );
137d6fc0 73
74 # Test quoted number processing
c8a14fb6 75 diag "tests with quoted numbers" if $Verbose;
137d6fc0 76 $version = $CLASS->new("5.005_03");
c76df65e 77 is ( "$version" , "5.005_030" , '"5.005_03" eq "5.005_030"' );
137d6fc0 78 $version = $CLASS->new("v1.23");
9137345a 79 is ( "$version" , "v1.23.0" , '"v1.23" eq "v1.23.0"' );
137d6fc0 80
81 # Test stringify operator
c8a14fb6 82 diag "tests with stringify" if $Verbose;
137d6fc0 83 $version = $CLASS->new("5.005");
b9381830 84 is ( "$version" , "5.005" , '5.005 eq "5.005"' );
137d6fc0 85 $version = $CLASS->new("5.006.001");
9137345a 86 is ( "$version" , "v5.6.1" , '5.006.001 eq v5.6.1' );
137d6fc0 87 $version = $CLASS->new("1.2.3_4");
9137345a 88 is ( "$version" , "v1.2.3_4" , 'alpha version 1.2.3_4 eq v1.2.3_4' );
137d6fc0 89
90 # test illegal formats
c8a14fb6 91 diag "test illegal formats" if $Verbose;
137d6fc0 92 eval {my $version = $CLASS->new("1.2_3_4")};
93 like($@, qr/multiple underscores/,
94 "Invalid version format (multiple underscores)");
95
96 eval {my $version = $CLASS->new("1.2_3.4")};
97 like($@, qr/underscores before decimal/,
98 "Invalid version format (underscores before decimal)");
99
34ba6322 100 eval {my $version = $CLASS->new("1_2")};
101 like($@, qr/alpha without decimal/,
102 "Invalid version format (alpha without decimal)");
103
cd57dc11 104 # for this first test, just upgrade the warn() to die()
105 eval {
106 local $SIG{__WARN__} = sub { die $_[0] };
107 $version = $CLASS->new("1.2b3");
108 };
109 my $warnregex = "Version string '.+' contains invalid data; ".
110 "ignoring: '.+'";
111
112 like($@, qr/$warnregex/,
113 "Version string contains invalid data; ignoring");
114
115 # from here on out capture the warning and test independently
116 my $warning;
117 local $SIG{__WARN__} = sub { $warning = $_[0] };
118 $version = $CLASS->new("99 and 44/100 pure");
119
120 like($warning, qr/$warnregex/,
121 "Version string contains invalid data; ignoring");
13f8f398 122 ok ("$version" eq "99.000", '$version eq "99.000"');
137d6fc0 123 ok ($version->numify == 99.0, '$version->numify == 99.0');
9137345a 124 ok ($version->normal eq "v99.0.0", '$version->normal eq v99.0.0');
137d6fc0 125
126 $version = $CLASS->new("something");
cd57dc11 127 like($warning, qr/$warnregex/,
128 "Version string contains invalid data; ignoring");
137d6fc0 129 ok (defined $version, 'defined $version');
130
131 # reset the test object to something reasonable
132 $version = $CLASS->new("1.2.3");
133
134 # Test boolean operator
135 ok ($version, 'boolean');
136
137 # Test class membership
cb5772bb 138 isa_ok ( $version, $CLASS );
137d6fc0 139
140 # Test comparison operators with self
c8a14fb6 141 diag "tests with self" if $Verbose;
137d6fc0 142 ok ( $version eq $version, '$version eq $version' );
143 is ( $version cmp $version, 0, '$version cmp $version == 0' );
144 ok ( $version == $version, '$version == $version' );
145
146 # test first with non-object
147 $version = $CLASS->new("5.006.001");
148 $new_version = "5.8.0";
c8a14fb6 149 diag "tests with non-objects" if $Verbose;
137d6fc0 150 ok ( $version ne $new_version, '$version ne $new_version' );
151 ok ( $version lt $new_version, '$version lt $new_version' );
152 ok ( $new_version gt $version, '$new_version gt $version' );
153 ok ( ref(\$new_version) eq 'SCALAR', 'no auto-upgrade');
154 $new_version = "$version";
155 ok ( $version eq $new_version, '$version eq $new_version' );
156 ok ( $new_version eq $version, '$new_version eq $version' );
157
158 # now test with existing object
159 $new_version = $CLASS->new("5.8.0");
c8a14fb6 160 diag "tests with objects" if $Verbose;
137d6fc0 161 ok ( $version ne $new_version, '$version ne $new_version' );
162 ok ( $version lt $new_version, '$version lt $new_version' );
163 ok ( $new_version gt $version, '$new_version gt $version' );
164 $new_version = $CLASS->new("$version");
165 ok ( $version eq $new_version, '$version eq $new_version' );
166
167 # Test Numeric Comparison operators
168 # test first with non-object
169 $new_version = "5.8.0";
c8a14fb6 170 diag "numeric tests with non-objects" if $Verbose;
137d6fc0 171 ok ( $version == $version, '$version == $version' );
172 ok ( $version < $new_version, '$version < $new_version' );
173 ok ( $new_version > $version, '$new_version > $version' );
174 ok ( $version != $new_version, '$version != $new_version' );
175
176 # now test with existing object
177 $new_version = $CLASS->new($new_version);
c8a14fb6 178 diag "numeric tests with objects" if $Verbose;
137d6fc0 179 ok ( $version < $new_version, '$version < $new_version' );
180 ok ( $new_version > $version, '$new_version > $version' );
181 ok ( $version != $new_version, '$version != $new_version' );
182
183 # now test with actual numbers
c8a14fb6 184 diag "numeric tests with numbers" if $Verbose;
137d6fc0 185 ok ( $version->numify() == 5.006001, '$version->numify() == 5.006001' );
186 ok ( $version->numify() <= 5.006001, '$version->numify() <= 5.006001' );
187 ok ( $version->numify() < 5.008, '$version->numify() < 5.008' );
188 #ok ( $version->numify() > v5.005_02, '$version->numify() > 5.005_02' );
189
190 # test with long decimals
c8a14fb6 191 diag "Tests with extended decimal versions" if $Verbose;
137d6fc0 192 $version = $CLASS->new(1.002003);
193 ok ( $version eq "1.2.3", '$version eq "1.2.3"');
194 ok ( $version->numify == 1.002003, '$version->numify == 1.002003');
195 $version = $CLASS->new("2002.09.30.1");
196 ok ( $version eq "2002.9.30.1",'$version eq 2002.9.30.1');
197 ok ( $version->numify == 2002.009030001,
198 '$version->numify == 2002.009030001');
199
200 # now test with alpha version form with string
201 $version = $CLASS->new("1.2.3");
202 $new_version = "1.2.3_4";
c8a14fb6 203 diag "tests with alpha-style non-objects" if $Verbose;
137d6fc0 204 ok ( $version lt $new_version, '$version lt $new_version' );
205 ok ( $new_version gt $version, '$new_version gt $version' );
206 ok ( $version ne $new_version, '$version ne $new_version' );
207
208 $version = $CLASS->new("1.2.4");
c8a14fb6 209 diag "numeric tests with alpha-style non-objects"
210 if $Verbose;
137d6fc0 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 # now test with alpha version form with object
216 $version = $CLASS->new("1.2.3");
217 $new_version = $CLASS->new("1.2.3_4");
c8a14fb6 218 diag "tests with alpha-style objects" if $Verbose;
137d6fc0 219 ok ( $version < $new_version, '$version < $new_version' );
220 ok ( $new_version > $version, '$new_version > $version' );
221 ok ( $version != $new_version, '$version != $new_version' );
222 ok ( !$version->is_alpha, '!$version->is_alpha');
223 ok ( $new_version->is_alpha, '$new_version->is_alpha');
224
225 $version = $CLASS->new("1.2.4");
c8a14fb6 226 diag "tests with alpha-style objects" if $Verbose;
137d6fc0 227 ok ( $version > $new_version, '$version > $new_version' );
228 ok ( $new_version < $version, '$new_version < $version' );
229 ok ( $version != $new_version, '$version != $new_version' );
230
13f8f398 231 $version = $CLASS->new("1.2.3.4");
232 $new_version = $CLASS->new("1.2.3_4");
c8a14fb6 233 diag "tests with alpha-style objects with same subversion"
234 if $Verbose;
137d6fc0 235 ok ( $version > $new_version, '$version > $new_version' );
236 ok ( $new_version < $version, '$new_version < $version' );
237 ok ( $version != $new_version, '$version != $new_version' );
238
c8a14fb6 239 diag "test implicit [in]equality" if $Verbose;
13f8f398 240 $version = $CLASS->new("v1.2.3");
241 $new_version = $CLASS->new("1.2.3.0");
137d6fc0 242 ok ( $version == $new_version, '$version == $new_version' );
13f8f398 243 $new_version = $CLASS->new("1.2.3_0");
137d6fc0 244 ok ( $version == $new_version, '$version == $new_version' );
13f8f398 245 $new_version = $CLASS->new("1.2.3.1");
137d6fc0 246 ok ( $version < $new_version, '$version < $new_version' );
13f8f398 247 $new_version = $CLASS->new("1.2.3_1");
137d6fc0 248 ok ( $version < $new_version, '$version < $new_version' );
249 $new_version = $CLASS->new("1.1.999");
250 ok ( $version > $new_version, '$version > $new_version' );
251
252 # that which is not expressly permitted is forbidden
c8a14fb6 253 diag "forbidden operations" if $Verbose;
137d6fc0 254 ok ( !eval { ++$version }, "noop ++" );
255 ok ( !eval { --$version }, "noop --" );
256 ok ( !eval { $version/1 }, "noop /" );
257 ok ( !eval { $version*3 }, "noop *" );
258 ok ( !eval { abs($version) }, "noop abs" );
259
c8a14fb6 260SKIP: {
261 skip "version require'd instead of use'd, cannot test qv", 3
262 if defined $no_qv;
137d6fc0 263 # test the qv() sub
c8a14fb6 264 diag "testing qv" if $Verbose;
137d6fc0 265 $version = qv("1.2");
c8a14fb6 266 cmp_ok ( $version, "eq", "v1.2.0", 'qv("1.2") eq "1.2.0"' );
137d6fc0 267 $version = qv(1.2);
c8a14fb6 268 cmp_ok ( $version, "eq", "v1.2.0", 'qv(1.2) eq "1.2.0"' );
43eaf59d 269 isa_ok( qv('5.008'), $CLASS );
c8a14fb6 270}
137d6fc0 271
13f8f398 272 # test creation from existing version object
c8a14fb6 273 diag "create new from existing version" if $Verbose;
cb5772bb 274 ok (eval {$new_version = $CLASS->new($version)},
13f8f398 275 "new from existing object");
9137345a 276 ok ($new_version == $version, "class->new($version) identical");
277 $new_version = $version->new();
c8a14fb6 278 isa_ok ($new_version, $CLASS );
279 is ($new_version, "0.000", "version->new() doesn't clone");
9137345a 280 $new_version = $version->new("1.2.3");
281 is ($new_version, "v1.2.3" , '$version->new("1.2.3") works too');
13f8f398 282
137d6fc0 283 # test the CVS revision mode
c8a14fb6 284 diag "testing CVS Revision" if $Verbose;
cb5772bb 285 $version = new $CLASS qw$Revision: 1.2$;
137d6fc0 286 ok ( $version eq "1.2.0", 'qw$Revision: 1.2$ eq 1.2.0' );
cb5772bb 287 $version = new $CLASS qw$Revision: 1.2.3.4$;
d2397f31 288 ok ( $version eq "1.2.3.4", 'qw$Revision: 1.2.3.4$ eq 1.2.3.4' );
137d6fc0 289
9137345a 290 # test the CPAN style reduced significant digit form
c8a14fb6 291 diag "testing CPAN-style versions" if $Verbose;
9137345a 292 $version = $CLASS->new("1.23_01");
293 is ( "$version" , "1.23_0100", "CPAN-style alpha version" );
294 ok ( $version > 1.23, "1.23_01 > 1.23");
295 ok ( $version < 1.24, "1.23_01 < 1.24");
296
137d6fc0 297 # test reformed UNIVERSAL::VERSION
c8a14fb6 298 diag "Replacement UNIVERSAL::VERSION tests" if $Verbose;
137d6fc0 299
300 # we know this file is here since we require it ourselves
13f8f398 301 $version = $Test::More::VERSION;
137d6fc0 302 eval "use Test::More $version";
b9381830 303 unlike($@, qr/Test::More version $version/,
137d6fc0 304 'Replacement eval works with exact version');
305
c8a14fb6 306 # test as class method
307 $new_version = Test::More->VERSION;
308 cmp_ok($new_version,'cmp',$version, "Called as class method");
309
310 # this should fail even with old UNIVERSAL::VERSION
311 $version = $Test::More::VERSION+0.01;
13f8f398 312 eval "use Test::More $version";
b9381830 313 like($@, qr/Test::More version $version/,
137d6fc0 314 'Replacement eval works with incremented version');
315
b9381830 316 $version =~ s/\.0$//; #convert to string and remove trailing '.0'
137d6fc0 317 chop($version); # shorten by 1 digit, should still succeed
318 eval "use Test::More $version";
b9381830 319 unlike($@, qr/Test::More version $version/,
137d6fc0 320 'Replacement eval works with single digit');
321
322 $version += 0.1; # this would fail with old UNIVERSAL::VERSION
323 eval "use Test::More $version";
b9381830 324 like($@, qr/Test::More version $version/,
137d6fc0 325 'Replacement eval works with incremented digit');
326
c8a14fb6 327 { # dummy up some variously broken modules for testing
328 open F, ">xxx.pm" or die "Cannot open xxx.pm: $!\n";
329 print F "1;\n";
330 close F;
331 my $error_regex;
332 if ( $] < 5.008 ) {
333 $error_regex = 'xxx does not define \$xxx::VERSION';
334 }
335 else {
336 $error_regex = 'xxx defines neither package nor VERSION';
337 }
338
339 eval "use lib '.'; use xxx 3;";
340 like ($@, qr/$error_regex/,
341 'Replacement handles modules without package or VERSION');
342 eval "use lib '.'; use xxx; $version = xxx->VERSION";
343 unlike ($@, qr/$error_regex/,
344 'Replacement handles modules without package or VERSION');
345 is ($versiona, undef, "Called as class method");
346 unlink 'xxx.pm';
347 }
348
349 { # dummy up some variously broken modules for testing
350 open F, ">yyy.pm" or die "Cannot open yyy.pm: $!\n";
351 print F "package yyy;\n#look ma no VERSION\n1;\n";
352 close F;
353 eval "use lib '.'; use yyy 3;";
354 like ($@, qr/^yyy does not define \$yyy::VERSION/,
355 'Replacement handles modules without VERSION');
356 eval "use lib '.'; use yyy; print yyy->VERSION";
357 unlike ($@, qr/^yyy does not define \$yyy::VERSION/,
358 'Replacement handles modules without VERSION');
359 unlink 'yyy.pm';
360 }
361
362 { # dummy up some variously broken modules for testing
363 open F, ">zzz.pm" or die "Cannot open zzz.pm: $!\n";
364 print F "package zzz;\n\@VERSION = ();\n1;\n";
365 close F;
366 eval "use lib '.'; use zzz 3;";
367 like ($@, qr/^zzz does not define \$zzz::VERSION/,
368 'Replacement handles modules without VERSION');
369 eval "use lib '.'; use zzz; print zzz->VERSION";
370 unlike ($@, qr/^zzz does not define \$zzz::VERSION/,
371 'Replacement handles modules without VERSION');
372 unlink 'zzz.pm';
373 }
374
137d6fc0 375SKIP: {
c8a14fb6 376 skip 'Cannot test bare v-strings with Perl < 5.8.1', 4
137d6fc0 377 if $] < 5.008_001;
c8a14fb6 378 diag "Tests with v-strings" if $Verbose;
137d6fc0 379 $version = $CLASS->new(1.2.3);
9137345a 380 ok("$version" eq "v1.2.3", '"$version" eq 1.2.3');
137d6fc0 381 $version = $CLASS->new(1.0.0);
382 $new_version = $CLASS->new(1);
383 ok($version == $new_version, '$version == $new_version');
384 ok($version eq $new_version, '$version eq $new_version');
c8a14fb6 385 skip "version require'd instead of use'd, cannot test qv", 1
386 if defined $no_qv;
137d6fc0 387 $version = qv(1.2.3);
9137345a 388 ok("$version" eq "v1.2.3", 'v-string initialized qv()');
137d6fc0 389 }
e0218a61 390
c8a14fb6 391 diag "Tests with real-world (malformed) data" if $Verbose;
e0218a61 392
393 # trailing zero testing (reported by Andreas Koenig).
394 $version = $CLASS->new("1");
395 ok($version->numify eq "1.000", "trailing zeros preserved");
396 $version = $CLASS->new("1.0");
397 ok($version->numify eq "1.000", "trailing zeros preserved");
398 $version = $CLASS->new("1.0.0");
399 ok($version->numify eq "1.000000", "trailing zeros preserved");
400 $version = $CLASS->new("1.0.0.0");
401 ok($version->numify eq "1.000000000", "trailing zeros preserved");
402
403 # leading zero testing (reported by Andreas Koenig).
404 $version = $CLASS->new(".7");
405 ok($version->numify eq "0.700", "leading zero inferred");
406
407 # leading space testing (reported by Andreas Koenig).
408 $version = $CLASS->new(" 1.7");
409 ok($version->numify eq "1.700", "leading space ignored");
410
c8a14fb6 411SKIP: {
412
413 # dummy up a legal module for testing RT#19017
414 open F, ">www.pm" or die "Cannot open www.pm: $!\n";
415 print F <<"EOF";
416package www;
417use version; \$VERSION = qv('0.0.4');
4181;
419EOF
420 close F;
421
422 eval "use lib '.'; use www 0.000008;";
423 like ($@, qr/^www version 0.000008 \(v0.0.8\) required/,
424 "Make sure very small versions don't freak");
425 eval "use lib '.'; use www 1;";
426 like ($@, qr/^www version 1.000 \(v1.0.0\) required/,
427 "Comparing vs. version with no decimal");
428 eval "use lib '.'; use www 1.;";
429 like ($@, qr/^www version 1.000 \(v1.0.0\) required/,
430 "Comparing ");
431
432 skip 'Cannot "use" extended versions with Perl < 5.6.2', 1
433 if $] < 5.006_002;
434 eval "use lib '.'; use www 0.0.8;";
435 like ($@, qr/^www version 0.000008 \(v0.0.8\) required/,
436 "Make sure very small versions don't freak");
437
438 unlink 'www.pm';
439 }
137d6fc0 440}
cb5772bb 441
4421;