This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
pod punctuation nit in vmsish
[perl5.git] / lib / version.t
CommitLineData
129318bd 1#! /usr/local/perl -w
a7ad731c 2
34ba6322 3use Test::More qw(no_plan);
72287d96 4use Data::Dumper;
c8a14fb6
RGS
5require Test::Harness;
6no warnings 'once';
7*Verbose = \$Test::Harness::Verbose;
f941e658
JP
8use POSIX qw/locale_h/;
9use File::Temp qw/tempfile/;
10use File::Basename;
a7ad731c 11
5eb567df 12BEGIN {
f941e658
JP
13 use_ok("version", 0.77);
14 # If we made it this far, we are ok.
5eb567df
RGS
15}
16
f941e658
JP
17my $Verbose;
18
19diag "Tests with base class" unless $ENV{PERL_CORE};
137d6fc0 20
f941e658
JP
21BaseTests("version","new","qv");
22BaseTests("version","new","declare");
23BaseTests("version","parse", "qv");
24BaseTests("version","parse", "declare");
137d6fc0 25
98dc9551 26# dummy up a redundant call to satisfy David Wheeler
f941e658
JP
27local $SIG{__WARN__} = sub { die $_[0] };
28eval 'use version;';
29unlike ($@, qr/^Subroutine main::declare redefined/,
30 "Only export declare once per package (to prevent redefined warnings).");
137d6fc0 31
e0218a61 32package version::Bad;
f941e658 33use base 'version';
e0218a61
JP
34sub new { my($self,$n)=@_; bless \$n, $self }
35
137d6fc0 36package main;
f941e658
JP
37
38my $warning;
39local $SIG{__WARN__} = sub { $warning = $_[0] };
40my ($fh, $filename) = tempfile('tXXXXXXX', SUFFIX => '.pm', UNLINK => 1);
41(my $package = basename($filename)) =~ s/\.pm$//;
42print $fh <<"EOF";
43# This is an empty subclass
44package $package;
45use base 'version';
46use vars '\$VERSION';
47\$VERSION=0.001;
48EOF
49close $fh;
50
51sub main_reset {
52 delete $main::INC{'$package'};
53 undef &qv; undef *::qv; # avoid 'used once' warning
54 undef &declare; undef *::declare; # avoid 'used once' warning
55}
56
57diag "Tests with empty derived class" unless $ENV{PERL_CORE};
58
59use_ok($package, 0.001);
60my $testobj = $package->new(1.002_003);
61isa_ok( $testobj, $package );
137d6fc0 62ok( $testobj->numify == 1.002003, "Numified correctly" );
9137345a
JP
63ok( $testobj->stringify eq "1.002003", "Stringified correctly" );
64ok( $testobj->normal eq "v1.2.3", "Normalified correctly" );
137d6fc0 65
f941e658 66my $verobj = version::->new("1.2.4");
137d6fc0 67ok( $verobj > $testobj, "Comparison vs parent class" );
137d6fc0 68
f941e658
JP
69BaseTests($package, "new", "qv");
70main_reset;
71use_ok($package, 0.001, "declare");
72BaseTests($package, "new", "declare");
73main_reset;
74use_ok($package, 0.001);
75BaseTests($package, "parse", "qv");
76main_reset;
77use_ok($package, 0.001, "declare");
78BaseTests($package, "parse", "declare");
79
80diag "tests with bad subclass" unless $ENV{PERL_CORE};
e0218a61
JP
81$testobj = version::Bad->new(1.002_003);
82isa_ok( $testobj, "version::Bad" );
83eval { my $string = $testobj->numify };
84like($@, qr/Invalid version object/,
85 "Bad subclass numify");
86eval { my $string = $testobj->normal };
87like($@, qr/Invalid version object/,
88 "Bad subclass normal");
89eval { my $string = $testobj->stringify };
90like($@, qr/Invalid version object/,
91 "Bad subclass stringify");
f941e658 92eval { my $test = ($testobj > 1.0) };
e0218a61
JP
93like($@, qr/Invalid version object/,
94 "Bad subclass vcmp");
5de8bffd
DG
95
96# Invalid structure
97eval { $a = \\version->new(1); bless $a, "version"; print "# $a\n" };
98like($@, qr/Invalid version object/,
99 "Bad internal structure (RT#78286)");
9b463b21
DG
100
101# do strict lax tests in a sub to isolate a package to test importing
5de8bffd
DG
102strict_lax_tests();
103
9b463b21
DG
104sub strict_lax_tests {
105 package temp12345;
106 # copied from perl core test t/op/packagev.t
107 # format: STRING STRICT_OK LAX_OK
108 my $strict_lax_data = << 'CASE_DATA';
1091.00 pass pass
1101.00001 pass pass
1110.123 pass pass
11212.345 pass pass
11342 pass pass
1140 pass pass
1150.0 pass pass
116v1.2.3 pass pass
117v1.2.3.4 pass pass
118v0.1.2 pass pass
119v0.0.0 pass pass
12001 fail pass
12101.0203 fail pass
122v01 fail pass
123v01.02.03 fail pass
124.1 fail pass
125.1.2 fail pass
1261. fail pass
1271.a fail fail
1281._ fail fail
1291.02_03 fail pass
130v1.2_3 fail pass
131v1.02_03 fail pass
132v1.2_3_4 fail fail
133v1.2_3.4 fail fail
1341.2_3.4 fail fail
1350_ fail fail
1361_ fail fail
1371_. fail fail
1381.1_ fail fail
1391.02_03_04 fail fail
1401.2.3 fail pass
141v1.2 fail pass
142v0 fail pass
143v1 fail pass
144v.1.2.3 fail fail
145v fail fail
146v1.2345.6 fail pass
147undef fail pass
1481a fail fail
1491.2a3 fail fail
150bar fail fail
151_ fail fail
152CASE_DATA
153
154 require version;
155 version->import( qw/is_strict is_lax/ );
156 for my $case ( split qr/\n/, $strict_lax_data ) {
157 my ($v, $strict, $lax) = split qr/\t+/, $case;
158 main::ok( $strict eq 'pass' ? is_strict($v) : ! is_strict($v), "is_strict($v) [$strict]" );
159 main::ok( $strict eq 'pass' ? version::is_strict($v) : ! version::is_strict($v), "version::is_strict($v) [$strict]" );
160 main::ok( $lax eq 'pass' ? is_lax($v) : ! is_lax($v), "is_lax($v) [$lax]" );
161 main::ok( $lax eq 'pass' ? version::is_lax($v) : ! version::is_lax($v), "version::is_lax($v) [$lax]" );
162 }
163}
e0218a61 164
137d6fc0
JP
165sub BaseTests {
166
f941e658 167 my ($CLASS, $method, $qv_declare) = @_;
692a467c
JP
168 my $warning;
169 local $SIG{__WARN__} = sub { $warning = $_[0] };
317f7c8a
RGS
170
171 # Insert your test code below, the Test module is use()ed here so read
172 # its man page ( perldoc Test ) for help writing this test script.
173
174 # Test bare number processing
f941e658
JP
175 diag "tests with bare numbers" unless $ENV{PERL_CORE};
176 $version = $CLASS->$method(5.005_03);
8cb289bd 177 is ( "$version" , "5.00503" , '5.005_03 eq 5.00503' );
f941e658 178 $version = $CLASS->$method(1.23);
8cb289bd 179 is ( "$version" , "1.23" , '1.23 eq "1.23"' );
317f7c8a
RGS
180
181 # Test quoted number processing
f941e658
JP
182 diag "tests with quoted numbers" unless $ENV{PERL_CORE};
183 $version = $CLASS->$method("5.005_03");
8cb289bd 184 is ( "$version" , "5.005_03" , '"5.005_03" eq "5.005_03"' );
f941e658 185 $version = $CLASS->$method("v1.23");
8cb289bd 186 is ( "$version" , "v1.23" , '"v1.23" eq "v1.23"' );
317f7c8a
RGS
187
188 # Test stringify operator
f941e658
JP
189 diag "tests with stringify" unless $ENV{PERL_CORE};
190 $version = $CLASS->$method("5.005");
317f7c8a 191 is ( "$version" , "5.005" , '5.005 eq "5.005"' );
f941e658 192 $version = $CLASS->$method("5.006.001");
8cb289bd 193 is ( "$version" , "5.006.001" , '5.006.001 eq v5.6.1' );
692a467c
JP
194 unlike ($warning, qr/v-string without leading 'v' deprecated/, 'No leading v');
195 $version = $CLASS->$method("v1.2.3_4");
196 is ( "$version" , "v1.2.3_4" , 'alpha version 1.2.3_4 eq v1.2.3_4' );
317f7c8a
RGS
197
198 # test illegal formats
f941e658 199 diag "test illegal formats" unless $ENV{PERL_CORE};
d54f8cf7 200 eval {my $version = $CLASS->$method("1.2_3_4")};
317f7c8a
RGS
201 like($@, qr/multiple underscores/,
202 "Invalid version format (multiple underscores)");
203
d54f8cf7 204 eval {my $version = $CLASS->$method("1.2_3.4")};
317f7c8a
RGS
205 like($@, qr/underscores before decimal/,
206 "Invalid version format (underscores before decimal)");
207
d54f8cf7 208 eval {my $version = $CLASS->$method("1_2")};
317f7c8a
RGS
209 like($@, qr/alpha without decimal/,
210 "Invalid version format (alpha without decimal)");
211
91152fc1
DG
212 eval { $version = $CLASS->$method("1.2b3")};
213 like($@, qr/non-numeric data/,
214 "Invalid version format (non-numeric data)");
317f7c8a
RGS
215
216 # from here on out capture the warning and test independently
f34c6aaf 217 {
91152fc1 218 eval{$version = $CLASS->$method("99 and 44/100 pure")};
317f7c8a 219
91152fc1
DG
220 like($@, qr/non-numeric data/,
221 "Invalid version format (non-numeric data)");
317f7c8a 222
91152fc1
DG
223 eval{$version = $CLASS->$method("something")};
224 like($@, qr/non-numeric data/,
225 "Invalid version format (non-numeric data)");
317f7c8a
RGS
226
227 # reset the test object to something reasonable
f941e658 228 $version = $CLASS->$method("1.2.3");
317f7c8a
RGS
229
230 # Test boolean operator
231 ok ($version, 'boolean');
232
233 # Test class membership
234 isa_ok ( $version, $CLASS );
235
236 # Test comparison operators with self
f941e658 237 diag "tests with self" unless $ENV{PERL_CORE};
8cb289bd 238 is ( $version <=> $version, 0, '$version <=> $version == 0' );
317f7c8a
RGS
239 ok ( $version == $version, '$version == $version' );
240
317f7c8a
RGS
241 # Test Numeric Comparison operators
242 # test first with non-object
f941e658 243 $version = $CLASS->$method("5.006.001");
317f7c8a 244 $new_version = "5.8.0";
f941e658 245 diag "numeric tests with non-objects" unless $ENV{PERL_CORE};
317f7c8a
RGS
246 ok ( $version == $version, '$version == $version' );
247 ok ( $version < $new_version, '$version < $new_version' );
248 ok ( $new_version > $version, '$new_version > $version' );
249 ok ( $version != $new_version, '$version != $new_version' );
250
251 # now test with existing object
f941e658
JP
252 $new_version = $CLASS->$method($new_version);
253 diag "numeric tests with objects" unless $ENV{PERL_CORE};
317f7c8a
RGS
254 ok ( $version < $new_version, '$version < $new_version' );
255 ok ( $new_version > $version, '$new_version > $version' );
256 ok ( $version != $new_version, '$version != $new_version' );
257
258 # now test with actual numbers
f941e658 259 diag "numeric tests with numbers" unless $ENV{PERL_CORE};
317f7c8a
RGS
260 ok ( $version->numify() == 5.006001, '$version->numify() == 5.006001' );
261 ok ( $version->numify() <= 5.006001, '$version->numify() <= 5.006001' );
262 ok ( $version->numify() < 5.008, '$version->numify() < 5.008' );
263 #ok ( $version->numify() > v5.005_02, '$version->numify() > 5.005_02' );
264
265 # test with long decimals
f941e658
JP
266 diag "Tests with extended decimal versions" unless $ENV{PERL_CORE};
267 $version = $CLASS->$method(1.002003);
8cb289bd 268 ok ( $version == "1.2.3", '$version == "1.2.3"');
317f7c8a 269 ok ( $version->numify == 1.002003, '$version->numify == 1.002003');
f941e658 270 $version = $CLASS->$method("2002.09.30.1");
8cb289bd 271 ok ( $version == "2002.9.30.1",'$version == 2002.9.30.1');
317f7c8a
RGS
272 ok ( $version->numify == 2002.009030001,
273 '$version->numify == 2002.009030001');
274
275 # now test with alpha version form with string
f941e658 276 $version = $CLASS->$method("1.2.3");
317f7c8a 277 $new_version = "1.2.3_4";
f941e658 278 diag "numeric tests with alpha-style non-objects" unless $ENV{PERL_CORE};
8cb289bd
RGS
279 ok ( $version < $new_version, '$version < $new_version' );
280 ok ( $new_version > $version, '$new_version > $version' );
281 ok ( $version != $new_version, '$version != $new_version' );
317f7c8a 282
f941e658 283 $version = $CLASS->$method("1.2.4");
317f7c8a 284 diag "numeric tests with alpha-style non-objects"
692a467c 285 unless $ENV{PERL_CORE};
317f7c8a
RGS
286 ok ( $version > $new_version, '$version > $new_version' );
287 ok ( $new_version < $version, '$new_version < $version' );
288 ok ( $version != $new_version, '$version != $new_version' );
289
290 # now test with alpha version form with object
f941e658
JP
291 $version = $CLASS->$method("1.2.3");
292 $new_version = $CLASS->$method("1.2.3_4");
293 diag "tests with alpha-style objects" unless $ENV{PERL_CORE};
317f7c8a
RGS
294 ok ( $version < $new_version, '$version < $new_version' );
295 ok ( $new_version > $version, '$new_version > $version' );
296 ok ( $version != $new_version, '$version != $new_version' );
297 ok ( !$version->is_alpha, '!$version->is_alpha');
298 ok ( $new_version->is_alpha, '$new_version->is_alpha');
299
f941e658
JP
300 $version = $CLASS->$method("1.2.4");
301 diag "tests with alpha-style objects" unless $ENV{PERL_CORE};
317f7c8a
RGS
302 ok ( $version > $new_version, '$version > $new_version' );
303 ok ( $new_version < $version, '$new_version < $version' );
304 ok ( $version != $new_version, '$version != $new_version' );
305
f941e658
JP
306 $version = $CLASS->$method("1.2.3.4");
307 $new_version = $CLASS->$method("1.2.3_4");
317f7c8a 308 diag "tests with alpha-style objects with same subversion"
692a467c 309 unless $ENV{PERL_CORE};
317f7c8a
RGS
310 ok ( $version > $new_version, '$version > $new_version' );
311 ok ( $new_version < $version, '$new_version < $version' );
312 ok ( $version != $new_version, '$version != $new_version' );
313
f941e658
JP
314 diag "test implicit [in]equality" unless $ENV{PERL_CORE};
315 $version = $CLASS->$method("v1.2.3");
316 $new_version = $CLASS->$method("1.2.3.0");
317f7c8a 317 ok ( $version == $new_version, '$version == $new_version' );
f941e658 318 $new_version = $CLASS->$method("1.2.3_0");
317f7c8a 319 ok ( $version == $new_version, '$version == $new_version' );
f941e658 320 $new_version = $CLASS->$method("1.2.3.1");
317f7c8a 321 ok ( $version < $new_version, '$version < $new_version' );
f941e658 322 $new_version = $CLASS->$method("1.2.3_1");
317f7c8a 323 ok ( $version < $new_version, '$version < $new_version' );
f941e658 324 $new_version = $CLASS->$method("1.1.999");
317f7c8a
RGS
325 ok ( $version > $new_version, '$version > $new_version' );
326
327 # that which is not expressly permitted is forbidden
f941e658 328 diag "forbidden operations" unless $ENV{PERL_CORE};
317f7c8a
RGS
329 ok ( !eval { ++$version }, "noop ++" );
330 ok ( !eval { --$version }, "noop --" );
331 ok ( !eval { $version/1 }, "noop /" );
332 ok ( !eval { $version*3 }, "noop *" );
333 ok ( !eval { abs($version) }, "noop abs" );
137d6fc0 334
c8a14fb6 335SKIP: {
f941e658
JP
336 skip "version require'd instead of use'd, cannot test $qv_declare", 3
337 unless defined $qv_declare;
338 # test the $qv_declare() sub
339 diag "testing $qv_declare" unless $ENV{PERL_CORE};
340 $version = $CLASS->$qv_declare("1.2");
341 is ( "$version", "v1.2", $qv_declare.'("1.2") == "1.2.0"' );
342 $version = $CLASS->$qv_declare(1.2);
343 is ( "$version", "v1.2", $qv_declare.'(1.2) == "1.2.0"' );
344 isa_ok( $CLASS->$qv_declare('5.008'), $CLASS );
c8a14fb6 345}
137d6fc0 346
317f7c8a 347 # test creation from existing version object
f941e658
JP
348 diag "create new from existing version" unless $ENV{PERL_CORE};
349 ok (eval {$new_version = $CLASS->$method($version)},
317f7c8a 350 "new from existing object");
f941e658 351 ok ($new_version == $version, "class->$method($version) identical");
d54f8cf7 352 $new_version = $version->$method(0);
317f7c8a 353 isa_ok ($new_version, $CLASS );
f941e658
JP
354 is ($new_version, "0", "version->$method() doesn't clone");
355 $new_version = $version->$method("1.2.3");
356 is ($new_version, "1.2.3" , '$version->$method("1.2.3") works too');
317f7c8a
RGS
357
358 # test the CVS revision mode
f941e658 359 diag "testing CVS Revision" unless $ENV{PERL_CORE};
317f7c8a 360 $version = new $CLASS qw$Revision: 1.2$;
8cb289bd 361 ok ( $version == "1.2.0", 'qw$Revision: 1.2$ == 1.2.0' );
317f7c8a 362 $version = new $CLASS qw$Revision: 1.2.3.4$;
8cb289bd 363 ok ( $version == "1.2.3.4", 'qw$Revision: 1.2.3.4$ == 1.2.3.4' );
317f7c8a
RGS
364
365 # test the CPAN style reduced significant digit form
f941e658
JP
366 diag "testing CPAN-style versions" unless $ENV{PERL_CORE};
367 $version = $CLASS->$method("1.23_01");
8cb289bd 368 is ( "$version" , "1.23_01", "CPAN-style alpha version" );
317f7c8a
RGS
369 ok ( $version > 1.23, "1.23_01 > 1.23");
370 ok ( $version < 1.24, "1.23_01 < 1.24");
371
372 # test reformed UNIVERSAL::VERSION
f941e658 373 diag "Replacement UNIVERSAL::VERSION tests" unless $ENV{PERL_CORE};
f34c6aaf
JP
374
375 my $error_regex = $] < 5.006
376 ? 'version \d required'
f941e658 377 : 'does not define \$t.{7}::VERSION';
317f7c8a 378
f34c6aaf 379 {
f941e658
JP
380 my ($fh, $filename) = tempfile('tXXXXXXX', SUFFIX => '.pm', UNLINK => 1);
381 (my $package = basename($filename)) =~ s/\.pm$//;
382 print $fh "package $package;\n\$$package\::VERSION=0.58;\n1;\n";
383 close $fh;
f34c6aaf 384
8cb289bd 385 $version = 0.58;
f941e658
JP
386 eval "use lib '.'; use $package $version";
387 unlike($@, qr/$package version $version/,
f34c6aaf
JP
388 'Replacement eval works with exact version');
389
390 # test as class method
f941e658 391 $new_version = $package->VERSION;
8cb289bd 392 cmp_ok($new_version,'==',$version, "Called as class method");
8dd04980 393
f34c6aaf
JP
394 eval "print Completely::Unknown::Module->VERSION";
395 if ( $] < 5.008 ) {
396 unlike($@, qr/$error_regex/,
397 "Don't freak if the module doesn't even exist");
398 }
399 else {
400 unlike($@, qr/defines neither package nor VERSION/,
401 "Don't freak if the module doesn't even exist");
402 }
403
404 # this should fail even with old UNIVERSAL::VERSION
8cb289bd 405 $version += 0.01;
f941e658
JP
406 eval "use lib '.'; use $package $version";
407 like($@, qr/$package version $version/,
f34c6aaf
JP
408 'Replacement eval works with incremented version');
409
410 $version =~ s/0+$//; #convert to string and remove trailing 0's
411 chop($version); # shorten by 1 digit, should still succeed
f941e658
JP
412 eval "use lib '.'; use $package $version";
413 unlike($@, qr/$package version $version/,
f34c6aaf
JP
414 'Replacement eval works with single digit');
415
416 # this would fail with old UNIVERSAL::VERSION
8cb289bd 417 $version += 0.1;
f941e658
JP
418 eval "use lib '.'; use $package $version";
419 like($@, qr/$package version $version/,
f34c6aaf 420 'Replacement eval works with incremented digit');
f941e658 421 unlink $filename;
f34c6aaf 422 }
317f7c8a
RGS
423
424 { # dummy up some variously broken modules for testing
f941e658
JP
425 my ($fh, $filename) = tempfile('tXXXXXXX', SUFFIX => '.pm', UNLINK => 1);
426 (my $package = basename($filename)) =~ s/\.pm$//;
427 print $fh "1;\n";
428 close $fh;
f34c6aaf 429
f941e658 430 eval "use lib '.'; use $package 3;";
317f7c8a 431 if ( $] < 5.008 ) {
f34c6aaf
JP
432 like($@, qr/$error_regex/,
433 'Replacement handles modules without package or VERSION');
c8a14fb6 434 }
317f7c8a 435 else {
f34c6aaf
JP
436 like($@, qr/defines neither package nor VERSION/,
437 'Replacement handles modules without package or VERSION');
c8a14fb6 438 }
f941e658 439 eval "use lib '.'; use $package; \$version = $package->VERSION";
317f7c8a
RGS
440 unlike ($@, qr/$error_regex/,
441 'Replacement handles modules without package or VERSION');
f34c6aaf 442 ok (!defined($version), "Called as class method");
f941e658 443 unlink $filename;
317f7c8a
RGS
444 }
445
446 { # dummy up some variously broken modules for testing
f941e658
JP
447 my ($fh, $filename) = tempfile('tXXXXXXX', SUFFIX => '.pm', UNLINK => 1);
448 (my $package = basename($filename)) =~ s/\.pm$//;
449 print $fh "package $package;\n#look ma no VERSION\n1;\n";
450 close $fh;
451 eval "use lib '.'; use $package 3;";
f34c6aaf 452 like ($@, qr/$error_regex/,
317f7c8a 453 'Replacement handles modules without VERSION');
f941e658 454 eval "use lib '.'; use $package; print $package->VERSION";
f34c6aaf 455 unlike ($@, qr/$error_regex/,
317f7c8a 456 'Replacement handles modules without VERSION');
f941e658 457 unlink $filename;
317f7c8a
RGS
458 }
459
460 { # dummy up some variously broken modules for testing
f941e658
JP
461 my ($fh, $filename) = tempfile('tXXXXXXX', SUFFIX => '.pm', UNLINK => 1);
462 (my $package = basename($filename)) =~ s/\.pm$//;
463 print $fh "package $package;\n\@VERSION = ();\n1;\n";
464 close $fh;
465 eval "use lib '.'; use $package 3;";
f34c6aaf 466 like ($@, qr/$error_regex/,
317f7c8a 467 'Replacement handles modules without VERSION');
f941e658 468 eval "use lib '.'; use $package; print $package->VERSION";
f34c6aaf 469 unlike ($@, qr/$error_regex/,
317f7c8a 470 'Replacement handles modules without VERSION');
f941e658 471 unlink $filename;
317f7c8a
RGS
472 }
473
137d6fc0 474SKIP: {
ac0e6a2f
RGS
475 skip 'Cannot test bare v-strings with Perl < 5.6.0', 4
476 if $] < 5.006_000;
f941e658
JP
477 diag "Tests with v-strings" unless $ENV{PERL_CORE};
478 $version = $CLASS->$method(1.2.3);
d54f8cf7 479 ok("$version" eq "v1.2.3", '"$version" eq 1.2.3');
f941e658
JP
480 $version = $CLASS->$method(1.0.0);
481 $new_version = $CLASS->$method(1);
317f7c8a 482 ok($version == $new_version, '$version == $new_version');
f941e658
JP
483 skip "version require'd instead of use'd, cannot test declare", 1
484 unless defined $qv_declare;
485 $version = &$qv_declare(1.2.3);
d54f8cf7
JP
486 ok("$version" eq "v1.2.3", 'v-string initialized $qv_declare()');
487 }
488
489SKIP: {
490 skip 'Cannot test bare alpha v-strings with Perl < 5.8.1', 2
491 if $] lt 5.008_001;
492 diag "Tests with bare alpha v-strings" unless $ENV{PERL_CORE};
493 $version = $CLASS->$method(v1.2.3_4);
494 is($version, "v1.2.3_4", '"$version" eq "v1.2.3_4"');
495 $version = $CLASS->$method(eval "v1.2.3_4");
496 is($version, "v1.2.3_4", '"$version" eq "v1.2.3_4" (from eval)');
317f7c8a
RGS
497 }
498
f941e658 499 diag "Tests with real-world (malformed) data" unless $ENV{PERL_CORE};
317f7c8a
RGS
500
501 # trailing zero testing (reported by Andreas Koenig).
f941e658 502 $version = $CLASS->$method("1");
317f7c8a 503 ok($version->numify eq "1.000", "trailing zeros preserved");
f941e658 504 $version = $CLASS->$method("1.0");
317f7c8a 505 ok($version->numify eq "1.000", "trailing zeros preserved");
f941e658 506 $version = $CLASS->$method("1.0.0");
317f7c8a 507 ok($version->numify eq "1.000000", "trailing zeros preserved");
f941e658 508 $version = $CLASS->$method("1.0.0.0");
317f7c8a
RGS
509 ok($version->numify eq "1.000000000", "trailing zeros preserved");
510
511 # leading zero testing (reported by Andreas Koenig).
f941e658 512 $version = $CLASS->$method(".7");
317f7c8a
RGS
513 ok($version->numify eq "0.700", "leading zero inferred");
514
515 # leading space testing (reported by Andreas Koenig).
f941e658 516 $version = $CLASS->$method(" 1.7");
317f7c8a
RGS
517 ok($version->numify eq "1.700", "leading space ignored");
518
519 # RT 19517 - deal with undef and 'undef' initialization
8cb289bd
RGS
520 ok("$version" ne 'undef', "Undef version comparison #1");
521 ok("$version" ne undef, "Undef version comparison #2");
f941e658 522 $version = $CLASS->$method('undef');
317f7c8a
RGS
523 unlike($warning, qr/^Version string 'undef' contains invalid data/,
524 "Version string 'undef'");
525
f941e658 526 $version = $CLASS->$method(undef);
317f7c8a
RGS
527 like($warning, qr/^Use of uninitialized value/,
528 "Version string 'undef'");
8cb289bd
RGS
529 ok($version == 'undef', "Undef version comparison #3");
530 ok($version == undef, "Undef version comparison #4");
f941e658 531 eval "\$version = \$CLASS->$method()"; # no parameter at all
317f7c8a 532 unlike($@, qr/^Bizarre copy of CODE/, "No initializer at all");
8cb289bd
RGS
533 ok($version == 'undef', "Undef version comparison #5");
534 ok($version == undef, "Undef version comparison #6");
317f7c8a 535
f941e658 536 $version = $CLASS->$method(0.000001);
317f7c8a
RGS
537 unlike($warning, qr/^Version string '1e-06' contains invalid data/,
538 "Very small version objects");
f34c6aaf 539 }
e0218a61 540
317f7c8a 541SKIP: {
f941e658
JP
542 my $warning;
543 local $SIG{__WARN__} = sub { $warning = $_[0] };
317f7c8a 544 # dummy up a legal module for testing RT#19017
f941e658
JP
545 my ($fh, $filename) = tempfile('tXXXXXXX', SUFFIX => '.pm', UNLINK => 1);
546 (my $package = basename($filename)) =~ s/\.pm$//;
547 print $fh <<"EOF";
548package $package;
549use $CLASS; \$VERSION = ${CLASS}->new('0.0.4');
c8a14fb6
RGS
5501;
551EOF
f941e658 552 close $fh;
317f7c8a 553
f941e658
JP
554 eval "use lib '.'; use $package 0.000008;";
555 like ($@, qr/^$package version 0.000008 required/,
317f7c8a 556 "Make sure very small versions don't freak");
f941e658
JP
557 eval "use lib '.'; use $package 1;";
558 like ($@, qr/^$package version 1 required/,
317f7c8a 559 "Comparing vs. version with no decimal");
f941e658
JP
560 eval "use lib '.'; use $package 1.;";
561 like ($@, qr/^$package version 1 required/,
317f7c8a 562 "Comparing vs. version with decimal only");
ac0e6a2f 563 if ( $] < 5.006_000 ) {
ac0e6a2f 564 skip 'Cannot "use" extended versions with Perl < 5.6.0', 3;
d69f6151 565 }
f941e658
JP
566 eval "use lib '.'; use $package v0.0.8;";
567 my $regex = "^$package version v0.0.8 required";
ac0e6a2f 568 like ($@, qr/$regex/, "Make sure very small versions don't freak");
317f7c8a 569
ac0e6a2f 570 $regex =~ s/8/4/; # set for second test
f941e658 571 eval "use lib '.'; use $package v0.0.4;";
ac0e6a2f 572 unlike($@, qr/$regex/, 'Succeed - required == VERSION');
f941e658
JP
573 cmp_ok ( $package->VERSION, 'eq', '0.0.4', 'No undef warnings' );
574 unlink $filename;
317f7c8a
RGS
575 }
576
f941e658
JP
577SKIP: {
578 skip 'Cannot test "use base qw(version)" when require is used', 3
579 unless defined $qv_declare;
580 my ($fh, $filename) = tempfile('tXXXXXXX', SUFFIX => '.pm', UNLINK => 1);
581 (my $package = basename($filename)) =~ s/\.pm$//;
582 print $fh <<"EOF";
583package $package;
92dcf8ce
JP
584use base qw(version);
5851;
586EOF
f941e658
JP
587 close $fh;
588 # need to eliminate any other $qv_declare()'s
589 undef *{"main\::$qv_declare"};
590 ok(!defined(&{"main\::$qv_declare"}), "make sure we cleared $qv_declare() properly");
591 eval "use lib '.'; use $package qw/declare qv/;";
592 ok(defined(&{"main\::$qv_declare"}), "make sure we exported $qv_declare() properly");
593 isa_ok( &$qv_declare(1.2), $package);
594 unlink $filename;
595}
d69f6151
JP
596
597SKIP: {
ac0e6a2f
RGS
598 if ( $] < 5.006_000 ) {
599 skip 'Cannot "use" extended versions with Perl < 5.6.0', 3;
600 }
f941e658
JP
601 my ($fh, $filename) = tempfile('tXXXXXXX', SUFFIX => '.pm', UNLINK => 1);
602 (my $package = basename($filename)) =~ s/\.pm$//;
603 print $fh <<"EOF";
604package $package;
ac0e6a2f
RGS
605\$VERSION = 1.0;
6061;
607EOF
f941e658
JP
608 close $fh;
609 eval "use lib '.'; use $package 1.001;";
610 like ($@, qr/^$package version 1.001 required/,
ac0e6a2f 611 "User typed numeric so we error with numeric");
f941e658
JP
612 eval "use lib '.'; use $package v1.1.0;";
613 like ($@, qr/^$package version v1.1.0 required/,
ac0e6a2f 614 "User typed extended so we error with extended");
f941e658 615 unlink $filename;
ac0e6a2f
RGS
616 }
617
618SKIP: {
d69f6151 619 # test locale handling
f34c6aaf
JP
620 my $warning;
621 local $SIG{__WARN__} = sub { $warning = $_[0] };
f941e658
JP
622
623$DB::single = 1;
91152fc1
DG
624 my $v = eval { $CLASS->$method('1,7') };
625# is( $@, "", 'Directly test comma as decimal compliance');
f941e658 626
d69f6151 627 my $ver = 1.23; # has to be floating point number
f941e658 628 my $orig_loc = setlocale( LC_ALL );
d69f6151
JP
629 my $loc;
630 while (<DATA>) {
631 chomp;
f941e658
JP
632 $loc = setlocale( LC_ALL, $_);
633 last if localeconv()->{decimal_point} eq ',';
d69f6151
JP
634 }
635 skip 'Cannot test locale handling without a comma locale', 4
636 unless ( $loc and ($ver eq '1,23') );
637
f941e658 638 diag ("Testing locale handling with $loc") unless $ENV{PERL_CORE};
d69f6151 639
f941e658
JP
640 $v = $CLASS->$method($ver);
641 unlike($warning, qr/Version string '1,23' contains invalid data/,
d69f6151 642 "Process locale-dependent floating point");
8cb289bd 643 is ($v, "1.23", "Locale doesn't apply to version objects");
d69f6151 644 ok ($v == $ver, "Comparison to locale floating point");
f941e658
JP
645
646 setlocale( LC_ALL, $orig_loc); # reset this before possible skip
647 skip 'Cannot test RT#46921 with Perl < 5.008', 1
648 if ($] < 5.008);
649 skip 'Cannot test RT#46921 with pure Perl module', 1
650 if exists $INC{'version/vpp.pm'};
651 my ($fh, $filename) = tempfile('tXXXXXXX', SUFFIX => '.pm', UNLINK => 1);
652 (my $package = basename($filename)) =~ s/\.pm$//;
653 print $fh <<"EOF";
654package $package;
655use POSIX qw(locale_h);
656\$^W = 1;
657use $CLASS;
658setlocale (LC_ALL, '$loc');
659use $CLASS ;
660eval "use Socket 1.7";
661setlocale( LC_ALL, '$orig_loc');
6621;
663EOF
664 close $fh;
665
666 eval "use lib '.'; use $package;";
667 unlike($warning, qr"Version string '1,7' contains invalid data",
668 'Handle locale action-at-a-distance');
d69f6151 669 }
f34c6aaf 670
f941e658 671 eval 'my $v = $CLASS->$method("1._1");';
f34c6aaf
JP
672 unlike($@, qr/^Invalid version format \(alpha with zero width\)/,
673 "Invalid version format 1._1");
ac0e6a2f 674
c812d146
JP
675 {
676 my $warning;
677 local $SIG{__WARN__} = sub { $warning = $_[0] };
f941e658 678 eval 'my $v = $CLASS->$method(~0);';
c812d146
JP
679 unlike($@, qr/Integer overflow in version/, "Too large version");
680 like($warning, qr/Integer overflow in version/, "Too large version");
681 }
682
72287d96
JP
683 {
684 # http://rt.cpan.org/Public/Bug/Display.html?id=30004
f941e658 685 my $v1 = $CLASS->$method("v0.1_1");
72287d96 686 (my $alpha1 = Dumper($v1)) =~ s/.+'alpha' => ([^,]+),.+/$1/ms;
f941e658 687 my $v2 = $CLASS->$method($v1);
72287d96
JP
688 (my $alpha2 = Dumper($v2)) =~ s/.+'alpha' => ([^,]+),.+/$1/ms;
689 is $alpha2, $alpha1, "Don't fall for Data::Dumper's tricks";
690 }
691
219bf418
RGS
692 {
693 # http://rt.perl.org/rt3/Ticket/Display.html?id=56606
694 my $badv = bless { version => [1,2,3] }, "version";
695 is $badv, '1.002003', "Deal with badly serialized versions from YAML";
696 my $badv2 = bless { qv => 1, version => [1,2,3] }, "version";
697 is $badv2, 'v1.2.3', "Deal with badly serialized versions from YAML ";
698 }
d54f8cf7
JP
699SKIP: {
700 if ( $] < 5.006_000 ) {
701 skip 'No v-string support at all < 5.6.0', 2;
702 }
703 # https://rt.cpan.org/Ticket/Display.html?id=49348
704 my $v = $CLASS->$method("420");
705 is "$v", "420", 'Correctly guesses this is not a v-string';
706 $v = $CLASS->$method(4.2.0);
707 is "$v", 'v4.2.0', 'Correctly guess that this is a v-string';
708 }
709SKIP: {
710 if ( $] < 5.006_000 ) {
711 skip 'No v-string support at all < 5.6.0', 4;
712 }
713 # https://rt.cpan.org/Ticket/Display.html?id=50347
714 # Check that the qv() implementation does not change
715
716 ok $CLASS->$method(1.2.3) < $CLASS->$method(1.2.3.1), 'Compare 3 and 4 digit v-strings' ;
717 ok $CLASS->$method(v1.2.3) < $CLASS->$method(v1.2.3.1), 'Compare 3 and 4 digit v-strings, leaving v';
718 ok $CLASS->$method("1.2.3") < $CLASS->$method("1.2.3.1"), 'Compare 3 and 4 digit v-strings, quoted';
719 ok $CLASS->$method("v1.2.3") < $CLASS->$method("v1.2.3.1"), 'Compare 3 and 4 digit v-strings, quoted leading v';
720 }
137d6fc0 721}
cb5772bb
RGS
722
7231;
d69f6151
JP
724
725__DATA__
726af_ZA
727af_ZA.utf8
728an_ES
729an_ES.utf8
730az_AZ.utf8
731be_BY
732be_BY.utf8
733bg_BG
734bg_BG.utf8
735br_FR
736br_FR@euro
737br_FR.utf8
738bs_BA
739bs_BA.utf8
740ca_ES
741ca_ES@euro
742ca_ES.utf8
743cs_CZ
744cs_CZ.utf8
745da_DK
746da_DK.utf8
747de_AT
748de_AT@euro
749de_AT.utf8
750de_BE
751de_BE@euro
752de_BE.utf8
753de_DE
754de_DE@euro
755de_DE.utf8
756de_LU
757de_LU@euro
758de_LU.utf8
759el_GR
760el_GR.utf8
761en_DK
762en_DK.utf8
763es_AR
764es_AR.utf8
765es_BO
766es_BO.utf8
767es_CL
768es_CL.utf8
769es_CO
770es_CO.utf8
771es_EC
772es_EC.utf8
773es_ES
774es_ES@euro
775es_ES.utf8
776es_PY
777es_PY.utf8
778es_UY
779es_UY.utf8
780es_VE
781es_VE.utf8
782et_EE
783et_EE.iso885915
784et_EE.utf8
785eu_ES
786eu_ES@euro
787eu_ES.utf8
788fi_FI
789fi_FI@euro
790fi_FI.utf8
791fo_FO
792fo_FO.utf8
793fr_BE
794fr_BE@euro
795fr_BE.utf8
796fr_CA
797fr_CA.utf8
798fr_CH
799fr_CH.utf8
800fr_FR
801fr_FR@euro
802fr_FR.utf8
803fr_LU
804fr_LU@euro
805fr_LU.utf8
806gl_ES
807gl_ES@euro
808gl_ES.utf8
809hr_HR
810hr_HR.utf8
811hu_HU
812hu_HU.utf8
813id_ID
814id_ID.utf8
815is_IS
816is_IS.utf8
817it_CH
818it_CH.utf8
819it_IT
820it_IT@euro
821it_IT.utf8
822ka_GE
823ka_GE.utf8
824kk_KZ
825kk_KZ.utf8
826kl_GL
827kl_GL.utf8
828lt_LT
829lt_LT.utf8
830lv_LV
831lv_LV.utf8
832mk_MK
833mk_MK.utf8
834mn_MN
835mn_MN.utf8
836nb_NO
837nb_NO.utf8
838nl_BE
839nl_BE@euro
840nl_BE.utf8
841nl_NL
842nl_NL@euro
843nl_NL.utf8
844nn_NO
845nn_NO.utf8
846no_NO
847no_NO.utf8
848oc_FR
849oc_FR.utf8
850pl_PL
851pl_PL.utf8
852pt_BR
853pt_BR.utf8
854pt_PT
855pt_PT@euro
856pt_PT.utf8
857ro_RO
858ro_RO.utf8
859ru_RU
860ru_RU.koi8r
861ru_RU.utf8
862ru_UA
863ru_UA.utf8
864se_NO
865se_NO.utf8
866sh_YU
867sh_YU.utf8
868sk_SK
869sk_SK.utf8
870sl_SI
871sl_SI.utf8
872sq_AL
873sq_AL.utf8
874sr_CS
875sr_CS.utf8
876sv_FI
877sv_FI@euro
878sv_FI.utf8
879sv_SE
880sv_SE.iso885915
881sv_SE.utf8
882tg_TJ
883tg_TJ.utf8
884tr_TR
885tr_TR.utf8
886tt_RU.utf8
887uk_UA
888uk_UA.utf8
889vi_VN
890vi_VN.tcvn
891wa_BE
892wa_BE@euro
893wa_BE.utf8
894