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'
5 #########################
7 use Test::More tests => 202;
9 diag "Tests with base class" unless $ENV{PERL_CORE};
12 use_ok("version", 0.50); # If we made it this far, we are ok.
17 diag "Tests with empty derived class" unless $ENV{PERL_CORE};
19 package version::Empty;
22 no warnings 'redefine';
23 *::qv = sub { return bless version::qv(shift), __PACKAGE__; };
27 sub new { my($self,$n)=@_; bless \$n, $self }
30 my $testobj = version::Empty->new(1.002_003);
31 isa_ok( $testobj, "version::Empty" );
32 ok( $testobj->numify == 1.002003, "Numified correctly" );
33 ok( $testobj->stringify eq "1.002003", "Stringified correctly" );
34 ok( $testobj->normal eq "v1.2.3", "Normalified correctly" );
36 my $verobj = version->new("1.2.4");
37 ok( $verobj > $testobj, "Comparison vs parent class" );
38 ok( $verobj gt $testobj, "Comparison vs parent class" );
39 BaseTests("version::Empty");
41 diag "tests with bad subclass" unless $ENV{PERL_CORE};
42 $testobj = version::Bad->new(1.002_003);
43 isa_ok( $testobj, "version::Bad" );
44 eval { my $string = $testobj->numify };
45 like($@, qr/Invalid version object/,
46 "Bad subclass numify");
47 eval { my $string = $testobj->normal };
48 like($@, qr/Invalid version object/,
49 "Bad subclass normal");
50 eval { my $string = $testobj->stringify };
51 like($@, qr/Invalid version object/,
52 "Bad subclass stringify");
53 eval { my $test = $testobj > 1.0 };
54 like($@, qr/Invalid version object/,
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.
64 # Test bare number processing
65 diag "tests with bare numbers" unless $ENV{PERL_CORE};
66 $version = $CLASS->new(5.005_03);
67 is ( "$version" , "5.005030" , '5.005_03 eq 5.5.30' );
68 $version = $CLASS->new(1.23);
69 is ( "$version" , "1.230" , '1.23 eq "1.230"' );
71 # Test quoted number processing
72 diag "tests with quoted numbers" unless $ENV{PERL_CORE};
73 $version = $CLASS->new("5.005_03");
74 is ( "$version" , "5.005_030" , '"5.005_03" eq "5.005_030"' );
75 $version = $CLASS->new("v1.23");
76 is ( "$version" , "v1.23.0" , '"v1.23" eq "v1.23.0"' );
78 # Test stringify operator
79 diag "tests with stringify" unless $ENV{PERL_CORE};
80 $version = $CLASS->new("5.005");
81 is ( "$version" , "5.005" , '5.005 eq "5.005"' );
82 $version = $CLASS->new("5.006.001");
83 is ( "$version" , "v5.6.1" , '5.006.001 eq v5.6.1' );
84 $version = $CLASS->new("1.2.3_4");
85 is ( "$version" , "v1.2.3_4" , 'alpha version 1.2.3_4 eq v1.2.3_4' );
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)");
93 eval {my $version = $CLASS->new("1.2_3.4")};
94 like($@, qr/underscores before decimal/,
95 "Invalid version format (underscores before decimal)");
97 $version = $CLASS->new("99 and 44/100 pure");
98 ok ("$version" eq "99.000", '$version eq "99.000"');
99 ok ($version->numify == 99.0, '$version->numify == 99.0');
100 ok ($version->normal eq "v99.0.0", '$version->normal eq v99.0.0');
102 $version = $CLASS->new("something");
103 ok (defined $version, 'defined $version');
105 # reset the test object to something reasonable
106 $version = $CLASS->new("1.2.3");
108 # Test boolean operator
109 ok ($version, 'boolean');
111 # Test class membership
112 isa_ok ( $version, $CLASS );
114 # Test comparison operators with self
115 diag "tests with self" unless $ENV{PERL_CORE};
116 ok ( $version eq $version, '$version eq $version' );
117 is ( $version cmp $version, 0, '$version cmp $version == 0' );
118 ok ( $version == $version, '$version == $version' );
120 # test first with non-object
121 $version = $CLASS->new("5.006.001");
122 $new_version = "5.8.0";
123 diag "tests with non-objects" unless $ENV{PERL_CORE};
124 ok ( $version ne $new_version, '$version ne $new_version' );
125 ok ( $version lt $new_version, '$version lt $new_version' );
126 ok ( $new_version gt $version, '$new_version gt $version' );
127 ok ( ref(\$new_version) eq 'SCALAR', 'no auto-upgrade');
128 $new_version = "$version";
129 ok ( $version eq $new_version, '$version eq $new_version' );
130 ok ( $new_version eq $version, '$new_version eq $version' );
132 # now test with existing object
133 $new_version = $CLASS->new("5.8.0");
134 diag "tests with objects" unless $ENV{PERL_CORE};
135 ok ( $version ne $new_version, '$version ne $new_version' );
136 ok ( $version lt $new_version, '$version lt $new_version' );
137 ok ( $new_version gt $version, '$new_version gt $version' );
138 $new_version = $CLASS->new("$version");
139 ok ( $version eq $new_version, '$version eq $new_version' );
141 # Test Numeric Comparison operators
142 # test first with non-object
143 $new_version = "5.8.0";
144 diag "numeric tests with non-objects" unless $ENV{PERL_CORE};
145 ok ( $version == $version, '$version == $version' );
146 ok ( $version < $new_version, '$version < $new_version' );
147 ok ( $new_version > $version, '$new_version > $version' );
148 ok ( $version != $new_version, '$version != $new_version' );
150 # now test with existing object
151 $new_version = $CLASS->new($new_version);
152 diag "numeric tests with objects" unless $ENV{PERL_CORE};
153 ok ( $version < $new_version, '$version < $new_version' );
154 ok ( $new_version > $version, '$new_version > $version' );
155 ok ( $version != $new_version, '$version != $new_version' );
157 # now test with actual numbers
158 diag "numeric tests with numbers" unless $ENV{PERL_CORE};
159 ok ( $version->numify() == 5.006001, '$version->numify() == 5.006001' );
160 ok ( $version->numify() <= 5.006001, '$version->numify() <= 5.006001' );
161 ok ( $version->numify() < 5.008, '$version->numify() < 5.008' );
162 #ok ( $version->numify() > v5.005_02, '$version->numify() > 5.005_02' );
164 # test with long decimals
165 diag "Tests with extended decimal versions" unless $ENV{PERL_CORE};
166 $version = $CLASS->new(1.002003);
167 ok ( $version eq "1.2.3", '$version eq "1.2.3"');
168 ok ( $version->numify == 1.002003, '$version->numify == 1.002003');
169 $version = $CLASS->new("2002.09.30.1");
170 ok ( $version eq "2002.9.30.1",'$version eq 2002.9.30.1');
171 ok ( $version->numify == 2002.009030001,
172 '$version->numify == 2002.009030001');
174 # now test with alpha version form with string
175 $version = $CLASS->new("1.2.3");
176 $new_version = "1.2.3_4";
177 diag "tests with alpha-style non-objects" unless $ENV{PERL_CORE};
178 ok ( $version lt $new_version, '$version lt $new_version' );
179 ok ( $new_version gt $version, '$new_version gt $version' );
180 ok ( $version ne $new_version, '$version ne $new_version' );
182 $version = $CLASS->new("1.2.4");
183 diag "numeric tests with alpha-style non-objects" unless $ENV{PERL_CORE};
184 ok ( $version > $new_version, '$version > $new_version' );
185 ok ( $new_version < $version, '$new_version < $version' );
186 ok ( $version != $new_version, '$version != $new_version' );
188 # now test with alpha version form with object
189 $version = $CLASS->new("1.2.3");
190 $new_version = $CLASS->new("1.2.3_4");
191 diag "tests with alpha-style objects" unless $ENV{PERL_CORE};
192 ok ( $version < $new_version, '$version < $new_version' );
193 ok ( $new_version > $version, '$new_version > $version' );
194 ok ( $version != $new_version, '$version != $new_version' );
195 ok ( !$version->is_alpha, '!$version->is_alpha');
196 ok ( $new_version->is_alpha, '$new_version->is_alpha');
198 $version = $CLASS->new("1.2.4");
199 diag "tests with alpha-style objects" unless $ENV{PERL_CORE};
200 ok ( $version > $new_version, '$version > $new_version' );
201 ok ( $new_version < $version, '$new_version < $version' );
202 ok ( $version != $new_version, '$version != $new_version' );
204 $version = $CLASS->new("1.2.3.4");
205 $new_version = $CLASS->new("1.2.3_4");
206 diag "tests with alpha-style objects with same subversion" 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' );
211 diag "test implicit [in]equality" unless $ENV{PERL_CORE};
212 $version = $CLASS->new("v1.2.3");
213 $new_version = $CLASS->new("1.2.3.0");
214 ok ( $version == $new_version, '$version == $new_version' );
215 $new_version = $CLASS->new("1.2.3_0");
216 ok ( $version == $new_version, '$version == $new_version' );
217 $new_version = $CLASS->new("1.2.3.1");
218 ok ( $version < $new_version, '$version < $new_version' );
219 $new_version = $CLASS->new("1.2.3_1");
220 ok ( $version < $new_version, '$version < $new_version' );
221 $new_version = $CLASS->new("1.1.999");
222 ok ( $version > $new_version, '$version > $new_version' );
224 # that which is not expressly permitted is forbidden
225 diag "forbidden operations" unless $ENV{PERL_CORE};
226 ok ( !eval { ++$version }, "noop ++" );
227 ok ( !eval { --$version }, "noop --" );
228 ok ( !eval { $version/1 }, "noop /" );
229 ok ( !eval { $version*3 }, "noop *" );
230 ok ( !eval { abs($version) }, "noop abs" );
233 diag "testing qv" unless $ENV{PERL_CORE};
234 $version = qv("1.2");
235 ok ( $version eq "1.2.0", 'qv("1.2") eq "1.2.0"' );
237 ok ( $version eq "1.2.0", 'qv(1.2) eq "1.2.0"' );
238 isa_ok( qv('5.008'), $CLASS );
240 # test creation from existing version object
241 diag "create new from existing version" unless $ENV{PERL_CORE};
242 ok (eval {$new_version = $CLASS->new($version)},
243 "new from existing object");
244 ok ($new_version == $version, "class->new($version) identical");
245 $new_version = $version->new();
246 ok ($new_version == $version, "$version->new() also identical");
247 $new_version = $version->new("1.2.3");
248 is ($new_version, "v1.2.3" , '$version->new("1.2.3") works too');
250 # test the CVS revision mode
251 diag "testing CVS Revision" unless $ENV{PERL_CORE};
252 $version = new $CLASS qw$Revision: 1.2$;
253 ok ( $version eq "1.2.0", 'qw$Revision: 1.2$ eq 1.2.0' );
254 $version = new $CLASS qw$Revision: 1.2.3.4$;
255 ok ( $version eq "1.2.3.4", 'qw$Revision: 1.2.3.4$ eq 1.2.3.4' );
257 # test the CPAN style reduced significant digit form
258 diag "testing CPAN-style versions" unless $ENV{PERL_CORE};
259 $version = $CLASS->new("1.23_01");
260 is ( "$version" , "1.23_0100", "CPAN-style alpha version" );
261 ok ( $version > 1.23, "1.23_01 > 1.23");
262 ok ( $version < 1.24, "1.23_01 < 1.24");
264 # test reformed UNIVERSAL::VERSION
265 diag "Replacement UNIVERSAL::VERSION tests" unless $ENV{PERL_CORE};
267 # we know this file is here since we require it ourselves
268 $version = $Test::More::VERSION;
269 eval "use Test::More $version";
270 unlike($@, qr/Test::More version $version/,
271 'Replacement eval works with exact version');
273 $version = $Test::More::VERSION+0.01; # this should fail even with old UNIVERSAL::VERSION
274 eval "use Test::More $version";
275 like($@, qr/Test::More version $version/,
276 'Replacement eval works with incremented version');
278 $version =~ s/\.0$//; #convert to string and remove trailing '.0'
279 chop($version); # shorten by 1 digit, should still succeed
280 eval "use Test::More $version";
281 unlike($@, qr/Test::More version $version/,
282 'Replacement eval works with single digit');
284 $version += 0.1; # this would fail with old UNIVERSAL::VERSION
285 eval "use Test::More $version";
286 like($@, qr/Test::More version $version/,
287 'Replacement eval works with incremented digit');
290 skip 'Cannot test v-strings with Perl < 5.8.1', 4
292 diag "Tests with v-strings" unless $ENV{PERL_CORE};
293 $version = $CLASS->new(1.2.3);
294 ok("$version" eq "v1.2.3", '"$version" eq 1.2.3');
295 $version = $CLASS->new(1.0.0);
296 $new_version = $CLASS->new(1);
297 ok($version == $new_version, '$version == $new_version');
298 ok($version eq $new_version, '$version eq $new_version');
299 $version = qv(1.2.3);
300 ok("$version" eq "v1.2.3", 'v-string initialized qv()');
303 diag "Tests with real-world (malformed) data" unless $ENV{PERL_CORE};
305 # trailing zero testing (reported by Andreas Koenig).
306 $version = $CLASS->new("1");
307 ok($version->numify eq "1.000", "trailing zeros preserved");
308 $version = $CLASS->new("1.0");
309 ok($version->numify eq "1.000", "trailing zeros preserved");
310 $version = $CLASS->new("1.0.0");
311 ok($version->numify eq "1.000000", "trailing zeros preserved");
312 $version = $CLASS->new("1.0.0.0");
313 ok($version->numify eq "1.000000000", "trailing zeros preserved");
315 # leading zero testing (reported by Andreas Koenig).
316 $version = $CLASS->new(".7");
317 ok($version->numify eq "0.700", "leading zero inferred");
319 # leading space testing (reported by Andreas Koenig).
320 $version = $CLASS->new(" 1.7");
321 ok($version->numify eq "1.700", "leading space ignored");