This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Perl_PerlIO_context_layers and PerlIO_apply_layers, as well as other unexported/undef...
[perl5.git] / lib / version.t
CommitLineData
129318bd 1#! /usr/local/perl -w
a7ad731c
HS
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
HS
4
5#########################
6
34ba6322 7use Test::More qw(no_plan);
a7ad731c 8
137d6fc0 9diag "Tests with base class" unless $ENV{PERL_CORE};
a7ad731c 10
5eb567df 11BEGIN {
43eaf59d 12 use_ok("version", 0.50); # If we made it this far, we are ok.
5eb567df
RGS
13}
14
137d6fc0
JP
15BaseTests("version");
16
17diag "Tests with empty derived class" unless $ENV{PERL_CORE};
18
19package version::Empty;
43eaf59d 20use base version;
137d6fc0 21$VERSION = 0.01;
43eaf59d
SP
22no warnings 'redefine';
23*::qv = sub { return bless version::qv(shift), __PACKAGE__; };
137d6fc0 24
e0218a61
JP
25package version::Bad;
26use base version;
27sub new { my($self,$n)=@_; bless \$n, $self }
28
137d6fc0 29package main;
e0218a61 30my $testobj = version::Empty->new(1.002_003);
137d6fc0
JP
31isa_ok( $testobj, "version::Empty" );
32ok( $testobj->numify == 1.002003, "Numified correctly" );
9137345a
JP
33ok( $testobj->stringify eq "1.002003", "Stringified correctly" );
34ok( $testobj->normal eq "v1.2.3", "Normalified correctly" );
137d6fc0 35
e0218a61 36my $verobj = version->new("1.2.4");
137d6fc0
JP
37ok( $verobj > $testobj, "Comparison vs parent class" );
38ok( $verobj gt $testobj, "Comparison vs parent class" );
39BaseTests("version::Empty");
40
e0218a61
JP
41diag "tests with bad subclass" unless $ENV{PERL_CORE};
42$testobj = version::Bad->new(1.002_003);
43isa_ok( $testobj, "version::Bad" );
44eval { my $string = $testobj->numify };
45like($@, qr/Invalid version object/,
46 "Bad subclass numify");
47eval { my $string = $testobj->normal };
48like($@, qr/Invalid version object/,
49 "Bad subclass normal");
50eval { my $string = $testobj->stringify };
51like($@, qr/Invalid version object/,
52 "Bad subclass stringify");
53eval { my $test = $testobj > 1.0 };
54like($@, qr/Invalid version object/,
55 "Bad subclass vcmp");
56
137d6fc0
JP
57sub BaseTests {
58
59 my $CLASS = shift;
60
61 # Insert your test code below, the Test module is use()ed here so read
62 # its man page ( perldoc Test ) for help writing this test script.
63
64 # Test bare number processing
65 diag "tests with bare numbers" unless $ENV{PERL_CORE};
66 $version = $CLASS->new(5.005_03);
9137345a 67 is ( "$version" , "5.005030" , '5.005_03 eq 5.5.30' );
137d6fc0 68 $version = $CLASS->new(1.23);
13f8f398 69 is ( "$version" , "1.230" , '1.23 eq "1.230"' );
137d6fc0
JP
70
71 # Test quoted number processing
72 diag "tests with quoted numbers" unless $ENV{PERL_CORE};
73 $version = $CLASS->new("5.005_03");
c76df65e 74 is ( "$version" , "5.005_030" , '"5.005_03" eq "5.005_030"' );
137d6fc0 75 $version = $CLASS->new("v1.23");
9137345a 76 is ( "$version" , "v1.23.0" , '"v1.23" eq "v1.23.0"' );
137d6fc0
JP
77
78 # Test stringify operator
79 diag "tests with stringify" unless $ENV{PERL_CORE};
80 $version = $CLASS->new("5.005");
b9381830 81 is ( "$version" , "5.005" , '5.005 eq "5.005"' );
137d6fc0 82 $version = $CLASS->new("5.006.001");
9137345a 83 is ( "$version" , "v5.6.1" , '5.006.001 eq v5.6.1' );
137d6fc0 84 $version = $CLASS->new("1.2.3_4");
9137345a 85 is ( "$version" , "v1.2.3_4" , 'alpha version 1.2.3_4 eq v1.2.3_4' );
137d6fc0
JP
86
87 # test illegal formats
88 diag "test illegal formats" unless $ENV{PERL_CORE};
89 eval {my $version = $CLASS->new("1.2_3_4")};
90 like($@, qr/multiple underscores/,
91 "Invalid version format (multiple underscores)");
92
93 eval {my $version = $CLASS->new("1.2_3.4")};
94 like($@, qr/underscores before decimal/,
95 "Invalid version format (underscores before decimal)");
96
34ba6322
SP
97 eval {my $version = $CLASS->new("1_2")};
98 like($@, qr/alpha without decimal/,
99 "Invalid version format (alpha without decimal)");
100
cd57dc11
SP
101 # for this first test, just upgrade the warn() to die()
102 eval {
103 local $SIG{__WARN__} = sub { die $_[0] };
104 $version = $CLASS->new("1.2b3");
105 };
106 my $warnregex = "Version string '.+' contains invalid data; ".
107 "ignoring: '.+'";
108
109 like($@, qr/$warnregex/,
110 "Version string contains invalid data; ignoring");
111
112 # from here on out capture the warning and test independently
113 my $warning;
114 local $SIG{__WARN__} = sub { $warning = $_[0] };
115 $version = $CLASS->new("99 and 44/100 pure");
116
117 like($warning, qr/$warnregex/,
118 "Version string contains invalid data; ignoring");
13f8f398 119 ok ("$version" eq "99.000", '$version eq "99.000"');
137d6fc0 120 ok ($version->numify == 99.0, '$version->numify == 99.0');
9137345a 121 ok ($version->normal eq "v99.0.0", '$version->normal eq v99.0.0');
137d6fc0
JP
122
123 $version = $CLASS->new("something");
cd57dc11
SP
124 like($warning, qr/$warnregex/,
125 "Version string contains invalid data; ignoring");
137d6fc0
JP
126 ok (defined $version, 'defined $version');
127
128 # reset the test object to something reasonable
129 $version = $CLASS->new("1.2.3");
130
131 # Test boolean operator
132 ok ($version, 'boolean');
133
134 # Test class membership
cb5772bb 135 isa_ok ( $version, $CLASS );
137d6fc0
JP
136
137 # Test comparison operators with self
138 diag "tests with self" unless $ENV{PERL_CORE};
139 ok ( $version eq $version, '$version eq $version' );
140 is ( $version cmp $version, 0, '$version cmp $version == 0' );
141 ok ( $version == $version, '$version == $version' );
142
143 # test first with non-object
144 $version = $CLASS->new("5.006.001");
145 $new_version = "5.8.0";
146 diag "tests with non-objects" unless $ENV{PERL_CORE};
147 ok ( $version ne $new_version, '$version ne $new_version' );
148 ok ( $version lt $new_version, '$version lt $new_version' );
149 ok ( $new_version gt $version, '$new_version gt $version' );
150 ok ( ref(\$new_version) eq 'SCALAR', 'no auto-upgrade');
151 $new_version = "$version";
152 ok ( $version eq $new_version, '$version eq $new_version' );
153 ok ( $new_version eq $version, '$new_version eq $version' );
154
155 # now test with existing object
156 $new_version = $CLASS->new("5.8.0");
157 diag "tests with objects" unless $ENV{PERL_CORE};
158 ok ( $version ne $new_version, '$version ne $new_version' );
159 ok ( $version lt $new_version, '$version lt $new_version' );
160 ok ( $new_version gt $version, '$new_version gt $version' );
161 $new_version = $CLASS->new("$version");
162 ok ( $version eq $new_version, '$version eq $new_version' );
163
164 # Test Numeric Comparison operators
165 # test first with non-object
166 $new_version = "5.8.0";
167 diag "numeric tests with non-objects" unless $ENV{PERL_CORE};
168 ok ( $version == $version, '$version == $version' );
169 ok ( $version < $new_version, '$version < $new_version' );
170 ok ( $new_version > $version, '$new_version > $version' );
171 ok ( $version != $new_version, '$version != $new_version' );
172
173 # now test with existing object
174 $new_version = $CLASS->new($new_version);
175 diag "numeric tests with objects" unless $ENV{PERL_CORE};
176 ok ( $version < $new_version, '$version < $new_version' );
177 ok ( $new_version > $version, '$new_version > $version' );
178 ok ( $version != $new_version, '$version != $new_version' );
179
180 # now test with actual numbers
181 diag "numeric tests with numbers" unless $ENV{PERL_CORE};
182 ok ( $version->numify() == 5.006001, '$version->numify() == 5.006001' );
183 ok ( $version->numify() <= 5.006001, '$version->numify() <= 5.006001' );
184 ok ( $version->numify() < 5.008, '$version->numify() < 5.008' );
185 #ok ( $version->numify() > v5.005_02, '$version->numify() > 5.005_02' );
186
187 # test with long decimals
188 diag "Tests with extended decimal versions" unless $ENV{PERL_CORE};
189 $version = $CLASS->new(1.002003);
190 ok ( $version eq "1.2.3", '$version eq "1.2.3"');
191 ok ( $version->numify == 1.002003, '$version->numify == 1.002003');
192 $version = $CLASS->new("2002.09.30.1");
193 ok ( $version eq "2002.9.30.1",'$version eq 2002.9.30.1');
194 ok ( $version->numify == 2002.009030001,
195 '$version->numify == 2002.009030001');
196
197 # now test with alpha version form with string
198 $version = $CLASS->new("1.2.3");
199 $new_version = "1.2.3_4";
200 diag "tests with alpha-style non-objects" unless $ENV{PERL_CORE};
201 ok ( $version lt $new_version, '$version lt $new_version' );
202 ok ( $new_version gt $version, '$new_version gt $version' );
203 ok ( $version ne $new_version, '$version ne $new_version' );
204
205 $version = $CLASS->new("1.2.4");
206 diag "numeric tests with alpha-style non-objects" unless $ENV{PERL_CORE};
207 ok ( $version > $new_version, '$version > $new_version' );
208 ok ( $new_version < $version, '$new_version < $version' );
209 ok ( $version != $new_version, '$version != $new_version' );
210
211 # now test with alpha version form with object
212 $version = $CLASS->new("1.2.3");
213 $new_version = $CLASS->new("1.2.3_4");
214 diag "tests with alpha-style objects" unless $ENV{PERL_CORE};
215 ok ( $version < $new_version, '$version < $new_version' );
216 ok ( $new_version > $version, '$new_version > $version' );
217 ok ( $version != $new_version, '$version != $new_version' );
218 ok ( !$version->is_alpha, '!$version->is_alpha');
219 ok ( $new_version->is_alpha, '$new_version->is_alpha');
220
221 $version = $CLASS->new("1.2.4");
222 diag "tests with alpha-style objects" unless $ENV{PERL_CORE};
223 ok ( $version > $new_version, '$version > $new_version' );
224 ok ( $new_version < $version, '$new_version < $version' );
225 ok ( $version != $new_version, '$version != $new_version' );
226
13f8f398
JP
227 $version = $CLASS->new("1.2.3.4");
228 $new_version = $CLASS->new("1.2.3_4");
137d6fc0
JP
229 diag "tests with alpha-style objects with same subversion" unless $ENV{PERL_CORE};
230 ok ( $version > $new_version, '$version > $new_version' );
231 ok ( $new_version < $version, '$new_version < $version' );
232 ok ( $version != $new_version, '$version != $new_version' );
233
234 diag "test implicit [in]equality" unless $ENV{PERL_CORE};
13f8f398
JP
235 $version = $CLASS->new("v1.2.3");
236 $new_version = $CLASS->new("1.2.3.0");
137d6fc0 237 ok ( $version == $new_version, '$version == $new_version' );
13f8f398 238 $new_version = $CLASS->new("1.2.3_0");
137d6fc0 239 ok ( $version == $new_version, '$version == $new_version' );
13f8f398 240 $new_version = $CLASS->new("1.2.3.1");
137d6fc0 241 ok ( $version < $new_version, '$version < $new_version' );
13f8f398 242 $new_version = $CLASS->new("1.2.3_1");
137d6fc0
JP
243 ok ( $version < $new_version, '$version < $new_version' );
244 $new_version = $CLASS->new("1.1.999");
245 ok ( $version > $new_version, '$version > $new_version' );
246
247 # that which is not expressly permitted is forbidden
248 diag "forbidden operations" unless $ENV{PERL_CORE};
249 ok ( !eval { ++$version }, "noop ++" );
250 ok ( !eval { --$version }, "noop --" );
251 ok ( !eval { $version/1 }, "noop /" );
252 ok ( !eval { $version*3 }, "noop *" );
253 ok ( !eval { abs($version) }, "noop abs" );
254
255 # test the qv() sub
256 diag "testing qv" unless $ENV{PERL_CORE};
257 $version = qv("1.2");
258 ok ( $version eq "1.2.0", 'qv("1.2") eq "1.2.0"' );
259 $version = qv(1.2);
260 ok ( $version eq "1.2.0", 'qv(1.2) eq "1.2.0"' );
43eaf59d 261 isa_ok( qv('5.008'), $CLASS );
137d6fc0 262
13f8f398
JP
263 # test creation from existing version object
264 diag "create new from existing version" unless $ENV{PERL_CORE};
cb5772bb 265 ok (eval {$new_version = $CLASS->new($version)},
13f8f398 266 "new from existing object");
9137345a
JP
267 ok ($new_version == $version, "class->new($version) identical");
268 $new_version = $version->new();
269 ok ($new_version == $version, "$version->new() also identical");
270 $new_version = $version->new("1.2.3");
271 is ($new_version, "v1.2.3" , '$version->new("1.2.3") works too');
13f8f398 272
137d6fc0
JP
273 # test the CVS revision mode
274 diag "testing CVS Revision" unless $ENV{PERL_CORE};
cb5772bb 275 $version = new $CLASS qw$Revision: 1.2$;
137d6fc0 276 ok ( $version eq "1.2.0", 'qw$Revision: 1.2$ eq 1.2.0' );
cb5772bb 277 $version = new $CLASS qw$Revision: 1.2.3.4$;
d2397f31 278 ok ( $version eq "1.2.3.4", 'qw$Revision: 1.2.3.4$ eq 1.2.3.4' );
137d6fc0 279
9137345a
JP
280 # test the CPAN style reduced significant digit form
281 diag "testing CPAN-style versions" unless $ENV{PERL_CORE};
282 $version = $CLASS->new("1.23_01");
283 is ( "$version" , "1.23_0100", "CPAN-style alpha version" );
284 ok ( $version > 1.23, "1.23_01 > 1.23");
285 ok ( $version < 1.24, "1.23_01 < 1.24");
286
137d6fc0
JP
287 # test reformed UNIVERSAL::VERSION
288 diag "Replacement UNIVERSAL::VERSION tests" unless $ENV{PERL_CORE};
289
290 # we know this file is here since we require it ourselves
13f8f398 291 $version = $Test::More::VERSION;
137d6fc0 292 eval "use Test::More $version";
b9381830 293 unlike($@, qr/Test::More version $version/,
137d6fc0
JP
294 'Replacement eval works with exact version');
295
13f8f398
JP
296 $version = $Test::More::VERSION+0.01; # this should fail even with old UNIVERSAL::VERSION
297 eval "use Test::More $version";
b9381830 298 like($@, qr/Test::More version $version/,
137d6fc0
JP
299 'Replacement eval works with incremented version');
300
b9381830 301 $version =~ s/\.0$//; #convert to string and remove trailing '.0'
137d6fc0
JP
302 chop($version); # shorten by 1 digit, should still succeed
303 eval "use Test::More $version";
b9381830 304 unlike($@, qr/Test::More version $version/,
137d6fc0
JP
305 'Replacement eval works with single digit');
306
307 $version += 0.1; # this would fail with old UNIVERSAL::VERSION
308 eval "use Test::More $version";
b9381830 309 like($@, qr/Test::More version $version/,
137d6fc0
JP
310 'Replacement eval works with incremented digit');
311
312SKIP: {
b9381830 313 skip 'Cannot test v-strings with Perl < 5.8.1', 4
137d6fc0
JP
314 if $] < 5.008_001;
315 diag "Tests with v-strings" unless $ENV{PERL_CORE};
316 $version = $CLASS->new(1.2.3);
9137345a 317 ok("$version" eq "v1.2.3", '"$version" eq 1.2.3');
137d6fc0
JP
318 $version = $CLASS->new(1.0.0);
319 $new_version = $CLASS->new(1);
320 ok($version == $new_version, '$version == $new_version');
321 ok($version eq $new_version, '$version eq $new_version');
137d6fc0 322 $version = qv(1.2.3);
9137345a 323 ok("$version" eq "v1.2.3", 'v-string initialized qv()');
137d6fc0 324 }
e0218a61
JP
325
326 diag "Tests with real-world (malformed) data" unless $ENV{PERL_CORE};
327
328 # trailing zero testing (reported by Andreas Koenig).
329 $version = $CLASS->new("1");
330 ok($version->numify eq "1.000", "trailing zeros preserved");
331 $version = $CLASS->new("1.0");
332 ok($version->numify eq "1.000", "trailing zeros preserved");
333 $version = $CLASS->new("1.0.0");
334 ok($version->numify eq "1.000000", "trailing zeros preserved");
335 $version = $CLASS->new("1.0.0.0");
336 ok($version->numify eq "1.000000000", "trailing zeros preserved");
337
338 # leading zero testing (reported by Andreas Koenig).
339 $version = $CLASS->new(".7");
340 ok($version->numify eq "0.700", "leading zero inferred");
341
342 # leading space testing (reported by Andreas Koenig).
343 $version = $CLASS->new(" 1.7");
344 ok($version->numify eq "1.700", "leading space ignored");
345
137d6fc0 346}
cb5772bb
RGS
347
3481;