Commit | Line | Data |
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 |
7 | use Test::More qw(no_plan); |
72287d96 |
8 | use Data::Dumper; |
c8a14fb6 |
9 | require Test::Harness; |
10 | no warnings 'once'; |
11 | *Verbose = \$Test::Harness::Verbose; |
f941e658 |
12 | use POSIX qw/locale_h/; |
13 | use File::Temp qw/tempfile/; |
14 | use File::Basename; |
a7ad731c |
15 | |
5eb567df |
16 | BEGIN { |
f941e658 |
17 | use_ok("version", 0.77); |
18 | # If we made it this far, we are ok. |
5eb567df |
19 | } |
20 | |
f941e658 |
21 | my $Verbose; |
22 | |
23 | diag "Tests with base class" unless $ENV{PERL_CORE}; |
137d6fc0 |
24 | |
f941e658 |
25 | BaseTests("version","new","qv"); |
26 | BaseTests("version","new","declare"); |
27 | BaseTests("version","parse", "qv"); |
28 | BaseTests("version","parse", "declare"); |
137d6fc0 |
29 | |
f941e658 |
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)."); |
137d6fc0 |
35 | |
e0218a61 |
36 | package version::Bad; |
f941e658 |
37 | use base 'version'; |
e0218a61 |
38 | sub new { my($self,$n)=@_; bless \$n, $self } |
39 | |
137d6fc0 |
40 | package main; |
f941e658 |
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 ); |
137d6fc0 |
66 | ok( $testobj->numify == 1.002003, "Numified correctly" ); |
9137345a |
67 | ok( $testobj->stringify eq "1.002003", "Stringified correctly" ); |
68 | ok( $testobj->normal eq "v1.2.3", "Normalified correctly" ); |
137d6fc0 |
69 | |
f941e658 |
70 | my $verobj = version::->new("1.2.4"); |
137d6fc0 |
71 | ok( $verobj > $testobj, "Comparison vs parent class" ); |
137d6fc0 |
72 | |
f941e658 |
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}; |
e0218a61 |
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"); |
f941e658 |
96 | eval { my $test = ($testobj > 1.0) }; |
e0218a61 |
97 | like($@, qr/Invalid version object/, |
98 | "Bad subclass vcmp"); |
99 | |
137d6fc0 |
100 | sub 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 |
281 | SKIP: { |
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 |
420 | SKIP: { |
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 |
477 | SKIP: { |
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"; |
484 | package $package; |
485 | use $CLASS; \$VERSION = ${CLASS}->new('0.0.4'); |
c8a14fb6 |
486 | 1; |
487 | EOF |
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 |
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; |
92dcf8ce |
520 | use base qw(version); |
521 | 1; |
522 | EOF |
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 | |
533 | SKIP: { |
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"; |
540 | package $package; |
ac0e6a2f |
541 | \$VERSION = 1.0; |
542 | 1; |
543 | EOF |
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 | |
554 | SKIP: { |
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"; |
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'); |
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 | |
638 | 1; |
d69f6151 |
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 | |