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"); |
9b463b21 |
99 | strict_lax_tests(); |
100 | |
101 | # do strict lax tests in a sub to isolate a package to test importing |
102 | sub strict_lax_tests { |
103 | package temp12345; |
104 | # copied from perl core test t/op/packagev.t |
105 | # format: STRING STRICT_OK LAX_OK |
106 | my $strict_lax_data = << 'CASE_DATA'; |
107 | 1.00 pass pass |
108 | 1.00001 pass pass |
109 | 0.123 pass pass |
110 | 12.345 pass pass |
111 | 42 pass pass |
112 | 0 pass pass |
113 | 0.0 pass pass |
114 | v1.2.3 pass pass |
115 | v1.2.3.4 pass pass |
116 | v0.1.2 pass pass |
117 | v0.0.0 pass pass |
118 | 01 fail pass |
119 | 01.0203 fail pass |
120 | v01 fail pass |
121 | v01.02.03 fail pass |
122 | .1 fail pass |
123 | .1.2 fail pass |
124 | 1. fail pass |
125 | 1.a fail fail |
126 | 1._ fail fail |
127 | 1.02_03 fail pass |
128 | v1.2_3 fail pass |
129 | v1.02_03 fail pass |
130 | v1.2_3_4 fail fail |
131 | v1.2_3.4 fail fail |
132 | 1.2_3.4 fail fail |
133 | 0_ fail fail |
134 | 1_ fail fail |
135 | 1_. fail fail |
136 | 1.1_ fail fail |
137 | 1.02_03_04 fail fail |
138 | 1.2.3 fail pass |
139 | v1.2 fail pass |
140 | v0 fail pass |
141 | v1 fail pass |
142 | v.1.2.3 fail fail |
143 | v fail fail |
144 | v1.2345.6 fail pass |
145 | undef fail pass |
146 | 1a fail fail |
147 | 1.2a3 fail fail |
148 | bar fail fail |
149 | _ fail fail |
150 | CASE_DATA |
151 | |
152 | require version; |
153 | version->import( qw/is_strict is_lax/ ); |
154 | for my $case ( split qr/\n/, $strict_lax_data ) { |
155 | my ($v, $strict, $lax) = split qr/\t+/, $case; |
156 | main::ok( $strict eq 'pass' ? is_strict($v) : ! is_strict($v), "is_strict($v) [$strict]" ); |
157 | main::ok( $strict eq 'pass' ? version::is_strict($v) : ! version::is_strict($v), "version::is_strict($v) [$strict]" ); |
158 | main::ok( $lax eq 'pass' ? is_lax($v) : ! is_lax($v), "is_lax($v) [$lax]" ); |
159 | main::ok( $lax eq 'pass' ? version::is_lax($v) : ! version::is_lax($v), "version::is_lax($v) [$lax]" ); |
160 | } |
161 | } |
e0218a61 |
162 | |
137d6fc0 |
163 | sub BaseTests { |
164 | |
f941e658 |
165 | my ($CLASS, $method, $qv_declare) = @_; |
692a467c |
166 | my $warning; |
167 | local $SIG{__WARN__} = sub { $warning = $_[0] }; |
317f7c8a |
168 | |
169 | # Insert your test code below, the Test module is use()ed here so read |
170 | # its man page ( perldoc Test ) for help writing this test script. |
171 | |
172 | # Test bare number processing |
f941e658 |
173 | diag "tests with bare numbers" unless $ENV{PERL_CORE}; |
174 | $version = $CLASS->$method(5.005_03); |
8cb289bd |
175 | is ( "$version" , "5.00503" , '5.005_03 eq 5.00503' ); |
f941e658 |
176 | $version = $CLASS->$method(1.23); |
8cb289bd |
177 | is ( "$version" , "1.23" , '1.23 eq "1.23"' ); |
317f7c8a |
178 | |
179 | # Test quoted number processing |
f941e658 |
180 | diag "tests with quoted numbers" unless $ENV{PERL_CORE}; |
181 | $version = $CLASS->$method("5.005_03"); |
8cb289bd |
182 | is ( "$version" , "5.005_03" , '"5.005_03" eq "5.005_03"' ); |
f941e658 |
183 | $version = $CLASS->$method("v1.23"); |
8cb289bd |
184 | is ( "$version" , "v1.23" , '"v1.23" eq "v1.23"' ); |
317f7c8a |
185 | |
186 | # Test stringify operator |
f941e658 |
187 | diag "tests with stringify" unless $ENV{PERL_CORE}; |
188 | $version = $CLASS->$method("5.005"); |
317f7c8a |
189 | is ( "$version" , "5.005" , '5.005 eq "5.005"' ); |
f941e658 |
190 | $version = $CLASS->$method("5.006.001"); |
8cb289bd |
191 | is ( "$version" , "5.006.001" , '5.006.001 eq v5.6.1' ); |
692a467c |
192 | unlike ($warning, qr/v-string without leading 'v' deprecated/, 'No leading v'); |
193 | $version = $CLASS->$method("v1.2.3_4"); |
194 | is ( "$version" , "v1.2.3_4" , 'alpha version 1.2.3_4 eq v1.2.3_4' ); |
317f7c8a |
195 | |
196 | # test illegal formats |
f941e658 |
197 | diag "test illegal formats" unless $ENV{PERL_CORE}; |
91152fc1 |
198 | eval {$version = $CLASS->$method("1.2_3_4")}; |
317f7c8a |
199 | like($@, qr/multiple underscores/, |
200 | "Invalid version format (multiple underscores)"); |
201 | |
91152fc1 |
202 | eval {$version = $CLASS->$method("1.2_3.4")}; |
317f7c8a |
203 | like($@, qr/underscores before decimal/, |
204 | "Invalid version format (underscores before decimal)"); |
205 | |
91152fc1 |
206 | eval {$version = $CLASS->$method("1_2")}; |
317f7c8a |
207 | like($@, qr/alpha without decimal/, |
208 | "Invalid version format (alpha without decimal)"); |
209 | |
91152fc1 |
210 | eval { $version = $CLASS->$method("1.2b3")}; |
211 | like($@, qr/non-numeric data/, |
212 | "Invalid version format (non-numeric data)"); |
317f7c8a |
213 | |
214 | # from here on out capture the warning and test independently |
f34c6aaf |
215 | { |
91152fc1 |
216 | eval{$version = $CLASS->$method("99 and 44/100 pure")}; |
317f7c8a |
217 | |
91152fc1 |
218 | like($@, qr/non-numeric data/, |
219 | "Invalid version format (non-numeric data)"); |
317f7c8a |
220 | |
91152fc1 |
221 | eval{$version = $CLASS->$method("something")}; |
222 | like($@, qr/non-numeric data/, |
223 | "Invalid version format (non-numeric data)"); |
317f7c8a |
224 | |
225 | # reset the test object to something reasonable |
f941e658 |
226 | $version = $CLASS->$method("1.2.3"); |
317f7c8a |
227 | |
228 | # Test boolean operator |
229 | ok ($version, 'boolean'); |
230 | |
231 | # Test class membership |
232 | isa_ok ( $version, $CLASS ); |
233 | |
234 | # Test comparison operators with self |
f941e658 |
235 | diag "tests with self" unless $ENV{PERL_CORE}; |
8cb289bd |
236 | is ( $version <=> $version, 0, '$version <=> $version == 0' ); |
317f7c8a |
237 | ok ( $version == $version, '$version == $version' ); |
238 | |
317f7c8a |
239 | # Test Numeric Comparison operators |
240 | # test first with non-object |
f941e658 |
241 | $version = $CLASS->$method("5.006.001"); |
317f7c8a |
242 | $new_version = "5.8.0"; |
f941e658 |
243 | diag "numeric tests with non-objects" unless $ENV{PERL_CORE}; |
317f7c8a |
244 | ok ( $version == $version, '$version == $version' ); |
245 | ok ( $version < $new_version, '$version < $new_version' ); |
246 | ok ( $new_version > $version, '$new_version > $version' ); |
247 | ok ( $version != $new_version, '$version != $new_version' ); |
248 | |
249 | # now test with existing object |
f941e658 |
250 | $new_version = $CLASS->$method($new_version); |
251 | diag "numeric tests with objects" unless $ENV{PERL_CORE}; |
317f7c8a |
252 | ok ( $version < $new_version, '$version < $new_version' ); |
253 | ok ( $new_version > $version, '$new_version > $version' ); |
254 | ok ( $version != $new_version, '$version != $new_version' ); |
255 | |
256 | # now test with actual numbers |
f941e658 |
257 | diag "numeric tests with numbers" unless $ENV{PERL_CORE}; |
317f7c8a |
258 | ok ( $version->numify() == 5.006001, '$version->numify() == 5.006001' ); |
259 | ok ( $version->numify() <= 5.006001, '$version->numify() <= 5.006001' ); |
260 | ok ( $version->numify() < 5.008, '$version->numify() < 5.008' ); |
261 | #ok ( $version->numify() > v5.005_02, '$version->numify() > 5.005_02' ); |
262 | |
263 | # test with long decimals |
f941e658 |
264 | diag "Tests with extended decimal versions" unless $ENV{PERL_CORE}; |
265 | $version = $CLASS->$method(1.002003); |
8cb289bd |
266 | ok ( $version == "1.2.3", '$version == "1.2.3"'); |
317f7c8a |
267 | ok ( $version->numify == 1.002003, '$version->numify == 1.002003'); |
f941e658 |
268 | $version = $CLASS->$method("2002.09.30.1"); |
8cb289bd |
269 | ok ( $version == "2002.9.30.1",'$version == 2002.9.30.1'); |
317f7c8a |
270 | ok ( $version->numify == 2002.009030001, |
271 | '$version->numify == 2002.009030001'); |
272 | |
273 | # now test with alpha version form with string |
f941e658 |
274 | $version = $CLASS->$method("1.2.3"); |
317f7c8a |
275 | $new_version = "1.2.3_4"; |
f941e658 |
276 | diag "numeric tests with alpha-style non-objects" unless $ENV{PERL_CORE}; |
8cb289bd |
277 | ok ( $version < $new_version, '$version < $new_version' ); |
278 | ok ( $new_version > $version, '$new_version > $version' ); |
279 | ok ( $version != $new_version, '$version != $new_version' ); |
317f7c8a |
280 | |
f941e658 |
281 | $version = $CLASS->$method("1.2.4"); |
317f7c8a |
282 | diag "numeric tests with alpha-style non-objects" |
692a467c |
283 | unless $ENV{PERL_CORE}; |
317f7c8a |
284 | ok ( $version > $new_version, '$version > $new_version' ); |
285 | ok ( $new_version < $version, '$new_version < $version' ); |
286 | ok ( $version != $new_version, '$version != $new_version' ); |
287 | |
288 | # now test with alpha version form with object |
f941e658 |
289 | $version = $CLASS->$method("1.2.3"); |
290 | $new_version = $CLASS->$method("1.2.3_4"); |
291 | diag "tests with alpha-style objects" unless $ENV{PERL_CORE}; |
317f7c8a |
292 | ok ( $version < $new_version, '$version < $new_version' ); |
293 | ok ( $new_version > $version, '$new_version > $version' ); |
294 | ok ( $version != $new_version, '$version != $new_version' ); |
295 | ok ( !$version->is_alpha, '!$version->is_alpha'); |
296 | ok ( $new_version->is_alpha, '$new_version->is_alpha'); |
297 | |
f941e658 |
298 | $version = $CLASS->$method("1.2.4"); |
299 | diag "tests with alpha-style objects" unless $ENV{PERL_CORE}; |
317f7c8a |
300 | ok ( $version > $new_version, '$version > $new_version' ); |
301 | ok ( $new_version < $version, '$new_version < $version' ); |
302 | ok ( $version != $new_version, '$version != $new_version' ); |
303 | |
f941e658 |
304 | $version = $CLASS->$method("1.2.3.4"); |
305 | $new_version = $CLASS->$method("1.2.3_4"); |
317f7c8a |
306 | diag "tests with alpha-style objects with same subversion" |
692a467c |
307 | unless $ENV{PERL_CORE}; |
317f7c8a |
308 | ok ( $version > $new_version, '$version > $new_version' ); |
309 | ok ( $new_version < $version, '$new_version < $version' ); |
310 | ok ( $version != $new_version, '$version != $new_version' ); |
311 | |
f941e658 |
312 | diag "test implicit [in]equality" unless $ENV{PERL_CORE}; |
313 | $version = $CLASS->$method("v1.2.3"); |
314 | $new_version = $CLASS->$method("1.2.3.0"); |
317f7c8a |
315 | ok ( $version == $new_version, '$version == $new_version' ); |
f941e658 |
316 | $new_version = $CLASS->$method("1.2.3_0"); |
317f7c8a |
317 | ok ( $version == $new_version, '$version == $new_version' ); |
f941e658 |
318 | $new_version = $CLASS->$method("1.2.3.1"); |
317f7c8a |
319 | ok ( $version < $new_version, '$version < $new_version' ); |
f941e658 |
320 | $new_version = $CLASS->$method("1.2.3_1"); |
317f7c8a |
321 | ok ( $version < $new_version, '$version < $new_version' ); |
f941e658 |
322 | $new_version = $CLASS->$method("1.1.999"); |
317f7c8a |
323 | ok ( $version > $new_version, '$version > $new_version' ); |
324 | |
325 | # that which is not expressly permitted is forbidden |
f941e658 |
326 | diag "forbidden operations" unless $ENV{PERL_CORE}; |
317f7c8a |
327 | ok ( !eval { ++$version }, "noop ++" ); |
328 | ok ( !eval { --$version }, "noop --" ); |
329 | ok ( !eval { $version/1 }, "noop /" ); |
330 | ok ( !eval { $version*3 }, "noop *" ); |
331 | ok ( !eval { abs($version) }, "noop abs" ); |
137d6fc0 |
332 | |
c8a14fb6 |
333 | SKIP: { |
f941e658 |
334 | skip "version require'd instead of use'd, cannot test $qv_declare", 3 |
335 | unless defined $qv_declare; |
336 | # test the $qv_declare() sub |
337 | diag "testing $qv_declare" unless $ENV{PERL_CORE}; |
338 | $version = $CLASS->$qv_declare("1.2"); |
339 | is ( "$version", "v1.2", $qv_declare.'("1.2") == "1.2.0"' ); |
340 | $version = $CLASS->$qv_declare(1.2); |
341 | is ( "$version", "v1.2", $qv_declare.'(1.2) == "1.2.0"' ); |
342 | isa_ok( $CLASS->$qv_declare('5.008'), $CLASS ); |
c8a14fb6 |
343 | } |
137d6fc0 |
344 | |
317f7c8a |
345 | # test creation from existing version object |
f941e658 |
346 | diag "create new from existing version" unless $ENV{PERL_CORE}; |
347 | ok (eval {$new_version = $CLASS->$method($version)}, |
317f7c8a |
348 | "new from existing object"); |
f941e658 |
349 | ok ($new_version == $version, "class->$method($version) identical"); |
350 | $new_version = $version->$method(); |
317f7c8a |
351 | isa_ok ($new_version, $CLASS ); |
f941e658 |
352 | is ($new_version, "0", "version->$method() doesn't clone"); |
353 | $new_version = $version->$method("1.2.3"); |
354 | is ($new_version, "1.2.3" , '$version->$method("1.2.3") works too'); |
317f7c8a |
355 | |
356 | # test the CVS revision mode |
f941e658 |
357 | diag "testing CVS Revision" unless $ENV{PERL_CORE}; |
317f7c8a |
358 | $version = new $CLASS qw$Revision: 1.2$; |
8cb289bd |
359 | ok ( $version == "1.2.0", 'qw$Revision: 1.2$ == 1.2.0' ); |
317f7c8a |
360 | $version = new $CLASS qw$Revision: 1.2.3.4$; |
8cb289bd |
361 | ok ( $version == "1.2.3.4", 'qw$Revision: 1.2.3.4$ == 1.2.3.4' ); |
317f7c8a |
362 | |
363 | # test the CPAN style reduced significant digit form |
f941e658 |
364 | diag "testing CPAN-style versions" unless $ENV{PERL_CORE}; |
365 | $version = $CLASS->$method("1.23_01"); |
8cb289bd |
366 | is ( "$version" , "1.23_01", "CPAN-style alpha version" ); |
317f7c8a |
367 | ok ( $version > 1.23, "1.23_01 > 1.23"); |
368 | ok ( $version < 1.24, "1.23_01 < 1.24"); |
369 | |
370 | # test reformed UNIVERSAL::VERSION |
f941e658 |
371 | diag "Replacement UNIVERSAL::VERSION tests" unless $ENV{PERL_CORE}; |
f34c6aaf |
372 | |
373 | my $error_regex = $] < 5.006 |
374 | ? 'version \d required' |
f941e658 |
375 | : 'does not define \$t.{7}::VERSION'; |
317f7c8a |
376 | |
f34c6aaf |
377 | { |
f941e658 |
378 | my ($fh, $filename) = tempfile('tXXXXXXX', SUFFIX => '.pm', UNLINK => 1); |
379 | (my $package = basename($filename)) =~ s/\.pm$//; |
380 | print $fh "package $package;\n\$$package\::VERSION=0.58;\n1;\n"; |
381 | close $fh; |
f34c6aaf |
382 | |
8cb289bd |
383 | $version = 0.58; |
f941e658 |
384 | eval "use lib '.'; use $package $version"; |
385 | unlike($@, qr/$package version $version/, |
f34c6aaf |
386 | 'Replacement eval works with exact version'); |
387 | |
388 | # test as class method |
f941e658 |
389 | $new_version = $package->VERSION; |
8cb289bd |
390 | cmp_ok($new_version,'==',$version, "Called as class method"); |
8dd04980 |
391 | |
f34c6aaf |
392 | eval "print Completely::Unknown::Module->VERSION"; |
393 | if ( $] < 5.008 ) { |
394 | unlike($@, qr/$error_regex/, |
395 | "Don't freak if the module doesn't even exist"); |
396 | } |
397 | else { |
398 | unlike($@, qr/defines neither package nor VERSION/, |
399 | "Don't freak if the module doesn't even exist"); |
400 | } |
401 | |
402 | # this should fail even with old UNIVERSAL::VERSION |
8cb289bd |
403 | $version += 0.01; |
f941e658 |
404 | eval "use lib '.'; use $package $version"; |
405 | like($@, qr/$package version $version/, |
f34c6aaf |
406 | 'Replacement eval works with incremented version'); |
407 | |
408 | $version =~ s/0+$//; #convert to string and remove trailing 0's |
409 | chop($version); # shorten by 1 digit, should still succeed |
f941e658 |
410 | eval "use lib '.'; use $package $version"; |
411 | unlike($@, qr/$package version $version/, |
f34c6aaf |
412 | 'Replacement eval works with single digit'); |
413 | |
414 | # this would fail with old UNIVERSAL::VERSION |
8cb289bd |
415 | $version += 0.1; |
f941e658 |
416 | eval "use lib '.'; use $package $version"; |
417 | like($@, qr/$package version $version/, |
f34c6aaf |
418 | 'Replacement eval works with incremented digit'); |
f941e658 |
419 | unlink $filename; |
f34c6aaf |
420 | } |
317f7c8a |
421 | |
422 | { # dummy up some variously broken modules for testing |
f941e658 |
423 | my ($fh, $filename) = tempfile('tXXXXXXX', SUFFIX => '.pm', UNLINK => 1); |
424 | (my $package = basename($filename)) =~ s/\.pm$//; |
425 | print $fh "1;\n"; |
426 | close $fh; |
f34c6aaf |
427 | |
f941e658 |
428 | eval "use lib '.'; use $package 3;"; |
317f7c8a |
429 | if ( $] < 5.008 ) { |
f34c6aaf |
430 | like($@, qr/$error_regex/, |
431 | 'Replacement handles modules without package or VERSION'); |
c8a14fb6 |
432 | } |
317f7c8a |
433 | else { |
f34c6aaf |
434 | like($@, qr/defines neither package nor VERSION/, |
435 | 'Replacement handles modules without package or VERSION'); |
c8a14fb6 |
436 | } |
f941e658 |
437 | eval "use lib '.'; use $package; \$version = $package->VERSION"; |
317f7c8a |
438 | unlike ($@, qr/$error_regex/, |
439 | 'Replacement handles modules without package or VERSION'); |
f34c6aaf |
440 | ok (!defined($version), "Called as class method"); |
f941e658 |
441 | unlink $filename; |
317f7c8a |
442 | } |
443 | |
444 | { # dummy up some variously broken modules for testing |
f941e658 |
445 | my ($fh, $filename) = tempfile('tXXXXXXX', SUFFIX => '.pm', UNLINK => 1); |
446 | (my $package = basename($filename)) =~ s/\.pm$//; |
447 | print $fh "package $package;\n#look ma no VERSION\n1;\n"; |
448 | close $fh; |
449 | eval "use lib '.'; use $package 3;"; |
f34c6aaf |
450 | like ($@, qr/$error_regex/, |
317f7c8a |
451 | 'Replacement handles modules without VERSION'); |
f941e658 |
452 | eval "use lib '.'; use $package; print $package->VERSION"; |
f34c6aaf |
453 | unlike ($@, qr/$error_regex/, |
317f7c8a |
454 | 'Replacement handles modules without VERSION'); |
f941e658 |
455 | unlink $filename; |
317f7c8a |
456 | } |
457 | |
458 | { # dummy up some variously broken modules for testing |
f941e658 |
459 | my ($fh, $filename) = tempfile('tXXXXXXX', SUFFIX => '.pm', UNLINK => 1); |
460 | (my $package = basename($filename)) =~ s/\.pm$//; |
461 | print $fh "package $package;\n\@VERSION = ();\n1;\n"; |
462 | close $fh; |
463 | eval "use lib '.'; use $package 3;"; |
f34c6aaf |
464 | like ($@, qr/$error_regex/, |
317f7c8a |
465 | 'Replacement handles modules without VERSION'); |
f941e658 |
466 | eval "use lib '.'; use $package; print $package->VERSION"; |
f34c6aaf |
467 | unlike ($@, qr/$error_regex/, |
317f7c8a |
468 | 'Replacement handles modules without VERSION'); |
f941e658 |
469 | unlink $filename; |
317f7c8a |
470 | } |
471 | |
137d6fc0 |
472 | SKIP: { |
ac0e6a2f |
473 | skip 'Cannot test bare v-strings with Perl < 5.6.0', 4 |
474 | if $] < 5.006_000; |
f941e658 |
475 | diag "Tests with v-strings" unless $ENV{PERL_CORE}; |
476 | $version = $CLASS->$method(1.2.3); |
8cb289bd |
477 | ok("$version" == "v1.2.3", '"$version" == 1.2.3'); |
f941e658 |
478 | $version = $CLASS->$method(1.0.0); |
479 | $new_version = $CLASS->$method(1); |
317f7c8a |
480 | ok($version == $new_version, '$version == $new_version'); |
f941e658 |
481 | skip "version require'd instead of use'd, cannot test declare", 1 |
482 | unless defined $qv_declare; |
483 | $version = &$qv_declare(1.2.3); |
484 | ok("$version" == "v1.2.3", 'v-string initialized $qv_declare()'); |
317f7c8a |
485 | } |
486 | |
f941e658 |
487 | diag "Tests with real-world (malformed) data" unless $ENV{PERL_CORE}; |
317f7c8a |
488 | |
489 | # trailing zero testing (reported by Andreas Koenig). |
f941e658 |
490 | $version = $CLASS->$method("1"); |
317f7c8a |
491 | ok($version->numify eq "1.000", "trailing zeros preserved"); |
f941e658 |
492 | $version = $CLASS->$method("1.0"); |
317f7c8a |
493 | ok($version->numify eq "1.000", "trailing zeros preserved"); |
f941e658 |
494 | $version = $CLASS->$method("1.0.0"); |
317f7c8a |
495 | ok($version->numify eq "1.000000", "trailing zeros preserved"); |
f941e658 |
496 | $version = $CLASS->$method("1.0.0.0"); |
317f7c8a |
497 | ok($version->numify eq "1.000000000", "trailing zeros preserved"); |
498 | |
499 | # leading zero testing (reported by Andreas Koenig). |
f941e658 |
500 | $version = $CLASS->$method(".7"); |
317f7c8a |
501 | ok($version->numify eq "0.700", "leading zero inferred"); |
502 | |
503 | # leading space testing (reported by Andreas Koenig). |
f941e658 |
504 | $version = $CLASS->$method(" 1.7"); |
317f7c8a |
505 | ok($version->numify eq "1.700", "leading space ignored"); |
506 | |
507 | # RT 19517 - deal with undef and 'undef' initialization |
8cb289bd |
508 | ok("$version" ne 'undef', "Undef version comparison #1"); |
509 | ok("$version" ne undef, "Undef version comparison #2"); |
f941e658 |
510 | $version = $CLASS->$method('undef'); |
317f7c8a |
511 | unlike($warning, qr/^Version string 'undef' contains invalid data/, |
512 | "Version string 'undef'"); |
513 | |
f941e658 |
514 | $version = $CLASS->$method(undef); |
317f7c8a |
515 | like($warning, qr/^Use of uninitialized value/, |
516 | "Version string 'undef'"); |
8cb289bd |
517 | ok($version == 'undef', "Undef version comparison #3"); |
518 | ok($version == undef, "Undef version comparison #4"); |
f941e658 |
519 | eval "\$version = \$CLASS->$method()"; # no parameter at all |
317f7c8a |
520 | unlike($@, qr/^Bizarre copy of CODE/, "No initializer at all"); |
8cb289bd |
521 | ok($version == 'undef', "Undef version comparison #5"); |
522 | ok($version == undef, "Undef version comparison #6"); |
317f7c8a |
523 | |
f941e658 |
524 | $version = $CLASS->$method(0.000001); |
317f7c8a |
525 | unlike($warning, qr/^Version string '1e-06' contains invalid data/, |
526 | "Very small version objects"); |
f34c6aaf |
527 | } |
e0218a61 |
528 | |
317f7c8a |
529 | SKIP: { |
f941e658 |
530 | my $warning; |
531 | local $SIG{__WARN__} = sub { $warning = $_[0] }; |
317f7c8a |
532 | # dummy up a legal module for testing RT#19017 |
f941e658 |
533 | my ($fh, $filename) = tempfile('tXXXXXXX', SUFFIX => '.pm', UNLINK => 1); |
534 | (my $package = basename($filename)) =~ s/\.pm$//; |
535 | print $fh <<"EOF"; |
536 | package $package; |
537 | use $CLASS; \$VERSION = ${CLASS}->new('0.0.4'); |
c8a14fb6 |
538 | 1; |
539 | EOF |
f941e658 |
540 | close $fh; |
317f7c8a |
541 | |
f941e658 |
542 | eval "use lib '.'; use $package 0.000008;"; |
543 | like ($@, qr/^$package version 0.000008 required/, |
317f7c8a |
544 | "Make sure very small versions don't freak"); |
f941e658 |
545 | eval "use lib '.'; use $package 1;"; |
546 | like ($@, qr/^$package version 1 required/, |
317f7c8a |
547 | "Comparing vs. version with no decimal"); |
f941e658 |
548 | eval "use lib '.'; use $package 1.;"; |
549 | like ($@, qr/^$package version 1 required/, |
317f7c8a |
550 | "Comparing vs. version with decimal only"); |
ac0e6a2f |
551 | if ( $] < 5.006_000 ) { |
ac0e6a2f |
552 | skip 'Cannot "use" extended versions with Perl < 5.6.0', 3; |
d69f6151 |
553 | } |
f941e658 |
554 | eval "use lib '.'; use $package v0.0.8;"; |
555 | my $regex = "^$package version v0.0.8 required"; |
ac0e6a2f |
556 | like ($@, qr/$regex/, "Make sure very small versions don't freak"); |
317f7c8a |
557 | |
ac0e6a2f |
558 | $regex =~ s/8/4/; # set for second test |
f941e658 |
559 | eval "use lib '.'; use $package v0.0.4;"; |
ac0e6a2f |
560 | unlike($@, qr/$regex/, 'Succeed - required == VERSION'); |
f941e658 |
561 | cmp_ok ( $package->VERSION, 'eq', '0.0.4', 'No undef warnings' ); |
562 | unlink $filename; |
317f7c8a |
563 | } |
564 | |
f941e658 |
565 | SKIP: { |
566 | skip 'Cannot test "use base qw(version)" when require is used', 3 |
567 | unless defined $qv_declare; |
568 | my ($fh, $filename) = tempfile('tXXXXXXX', SUFFIX => '.pm', UNLINK => 1); |
569 | (my $package = basename($filename)) =~ s/\.pm$//; |
570 | print $fh <<"EOF"; |
571 | package $package; |
92dcf8ce |
572 | use base qw(version); |
573 | 1; |
574 | EOF |
f941e658 |
575 | close $fh; |
576 | # need to eliminate any other $qv_declare()'s |
577 | undef *{"main\::$qv_declare"}; |
578 | ok(!defined(&{"main\::$qv_declare"}), "make sure we cleared $qv_declare() properly"); |
579 | eval "use lib '.'; use $package qw/declare qv/;"; |
580 | ok(defined(&{"main\::$qv_declare"}), "make sure we exported $qv_declare() properly"); |
581 | isa_ok( &$qv_declare(1.2), $package); |
582 | unlink $filename; |
583 | } |
d69f6151 |
584 | |
585 | SKIP: { |
ac0e6a2f |
586 | if ( $] < 5.006_000 ) { |
587 | skip 'Cannot "use" extended versions with Perl < 5.6.0', 3; |
588 | } |
f941e658 |
589 | my ($fh, $filename) = tempfile('tXXXXXXX', SUFFIX => '.pm', UNLINK => 1); |
590 | (my $package = basename($filename)) =~ s/\.pm$//; |
591 | print $fh <<"EOF"; |
592 | package $package; |
ac0e6a2f |
593 | \$VERSION = 1.0; |
594 | 1; |
595 | EOF |
f941e658 |
596 | close $fh; |
597 | eval "use lib '.'; use $package 1.001;"; |
598 | like ($@, qr/^$package version 1.001 required/, |
ac0e6a2f |
599 | "User typed numeric so we error with numeric"); |
f941e658 |
600 | eval "use lib '.'; use $package v1.1.0;"; |
601 | like ($@, qr/^$package version v1.1.0 required/, |
ac0e6a2f |
602 | "User typed extended so we error with extended"); |
f941e658 |
603 | unlink $filename; |
ac0e6a2f |
604 | } |
605 | |
606 | SKIP: { |
d69f6151 |
607 | # test locale handling |
f34c6aaf |
608 | my $warning; |
609 | local $SIG{__WARN__} = sub { $warning = $_[0] }; |
f941e658 |
610 | |
611 | $DB::single = 1; |
91152fc1 |
612 | my $v = eval { $CLASS->$method('1,7') }; |
613 | # is( $@, "", 'Directly test comma as decimal compliance'); |
f941e658 |
614 | |
d69f6151 |
615 | my $ver = 1.23; # has to be floating point number |
f941e658 |
616 | my $orig_loc = setlocale( LC_ALL ); |
d69f6151 |
617 | my $loc; |
618 | while (<DATA>) { |
619 | chomp; |
f941e658 |
620 | $loc = setlocale( LC_ALL, $_); |
621 | last if localeconv()->{decimal_point} eq ','; |
d69f6151 |
622 | } |
623 | skip 'Cannot test locale handling without a comma locale', 4 |
624 | unless ( $loc and ($ver eq '1,23') ); |
625 | |
f941e658 |
626 | diag ("Testing locale handling with $loc") unless $ENV{PERL_CORE}; |
d69f6151 |
627 | |
f941e658 |
628 | $v = $CLASS->$method($ver); |
629 | unlike($warning, qr/Version string '1,23' contains invalid data/, |
d69f6151 |
630 | "Process locale-dependent floating point"); |
8cb289bd |
631 | is ($v, "1.23", "Locale doesn't apply to version objects"); |
d69f6151 |
632 | ok ($v == $ver, "Comparison to locale floating point"); |
f941e658 |
633 | |
634 | setlocale( LC_ALL, $orig_loc); # reset this before possible skip |
635 | skip 'Cannot test RT#46921 with Perl < 5.008', 1 |
636 | if ($] < 5.008); |
637 | skip 'Cannot test RT#46921 with pure Perl module', 1 |
638 | if exists $INC{'version/vpp.pm'}; |
639 | my ($fh, $filename) = tempfile('tXXXXXXX', SUFFIX => '.pm', UNLINK => 1); |
640 | (my $package = basename($filename)) =~ s/\.pm$//; |
641 | print $fh <<"EOF"; |
642 | package $package; |
643 | use POSIX qw(locale_h); |
644 | \$^W = 1; |
645 | use $CLASS; |
646 | setlocale (LC_ALL, '$loc'); |
647 | use $CLASS ; |
648 | eval "use Socket 1.7"; |
649 | setlocale( LC_ALL, '$orig_loc'); |
650 | 1; |
651 | EOF |
652 | close $fh; |
653 | |
654 | eval "use lib '.'; use $package;"; |
655 | unlike($warning, qr"Version string '1,7' contains invalid data", |
656 | 'Handle locale action-at-a-distance'); |
d69f6151 |
657 | } |
f34c6aaf |
658 | |
f941e658 |
659 | eval 'my $v = $CLASS->$method("1._1");'; |
f34c6aaf |
660 | unlike($@, qr/^Invalid version format \(alpha with zero width\)/, |
661 | "Invalid version format 1._1"); |
ac0e6a2f |
662 | |
c812d146 |
663 | { |
664 | my $warning; |
665 | local $SIG{__WARN__} = sub { $warning = $_[0] }; |
f941e658 |
666 | eval 'my $v = $CLASS->$method(~0);'; |
c812d146 |
667 | unlike($@, qr/Integer overflow in version/, "Too large version"); |
668 | like($warning, qr/Integer overflow in version/, "Too large version"); |
669 | } |
670 | |
72287d96 |
671 | { |
672 | # http://rt.cpan.org/Public/Bug/Display.html?id=30004 |
f941e658 |
673 | my $v1 = $CLASS->$method("v0.1_1"); |
72287d96 |
674 | (my $alpha1 = Dumper($v1)) =~ s/.+'alpha' => ([^,]+),.+/$1/ms; |
f941e658 |
675 | my $v2 = $CLASS->$method($v1); |
72287d96 |
676 | (my $alpha2 = Dumper($v2)) =~ s/.+'alpha' => ([^,]+),.+/$1/ms; |
677 | is $alpha2, $alpha1, "Don't fall for Data::Dumper's tricks"; |
678 | } |
679 | |
219bf418 |
680 | { |
681 | # http://rt.perl.org/rt3/Ticket/Display.html?id=56606 |
682 | my $badv = bless { version => [1,2,3] }, "version"; |
683 | is $badv, '1.002003', "Deal with badly serialized versions from YAML"; |
684 | my $badv2 = bless { qv => 1, version => [1,2,3] }, "version"; |
685 | is $badv2, 'v1.2.3', "Deal with badly serialized versions from YAML "; |
686 | } |
137d6fc0 |
687 | } |
cb5772bb |
688 | |
689 | 1; |
d69f6151 |
690 | |
691 | __DATA__ |
692 | af_ZA |
693 | af_ZA.utf8 |
694 | an_ES |
695 | an_ES.utf8 |
696 | az_AZ.utf8 |
697 | be_BY |
698 | be_BY.utf8 |
699 | bg_BG |
700 | bg_BG.utf8 |
701 | br_FR |
702 | br_FR@euro |
703 | br_FR.utf8 |
704 | bs_BA |
705 | bs_BA.utf8 |
706 | ca_ES |
707 | ca_ES@euro |
708 | ca_ES.utf8 |
709 | cs_CZ |
710 | cs_CZ.utf8 |
711 | da_DK |
712 | da_DK.utf8 |
713 | de_AT |
714 | de_AT@euro |
715 | de_AT.utf8 |
716 | de_BE |
717 | de_BE@euro |
718 | de_BE.utf8 |
719 | de_DE |
720 | de_DE@euro |
721 | de_DE.utf8 |
722 | de_LU |
723 | de_LU@euro |
724 | de_LU.utf8 |
725 | el_GR |
726 | el_GR.utf8 |
727 | en_DK |
728 | en_DK.utf8 |
729 | es_AR |
730 | es_AR.utf8 |
731 | es_BO |
732 | es_BO.utf8 |
733 | es_CL |
734 | es_CL.utf8 |
735 | es_CO |
736 | es_CO.utf8 |
737 | es_EC |
738 | es_EC.utf8 |
739 | es_ES |
740 | es_ES@euro |
741 | es_ES.utf8 |
742 | es_PY |
743 | es_PY.utf8 |
744 | es_UY |
745 | es_UY.utf8 |
746 | es_VE |
747 | es_VE.utf8 |
748 | et_EE |
749 | et_EE.iso885915 |
750 | et_EE.utf8 |
751 | eu_ES |
752 | eu_ES@euro |
753 | eu_ES.utf8 |
754 | fi_FI |
755 | fi_FI@euro |
756 | fi_FI.utf8 |
757 | fo_FO |
758 | fo_FO.utf8 |
759 | fr_BE |
760 | fr_BE@euro |
761 | fr_BE.utf8 |
762 | fr_CA |
763 | fr_CA.utf8 |
764 | fr_CH |
765 | fr_CH.utf8 |
766 | fr_FR |
767 | fr_FR@euro |
768 | fr_FR.utf8 |
769 | fr_LU |
770 | fr_LU@euro |
771 | fr_LU.utf8 |
772 | gl_ES |
773 | gl_ES@euro |
774 | gl_ES.utf8 |
775 | hr_HR |
776 | hr_HR.utf8 |
777 | hu_HU |
778 | hu_HU.utf8 |
779 | id_ID |
780 | id_ID.utf8 |
781 | is_IS |
782 | is_IS.utf8 |
783 | it_CH |
784 | it_CH.utf8 |
785 | it_IT |
786 | it_IT@euro |
787 | it_IT.utf8 |
788 | ka_GE |
789 | ka_GE.utf8 |
790 | kk_KZ |
791 | kk_KZ.utf8 |
792 | kl_GL |
793 | kl_GL.utf8 |
794 | lt_LT |
795 | lt_LT.utf8 |
796 | lv_LV |
797 | lv_LV.utf8 |
798 | mk_MK |
799 | mk_MK.utf8 |
800 | mn_MN |
801 | mn_MN.utf8 |
802 | nb_NO |
803 | nb_NO.utf8 |
804 | nl_BE |
805 | nl_BE@euro |
806 | nl_BE.utf8 |
807 | nl_NL |
808 | nl_NL@euro |
809 | nl_NL.utf8 |
810 | nn_NO |
811 | nn_NO.utf8 |
812 | no_NO |
813 | no_NO.utf8 |
814 | oc_FR |
815 | oc_FR.utf8 |
816 | pl_PL |
817 | pl_PL.utf8 |
818 | pt_BR |
819 | pt_BR.utf8 |
820 | pt_PT |
821 | pt_PT@euro |
822 | pt_PT.utf8 |
823 | ro_RO |
824 | ro_RO.utf8 |
825 | ru_RU |
826 | ru_RU.koi8r |
827 | ru_RU.utf8 |
828 | ru_UA |
829 | ru_UA.utf8 |
830 | se_NO |
831 | se_NO.utf8 |
832 | sh_YU |
833 | sh_YU.utf8 |
834 | sk_SK |
835 | sk_SK.utf8 |
836 | sl_SI |
837 | sl_SI.utf8 |
838 | sq_AL |
839 | sq_AL.utf8 |
840 | sr_CS |
841 | sr_CS.utf8 |
842 | sv_FI |
843 | sv_FI@euro |
844 | sv_FI.utf8 |
845 | sv_SE |
846 | sv_SE.iso885915 |
847 | sv_SE.utf8 |
848 | tg_TJ |
849 | tg_TJ.utf8 |
850 | tr_TR |
851 | tr_TR.utf8 |
852 | tt_RU.utf8 |
853 | uk_UA |
854 | uk_UA.utf8 |
855 | vi_VN |
856 | vi_VN.tcvn |
857 | wa_BE |
858 | wa_BE@euro |
859 | wa_BE.utf8 |
860 | |