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