summaryrefslogtreecommitdiff
path: root/lib/version.t
blob: c60d6563f143f524045674375cde428eff706014 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#! /usr/local/perl -w
# Before `make install' is performed this script should be runnable with
# `make test'. After `make install' it should work as `perl test.pl'

#########################

use Test::More tests => 200;

diag "Tests with base class" unless $ENV{PERL_CORE};

BEGIN {
    use_ok("version", 0.47); # If we made it this far, we are ok.
}

BaseTests("version");

diag "Tests with empty derived class" unless $ENV{PERL_CORE};

package version::Empty;
use vars qw($VERSION @ISA);
use version;
@ISA = qw(version);
$VERSION = 0.01;

package version::Bad;
use base version;
sub new { my($self,$n)=@_;  bless \$n, $self }

package main;
my $testobj = version::Empty->new(1.002_003);
isa_ok( $testobj, "version::Empty" );
ok( $testobj->numify == 1.002003, "Numified correctly" );
ok( $testobj->stringify eq "1.002003", "Stringified correctly" );
ok( $testobj->normal eq "v1.2.3", "Normalified correctly" );

my $verobj = version->new("1.2.4");
ok( $verobj > $testobj, "Comparison vs parent class" );
ok( $verobj gt $testobj, "Comparison vs parent class" );
BaseTests("version::Empty");

diag "tests with bad subclass" unless $ENV{PERL_CORE};
$testobj = version::Bad->new(1.002_003);
isa_ok( $testobj, "version::Bad" );
eval { my $string = $testobj->numify };
like($@, qr/Invalid version object/,
    "Bad subclass numify");
eval { my $string = $testobj->normal };
like($@, qr/Invalid version object/,
    "Bad subclass normal");
eval { my $string = $testobj->stringify };
like($@, qr/Invalid version object/,
    "Bad subclass stringify");
eval { my $test = $testobj > 1.0 };
like($@, qr/Invalid version object/,
    "Bad subclass vcmp");

sub BaseTests {

	my $CLASS = shift;
	
	# Insert your test code below, the Test module is use()ed here so read
	# its man page ( perldoc Test ) for help writing this test script.
	
	# Test bare number processing
	diag "tests with bare numbers" unless $ENV{PERL_CORE};
	$version = $CLASS->new(5.005_03);
	is ( "$version" , "5.005030" , '5.005_03 eq 5.5.30' );
	$version = $CLASS->new(1.23);
	is ( "$version" , "1.230" , '1.23 eq "1.230"' );
	
	# Test quoted number processing
	diag "tests with quoted numbers" unless $ENV{PERL_CORE};
	$version = $CLASS->new("5.005_03");
	is ( "$version" , "5.005_030" , '"5.005_03" eq "5.005_030"' );
	$version = $CLASS->new("v1.23");
	is ( "$version" , "v1.23.0" , '"v1.23" eq "v1.23.0"' );
	
	# Test stringify operator
	diag "tests with stringify" unless $ENV{PERL_CORE};
	$version = $CLASS->new("5.005");
	is ( "$version" , "5.005" , '5.005 eq "5.005"' );
	$version = $CLASS->new("5.006.001");
	is ( "$version" , "v5.6.1" , '5.006.001 eq v5.6.1' );
	$version = $CLASS->new("1.2.3_4");
	is ( "$version" , "v1.2.3_4" , 'alpha version 1.2.3_4 eq v1.2.3_4' );
	
	# test illegal formats
	diag "test illegal formats" unless $ENV{PERL_CORE};
	eval {my $version = $CLASS->new("1.2_3_4")};
	like($@, qr/multiple underscores/,
	    "Invalid version format (multiple underscores)");
	
	eval {my $version = $CLASS->new("1.2_3.4")};
	like($@, qr/underscores before decimal/,
	    "Invalid version format (underscores before decimal)");
	
	$version = $CLASS->new("99 and 44/100 pure");
	ok ("$version" eq "99.000", '$version eq "99.000"');
	ok ($version->numify == 99.0, '$version->numify == 99.0');
	ok ($version->normal eq "v99.0.0", '$version->normal eq v99.0.0');
	
	$version = $CLASS->new("something");
	ok (defined $version, 'defined $version');
	
	# reset the test object to something reasonable
	$version = $CLASS->new("1.2.3");
	
	# Test boolean operator
	ok ($version, 'boolean');
	
	# Test class membership
	isa_ok ( $version, $CLASS );
	
	# Test comparison operators with self
	diag "tests with self" unless $ENV{PERL_CORE};
	ok ( $version eq $version, '$version eq $version' );
	is ( $version cmp $version, 0, '$version cmp $version == 0' );
	ok ( $version == $version, '$version == $version' );
	
	# test first with non-object
	$version = $CLASS->new("5.006.001");
	$new_version = "5.8.0";
	diag "tests with non-objects" unless $ENV{PERL_CORE};
	ok ( $version ne $new_version, '$version ne $new_version' );
	ok ( $version lt $new_version, '$version lt $new_version' );
	ok ( $new_version gt $version, '$new_version gt $version' );
	ok ( ref(\$new_version) eq 'SCALAR', 'no auto-upgrade');
	$new_version = "$version";
	ok ( $version eq $new_version, '$version eq $new_version' );
	ok ( $new_version eq $version, '$new_version eq $version' );
	
	# now test with existing object
	$new_version = $CLASS->new("5.8.0");
	diag "tests with objects" unless $ENV{PERL_CORE};
	ok ( $version ne $new_version, '$version ne $new_version' );
	ok ( $version lt $new_version, '$version lt $new_version' );
	ok ( $new_version gt $version, '$new_version gt $version' );
	$new_version = $CLASS->new("$version");
	ok ( $version eq $new_version, '$version eq $new_version' );
	
	# Test Numeric Comparison operators
	# test first with non-object
	$new_version = "5.8.0";
	diag "numeric tests with non-objects" unless $ENV{PERL_CORE};
	ok ( $version == $version, '$version == $version' );
	ok ( $version < $new_version, '$version < $new_version' );
	ok ( $new_version > $version, '$new_version > $version' );
	ok ( $version != $new_version, '$version != $new_version' );
	
	# now test with existing object
	$new_version = $CLASS->new($new_version);
	diag "numeric tests with objects" unless $ENV{PERL_CORE};
	ok ( $version < $new_version, '$version < $new_version' );
	ok ( $new_version > $version, '$new_version > $version' );
	ok ( $version != $new_version, '$version != $new_version' );
	
	# now test with actual numbers
	diag "numeric tests with numbers" unless $ENV{PERL_CORE};
	ok ( $version->numify() == 5.006001, '$version->numify() == 5.006001' );
	ok ( $version->numify() <= 5.006001, '$version->numify() <= 5.006001' );
	ok ( $version->numify() < 5.008, '$version->numify() < 5.008' );
	#ok ( $version->numify() > v5.005_02, '$version->numify() > 5.005_02' );
	
	# test with long decimals
	diag "Tests with extended decimal versions" unless $ENV{PERL_CORE};
	$version = $CLASS->new(1.002003);
	ok ( $version eq "1.2.3", '$version eq "1.2.3"');
	ok ( $version->numify == 1.002003, '$version->numify == 1.002003');
	$version = $CLASS->new("2002.09.30.1");
	ok ( $version eq "2002.9.30.1",'$version eq 2002.9.30.1');
	ok ( $version->numify == 2002.009030001,
	    '$version->numify == 2002.009030001');
	
	# now test with alpha version form with string
	$version = $CLASS->new("1.2.3");
	$new_version = "1.2.3_4";
	diag "tests with alpha-style non-objects" unless $ENV{PERL_CORE};
	ok ( $version lt $new_version, '$version lt $new_version' );
	ok ( $new_version gt $version, '$new_version gt $version' );
	ok ( $version ne $new_version, '$version ne $new_version' );
	
	$version = $CLASS->new("1.2.4");
	diag "numeric tests with alpha-style non-objects" unless $ENV{PERL_CORE};
	ok ( $version > $new_version, '$version > $new_version' );
	ok ( $new_version < $version, '$new_version < $version' );
	ok ( $version != $new_version, '$version != $new_version' );
	
	# now test with alpha version form with object
	$version = $CLASS->new("1.2.3");
	$new_version = $CLASS->new("1.2.3_4");
	diag "tests with alpha-style objects" unless $ENV{PERL_CORE};
	ok ( $version < $new_version, '$version < $new_version' );
	ok ( $new_version > $version, '$new_version > $version' );
	ok ( $version != $new_version, '$version != $new_version' );
	ok ( !$version->is_alpha, '!$version->is_alpha');
	ok ( $new_version->is_alpha, '$new_version->is_alpha');
	
	$version = $CLASS->new("1.2.4");
	diag "tests with alpha-style objects" unless $ENV{PERL_CORE};
	ok ( $version > $new_version, '$version > $new_version' );
	ok ( $new_version < $version, '$new_version < $version' );
	ok ( $version != $new_version, '$version != $new_version' );
	
	$version = $CLASS->new("1.2.3.4");
	$new_version = $CLASS->new("1.2.3_4");
	diag "tests with alpha-style objects with same subversion" unless $ENV{PERL_CORE};
	ok ( $version > $new_version, '$version > $new_version' );
	ok ( $new_version < $version, '$new_version < $version' );
	ok ( $version != $new_version, '$version != $new_version' );
	
	diag "test implicit [in]equality" unless $ENV{PERL_CORE};
	$version = $CLASS->new("v1.2.3");
	$new_version = $CLASS->new("1.2.3.0");
	ok ( $version == $new_version, '$version == $new_version' );
	$new_version = $CLASS->new("1.2.3_0");
	ok ( $version == $new_version, '$version == $new_version' );
	$new_version = $CLASS->new("1.2.3.1");
	ok ( $version < $new_version, '$version < $new_version' );
	$new_version = $CLASS->new("1.2.3_1");
	ok ( $version < $new_version, '$version < $new_version' );
	$new_version = $CLASS->new("1.1.999");
	ok ( $version > $new_version, '$version > $new_version' );
	
	# that which is not expressly permitted is forbidden
	diag "forbidden operations" unless $ENV{PERL_CORE};
	ok ( !eval { ++$version }, "noop ++" );
	ok ( !eval { --$version }, "noop --" );
	ok ( !eval { $version/1 }, "noop /" );
	ok ( !eval { $version*3 }, "noop *" );
	ok ( !eval { abs($version) }, "noop abs" );

	# test the qv() sub
	diag "testing qv" unless $ENV{PERL_CORE};
	$version = qv("1.2");
	ok ( $version eq "1.2.0", 'qv("1.2") eq "1.2.0"' );
	$version = qv(1.2);
	ok ( $version eq "1.2.0", 'qv(1.2) eq "1.2.0"' );

	# test creation from existing version object
	diag "create new from existing version" unless $ENV{PERL_CORE};
	ok (eval {$new_version = $CLASS->new($version)},
		"new from existing object");
	ok ($new_version == $version, "class->new($version) identical");
	$new_version = $version->new();
	ok ($new_version == $version, "$version->new() also identical");
	$new_version = $version->new("1.2.3");
	is ($new_version, "v1.2.3" , '$version->new("1.2.3") works too');

	# test the CVS revision mode
	diag "testing CVS Revision" unless $ENV{PERL_CORE};
	$version = new $CLASS qw$Revision: 1.2$;
	ok ( $version eq "1.2.0", 'qw$Revision: 1.2$ eq 1.2.0' );
	$version = new $CLASS qw$Revision: 1.2.3.4$;
	ok ( $version eq "1.2.3.4", 'qw$Revision: 1.2.3.4$ eq 1.2.3.4' );
	
	# test the CPAN style reduced significant digit form
	diag "testing CPAN-style versions" unless $ENV{PERL_CORE};
	$version = $CLASS->new("1.23_01");
	is ( "$version" , "1.23_0100", "CPAN-style alpha version" );
	ok ( $version > 1.23, "1.23_01 > 1.23");
	ok ( $version < 1.24, "1.23_01 < 1.24");

	# test reformed UNIVERSAL::VERSION
	diag "Replacement UNIVERSAL::VERSION tests" unless $ENV{PERL_CORE};
	
	# we know this file is here since we require it ourselves
	$version = $Test::More::VERSION;
	eval "use Test::More $version";
	unlike($@, qr/Test::More version $version/,
		'Replacement eval works with exact version');
	
	$version = $Test::More::VERSION+0.01; # this should fail even with old UNIVERSAL::VERSION
	eval "use Test::More $version";
	like($@, qr/Test::More version $version/,
		'Replacement eval works with incremented version');
	
	$version =~ s/\.0$//; #convert to string and remove trailing '.0'
	chop($version);	# shorten by 1 digit, should still succeed
	eval "use Test::More $version";
	unlike($@, qr/Test::More version $version/,
		'Replacement eval works with single digit');
	
	$version += 0.1; # this would fail with old UNIVERSAL::VERSION
	eval "use Test::More $version";
	like($@, qr/Test::More version $version/,
		'Replacement eval works with incremented digit');
	
SKIP: 	{
	    skip 'Cannot test v-strings with Perl < 5.8.1', 4
		    if $] < 5.008_001; 
	    diag "Tests with v-strings" unless $ENV{PERL_CORE};
	    $version = $CLASS->new(1.2.3);
	    ok("$version" eq "v1.2.3", '"$version" eq 1.2.3');
	    $version = $CLASS->new(1.0.0);
	    $new_version = $CLASS->new(1);
	    ok($version == $new_version, '$version == $new_version');
	    ok($version eq $new_version, '$version eq $new_version');
	    $version = qv(1.2.3);
	    ok("$version" eq "v1.2.3", 'v-string initialized qv()');
	}

	diag "Tests with real-world (malformed) data" unless $ENV{PERL_CORE};

	# trailing zero testing (reported by Andreas Koenig).
	$version = $CLASS->new("1");
	ok($version->numify eq "1.000", "trailing zeros preserved");
	$version = $CLASS->new("1.0");
	ok($version->numify eq "1.000", "trailing zeros preserved");
	$version = $CLASS->new("1.0.0");
	ok($version->numify eq "1.000000", "trailing zeros preserved");
	$version = $CLASS->new("1.0.0.0");
	ok($version->numify eq "1.000000000", "trailing zeros preserved");
	
	# leading zero testing (reported by Andreas Koenig).
	$version = $CLASS->new(".7");
	ok($version->numify eq "0.700", "leading zero inferred");

	# leading space testing (reported by Andreas Koenig).
	$version = $CLASS->new(" 1.7");
	ok($version->numify eq "1.700", "leading space ignored");

}

1;