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);
10 *Verbose = \$Test::Harness::Verbose;
12 diag "Tests with base class" unless $ENV{PERL_CORE};
15 use_ok("version", 0.50); # If we made it this far, we are ok.
20 diag "Tests with empty derived class" unless $ENV{PERL_CORE};
22 package version::Empty;
25 no warnings 'redefine';
26 *::qv = sub { return bless version::qv(shift), __PACKAGE__; };
30 sub new { my($self,$n)=@_; bless \$n, $self }
33 my $testobj = version::Empty->new(1.002_003);
34 isa_ok( $testobj, "version::Empty" );
35 ok( $testobj->numify == 1.002003, "Numified correctly" );
36 ok( $testobj->stringify eq "1.002003", "Stringified correctly" );
37 ok( $testobj->normal eq "v1.2.3", "Normalified correctly" );
39 my $verobj = version->new("1.2.4");
40 ok( $verobj > $testobj, "Comparison vs parent class" );
41 ok( $verobj gt $testobj, "Comparison vs parent class" );
42 BaseTests("version::Empty");
44 diag "tests with bad subclass" unless $ENV{PERL_CORE};
45 $testobj = version::Bad->new(1.002_003);
46 isa_ok( $testobj, "version::Bad" );
47 eval { my $string = $testobj->numify };
48 like($@, qr/Invalid version object/,
49 "Bad subclass numify");
50 eval { my $string = $testobj->normal };
51 like($@, qr/Invalid version object/,
52 "Bad subclass normal");
53 eval { my $string = $testobj->stringify };
54 like($@, qr/Invalid version object/,
55 "Bad subclass stringify");
56 eval { my $test = $testobj > 1.0 };
57 like($@, qr/Invalid version object/,
60 # dummy up a redundant call to satify David Wheeler
61 local $SIG{__WARN__} = sub { die $_[0] };
63 unlike ($@, qr/^Subroutine main::qv redefined/,
64 "Only export qv once per package (to prevent redefined warnings).");
68 my ($CLASS, $no_qv) = @_;
70 # Insert your test code below, the Test module is use()ed here so read
71 # its man page ( perldoc Test ) for help writing this test script.
73 # Test bare number processing
74 diag "tests with bare numbers" if $Verbose;
75 $version = $CLASS->new(5.005_03);
76 is ( "$version" , "5.00503" , '5.005_03 eq 5.00503' );
77 $version = $CLASS->new(1.23);
78 is ( "$version" , "1.23" , '1.23 eq "1.23"' );
80 # Test quoted number processing
81 diag "tests with quoted numbers" if $Verbose;
82 $version = $CLASS->new("5.005_03");
83 is ( "$version" , "5.005_03" , '"5.005_03" eq "5.005_03"' );
84 $version = $CLASS->new("v1.23");
85 is ( "$version" , "v1.23" , '"v1.23" eq "v1.23"' );
87 # Test stringify operator
88 diag "tests with stringify" if $Verbose;
89 $version = $CLASS->new("5.005");
90 is ( "$version" , "5.005" , '5.005 eq "5.005"' );
91 $version = $CLASS->new("5.006.001");
92 is ( "$version" , "5.006.001" , '5.006.001 eq v5.6.1' );
93 $version = $CLASS->new("1.2.3_4");
94 is ( "$version" , "1.2.3_4" , 'alpha version 1.2.3_4 eq v1.2.3_4' );
96 # test illegal formats
97 diag "test illegal formats" if $Verbose;
98 eval {my $version = $CLASS->new("1.2_3_4")};
99 like($@, qr/multiple underscores/,
100 "Invalid version format (multiple underscores)");
102 eval {my $version = $CLASS->new("1.2_3.4")};
103 like($@, qr/underscores before decimal/,
104 "Invalid version format (underscores before decimal)");
106 eval {my $version = $CLASS->new("1_2")};
107 like($@, qr/alpha without decimal/,
108 "Invalid version format (alpha without decimal)");
110 # for this first test, just upgrade the warn() to die()
112 local $SIG{__WARN__} = sub { die $_[0] };
113 $version = $CLASS->new("1.2b3");
115 my $warnregex = "Version string '.+' contains invalid data; ".
118 like($@, qr/$warnregex/,
119 "Version string contains invalid data; ignoring");
121 # from here on out capture the warning and test independently
124 local $SIG{__WARN__} = sub { $warning = $_[0] };
125 $version = $CLASS->new("99 and 44/100 pure");
127 like($warning, qr/$warnregex/,
128 "Version string contains invalid data; ignoring");
129 is ("$version", "99", '$version eq "99"');
130 ok ($version->numify == 99.0, '$version->numify == 99.0');
131 ok ($version->normal eq "v99.0.0", '$version->normal eq v99.0.0');
133 $version = $CLASS->new("something");
134 like($warning, qr/$warnregex/,
135 "Version string contains invalid data; ignoring");
136 ok (defined $version, 'defined $version');
138 # reset the test object to something reasonable
139 $version = $CLASS->new("1.2.3");
141 # Test boolean operator
142 ok ($version, 'boolean');
144 # Test class membership
145 isa_ok ( $version, $CLASS );
147 # Test comparison operators with self
148 diag "tests with self" if $Verbose;
149 is ( $version <=> $version, 0, '$version <=> $version == 0' );
150 ok ( $version == $version, '$version == $version' );
152 # Test Numeric Comparison operators
153 # test first with non-object
154 $version = $CLASS->new("5.006.001");
155 $new_version = "5.8.0";
156 diag "numeric tests with non-objects" if $Verbose;
157 ok ( $version == $version, '$version == $version' );
158 ok ( $version < $new_version, '$version < $new_version' );
159 ok ( $new_version > $version, '$new_version > $version' );
160 ok ( $version != $new_version, '$version != $new_version' );
162 # now test with existing object
163 $new_version = $CLASS->new($new_version);
164 diag "numeric tests with objects" if $Verbose;
165 ok ( $version < $new_version, '$version < $new_version' );
166 ok ( $new_version > $version, '$new_version > $version' );
167 ok ( $version != $new_version, '$version != $new_version' );
169 # now test with actual numbers
170 diag "numeric tests with numbers" if $Verbose;
171 ok ( $version->numify() == 5.006001, '$version->numify() == 5.006001' );
172 ok ( $version->numify() <= 5.006001, '$version->numify() <= 5.006001' );
173 ok ( $version->numify() < 5.008, '$version->numify() < 5.008' );
174 #ok ( $version->numify() > v5.005_02, '$version->numify() > 5.005_02' );
176 # test with long decimals
177 diag "Tests with extended decimal versions" if $Verbose;
178 $version = $CLASS->new(1.002003);
179 ok ( $version == "1.2.3", '$version == "1.2.3"');
180 ok ( $version->numify == 1.002003, '$version->numify == 1.002003');
181 $version = $CLASS->new("2002.09.30.1");
182 ok ( $version == "2002.9.30.1",'$version == 2002.9.30.1');
183 ok ( $version->numify == 2002.009030001,
184 '$version->numify == 2002.009030001');
186 # now test with alpha version form with string
187 $version = $CLASS->new("1.2.3");
188 $new_version = "1.2.3_4";
189 diag "numeric tests with alpha-style non-objects" if $Verbose;
190 ok ( $version < $new_version, '$version < $new_version' );
191 ok ( $new_version > $version, '$new_version > $version' );
192 ok ( $version != $new_version, '$version != $new_version' );
194 $version = $CLASS->new("1.2.4");
195 diag "numeric tests with alpha-style non-objects"
197 ok ( $version > $new_version, '$version > $new_version' );
198 ok ( $new_version < $version, '$new_version < $version' );
199 ok ( $version != $new_version, '$version != $new_version' );
201 # now test with alpha version form with object
202 $version = $CLASS->new("1.2.3");
203 $new_version = $CLASS->new("1.2.3_4");
204 diag "tests with alpha-style objects" if $Verbose;
205 ok ( $version < $new_version, '$version < $new_version' );
206 ok ( $new_version > $version, '$new_version > $version' );
207 ok ( $version != $new_version, '$version != $new_version' );
208 ok ( !$version->is_alpha, '!$version->is_alpha');
209 ok ( $new_version->is_alpha, '$new_version->is_alpha');
211 $version = $CLASS->new("1.2.4");
212 diag "tests with alpha-style objects" if $Verbose;
213 ok ( $version > $new_version, '$version > $new_version' );
214 ok ( $new_version < $version, '$new_version < $version' );
215 ok ( $version != $new_version, '$version != $new_version' );
217 $version = $CLASS->new("1.2.3.4");
218 $new_version = $CLASS->new("1.2.3_4");
219 diag "tests with alpha-style objects with same subversion"
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 diag "test implicit [in]equality" if $Verbose;
226 $version = $CLASS->new("v1.2.3");
227 $new_version = $CLASS->new("1.2.3.0");
228 ok ( $version == $new_version, '$version == $new_version' );
229 $new_version = $CLASS->new("1.2.3_0");
230 ok ( $version == $new_version, '$version == $new_version' );
231 $new_version = $CLASS->new("1.2.3.1");
232 ok ( $version < $new_version, '$version < $new_version' );
233 $new_version = $CLASS->new("1.2.3_1");
234 ok ( $version < $new_version, '$version < $new_version' );
235 $new_version = $CLASS->new("1.1.999");
236 ok ( $version > $new_version, '$version > $new_version' );
238 # that which is not expressly permitted is forbidden
239 diag "forbidden operations" if $Verbose;
240 ok ( !eval { ++$version }, "noop ++" );
241 ok ( !eval { --$version }, "noop --" );
242 ok ( !eval { $version/1 }, "noop /" );
243 ok ( !eval { $version*3 }, "noop *" );
244 ok ( !eval { abs($version) }, "noop abs" );
247 skip "version require'd instead of use'd, cannot test qv", 3
250 diag "testing qv" if $Verbose;
251 $version = qv("1.2");
252 is ( "$version", "v1.2", 'qv("1.2") == "1.2.0"' );
254 is ( "$version", "v1.2", 'qv(1.2) == "1.2.0"' );
255 isa_ok( qv('5.008'), $CLASS );
258 # test creation from existing version object
259 diag "create new from existing version" if $Verbose;
260 ok (eval {$new_version = $CLASS->new($version)},
261 "new from existing object");
262 ok ($new_version == $version, "class->new($version) identical");
263 $new_version = $version->new();
264 isa_ok ($new_version, $CLASS );
265 is ($new_version, "0", "version->new() doesn't clone");
266 $new_version = $version->new("1.2.3");
267 is ($new_version, "1.2.3" , '$version->new("1.2.3") works too');
269 # test the CVS revision mode
270 diag "testing CVS Revision" if $Verbose;
271 $version = new $CLASS qw$Revision: 1.2$;
272 ok ( $version == "1.2.0", 'qw$Revision: 1.2$ == 1.2.0' );
273 $version = new $CLASS qw$Revision: 1.2.3.4$;
274 ok ( $version == "1.2.3.4", 'qw$Revision: 1.2.3.4$ == 1.2.3.4' );
276 # test the CPAN style reduced significant digit form
277 diag "testing CPAN-style versions" if $Verbose;
278 $version = $CLASS->new("1.23_01");
279 is ( "$version" , "1.23_01", "CPAN-style alpha version" );
280 ok ( $version > 1.23, "1.23_01 > 1.23");
281 ok ( $version < 1.24, "1.23_01 < 1.24");
283 # test reformed UNIVERSAL::VERSION
284 diag "Replacement UNIVERSAL::VERSION tests" if $Verbose;
286 my $error_regex = $] < 5.006
287 ? 'version \d required'
288 : 'does not define \$...::VERSION';
291 open F, ">aaa.pm" or die "Cannot open aaa.pm: $!\n";
292 print F "package aaa;\n\$aaa::VERSION=0.58;\n1;\n";
296 eval "use lib '.'; use aaa $version";
297 unlike($@, qr/aaa version $version/,
298 'Replacement eval works with exact version');
300 # test as class method
301 $new_version = "aaa"->VERSION;
302 cmp_ok($new_version,'==',$version, "Called as class method");
304 eval "print Completely::Unknown::Module->VERSION";
306 unlike($@, qr/$error_regex/,
307 "Don't freak if the module doesn't even exist");
310 unlike($@, qr/defines neither package nor VERSION/,
311 "Don't freak if the module doesn't even exist");
314 # this should fail even with old UNIVERSAL::VERSION
316 eval "use lib '.'; use aaa $version";
317 like($@, qr/aaa version $version/,
318 'Replacement eval works with incremented version');
320 $version =~ s/0+$//; #convert to string and remove trailing 0's
321 chop($version); # shorten by 1 digit, should still succeed
322 eval "use lib '.'; use aaa $version";
323 unlike($@, qr/aaa version $version/,
324 'Replacement eval works with single digit');
326 # this would fail with old UNIVERSAL::VERSION
328 eval "use lib '.'; use aaa $version";
329 like($@, qr/aaa version $version/,
330 'Replacement eval works with incremented digit');
334 { # dummy up some variously broken modules for testing
335 open F, ">xxx.pm" or die "Cannot open xxx.pm: $!\n";
339 eval "use lib '.'; use xxx 3;";
341 like($@, qr/$error_regex/,
342 'Replacement handles modules without package or VERSION');
345 like($@, qr/defines neither package nor VERSION/,
346 'Replacement handles modules without package or VERSION');
348 eval "use lib '.'; use xxx; \$version = xxx->VERSION";
349 unlike ($@, qr/$error_regex/,
350 'Replacement handles modules without package or VERSION');
351 ok (!defined($version), "Called as class method");
355 { # dummy up some variously broken modules for testing
356 open F, ">yyy.pm" or die "Cannot open yyy.pm: $!\n";
357 print F "package yyy;\n#look ma no VERSION\n1;\n";
359 eval "use lib '.'; use yyy 3;";
360 like ($@, qr/$error_regex/,
361 'Replacement handles modules without VERSION');
362 eval "use lib '.'; use yyy; print yyy->VERSION";
363 unlike ($@, qr/$error_regex/,
364 'Replacement handles modules without VERSION');
368 { # dummy up some variously broken modules for testing
369 open F, ">zzz.pm" or die "Cannot open zzz.pm: $!\n";
370 print F "package zzz;\n\@VERSION = ();\n1;\n";
372 eval "use lib '.'; use zzz 3;";
373 like ($@, qr/$error_regex/,
374 'Replacement handles modules without VERSION');
375 eval "use lib '.'; use zzz; print zzz->VERSION";
376 unlike ($@, qr/$error_regex/,
377 'Replacement handles modules without VERSION');
382 skip 'Cannot test bare v-strings with Perl < 5.6.0', 4
384 diag "Tests with v-strings" if $Verbose;
385 $version = $CLASS->new(1.2.3);
386 ok("$version" == "v1.2.3", '"$version" == 1.2.3');
387 $version = $CLASS->new(1.0.0);
388 $new_version = $CLASS->new(1);
389 ok($version == $new_version, '$version == $new_version');
390 skip "version require'd instead of use'd, cannot test qv", 1
392 $version = qv(1.2.3);
393 ok("$version" == "v1.2.3", 'v-string initialized qv()');
396 diag "Tests with real-world (malformed) data" if $Verbose;
398 # trailing zero testing (reported by Andreas Koenig).
399 $version = $CLASS->new("1");
400 ok($version->numify eq "1.000", "trailing zeros preserved");
401 $version = $CLASS->new("1.0");
402 ok($version->numify eq "1.000", "trailing zeros preserved");
403 $version = $CLASS->new("1.0.0");
404 ok($version->numify eq "1.000000", "trailing zeros preserved");
405 $version = $CLASS->new("1.0.0.0");
406 ok($version->numify eq "1.000000000", "trailing zeros preserved");
408 # leading zero testing (reported by Andreas Koenig).
409 $version = $CLASS->new(".7");
410 ok($version->numify eq "0.700", "leading zero inferred");
412 # leading space testing (reported by Andreas Koenig).
413 $version = $CLASS->new(" 1.7");
414 ok($version->numify eq "1.700", "leading space ignored");
416 # RT 19517 - deal with undef and 'undef' initialization
417 ok("$version" ne 'undef', "Undef version comparison #1");
418 ok("$version" ne undef, "Undef version comparison #2");
419 $version = $CLASS->new('undef');
420 unlike($warning, qr/^Version string 'undef' contains invalid data/,
421 "Version string 'undef'");
423 $version = $CLASS->new(undef);
424 like($warning, qr/^Use of uninitialized value/,
425 "Version string 'undef'");
426 ok($version == 'undef', "Undef version comparison #3");
427 ok($version == undef, "Undef version comparison #4");
428 eval "\$version = \$CLASS->new()"; # no parameter at all
429 unlike($@, qr/^Bizarre copy of CODE/, "No initializer at all");
430 ok($version == 'undef', "Undef version comparison #5");
431 ok($version == undef, "Undef version comparison #6");
433 $version = $CLASS->new(0.000001);
434 unlike($warning, qr/^Version string '1e-06' contains invalid data/,
435 "Very small version objects");
439 # dummy up a legal module for testing RT#19017
440 open F, ">www.pm" or die "Cannot open www.pm: $!\n";
443 use version; \$VERSION = qv('0.0.4');
448 eval "use lib '.'; use www 0.000008;";
449 like ($@, qr/^www version 0.000008 required/,
450 "Make sure very small versions don't freak");
451 eval "use lib '.'; use www 1;";
452 like ($@, qr/^www version 1 required/,
453 "Comparing vs. version with no decimal");
454 eval "use lib '.'; use www 1.;";
455 like ($@, qr/^www version 1 required/,
456 "Comparing vs. version with decimal only");
458 if ( $] < 5.006_000 ) {
460 skip 'Cannot "use" extended versions with Perl < 5.6.0', 3;
462 eval "use lib '.'; use www v0.0.8;";
463 my $regex = "^www version v0.0.8 required";
464 like ($@, qr/$regex/, "Make sure very small versions don't freak");
466 $regex =~ s/8/4/; # set for second test
467 eval "use lib '.'; use www v0.0.4;";
468 unlike($@, qr/$regex/, 'Succeed - required == VERSION');
469 cmp_ok ( "www"->VERSION, 'eq', '0.0.4', 'No undef warnings' );
474 open F, ">vvv.pm" or die "Cannot open vvv.pm: $!\n";
477 use base qw(version);
481 # need to eliminate any other qv()'s
483 ok(!defined(&{"main\::qv"}), "make sure we cleared qv() properly");
484 eval "use lib '.'; use vvv;";
485 ok(defined(&{"main\::qv"}), "make sure we exported qv() properly");
486 isa_ok( qv(1.2), "vvv");
490 if ( $] < 5.006_000 ) {
491 skip 'Cannot "use" extended versions with Perl < 5.6.0', 3;
493 open F, ">uuu.pm" or die "Cannot open uuu.pm: $!\n";
500 eval "use lib '.'; use uuu 1.001;";
501 like ($@, qr/^uuu version 1.001 required/,
502 "User typed numeric so we error with numeric");
503 eval "use lib '.'; use uuu v1.1.0;";
504 like ($@, qr/^uuu version v1.1.0 required/,
505 "User typed extended so we error with extended");
510 # test locale handling
512 local $SIG{__WARN__} = sub { $warning = $_[0] };
513 my $ver = 1.23; # has to be floating point number
517 $loc = POSIX::setlocale( &POSIX::LC_ALL, $_);
518 last if POSIX::localeconv()->{decimal_point} eq ',';
520 skip 'Cannot test locale handling without a comma locale', 4
521 unless ( $loc and ($ver eq '1,23') );
523 diag ("Testing locale handling with $loc") if $Verbose;
525 my $v = $CLASS->new($ver);
526 unlike($warning,qr/Version string '1,23' contains invalid data/,
527 "Process locale-dependent floating point");
528 is ($v, "1.23", "Locale doesn't apply to version objects");
529 ok ($v == $ver, "Comparison to locale floating point");
532 eval 'my $v = $CLASS->new("1._1");';
533 unlike($@, qr/^Invalid version format \(alpha with zero width\)/,
534 "Invalid version format 1._1");