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