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;
13 diag "Tests with base class" unless $ENV{PERL_CORE};
16 use_ok("version", 0.50); # If we made it this far, we are ok.
21 diag "Tests with empty derived class" unless $ENV{PERL_CORE};
23 package version::Empty;
26 no warnings 'redefine';
27 *::qv = sub { return bless version::qv(shift), __PACKAGE__; };
31 sub new { my($self,$n)=@_; bless \$n, $self }
34 my $testobj = version::Empty->new(1.002_003);
35 isa_ok( $testobj, "version::Empty" );
36 ok( $testobj->numify == 1.002003, "Numified correctly" );
37 ok( $testobj->stringify eq "1.002003", "Stringified correctly" );
38 ok( $testobj->normal eq "v1.2.3", "Normalified correctly" );
40 my $verobj = version->new("1.2.4");
41 ok( $verobj > $testobj, "Comparison vs parent class" );
42 ok( $verobj gt $testobj, "Comparison vs parent class" );
43 BaseTests("version::Empty");
45 diag "tests with bad subclass" unless $ENV{PERL_CORE};
46 $testobj = version::Bad->new(1.002_003);
47 isa_ok( $testobj, "version::Bad" );
48 eval { my $string = $testobj->numify };
49 like($@, qr/Invalid version object/,
50 "Bad subclass numify");
51 eval { my $string = $testobj->normal };
52 like($@, qr/Invalid version object/,
53 "Bad subclass normal");
54 eval { my $string = $testobj->stringify };
55 like($@, qr/Invalid version object/,
56 "Bad subclass stringify");
57 eval { my $test = $testobj > 1.0 };
58 like($@, qr/Invalid version object/,
61 # dummy up a redundant call to satify David Wheeler
62 local $SIG{__WARN__} = sub { die $_[0] };
64 unlike ($@, qr/^Subroutine main::qv redefined/,
65 "Only export qv once per package (to prevent redefined warnings).");
69 my ($CLASS, $no_qv) = @_;
71 # Insert your test code below, the Test module is use()ed here so read
72 # its man page ( perldoc Test ) for help writing this test script.
74 # Test bare number processing
75 diag "tests with bare numbers" if $Verbose;
76 $version = $CLASS->new(5.005_03);
77 is ( "$version" , "5.00503" , '5.005_03 eq 5.00503' );
78 $version = $CLASS->new(1.23);
79 is ( "$version" , "1.23" , '1.23 eq "1.23"' );
81 # Test quoted number processing
82 diag "tests with quoted numbers" if $Verbose;
83 $version = $CLASS->new("5.005_03");
84 is ( "$version" , "5.005_03" , '"5.005_03" eq "5.005_03"' );
85 $version = $CLASS->new("v1.23");
86 is ( "$version" , "v1.23" , '"v1.23" eq "v1.23"' );
88 # Test stringify operator
89 diag "tests with stringify" if $Verbose;
90 $version = $CLASS->new("5.005");
91 is ( "$version" , "5.005" , '5.005 eq "5.005"' );
92 $version = $CLASS->new("5.006.001");
93 is ( "$version" , "5.006.001" , '5.006.001 eq v5.6.1' );
94 $version = $CLASS->new("1.2.3_4");
95 is ( "$version" , "1.2.3_4" , 'alpha version 1.2.3_4 eq v1.2.3_4' );
97 # test illegal formats
98 diag "test illegal formats" if $Verbose;
99 eval {my $version = $CLASS->new("1.2_3_4")};
100 like($@, qr/multiple underscores/,
101 "Invalid version format (multiple underscores)");
103 eval {my $version = $CLASS->new("1.2_3.4")};
104 like($@, qr/underscores before decimal/,
105 "Invalid version format (underscores before decimal)");
107 eval {my $version = $CLASS->new("1_2")};
108 like($@, qr/alpha without decimal/,
109 "Invalid version format (alpha without decimal)");
111 # for this first test, just upgrade the warn() to die()
113 local $SIG{__WARN__} = sub { die $_[0] };
114 $version = $CLASS->new("1.2b3");
116 my $warnregex = "Version string '.+' contains invalid data; ".
119 like($@, qr/$warnregex/,
120 "Version string contains invalid data; ignoring");
122 # from here on out capture the warning and test independently
125 local $SIG{__WARN__} = sub { $warning = $_[0] };
126 $version = $CLASS->new("99 and 44/100 pure");
128 like($warning, qr/$warnregex/,
129 "Version string contains invalid data; ignoring");
130 is ("$version", "99", '$version eq "99"');
131 ok ($version->numify == 99.0, '$version->numify == 99.0');
132 ok ($version->normal eq "v99.0.0", '$version->normal eq v99.0.0');
134 $version = $CLASS->new("something");
135 like($warning, qr/$warnregex/,
136 "Version string contains invalid data; ignoring");
137 ok (defined $version, 'defined $version');
139 # reset the test object to something reasonable
140 $version = $CLASS->new("1.2.3");
142 # Test boolean operator
143 ok ($version, 'boolean');
145 # Test class membership
146 isa_ok ( $version, $CLASS );
148 # Test comparison operators with self
149 diag "tests with self" if $Verbose;
150 is ( $version <=> $version, 0, '$version <=> $version == 0' );
151 ok ( $version == $version, '$version == $version' );
153 # Test Numeric Comparison operators
154 # test first with non-object
155 $version = $CLASS->new("5.006.001");
156 $new_version = "5.8.0";
157 diag "numeric tests with non-objects" if $Verbose;
158 ok ( $version == $version, '$version == $version' );
159 ok ( $version < $new_version, '$version < $new_version' );
160 ok ( $new_version > $version, '$new_version > $version' );
161 ok ( $version != $new_version, '$version != $new_version' );
163 # now test with existing object
164 $new_version = $CLASS->new($new_version);
165 diag "numeric tests with objects" if $Verbose;
166 ok ( $version < $new_version, '$version < $new_version' );
167 ok ( $new_version > $version, '$new_version > $version' );
168 ok ( $version != $new_version, '$version != $new_version' );
170 # now test with actual numbers
171 diag "numeric tests with numbers" if $Verbose;
172 ok ( $version->numify() == 5.006001, '$version->numify() == 5.006001' );
173 ok ( $version->numify() <= 5.006001, '$version->numify() <= 5.006001' );
174 ok ( $version->numify() < 5.008, '$version->numify() < 5.008' );
175 #ok ( $version->numify() > v5.005_02, '$version->numify() > 5.005_02' );
177 # test with long decimals
178 diag "Tests with extended decimal versions" if $Verbose;
179 $version = $CLASS->new(1.002003);
180 ok ( $version == "1.2.3", '$version == "1.2.3"');
181 ok ( $version->numify == 1.002003, '$version->numify == 1.002003');
182 $version = $CLASS->new("2002.09.30.1");
183 ok ( $version == "2002.9.30.1",'$version == 2002.9.30.1');
184 ok ( $version->numify == 2002.009030001,
185 '$version->numify == 2002.009030001');
187 # now test with alpha version form with string
188 $version = $CLASS->new("1.2.3");
189 $new_version = "1.2.3_4";
190 diag "numeric tests with alpha-style non-objects" if $Verbose;
191 ok ( $version < $new_version, '$version < $new_version' );
192 ok ( $new_version > $version, '$new_version > $version' );
193 ok ( $version != $new_version, '$version != $new_version' );
195 $version = $CLASS->new("1.2.4");
196 diag "numeric tests with alpha-style non-objects"
198 ok ( $version > $new_version, '$version > $new_version' );
199 ok ( $new_version < $version, '$new_version < $version' );
200 ok ( $version != $new_version, '$version != $new_version' );
202 # now test with alpha version form with object
203 $version = $CLASS->new("1.2.3");
204 $new_version = $CLASS->new("1.2.3_4");
205 diag "tests with alpha-style objects" if $Verbose;
206 ok ( $version < $new_version, '$version < $new_version' );
207 ok ( $new_version > $version, '$new_version > $version' );
208 ok ( $version != $new_version, '$version != $new_version' );
209 ok ( !$version->is_alpha, '!$version->is_alpha');
210 ok ( $new_version->is_alpha, '$new_version->is_alpha');
212 $version = $CLASS->new("1.2.4");
213 diag "tests with alpha-style objects" if $Verbose;
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->new("1.2.3.4");
219 $new_version = $CLASS->new("1.2.3_4");
220 diag "tests with alpha-style objects with same subversion"
222 ok ( $version > $new_version, '$version > $new_version' );
223 ok ( $new_version < $version, '$new_version < $version' );
224 ok ( $version != $new_version, '$version != $new_version' );
226 diag "test implicit [in]equality" if $Verbose;
227 $version = $CLASS->new("v1.2.3");
228 $new_version = $CLASS->new("1.2.3.0");
229 ok ( $version == $new_version, '$version == $new_version' );
230 $new_version = $CLASS->new("1.2.3_0");
231 ok ( $version == $new_version, '$version == $new_version' );
232 $new_version = $CLASS->new("1.2.3.1");
233 ok ( $version < $new_version, '$version < $new_version' );
234 $new_version = $CLASS->new("1.2.3_1");
235 ok ( $version < $new_version, '$version < $new_version' );
236 $new_version = $CLASS->new("1.1.999");
237 ok ( $version > $new_version, '$version > $new_version' );
239 # that which is not expressly permitted is forbidden
240 diag "forbidden operations" if $Verbose;
241 ok ( !eval { ++$version }, "noop ++" );
242 ok ( !eval { --$version }, "noop --" );
243 ok ( !eval { $version/1 }, "noop /" );
244 ok ( !eval { $version*3 }, "noop *" );
245 ok ( !eval { abs($version) }, "noop abs" );
248 skip "version require'd instead of use'd, cannot test qv", 3
251 diag "testing qv" if $Verbose;
252 $version = qv("1.2");
253 is ( "$version", "v1.2", 'qv("1.2") == "1.2.0"' );
255 is ( "$version", "v1.2", 'qv(1.2) == "1.2.0"' );
256 isa_ok( qv('5.008'), $CLASS );
259 # test creation from existing version object
260 diag "create new from existing version" if $Verbose;
261 ok (eval {$new_version = $CLASS->new($version)},
262 "new from existing object");
263 ok ($new_version == $version, "class->new($version) identical");
264 $new_version = $version->new();
265 isa_ok ($new_version, $CLASS );
266 is ($new_version, "0", "version->new() doesn't clone");
267 $new_version = $version->new("1.2.3");
268 is ($new_version, "1.2.3" , '$version->new("1.2.3") works too');
270 # test the CVS revision mode
271 diag "testing CVS Revision" if $Verbose;
272 $version = new $CLASS qw$Revision: 1.2$;
273 ok ( $version == "1.2.0", 'qw$Revision: 1.2$ == 1.2.0' );
274 $version = new $CLASS qw$Revision: 1.2.3.4$;
275 ok ( $version == "1.2.3.4", 'qw$Revision: 1.2.3.4$ == 1.2.3.4' );
277 # test the CPAN style reduced significant digit form
278 diag "testing CPAN-style versions" if $Verbose;
279 $version = $CLASS->new("1.23_01");
280 is ( "$version" , "1.23_01", "CPAN-style alpha version" );
281 ok ( $version > 1.23, "1.23_01 > 1.23");
282 ok ( $version < 1.24, "1.23_01 < 1.24");
284 # test reformed UNIVERSAL::VERSION
285 diag "Replacement UNIVERSAL::VERSION tests" if $Verbose;
287 my $error_regex = $] < 5.006
288 ? 'version \d required'
289 : 'does not define \$...::VERSION';
292 open F, ">aaa.pm" or die "Cannot open aaa.pm: $!\n";
293 print F "package aaa;\n\$aaa::VERSION=0.58;\n1;\n";
297 eval "use lib '.'; use aaa $version";
298 unlike($@, qr/aaa version $version/,
299 'Replacement eval works with exact version');
301 # test as class method
302 $new_version = "aaa"->VERSION;
303 cmp_ok($new_version,'==',$version, "Called as class method");
305 eval "print Completely::Unknown::Module->VERSION";
307 unlike($@, qr/$error_regex/,
308 "Don't freak if the module doesn't even exist");
311 unlike($@, qr/defines neither package nor VERSION/,
312 "Don't freak if the module doesn't even exist");
315 # this should fail even with old UNIVERSAL::VERSION
317 eval "use lib '.'; use aaa $version";
318 like($@, qr/aaa version $version/,
319 'Replacement eval works with incremented version');
321 $version =~ s/0+$//; #convert to string and remove trailing 0's
322 chop($version); # shorten by 1 digit, should still succeed
323 eval "use lib '.'; use aaa $version";
324 unlike($@, qr/aaa version $version/,
325 'Replacement eval works with single digit');
327 # this would fail with old UNIVERSAL::VERSION
329 eval "use lib '.'; use aaa $version";
330 like($@, qr/aaa version $version/,
331 'Replacement eval works with incremented digit');
335 { # dummy up some variously broken modules for testing
336 open F, ">xxx.pm" or die "Cannot open xxx.pm: $!\n";
340 eval "use lib '.'; use xxx 3;";
342 like($@, qr/$error_regex/,
343 'Replacement handles modules without package or VERSION');
346 like($@, qr/defines neither package nor VERSION/,
347 'Replacement handles modules without package or VERSION');
349 eval "use lib '.'; use xxx; \$version = xxx->VERSION";
350 unlike ($@, qr/$error_regex/,
351 'Replacement handles modules without package or VERSION');
352 ok (!defined($version), "Called as class method");
356 { # dummy up some variously broken modules for testing
357 open F, ">yyy.pm" or die "Cannot open yyy.pm: $!\n";
358 print F "package yyy;\n#look ma no VERSION\n1;\n";
360 eval "use lib '.'; use yyy 3;";
361 like ($@, qr/$error_regex/,
362 'Replacement handles modules without VERSION');
363 eval "use lib '.'; use yyy; print yyy->VERSION";
364 unlike ($@, qr/$error_regex/,
365 'Replacement handles modules without VERSION');
369 { # dummy up some variously broken modules for testing
370 open F, ">zzz.pm" or die "Cannot open zzz.pm: $!\n";
371 print F "package zzz;\n\@VERSION = ();\n1;\n";
373 eval "use lib '.'; use zzz 3;";
374 like ($@, qr/$error_regex/,
375 'Replacement handles modules without VERSION');
376 eval "use lib '.'; use zzz; print zzz->VERSION";
377 unlike ($@, qr/$error_regex/,
378 'Replacement handles modules without VERSION');
383 skip 'Cannot test bare v-strings with Perl < 5.6.0', 4
385 diag "Tests with v-strings" if $Verbose;
386 $version = $CLASS->new(1.2.3);
387 ok("$version" == "v1.2.3", '"$version" == 1.2.3');
388 $version = $CLASS->new(1.0.0);
389 $new_version = $CLASS->new(1);
390 ok($version == $new_version, '$version == $new_version');
391 skip "version require'd instead of use'd, cannot test qv", 1
393 $version = qv(1.2.3);
394 ok("$version" == "v1.2.3", 'v-string initialized qv()');
397 diag "Tests with real-world (malformed) data" if $Verbose;
399 # trailing zero testing (reported by Andreas Koenig).
400 $version = $CLASS->new("1");
401 ok($version->numify eq "1.000", "trailing zeros preserved");
402 $version = $CLASS->new("1.0");
403 ok($version->numify eq "1.000", "trailing zeros preserved");
404 $version = $CLASS->new("1.0.0");
405 ok($version->numify eq "1.000000", "trailing zeros preserved");
406 $version = $CLASS->new("1.0.0.0");
407 ok($version->numify eq "1.000000000", "trailing zeros preserved");
409 # leading zero testing (reported by Andreas Koenig).
410 $version = $CLASS->new(".7");
411 ok($version->numify eq "0.700", "leading zero inferred");
413 # leading space testing (reported by Andreas Koenig).
414 $version = $CLASS->new(" 1.7");
415 ok($version->numify eq "1.700", "leading space ignored");
417 # RT 19517 - deal with undef and 'undef' initialization
418 ok("$version" ne 'undef', "Undef version comparison #1");
419 ok("$version" ne undef, "Undef version comparison #2");
420 $version = $CLASS->new('undef');
421 unlike($warning, qr/^Version string 'undef' contains invalid data/,
422 "Version string 'undef'");
424 $version = $CLASS->new(undef);
425 like($warning, qr/^Use of uninitialized value/,
426 "Version string 'undef'");
427 ok($version == 'undef', "Undef version comparison #3");
428 ok($version == undef, "Undef version comparison #4");
429 eval "\$version = \$CLASS->new()"; # no parameter at all
430 unlike($@, qr/^Bizarre copy of CODE/, "No initializer at all");
431 ok($version == 'undef', "Undef version comparison #5");
432 ok($version == undef, "Undef version comparison #6");
434 $version = $CLASS->new(0.000001);
435 unlike($warning, qr/^Version string '1e-06' contains invalid data/,
436 "Very small version objects");
440 # dummy up a legal module for testing RT#19017
441 open F, ">www.pm" or die "Cannot open www.pm: $!\n";
444 use version; \$VERSION = qv('0.0.4');
449 eval "use lib '.'; use www 0.000008;";
450 like ($@, qr/^www version 0.000008 required/,
451 "Make sure very small versions don't freak");
452 eval "use lib '.'; use www 1;";
453 like ($@, qr/^www version 1 required/,
454 "Comparing vs. version with no decimal");
455 eval "use lib '.'; use www 1.;";
456 like ($@, qr/^www version 1 required/,
457 "Comparing vs. version with decimal only");
459 if ( $] < 5.006_000 ) {
461 skip 'Cannot "use" extended versions with Perl < 5.6.0', 3;
463 eval "use lib '.'; use www v0.0.8;";
464 my $regex = "^www version v0.0.8 required";
465 like ($@, qr/$regex/, "Make sure very small versions don't freak");
467 $regex =~ s/8/4/; # set for second test
468 eval "use lib '.'; use www v0.0.4;";
469 unlike($@, qr/$regex/, 'Succeed - required == VERSION');
470 cmp_ok ( "www"->VERSION, 'eq', '0.0.4', 'No undef warnings' );
475 open F, ">vvv.pm" or die "Cannot open vvv.pm: $!\n";
478 use base qw(version);
482 # need to eliminate any other qv()'s
484 ok(!defined(&{"main\::qv"}), "make sure we cleared qv() properly");
485 eval "use lib '.'; use vvv;";
486 ok(defined(&{"main\::qv"}), "make sure we exported qv() properly");
487 isa_ok( qv(1.2), "vvv");
491 if ( $] < 5.006_000 ) {
492 skip 'Cannot "use" extended versions with Perl < 5.6.0', 3;
494 open F, ">uuu.pm" or die "Cannot open uuu.pm: $!\n";
501 eval "use lib '.'; use uuu 1.001;";
502 like ($@, qr/^uuu version 1.001 required/,
503 "User typed numeric so we error with numeric");
504 eval "use lib '.'; use uuu v1.1.0;";
505 like ($@, qr/^uuu version v1.1.0 required/,
506 "User typed extended so we error with extended");
511 # test locale handling
513 local $SIG{__WARN__} = sub { $warning = $_[0] };
514 my $ver = 1.23; # has to be floating point number
518 $loc = POSIX::setlocale( &POSIX::LC_ALL, $_);
519 last if POSIX::localeconv()->{decimal_point} eq ',';
521 skip 'Cannot test locale handling without a comma locale', 4
522 unless ( $loc and ($ver eq '1,23') );
524 diag ("Testing locale handling with $loc") if $Verbose;
526 my $v = $CLASS->new($ver);
527 unlike($warning,qr/Version string '1,23' contains invalid data/,
528 "Process locale-dependent floating point");
529 is ($v, "1.23", "Locale doesn't apply to version objects");
530 ok ($v == $ver, "Comparison to locale floating point");
533 eval 'my $v = $CLASS->new("1._1");';
534 unlike($@, qr/^Invalid version format \(alpha with zero width\)/,
535 "Invalid version format 1._1");
539 local $SIG{__WARN__} = sub { $warning = $_[0] };
540 eval 'my $v = $CLASS->new(~0);';
541 unlike($@, qr/Integer overflow in version/, "Too large version");
542 like($warning, qr/Integer overflow in version/, "Too large version");
546 # http://rt.cpan.org/Public/Bug/Display.html?id=30004
547 my $v1 = $CLASS->new("v0.1_1");
548 (my $alpha1 = Dumper($v1)) =~ s/.+'alpha' => ([^,]+),.+/$1/ms;
549 my $v2 = $CLASS->new($v1);
550 (my $alpha2 = Dumper($v2)) =~ s/.+'alpha' => ([^,]+),.+/$1/ms;
551 is $alpha2, $alpha1, "Don't fall for Data::Dumper's tricks";
555 # http://rt.perl.org/rt3/Ticket/Display.html?id=56606
556 my $badv = bless { version => [1,2,3] }, "version";
557 is $badv, '1.002003', "Deal with badly serialized versions from YAML";
558 my $badv2 = bless { qv => 1, version => [1,2,3] }, "version";
559 is $badv2, 'v1.2.3', "Deal with badly serialized versions from YAML ";