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'
5 #########################
7 use Test::More qw(no_plan);
11 *Verbose = \$Test::Harness::Verbose;
12 use POSIX qw/locale_h/;
13 use File::Temp qw/tempfile/;
17 use_ok("version", 0.77);
18 # If we made it this far, we are ok.
23 diag "Tests with base class" unless $ENV{PERL_CORE};
25 BaseTests("version","new","qv");
26 BaseTests("version","new","declare");
27 BaseTests("version","parse", "qv");
28 BaseTests("version","parse", "declare");
30 # dummy up a redundant call to satify David Wheeler
31 local $SIG{__WARN__} = sub { die $_[0] };
33 unlike ($@, qr/^Subroutine main::declare redefined/,
34 "Only export declare once per package (to prevent redefined warnings).");
38 sub new { my($self,$n)=@_; bless \$n, $self }
43 local $SIG{__WARN__} = sub { $warning = $_[0] };
44 my ($fh, $filename) = tempfile('tXXXXXXX', SUFFIX => '.pm', UNLINK => 1);
45 (my $package = basename($filename)) =~ s/\.pm$//;
47 # This is an empty subclass
56 delete $main::INC{'$package'};
57 undef &qv; undef *::qv; # avoid 'used once' warning
58 undef &declare; undef *::declare; # avoid 'used once' warning
61 diag "Tests with empty derived class" unless $ENV{PERL_CORE};
63 use_ok($package, 0.001);
64 my $testobj = $package->new(1.002_003);
65 isa_ok( $testobj, $package );
66 ok( $testobj->numify == 1.002003, "Numified correctly" );
67 ok( $testobj->stringify eq "1.002003", "Stringified correctly" );
68 ok( $testobj->normal eq "v1.2.3", "Normalified correctly" );
70 my $verobj = version::->new("1.2.4");
71 ok( $verobj > $testobj, "Comparison vs parent class" );
73 BaseTests($package, "new", "qv");
75 use_ok($package, 0.001, "declare");
76 BaseTests($package, "new", "declare");
78 use_ok($package, 0.001);
79 BaseTests($package, "parse", "qv");
81 use_ok($package, 0.001, "declare");
82 BaseTests($package, "parse", "declare");
84 diag "tests with bad subclass" unless $ENV{PERL_CORE};
85 $testobj = version::Bad->new(1.002_003);
86 isa_ok( $testobj, "version::Bad" );
87 eval { my $string = $testobj->numify };
88 like($@, qr/Invalid version object/,
89 "Bad subclass numify");
90 eval { my $string = $testobj->normal };
91 like($@, qr/Invalid version object/,
92 "Bad subclass normal");
93 eval { my $string = $testobj->stringify };
94 like($@, qr/Invalid version object/,
95 "Bad subclass stringify");
96 eval { my $test = ($testobj > 1.0) };
97 like($@, qr/Invalid version object/,
102 my ($CLASS, $method, $qv_declare) = @_;
104 local $SIG{__WARN__} = sub { $warning = $_[0] };
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.
109 # Test bare number processing
110 diag "tests with bare numbers" unless $ENV{PERL_CORE};
111 $version = $CLASS->$method(5.005_03);
112 is ( "$version" , "5.00503" , '5.005_03 eq 5.00503' );
113 $version = $CLASS->$method(1.23);
114 is ( "$version" , "1.23" , '1.23 eq "1.23"' );
116 # Test quoted number processing
117 diag "tests with quoted numbers" unless $ENV{PERL_CORE};
118 $version = $CLASS->$method("5.005_03");
119 is ( "$version" , "5.005_03" , '"5.005_03" eq "5.005_03"' );
120 $version = $CLASS->$method("v1.23");
121 is ( "$version" , "v1.23" , '"v1.23" eq "v1.23"' );
123 # Test stringify operator
124 diag "tests with stringify" unless $ENV{PERL_CORE};
125 $version = $CLASS->$method("5.005");
126 is ( "$version" , "5.005" , '5.005 eq "5.005"' );
127 $version = $CLASS->$method("5.006.001");
128 is ( "$version" , "5.006.001" , '5.006.001 eq v5.6.1' );
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' );
133 # test illegal formats
134 diag "test illegal formats" unless $ENV{PERL_CORE};
135 eval {$version = $CLASS->$method("1.2_3_4")};
136 like($@, qr/multiple underscores/,
137 "Invalid version format (multiple underscores)");
139 eval {$version = $CLASS->$method("1.2_3.4")};
140 like($@, qr/underscores before decimal/,
141 "Invalid version format (underscores before decimal)");
143 eval {$version = $CLASS->$method("1_2")};
144 like($@, qr/alpha without decimal/,
145 "Invalid version format (alpha without decimal)");
147 eval { $version = $CLASS->$method("1.2b3")};
148 like($@, qr/non-numeric data/,
149 "Invalid version format (non-numeric data)");
151 # from here on out capture the warning and test independently
153 eval{$version = $CLASS->$method("99 and 44/100 pure")};
155 like($@, qr/non-numeric data/,
156 "Invalid version format (non-numeric data)");
158 eval{$version = $CLASS->$method("something")};
159 like($@, qr/non-numeric data/,
160 "Invalid version format (non-numeric data)");
162 # reset the test object to something reasonable
163 $version = $CLASS->$method("1.2.3");
165 # Test boolean operator
166 ok ($version, 'boolean');
168 # Test class membership
169 isa_ok ( $version, $CLASS );
171 # Test comparison operators with self
172 diag "tests with self" unless $ENV{PERL_CORE};
173 is ( $version <=> $version, 0, '$version <=> $version == 0' );
174 ok ( $version == $version, '$version == $version' );
176 # Test Numeric Comparison operators
177 # test first with non-object
178 $version = $CLASS->$method("5.006.001");
179 $new_version = "5.8.0";
180 diag "numeric tests with non-objects" unless $ENV{PERL_CORE};
181 ok ( $version == $version, '$version == $version' );
182 ok ( $version < $new_version, '$version < $new_version' );
183 ok ( $new_version > $version, '$new_version > $version' );
184 ok ( $version != $new_version, '$version != $new_version' );
186 # now test with existing object
187 $new_version = $CLASS->$method($new_version);
188 diag "numeric tests with objects" unless $ENV{PERL_CORE};
189 ok ( $version < $new_version, '$version < $new_version' );
190 ok ( $new_version > $version, '$new_version > $version' );
191 ok ( $version != $new_version, '$version != $new_version' );
193 # now test with actual numbers
194 diag "numeric tests with numbers" unless $ENV{PERL_CORE};
195 ok ( $version->numify() == 5.006001, '$version->numify() == 5.006001' );
196 ok ( $version->numify() <= 5.006001, '$version->numify() <= 5.006001' );
197 ok ( $version->numify() < 5.008, '$version->numify() < 5.008' );
198 #ok ( $version->numify() > v5.005_02, '$version->numify() > 5.005_02' );
200 # test with long decimals
201 diag "Tests with extended decimal versions" unless $ENV{PERL_CORE};
202 $version = $CLASS->$method(1.002003);
203 ok ( $version == "1.2.3", '$version == "1.2.3"');
204 ok ( $version->numify == 1.002003, '$version->numify == 1.002003');
205 $version = $CLASS->$method("2002.09.30.1");
206 ok ( $version == "2002.9.30.1",'$version == 2002.9.30.1');
207 ok ( $version->numify == 2002.009030001,
208 '$version->numify == 2002.009030001');
210 # now test with alpha version form with string
211 $version = $CLASS->$method("1.2.3");
212 $new_version = "1.2.3_4";
213 diag "numeric tests with alpha-style non-objects" unless $ENV{PERL_CORE};
214 ok ( $version < $new_version, '$version < $new_version' );
215 ok ( $new_version > $version, '$new_version > $version' );
216 ok ( $version != $new_version, '$version != $new_version' );
218 $version = $CLASS->$method("1.2.4");
219 diag "numeric tests with alpha-style non-objects"
220 unless $ENV{PERL_CORE};
221 ok ( $version > $new_version, '$version > $new_version' );
222 ok ( $new_version < $version, '$new_version < $version' );
223 ok ( $version != $new_version, '$version != $new_version' );
225 # now test with alpha version form with object
226 $version = $CLASS->$method("1.2.3");
227 $new_version = $CLASS->$method("1.2.3_4");
228 diag "tests with alpha-style objects" unless $ENV{PERL_CORE};
229 ok ( $version < $new_version, '$version < $new_version' );
230 ok ( $new_version > $version, '$new_version > $version' );
231 ok ( $version != $new_version, '$version != $new_version' );
232 ok ( !$version->is_alpha, '!$version->is_alpha');
233 ok ( $new_version->is_alpha, '$new_version->is_alpha');
235 $version = $CLASS->$method("1.2.4");
236 diag "tests with alpha-style objects" unless $ENV{PERL_CORE};
237 ok ( $version > $new_version, '$version > $new_version' );
238 ok ( $new_version < $version, '$new_version < $version' );
239 ok ( $version != $new_version, '$version != $new_version' );
241 $version = $CLASS->$method("1.2.3.4");
242 $new_version = $CLASS->$method("1.2.3_4");
243 diag "tests with alpha-style objects with same subversion"
244 unless $ENV{PERL_CORE};
245 ok ( $version > $new_version, '$version > $new_version' );
246 ok ( $new_version < $version, '$new_version < $version' );
247 ok ( $version != $new_version, '$version != $new_version' );
249 diag "test implicit [in]equality" unless $ENV{PERL_CORE};
250 $version = $CLASS->$method("v1.2.3");
251 $new_version = $CLASS->$method("1.2.3.0");
252 ok ( $version == $new_version, '$version == $new_version' );
253 $new_version = $CLASS->$method("1.2.3_0");
254 ok ( $version == $new_version, '$version == $new_version' );
255 $new_version = $CLASS->$method("1.2.3.1");
256 ok ( $version < $new_version, '$version < $new_version' );
257 $new_version = $CLASS->$method("1.2.3_1");
258 ok ( $version < $new_version, '$version < $new_version' );
259 $new_version = $CLASS->$method("1.1.999");
260 ok ( $version > $new_version, '$version > $new_version' );
262 # that which is not expressly permitted is forbidden
263 diag "forbidden operations" unless $ENV{PERL_CORE};
264 ok ( !eval { ++$version }, "noop ++" );
265 ok ( !eval { --$version }, "noop --" );
266 ok ( !eval { $version/1 }, "noop /" );
267 ok ( !eval { $version*3 }, "noop *" );
268 ok ( !eval { abs($version) }, "noop abs" );
271 skip "version require'd instead of use'd, cannot test $qv_declare", 3
272 unless defined $qv_declare;
273 # test the $qv_declare() sub
274 diag "testing $qv_declare" unless $ENV{PERL_CORE};
275 $version = $CLASS->$qv_declare("1.2");
276 is ( "$version", "v1.2", $qv_declare.'("1.2") == "1.2.0"' );
277 $version = $CLASS->$qv_declare(1.2);
278 is ( "$version", "v1.2", $qv_declare.'(1.2) == "1.2.0"' );
279 isa_ok( $CLASS->$qv_declare('5.008'), $CLASS );
282 # test creation from existing version object
283 diag "create new from existing version" unless $ENV{PERL_CORE};
284 ok (eval {$new_version = $CLASS->$method($version)},
285 "new from existing object");
286 ok ($new_version == $version, "class->$method($version) identical");
287 $new_version = $version->$method();
288 isa_ok ($new_version, $CLASS );
289 is ($new_version, "0", "version->$method() doesn't clone");
290 $new_version = $version->$method("1.2.3");
291 is ($new_version, "1.2.3" , '$version->$method("1.2.3") works too');
293 # test the CVS revision mode
294 diag "testing CVS Revision" unless $ENV{PERL_CORE};
295 $version = new $CLASS qw$Revision: 1.2$;
296 ok ( $version == "1.2.0", 'qw$Revision: 1.2$ == 1.2.0' );
297 $version = new $CLASS qw$Revision: 1.2.3.4$;
298 ok ( $version == "1.2.3.4", 'qw$Revision: 1.2.3.4$ == 1.2.3.4' );
300 # test the CPAN style reduced significant digit form
301 diag "testing CPAN-style versions" unless $ENV{PERL_CORE};
302 $version = $CLASS->$method("1.23_01");
303 is ( "$version" , "1.23_01", "CPAN-style alpha version" );
304 ok ( $version > 1.23, "1.23_01 > 1.23");
305 ok ( $version < 1.24, "1.23_01 < 1.24");
307 # test reformed UNIVERSAL::VERSION
308 diag "Replacement UNIVERSAL::VERSION tests" unless $ENV{PERL_CORE};
310 my $error_regex = $] < 5.006
311 ? 'version \d required'
312 : 'does not define \$t.{7}::VERSION';
315 my ($fh, $filename) = tempfile('tXXXXXXX', SUFFIX => '.pm', UNLINK => 1);
316 (my $package = basename($filename)) =~ s/\.pm$//;
317 print $fh "package $package;\n\$$package\::VERSION=0.58;\n1;\n";
321 eval "use lib '.'; use $package $version";
322 unlike($@, qr/$package version $version/,
323 'Replacement eval works with exact version');
325 # test as class method
326 $new_version = $package->VERSION;
327 cmp_ok($new_version,'==',$version, "Called as class method");
329 eval "print Completely::Unknown::Module->VERSION";
331 unlike($@, qr/$error_regex/,
332 "Don't freak if the module doesn't even exist");
335 unlike($@, qr/defines neither package nor VERSION/,
336 "Don't freak if the module doesn't even exist");
339 # this should fail even with old UNIVERSAL::VERSION
341 eval "use lib '.'; use $package $version";
342 like($@, qr/$package version $version/,
343 'Replacement eval works with incremented version');
345 $version =~ s/0+$//; #convert to string and remove trailing 0's
346 chop($version); # shorten by 1 digit, should still succeed
347 eval "use lib '.'; use $package $version";
348 unlike($@, qr/$package version $version/,
349 'Replacement eval works with single digit');
351 # this would fail with old UNIVERSAL::VERSION
353 eval "use lib '.'; use $package $version";
354 like($@, qr/$package version $version/,
355 'Replacement eval works with incremented digit');
359 { # dummy up some variously broken modules for testing
360 my ($fh, $filename) = tempfile('tXXXXXXX', SUFFIX => '.pm', UNLINK => 1);
361 (my $package = basename($filename)) =~ s/\.pm$//;
365 eval "use lib '.'; use $package 3;";
367 like($@, qr/$error_regex/,
368 'Replacement handles modules without package or VERSION');
371 like($@, qr/defines neither package nor VERSION/,
372 'Replacement handles modules without package or VERSION');
374 eval "use lib '.'; use $package; \$version = $package->VERSION";
375 unlike ($@, qr/$error_regex/,
376 'Replacement handles modules without package or VERSION');
377 ok (!defined($version), "Called as class method");
381 { # dummy up some variously broken modules for testing
382 my ($fh, $filename) = tempfile('tXXXXXXX', SUFFIX => '.pm', UNLINK => 1);
383 (my $package = basename($filename)) =~ s/\.pm$//;
384 print $fh "package $package;\n#look ma no VERSION\n1;\n";
386 eval "use lib '.'; use $package 3;";
387 like ($@, qr/$error_regex/,
388 'Replacement handles modules without VERSION');
389 eval "use lib '.'; use $package; print $package->VERSION";
390 unlike ($@, qr/$error_regex/,
391 'Replacement handles modules without VERSION');
395 { # dummy up some variously broken modules for testing
396 my ($fh, $filename) = tempfile('tXXXXXXX', SUFFIX => '.pm', UNLINK => 1);
397 (my $package = basename($filename)) =~ s/\.pm$//;
398 print $fh "package $package;\n\@VERSION = ();\n1;\n";
400 eval "use lib '.'; use $package 3;";
401 like ($@, qr/$error_regex/,
402 'Replacement handles modules without VERSION');
403 eval "use lib '.'; use $package; print $package->VERSION";
404 unlike ($@, qr/$error_regex/,
405 'Replacement handles modules without VERSION');
410 skip 'Cannot test bare v-strings with Perl < 5.6.0', 4
412 diag "Tests with v-strings" unless $ENV{PERL_CORE};
413 $version = $CLASS->$method(1.2.3);
414 ok("$version" == "v1.2.3", '"$version" == 1.2.3');
415 $version = $CLASS->$method(1.0.0);
416 $new_version = $CLASS->$method(1);
417 ok($version == $new_version, '$version == $new_version');
418 skip "version require'd instead of use'd, cannot test declare", 1
419 unless defined $qv_declare;
420 $version = &$qv_declare(1.2.3);
421 ok("$version" == "v1.2.3", 'v-string initialized $qv_declare()');
424 diag "Tests with real-world (malformed) data" unless $ENV{PERL_CORE};
426 # trailing zero testing (reported by Andreas Koenig).
427 $version = $CLASS->$method("1");
428 ok($version->numify eq "1.000", "trailing zeros preserved");
429 $version = $CLASS->$method("1.0");
430 ok($version->numify eq "1.000", "trailing zeros preserved");
431 $version = $CLASS->$method("1.0.0");
432 ok($version->numify eq "1.000000", "trailing zeros preserved");
433 $version = $CLASS->$method("1.0.0.0");
434 ok($version->numify eq "1.000000000", "trailing zeros preserved");
436 # leading zero testing (reported by Andreas Koenig).
437 $version = $CLASS->$method(".7");
438 ok($version->numify eq "0.700", "leading zero inferred");
440 # leading space testing (reported by Andreas Koenig).
441 $version = $CLASS->$method(" 1.7");
442 ok($version->numify eq "1.700", "leading space ignored");
444 # RT 19517 - deal with undef and 'undef' initialization
445 ok("$version" ne 'undef', "Undef version comparison #1");
446 ok("$version" ne undef, "Undef version comparison #2");
447 $version = $CLASS->$method('undef');
448 unlike($warning, qr/^Version string 'undef' contains invalid data/,
449 "Version string 'undef'");
451 $version = $CLASS->$method(undef);
452 like($warning, qr/^Use of uninitialized value/,
453 "Version string 'undef'");
454 ok($version == 'undef', "Undef version comparison #3");
455 ok($version == undef, "Undef version comparison #4");
456 eval "\$version = \$CLASS->$method()"; # no parameter at all
457 unlike($@, qr/^Bizarre copy of CODE/, "No initializer at all");
458 ok($version == 'undef', "Undef version comparison #5");
459 ok($version == undef, "Undef version comparison #6");
461 $version = $CLASS->$method(0.000001);
462 unlike($warning, qr/^Version string '1e-06' contains invalid data/,
463 "Very small version objects");
468 local $SIG{__WARN__} = sub { $warning = $_[0] };
469 # dummy up a legal module for testing RT#19017
470 my ($fh, $filename) = tempfile('tXXXXXXX', SUFFIX => '.pm', UNLINK => 1);
471 (my $package = basename($filename)) =~ s/\.pm$//;
474 use $CLASS; \$VERSION = ${CLASS}->new('0.0.4');
479 eval "use lib '.'; use $package 0.000008;";
480 like ($@, qr/^$package version 0.000008 required/,
481 "Make sure very small versions don't freak");
482 eval "use lib '.'; use $package 1;";
483 like ($@, qr/^$package version 1 required/,
484 "Comparing vs. version with no decimal");
485 eval "use lib '.'; use $package 1.;";
486 like ($@, qr/^$package version 1 required/,
487 "Comparing vs. version with decimal only");
488 if ( $] < 5.006_000 ) {
489 skip 'Cannot "use" extended versions with Perl < 5.6.0', 3;
491 eval "use lib '.'; use $package v0.0.8;";
492 my $regex = "^$package version v0.0.8 required";
493 like ($@, qr/$regex/, "Make sure very small versions don't freak");
495 $regex =~ s/8/4/; # set for second test
496 eval "use lib '.'; use $package v0.0.4;";
497 unlike($@, qr/$regex/, 'Succeed - required == VERSION');
498 cmp_ok ( $package->VERSION, 'eq', '0.0.4', 'No undef warnings' );
503 skip 'Cannot test "use base qw(version)" when require is used', 3
504 unless defined $qv_declare;
505 my ($fh, $filename) = tempfile('tXXXXXXX', SUFFIX => '.pm', UNLINK => 1);
506 (my $package = basename($filename)) =~ s/\.pm$//;
509 use base qw(version);
513 # need to eliminate any other $qv_declare()'s
514 undef *{"main\::$qv_declare"};
515 ok(!defined(&{"main\::$qv_declare"}), "make sure we cleared $qv_declare() properly");
516 eval "use lib '.'; use $package qw/declare qv/;";
517 ok(defined(&{"main\::$qv_declare"}), "make sure we exported $qv_declare() properly");
518 isa_ok( &$qv_declare(1.2), $package);
523 if ( $] < 5.006_000 ) {
524 skip 'Cannot "use" extended versions with Perl < 5.6.0', 3;
526 my ($fh, $filename) = tempfile('tXXXXXXX', SUFFIX => '.pm', UNLINK => 1);
527 (my $package = basename($filename)) =~ s/\.pm$//;
534 eval "use lib '.'; use $package 1.001;";
535 like ($@, qr/^$package version 1.001 required/,
536 "User typed numeric so we error with numeric");
537 eval "use lib '.'; use $package v1.1.0;";
538 like ($@, qr/^$package version v1.1.0 required/,
539 "User typed extended so we error with extended");
544 # test locale handling
546 local $SIG{__WARN__} = sub { $warning = $_[0] };
549 my $v = eval { $CLASS->$method('1,7') };
550 # is( $@, "", 'Directly test comma as decimal compliance');
552 my $ver = 1.23; # has to be floating point number
553 my $orig_loc = setlocale( LC_ALL );
557 $loc = setlocale( LC_ALL, $_);
558 last if localeconv()->{decimal_point} eq ',';
560 skip 'Cannot test locale handling without a comma locale', 4
561 unless ( $loc and ($ver eq '1,23') );
563 diag ("Testing locale handling with $loc") unless $ENV{PERL_CORE};
565 $v = $CLASS->$method($ver);
566 unlike($warning, qr/Version string '1,23' contains invalid data/,
567 "Process locale-dependent floating point");
568 is ($v, "1.23", "Locale doesn't apply to version objects");
569 ok ($v == $ver, "Comparison to locale floating point");
571 setlocale( LC_ALL, $orig_loc); # reset this before possible skip
572 skip 'Cannot test RT#46921 with Perl < 5.008', 1
574 skip 'Cannot test RT#46921 with pure Perl module', 1
575 if exists $INC{'version/vpp.pm'};
576 my ($fh, $filename) = tempfile('tXXXXXXX', SUFFIX => '.pm', UNLINK => 1);
577 (my $package = basename($filename)) =~ s/\.pm$//;
580 use POSIX qw(locale_h);
583 setlocale (LC_ALL, '$loc');
585 eval "use Socket 1.7";
586 setlocale( LC_ALL, '$orig_loc');
591 eval "use lib '.'; use $package;";
592 unlike($warning, qr"Version string '1,7' contains invalid data",
593 'Handle locale action-at-a-distance');
596 eval 'my $v = $CLASS->$method("1._1");';
597 unlike($@, qr/^Invalid version format \(alpha with zero width\)/,
598 "Invalid version format 1._1");
602 local $SIG{__WARN__} = sub { $warning = $_[0] };
603 eval 'my $v = $CLASS->$method(~0);';
604 unlike($@, qr/Integer overflow in version/, "Too large version");
605 like($warning, qr/Integer overflow in version/, "Too large version");
609 # http://rt.cpan.org/Public/Bug/Display.html?id=30004
610 my $v1 = $CLASS->$method("v0.1_1");
611 (my $alpha1 = Dumper($v1)) =~ s/.+'alpha' => ([^,]+),.+/$1/ms;
612 my $v2 = $CLASS->$method($v1);
613 (my $alpha2 = Dumper($v2)) =~ s/.+'alpha' => ([^,]+),.+/$1/ms;
614 is $alpha2, $alpha1, "Don't fall for Data::Dumper's tricks";
618 # http://rt.perl.org/rt3/Ticket/Display.html?id=56606
619 my $badv = bless { version => [1,2,3] }, "version";
620 is $badv, '1.002003', "Deal with badly serialized versions from YAML";
621 my $badv2 = bless { qv => 1, version => [1,2,3] }, "version";
622 is $badv2, 'v1.2.3', "Deal with badly serialized versions from YAML ";