This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Unmatched parentheses (thanks to Steve Peters)
[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
9137345a 7use Test::More tests => 183;
a7ad731c 8
137d6fc0 9diag "Tests with base class" unless $ENV{PERL_CORE};
a7ad731c 10
5eb567df
RGS
11BEGIN {
12 use_ok("version", 0.30); # If we made it this far, we are ok.
13}
14
137d6fc0
JP
15BaseTests("version");
16
17diag "Tests with empty derived class" unless $ENV{PERL_CORE};
18
19package version::Empty;
20use vars qw($VERSION @ISA);
137d6fc0 21use version 0.30;
9137345a 22@ISA = qw(version);
137d6fc0
JP
23$VERSION = 0.01;
24
25package main;
26my $testobj = new version::Empty 1.002_003;
27isa_ok( $testobj, "version::Empty" );
28ok( $testobj->numify == 1.002003, "Numified correctly" );
9137345a
JP
29ok( $testobj->stringify eq "1.002003", "Stringified correctly" );
30ok( $testobj->normal eq "v1.2.3", "Normalified correctly" );
137d6fc0
JP
31
32my $verobj = new version "1.2.4";
33ok( $verobj > $testobj, "Comparison vs parent class" );
34ok( $verobj gt $testobj, "Comparison vs parent class" );
35BaseTests("version::Empty");
36
37sub BaseTests {
38
39 my $CLASS = shift;
40
41 # Insert your test code below, the Test module is use()ed here so read
42 # its man page ( perldoc Test ) for help writing this test script.
43
44 # Test bare number processing
45 diag "tests with bare numbers" unless $ENV{PERL_CORE};
46 $version = $CLASS->new(5.005_03);
9137345a 47 is ( "$version" , "5.005030" , '5.005_03 eq 5.5.30' );
137d6fc0 48 $version = $CLASS->new(1.23);
13f8f398 49 is ( "$version" , "1.230" , '1.23 eq "1.230"' );
137d6fc0
JP
50
51 # Test quoted number processing
52 diag "tests with quoted numbers" unless $ENV{PERL_CORE};
53 $version = $CLASS->new("5.005_03");
c76df65e 54 is ( "$version" , "5.005_030" , '"5.005_03" eq "5.005_030"' );
137d6fc0 55 $version = $CLASS->new("v1.23");
9137345a 56 is ( "$version" , "v1.23.0" , '"v1.23" eq "v1.23.0"' );
137d6fc0
JP
57
58 # Test stringify operator
59 diag "tests with stringify" unless $ENV{PERL_CORE};
60 $version = $CLASS->new("5.005");
b9381830 61 is ( "$version" , "5.005" , '5.005 eq "5.005"' );
137d6fc0 62 $version = $CLASS->new("5.006.001");
9137345a 63 is ( "$version" , "v5.6.1" , '5.006.001 eq v5.6.1' );
137d6fc0 64 $version = $CLASS->new("1.2.3_4");
9137345a 65 is ( "$version" , "v1.2.3_4" , 'alpha version 1.2.3_4 eq v1.2.3_4' );
137d6fc0
JP
66
67 # test illegal formats
68 diag "test illegal formats" unless $ENV{PERL_CORE};
69 eval {my $version = $CLASS->new("1.2_3_4")};
70 like($@, qr/multiple underscores/,
71 "Invalid version format (multiple underscores)");
72
73 eval {my $version = $CLASS->new("1.2_3.4")};
74 like($@, qr/underscores before decimal/,
75 "Invalid version format (underscores before decimal)");
76
77 $version = $CLASS->new("99 and 44/100 pure");
13f8f398 78 ok ("$version" eq "99.000", '$version eq "99.000"');
137d6fc0 79 ok ($version->numify == 99.0, '$version->numify == 99.0');
9137345a 80 ok ($version->normal eq "v99.0.0", '$version->normal eq v99.0.0');
137d6fc0
JP
81
82 $version = $CLASS->new("something");
83 ok (defined $version, 'defined $version');
84
85 # reset the test object to something reasonable
86 $version = $CLASS->new("1.2.3");
87
88 # Test boolean operator
89 ok ($version, 'boolean');
90
91 # Test class membership
92 isa_ok ( $version, "version" );
93
94 # Test comparison operators with self
95 diag "tests with self" unless $ENV{PERL_CORE};
96 ok ( $version eq $version, '$version eq $version' );
97 is ( $version cmp $version, 0, '$version cmp $version == 0' );
98 ok ( $version == $version, '$version == $version' );
99
100 # test first with non-object
101 $version = $CLASS->new("5.006.001");
102 $new_version = "5.8.0";
103 diag "tests with non-objects" unless $ENV{PERL_CORE};
104 ok ( $version ne $new_version, '$version ne $new_version' );
105 ok ( $version lt $new_version, '$version lt $new_version' );
106 ok ( $new_version gt $version, '$new_version gt $version' );
107 ok ( ref(\$new_version) eq 'SCALAR', 'no auto-upgrade');
108 $new_version = "$version";
109 ok ( $version eq $new_version, '$version eq $new_version' );
110 ok ( $new_version eq $version, '$new_version eq $version' );
111
112 # now test with existing object
113 $new_version = $CLASS->new("5.8.0");
114 diag "tests with objects" unless $ENV{PERL_CORE};
115 ok ( $version ne $new_version, '$version ne $new_version' );
116 ok ( $version lt $new_version, '$version lt $new_version' );
117 ok ( $new_version gt $version, '$new_version gt $version' );
118 $new_version = $CLASS->new("$version");
119 ok ( $version eq $new_version, '$version eq $new_version' );
120
121 # Test Numeric Comparison operators
122 # test first with non-object
123 $new_version = "5.8.0";
124 diag "numeric tests with non-objects" unless $ENV{PERL_CORE};
125 ok ( $version == $version, '$version == $version' );
126 ok ( $version < $new_version, '$version < $new_version' );
127 ok ( $new_version > $version, '$new_version > $version' );
128 ok ( $version != $new_version, '$version != $new_version' );
129
130 # now test with existing object
131 $new_version = $CLASS->new($new_version);
132 diag "numeric tests with objects" unless $ENV{PERL_CORE};
133 ok ( $version < $new_version, '$version < $new_version' );
134 ok ( $new_version > $version, '$new_version > $version' );
135 ok ( $version != $new_version, '$version != $new_version' );
136
137 # now test with actual numbers
138 diag "numeric tests with numbers" unless $ENV{PERL_CORE};
139 ok ( $version->numify() == 5.006001, '$version->numify() == 5.006001' );
140 ok ( $version->numify() <= 5.006001, '$version->numify() <= 5.006001' );
141 ok ( $version->numify() < 5.008, '$version->numify() < 5.008' );
142 #ok ( $version->numify() > v5.005_02, '$version->numify() > 5.005_02' );
143
144 # test with long decimals
145 diag "Tests with extended decimal versions" unless $ENV{PERL_CORE};
146 $version = $CLASS->new(1.002003);
147 ok ( $version eq "1.2.3", '$version eq "1.2.3"');
148 ok ( $version->numify == 1.002003, '$version->numify == 1.002003');
149 $version = $CLASS->new("2002.09.30.1");
150 ok ( $version eq "2002.9.30.1",'$version eq 2002.9.30.1');
151 ok ( $version->numify == 2002.009030001,
152 '$version->numify == 2002.009030001');
153
154 # now test with alpha version form with string
155 $version = $CLASS->new("1.2.3");
156 $new_version = "1.2.3_4";
157 diag "tests with alpha-style non-objects" unless $ENV{PERL_CORE};
158 ok ( $version lt $new_version, '$version lt $new_version' );
159 ok ( $new_version gt $version, '$new_version gt $version' );
160 ok ( $version ne $new_version, '$version ne $new_version' );
161
162 $version = $CLASS->new("1.2.4");
163 diag "numeric tests with alpha-style non-objects" unless $ENV{PERL_CORE};
164 ok ( $version > $new_version, '$version > $new_version' );
165 ok ( $new_version < $version, '$new_version < $version' );
166 ok ( $version != $new_version, '$version != $new_version' );
167
168 # now test with alpha version form with object
169 $version = $CLASS->new("1.2.3");
170 $new_version = $CLASS->new("1.2.3_4");
171 diag "tests with alpha-style objects" unless $ENV{PERL_CORE};
172 ok ( $version < $new_version, '$version < $new_version' );
173 ok ( $new_version > $version, '$new_version > $version' );
174 ok ( $version != $new_version, '$version != $new_version' );
175 ok ( !$version->is_alpha, '!$version->is_alpha');
176 ok ( $new_version->is_alpha, '$new_version->is_alpha');
177
178 $version = $CLASS->new("1.2.4");
179 diag "tests with alpha-style objects" unless $ENV{PERL_CORE};
180 ok ( $version > $new_version, '$version > $new_version' );
181 ok ( $new_version < $version, '$new_version < $version' );
182 ok ( $version != $new_version, '$version != $new_version' );
183
13f8f398
JP
184 $version = $CLASS->new("1.2.3.4");
185 $new_version = $CLASS->new("1.2.3_4");
137d6fc0
JP
186 diag "tests with alpha-style objects with same subversion" unless $ENV{PERL_CORE};
187 ok ( $version > $new_version, '$version > $new_version' );
188 ok ( $new_version < $version, '$new_version < $version' );
189 ok ( $version != $new_version, '$version != $new_version' );
190
191 diag "test implicit [in]equality" unless $ENV{PERL_CORE};
13f8f398
JP
192 $version = $CLASS->new("v1.2.3");
193 $new_version = $CLASS->new("1.2.3.0");
137d6fc0 194 ok ( $version == $new_version, '$version == $new_version' );
13f8f398 195 $new_version = $CLASS->new("1.2.3_0");
137d6fc0 196 ok ( $version == $new_version, '$version == $new_version' );
13f8f398 197 $new_version = $CLASS->new("1.2.3.1");
137d6fc0 198 ok ( $version < $new_version, '$version < $new_version' );
13f8f398 199 $new_version = $CLASS->new("1.2.3_1");
137d6fc0
JP
200 ok ( $version < $new_version, '$version < $new_version' );
201 $new_version = $CLASS->new("1.1.999");
202 ok ( $version > $new_version, '$version > $new_version' );
203
204 # that which is not expressly permitted is forbidden
205 diag "forbidden operations" unless $ENV{PERL_CORE};
206 ok ( !eval { ++$version }, "noop ++" );
207 ok ( !eval { --$version }, "noop --" );
208 ok ( !eval { $version/1 }, "noop /" );
209 ok ( !eval { $version*3 }, "noop *" );
210 ok ( !eval { abs($version) }, "noop abs" );
211
212 # test the qv() sub
213 diag "testing qv" unless $ENV{PERL_CORE};
214 $version = qv("1.2");
215 ok ( $version eq "1.2.0", 'qv("1.2") eq "1.2.0"' );
216 $version = qv(1.2);
217 ok ( $version eq "1.2.0", 'qv(1.2) eq "1.2.0"' );
218
13f8f398
JP
219 # test creation from existing version object
220 diag "create new from existing version" unless $ENV{PERL_CORE};
221 ok (eval {$new_version = version->new($version)},
222 "new from existing object");
9137345a
JP
223 ok ($new_version == $version, "class->new($version) identical");
224 $new_version = $version->new();
225 ok ($new_version == $version, "$version->new() also identical");
226 $new_version = $version->new("1.2.3");
227 is ($new_version, "v1.2.3" , '$version->new("1.2.3") works too');
13f8f398 228
137d6fc0
JP
229 # test the CVS revision mode
230 diag "testing CVS Revision" unless $ENV{PERL_CORE};
231 $version = new version qw$Revision: 1.2$;
232 ok ( $version eq "1.2.0", 'qw$Revision: 1.2$ eq 1.2.0' );
d2397f31
JP
233 $version = new version qw$Revision: 1.2.3.4$;
234 ok ( $version eq "1.2.3.4", 'qw$Revision: 1.2.3.4$ eq 1.2.3.4' );
137d6fc0 235
9137345a
JP
236 # test the CPAN style reduced significant digit form
237 diag "testing CPAN-style versions" unless $ENV{PERL_CORE};
238 $version = $CLASS->new("1.23_01");
239 is ( "$version" , "1.23_0100", "CPAN-style alpha version" );
240 ok ( $version > 1.23, "1.23_01 > 1.23");
241 ok ( $version < 1.24, "1.23_01 < 1.24");
242
137d6fc0
JP
243 # test reformed UNIVERSAL::VERSION
244 diag "Replacement UNIVERSAL::VERSION tests" unless $ENV{PERL_CORE};
245
246 # we know this file is here since we require it ourselves
13f8f398 247 $version = $Test::More::VERSION;
137d6fc0 248 eval "use Test::More $version";
b9381830 249 unlike($@, qr/Test::More version $version/,
137d6fc0
JP
250 'Replacement eval works with exact version');
251
13f8f398
JP
252 $version = $Test::More::VERSION+0.01; # this should fail even with old UNIVERSAL::VERSION
253 eval "use Test::More $version";
b9381830 254 like($@, qr/Test::More version $version/,
137d6fc0
JP
255 'Replacement eval works with incremented version');
256
b9381830 257 $version =~ s/\.0$//; #convert to string and remove trailing '.0'
137d6fc0
JP
258 chop($version); # shorten by 1 digit, should still succeed
259 eval "use Test::More $version";
b9381830 260 unlike($@, qr/Test::More version $version/,
137d6fc0
JP
261 'Replacement eval works with single digit');
262
263 $version += 0.1; # this would fail with old UNIVERSAL::VERSION
264 eval "use Test::More $version";
b9381830 265 like($@, qr/Test::More version $version/,
137d6fc0
JP
266 'Replacement eval works with incremented digit');
267
268SKIP: {
b9381830 269 skip 'Cannot test v-strings with Perl < 5.8.1', 4
137d6fc0
JP
270 if $] < 5.008_001;
271 diag "Tests with v-strings" unless $ENV{PERL_CORE};
272 $version = $CLASS->new(1.2.3);
9137345a 273 ok("$version" eq "v1.2.3", '"$version" eq 1.2.3');
137d6fc0
JP
274 $version = $CLASS->new(1.0.0);
275 $new_version = $CLASS->new(1);
276 ok($version == $new_version, '$version == $new_version');
277 ok($version eq $new_version, '$version eq $new_version');
137d6fc0 278 $version = qv(1.2.3);
9137345a 279 ok("$version" eq "v1.2.3", 'v-string initialized qv()');
137d6fc0
JP
280 }
281}