This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Move Log::Message::Simple from ext/ to cpan/
[perl5.git] / lib / version.t
1 #! /usr/local/perl -w
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'
4
5 #########################
6
7 use Test::More qw(no_plan);
8 use Data::Dumper;
9 require Test::Harness;
10 no warnings 'once';
11 *Verbose = \$Test::Harness::Verbose;
12 use POSIX qw/locale_h/;
13 use File::Temp qw/tempfile/;
14 use File::Basename;
15
16 BEGIN {
17     use_ok("version", 0.77);
18     # If we made it this far, we are ok.
19 }
20
21 my $Verbose;
22
23 diag "Tests with base class" unless $ENV{PERL_CORE};
24
25 BaseTests("version","new","qv");
26 BaseTests("version","new","declare");
27 BaseTests("version","parse", "qv");
28 BaseTests("version","parse", "declare");
29
30 # dummy up a redundant call to satify David Wheeler
31 local $SIG{__WARN__} = sub { die $_[0] };
32 eval 'use version;';
33 unlike ($@, qr/^Subroutine main::declare redefined/,
34     "Only export declare once per package (to prevent redefined warnings)."); 
35
36 package version::Bad;
37 use base 'version';
38 sub new { my($self,$n)=@_;  bless \$n, $self }
39
40 package main;
41
42 my $warning;
43 local $SIG{__WARN__} = sub { $warning = $_[0] };
44 my ($fh, $filename) = tempfile('tXXXXXXX', SUFFIX => '.pm', UNLINK => 1);
45 (my $package = basename($filename)) =~ s/\.pm$//;
46 print $fh <<"EOF";
47 # This is an empty subclass
48 package $package;
49 use base 'version';
50 use vars '\$VERSION';
51 \$VERSION=0.001;
52 EOF
53 close $fh;
54
55 sub 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
61 diag "Tests with empty derived class"  unless $ENV{PERL_CORE};
62
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" );
69
70 my $verobj = version::->new("1.2.4");
71 ok( $verobj > $testobj, "Comparison vs parent class" );
72
73 BaseTests($package, "new", "qv");
74 main_reset;
75 use_ok($package, 0.001, "declare");
76 BaseTests($package, "new", "declare");
77 main_reset;
78 use_ok($package, 0.001);
79 BaseTests($package, "parse", "qv");
80 main_reset;
81 use_ok($package, 0.001, "declare");
82 BaseTests($package, "parse", "declare");
83
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/,
98     "Bad subclass vcmp");
99
100 sub BaseTests {
101
102     my ($CLASS, $method, $qv_declare) = @_;
103     my $warning;
104     local $SIG{__WARN__} = sub { $warning = $_[0] };
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
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"' );
115     
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"' );
122     
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' );
132     
133     # test illegal formats
134     diag "test illegal formats" unless $ENV{PERL_CORE};
135     eval {my $version = $CLASS->$method("1.2_3_4")};
136     like($@, qr/multiple underscores/,
137         "Invalid version format (multiple underscores)");
138     
139     eval {my $version = $CLASS->$method("1.2_3.4")};
140     like($@, qr/underscores before decimal/,
141         "Invalid version format (underscores before decimal)");
142     
143     eval {my $version = $CLASS->$method("1_2")};
144     like($@, qr/alpha without decimal/,
145         "Invalid version format (alpha without decimal)");
146     
147     # for this test, upgrade the warn() to die()
148     eval {
149         local $SIG{__WARN__} = sub { die $_[0] };
150         $version = $CLASS->$method("1.2b3");
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
159     {
160     $version = $CLASS->$method("99 and 44/100 pure");
161
162     like($warning, qr/$warnregex/,
163         "Version string contains invalid data; ignoring");
164     is ("$version", "99", '$version eq "99"');
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     
168     $version = $CLASS->$method("something");
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
174     $version = $CLASS->$method("1.2.3");
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
183     diag "tests with self" unless $ENV{PERL_CORE};
184     is ( $version <=> $version, 0, '$version <=> $version == 0' );
185     ok ( $version == $version, '$version == $version' );
186     
187     # Test Numeric Comparison operators
188     # test first with non-object
189     $version = $CLASS->$method("5.006.001");
190     $new_version = "5.8.0";
191     diag "numeric tests with non-objects" unless $ENV{PERL_CORE};
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
198     $new_version = $CLASS->$method($new_version);
199     diag "numeric tests with objects" unless $ENV{PERL_CORE};
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
205     diag "numeric tests with numbers" unless $ENV{PERL_CORE};
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
212     diag "Tests with extended decimal versions" unless $ENV{PERL_CORE};
213     $version = $CLASS->$method(1.002003);
214     ok ( $version == "1.2.3", '$version == "1.2.3"');
215     ok ( $version->numify == 1.002003, '$version->numify == 1.002003');
216     $version = $CLASS->$method("2002.09.30.1");
217     ok ( $version == "2002.9.30.1",'$version == 2002.9.30.1');
218     ok ( $version->numify == 2002.009030001,
219         '$version->numify == 2002.009030001');
220     
221     # now test with alpha version form with string
222     $version = $CLASS->$method("1.2.3");
223     $new_version = "1.2.3_4";
224     diag "numeric tests with alpha-style non-objects" unless $ENV{PERL_CORE};
225     ok ( $version < $new_version, '$version < $new_version' );
226     ok ( $new_version > $version, '$new_version > $version' );
227     ok ( $version != $new_version, '$version != $new_version' );
228     
229     $version = $CLASS->$method("1.2.4");
230     diag "numeric tests with alpha-style non-objects"
231         unless $ENV{PERL_CORE};
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
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};
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     
246     $version = $CLASS->$method("1.2.4");
247     diag "tests with alpha-style objects" unless $ENV{PERL_CORE};
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     
252     $version = $CLASS->$method("1.2.3.4");
253     $new_version = $CLASS->$method("1.2.3_4");
254     diag "tests with alpha-style objects with same subversion"
255         unless $ENV{PERL_CORE};
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     
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");
263     ok ( $version == $new_version, '$version == $new_version' );
264     $new_version = $CLASS->$method("1.2.3_0");
265     ok ( $version == $new_version, '$version == $new_version' );
266     $new_version = $CLASS->$method("1.2.3.1");
267     ok ( $version < $new_version, '$version < $new_version' );
268     $new_version = $CLASS->$method("1.2.3_1");
269     ok ( $version < $new_version, '$version < $new_version' );
270     $new_version = $CLASS->$method("1.1.999");
271     ok ( $version > $new_version, '$version > $new_version' );
272     
273     # that which is not expressly permitted is forbidden
274     diag "forbidden operations" unless $ENV{PERL_CORE};
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" );
280
281 SKIP: {
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 );
291 }
292
293     # test creation from existing version object
294     diag "create new from existing version" unless $ENV{PERL_CORE};
295     ok (eval {$new_version = $CLASS->$method($version)},
296             "new from existing object");
297     ok ($new_version == $version, "class->$method($version) identical");
298     $new_version = $version->$method();
299     isa_ok ($new_version, $CLASS );
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');
303
304     # test the CVS revision mode
305     diag "testing CVS Revision" unless $ENV{PERL_CORE};
306     $version = new $CLASS qw$Revision: 1.2$;
307     ok ( $version == "1.2.0", 'qw$Revision: 1.2$ == 1.2.0' );
308     $version = new $CLASS qw$Revision: 1.2.3.4$;
309     ok ( $version == "1.2.3.4", 'qw$Revision: 1.2.3.4$ == 1.2.3.4' );
310     
311     # test the CPAN style reduced significant digit form
312     diag "testing CPAN-style versions" unless $ENV{PERL_CORE};
313     $version = $CLASS->$method("1.23_01");
314     is ( "$version" , "1.23_01", "CPAN-style alpha version" );
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
319     diag "Replacement UNIVERSAL::VERSION tests" unless $ENV{PERL_CORE};
320
321     my $error_regex = $] < 5.006
322         ? 'version \d required'
323         : 'does not define \$t.{7}::VERSION';
324     
325     {
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;
330
331         $version = 0.58;
332         eval "use lib '.'; use $package $version";
333         unlike($@, qr/$package version $version/,
334                 'Replacement eval works with exact version');
335         
336         # test as class method
337         $new_version = $package->VERSION;
338         cmp_ok($new_version,'==',$version, "Called as class method");
339
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
351         $version += 0.01;
352         eval "use lib '.'; use $package $version";
353         like($@, qr/$package version $version/,
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
358         eval "use lib '.'; use $package $version";
359         unlike($@, qr/$package version $version/,
360                 'Replacement eval works with single digit');
361         
362         # this would fail with old UNIVERSAL::VERSION
363         $version += 0.1;
364         eval "use lib '.'; use $package $version";
365         like($@, qr/$package version $version/,
366                 'Replacement eval works with incremented digit');
367         unlink $filename;
368     }
369
370     { # dummy up some variously broken modules for testing
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;
375
376         eval "use lib '.'; use $package 3;";
377         if ( $] < 5.008 ) {
378             like($@, qr/$error_regex/,
379                 'Replacement handles modules without package or VERSION'); 
380         }
381         else {
382             like($@, qr/defines neither package nor VERSION/,
383                 'Replacement handles modules without package or VERSION'); 
384         }
385         eval "use lib '.'; use $package; \$version = $package->VERSION";
386         unlike ($@, qr/$error_regex/,
387             'Replacement handles modules without package or VERSION'); 
388         ok (!defined($version), "Called as class method");
389         unlink $filename;
390     }
391     
392     { # dummy up some variously broken modules for testing
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;";
398         like ($@, qr/$error_regex/,
399             'Replacement handles modules without VERSION'); 
400         eval "use lib '.'; use $package; print $package->VERSION";
401         unlike ($@, qr/$error_regex/,
402             'Replacement handles modules without VERSION'); 
403         unlink $filename;
404     }
405
406     { # dummy up some variously broken modules for testing
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;";
412         like ($@, qr/$error_regex/,
413             'Replacement handles modules without VERSION'); 
414         eval "use lib '.'; use $package; print $package->VERSION";
415         unlike ($@, qr/$error_regex/,
416             'Replacement handles modules without VERSION'); 
417         unlink $filename;
418     }
419
420 SKIP:   {
421         skip 'Cannot test bare v-strings with Perl < 5.6.0', 4
422                 if $] < 5.006_000; 
423         diag "Tests with v-strings" unless $ENV{PERL_CORE};
424         $version = $CLASS->$method(1.2.3);
425         ok("$version" == "v1.2.3", '"$version" == 1.2.3');
426         $version = $CLASS->$method(1.0.0);
427         $new_version = $CLASS->$method(1);
428         ok($version == $new_version, '$version == $new_version');
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()');
433     }
434
435     diag "Tests with real-world (malformed) data" unless $ENV{PERL_CORE};
436
437     # trailing zero testing (reported by Andreas Koenig).
438     $version = $CLASS->$method("1");
439     ok($version->numify eq "1.000", "trailing zeros preserved");
440     $version = $CLASS->$method("1.0");
441     ok($version->numify eq "1.000", "trailing zeros preserved");
442     $version = $CLASS->$method("1.0.0");
443     ok($version->numify eq "1.000000", "trailing zeros preserved");
444     $version = $CLASS->$method("1.0.0.0");
445     ok($version->numify eq "1.000000000", "trailing zeros preserved");
446     
447     # leading zero testing (reported by Andreas Koenig).
448     $version = $CLASS->$method(".7");
449     ok($version->numify eq "0.700", "leading zero inferred");
450
451     # leading space testing (reported by Andreas Koenig).
452     $version = $CLASS->$method(" 1.7");
453     ok($version->numify eq "1.700", "leading space ignored");
454
455     # RT 19517 - deal with undef and 'undef' initialization
456     ok("$version" ne 'undef', "Undef version comparison #1");
457     ok("$version" ne undef, "Undef version comparison #2");
458     $version = $CLASS->$method('undef');
459     unlike($warning, qr/^Version string 'undef' contains invalid data/,
460         "Version string 'undef'");
461
462     $version = $CLASS->$method(undef);
463     like($warning, qr/^Use of uninitialized value/,
464         "Version string 'undef'");
465     ok($version == 'undef', "Undef version comparison #3");
466     ok($version ==  undef,  "Undef version comparison #4");
467     eval "\$version = \$CLASS->$method()"; # no parameter at all
468     unlike($@, qr/^Bizarre copy of CODE/, "No initializer at all");
469     ok($version == 'undef', "Undef version comparison #5");
470     ok($version ==  undef,  "Undef version comparison #6");
471
472     $version = $CLASS->$method(0.000001);
473     unlike($warning, qr/^Version string '1e-06' contains invalid data/,
474         "Very small version objects");
475     }
476
477 SKIP: {
478         my $warning;
479         local $SIG{__WARN__} = sub { $warning = $_[0] };
480         # dummy up a legal module for testing RT#19017
481         my ($fh, $filename) = tempfile('tXXXXXXX', SUFFIX => '.pm', UNLINK => 1);
482         (my $package = basename($filename)) =~ s/\.pm$//;
483         print $fh <<"EOF";
484 package $package;
485 use $CLASS; \$VERSION = ${CLASS}->new('0.0.4');
486 1;
487 EOF
488         close $fh;
489
490         eval "use lib '.'; use $package 0.000008;";
491         like ($@, qr/^$package version 0.000008 required/,
492             "Make sure very small versions don't freak"); 
493         eval "use lib '.'; use $package 1;";
494         like ($@, qr/^$package version 1 required/,
495             "Comparing vs. version with no decimal"); 
496         eval "use lib '.'; use $package 1.;";
497         like ($@, qr/^$package version 1 required/,
498             "Comparing vs. version with decimal only"); 
499         if ( $] < 5.006_000 ) {
500             skip 'Cannot "use" extended versions with Perl < 5.6.0', 3; 
501         }
502         eval "use lib '.'; use $package v0.0.8;";
503         my $regex = "^$package version v0.0.8 required";
504         like ($@, qr/$regex/, "Make sure very small versions don't freak"); 
505
506         $regex =~ s/8/4/; # set for second test
507         eval "use lib '.'; use $package v0.0.4;";
508         unlike($@, qr/$regex/, 'Succeed - required == VERSION');
509         cmp_ok ( $package->VERSION, 'eq', '0.0.4', 'No undef warnings' );
510         unlink $filename;
511     }
512
513 SKIP: {
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";
519 package $package;
520 use base qw(version);
521 1;
522 EOF
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 }
532
533 SKIP: {
534         if ( $] < 5.006_000 ) {
535             skip 'Cannot "use" extended versions with Perl < 5.6.0', 3; 
536         }
537         my ($fh, $filename) = tempfile('tXXXXXXX', SUFFIX => '.pm', UNLINK => 1);
538         (my $package = basename($filename)) =~ s/\.pm$//;
539         print $fh <<"EOF";
540 package $package;
541 \$VERSION = 1.0;
542 1;
543 EOF
544         close $fh;
545         eval "use lib '.'; use $package 1.001;";
546         like ($@, qr/^$package version 1.001 required/,
547             "User typed numeric so we error with numeric"); 
548         eval "use lib '.'; use $package v1.1.0;";
549         like ($@, qr/^$package version v1.1.0 required/,
550             "User typed extended so we error with extended"); 
551         unlink $filename;
552     }
553
554 SKIP: {
555         # test locale handling
556         my $warning;
557         local $SIG{__WARN__} = sub { $warning = $_[0] };
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
564         my $ver = 1.23;  # has to be floating point number
565         my $orig_loc = setlocale( LC_ALL );
566         my $loc;
567         while (<DATA>) {
568             chomp;
569             $loc = setlocale( LC_ALL, $_);
570             last if localeconv()->{decimal_point} eq ',';
571         }
572         skip 'Cannot test locale handling without a comma locale', 4
573             unless ( $loc and ($ver eq '1,23') );
574
575         diag ("Testing locale handling with $loc") unless $ENV{PERL_CORE};
576
577         $v = $CLASS->$method($ver);
578         unlike($warning, qr/Version string '1,23' contains invalid data/,
579             "Process locale-dependent floating point");
580         is ($v, "1.23", "Locale doesn't apply to version objects");
581         ok ($v == $ver, "Comparison to locale floating point");
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";
591 package $package;
592 use POSIX qw(locale_h);
593 \$^W = 1;
594 use $CLASS;
595 setlocale (LC_ALL, '$loc');
596 use $CLASS ;
597 eval "use Socket 1.7";
598 setlocale( LC_ALL, '$orig_loc');
599 1;
600 EOF
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');
606     }
607
608     eval 'my $v = $CLASS->$method("1._1");';
609     unlike($@, qr/^Invalid version format \(alpha with zero width\)/,
610         "Invalid version format 1._1");
611
612     {
613         my $warning;
614         local $SIG{__WARN__} = sub { $warning = $_[0] };
615         eval 'my $v = $CLASS->$method(~0);';
616         unlike($@, qr/Integer overflow in version/, "Too large version");
617         like($warning, qr/Integer overflow in version/, "Too large version");
618     }
619
620     {
621         # http://rt.cpan.org/Public/Bug/Display.html?id=30004
622         my $v1 = $CLASS->$method("v0.1_1");
623         (my $alpha1 = Dumper($v1)) =~ s/.+'alpha' => ([^,]+),.+/$1/ms;
624         my $v2 = $CLASS->$method($v1);
625         (my $alpha2 = Dumper($v2)) =~ s/.+'alpha' => ([^,]+),.+/$1/ms;
626         is $alpha2, $alpha1, "Don't fall for Data::Dumper's tricks";
627     }
628
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     }
636 }
637
638 1;
639
640 __DATA__
641 af_ZA
642 af_ZA.utf8
643 an_ES
644 an_ES.utf8
645 az_AZ.utf8
646 be_BY
647 be_BY.utf8
648 bg_BG
649 bg_BG.utf8
650 br_FR
651 br_FR@euro
652 br_FR.utf8
653 bs_BA
654 bs_BA.utf8
655 ca_ES
656 ca_ES@euro
657 ca_ES.utf8
658 cs_CZ
659 cs_CZ.utf8
660 da_DK
661 da_DK.utf8
662 de_AT
663 de_AT@euro
664 de_AT.utf8
665 de_BE
666 de_BE@euro
667 de_BE.utf8
668 de_DE
669 de_DE@euro
670 de_DE.utf8
671 de_LU
672 de_LU@euro
673 de_LU.utf8
674 el_GR
675 el_GR.utf8
676 en_DK
677 en_DK.utf8
678 es_AR
679 es_AR.utf8
680 es_BO
681 es_BO.utf8
682 es_CL
683 es_CL.utf8
684 es_CO
685 es_CO.utf8
686 es_EC
687 es_EC.utf8
688 es_ES
689 es_ES@euro
690 es_ES.utf8
691 es_PY
692 es_PY.utf8
693 es_UY
694 es_UY.utf8
695 es_VE
696 es_VE.utf8
697 et_EE
698 et_EE.iso885915
699 et_EE.utf8
700 eu_ES
701 eu_ES@euro
702 eu_ES.utf8
703 fi_FI
704 fi_FI@euro
705 fi_FI.utf8
706 fo_FO
707 fo_FO.utf8
708 fr_BE
709 fr_BE@euro
710 fr_BE.utf8
711 fr_CA
712 fr_CA.utf8
713 fr_CH
714 fr_CH.utf8
715 fr_FR
716 fr_FR@euro
717 fr_FR.utf8
718 fr_LU
719 fr_LU@euro
720 fr_LU.utf8
721 gl_ES
722 gl_ES@euro
723 gl_ES.utf8
724 hr_HR
725 hr_HR.utf8
726 hu_HU
727 hu_HU.utf8
728 id_ID
729 id_ID.utf8
730 is_IS
731 is_IS.utf8
732 it_CH
733 it_CH.utf8
734 it_IT
735 it_IT@euro
736 it_IT.utf8
737 ka_GE
738 ka_GE.utf8
739 kk_KZ
740 kk_KZ.utf8
741 kl_GL
742 kl_GL.utf8
743 lt_LT
744 lt_LT.utf8
745 lv_LV
746 lv_LV.utf8
747 mk_MK
748 mk_MK.utf8
749 mn_MN
750 mn_MN.utf8
751 nb_NO
752 nb_NO.utf8
753 nl_BE
754 nl_BE@euro
755 nl_BE.utf8
756 nl_NL
757 nl_NL@euro
758 nl_NL.utf8
759 nn_NO
760 nn_NO.utf8
761 no_NO
762 no_NO.utf8
763 oc_FR
764 oc_FR.utf8
765 pl_PL
766 pl_PL.utf8
767 pt_BR
768 pt_BR.utf8
769 pt_PT
770 pt_PT@euro
771 pt_PT.utf8
772 ro_RO
773 ro_RO.utf8
774 ru_RU
775 ru_RU.koi8r
776 ru_RU.utf8
777 ru_UA
778 ru_UA.utf8
779 se_NO
780 se_NO.utf8
781 sh_YU
782 sh_YU.utf8
783 sk_SK
784 sk_SK.utf8
785 sl_SI
786 sl_SI.utf8
787 sq_AL
788 sq_AL.utf8
789 sr_CS
790 sr_CS.utf8
791 sv_FI
792 sv_FI@euro
793 sv_FI.utf8
794 sv_SE
795 sv_SE.iso885915
796 sv_SE.utf8
797 tg_TJ
798 tg_TJ.utf8
799 tr_TR
800 tr_TR.utf8
801 tt_RU.utf8
802 uk_UA
803 uk_UA.utf8
804 vi_VN
805 vi_VN.tcvn
806 wa_BE
807 wa_BE@euro
808 wa_BE.utf8
809