This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
new files lib/version.pm and lib/version.t for change #17990.
[perl5.git] / lib / version.t
CommitLineData
a7ad731c
HS
1# Before `make install' is performed this script should be runnable with
2# `make test'. After `make install' it should work as `perl test.pl'
3# $Revision: 2.0 $
4
5#########################
6
7use Test::More tests => 60;
8use_ok(version); # If we made it this far, we are ok.
9
10my ($version, $new_version);
11#########################
12
13# Insert your test code below, the Test module is use()ed here so read
14# its man page ( perldoc Test ) for help writing this test script.
15
16# Test stringify operator
17diag "tests with stringify" unless $ENV{PERL_CORE};
18$version = new version "5.005";
19is ( "$version" , "5.5" , '5.005 eq 5.5' );
20$version = new version "5.005_03";
21is ( "$version" , "5.5.30" , 'perl version 5.005_03 eq 5.5.30' );
22$version = new version "5.006.001";
23is ( "$version" , "5.6.1" , '5.006.001 eq 5.6.1' );
24$version = new version "1.2.3_4";
25is ( "$version" , "1.2.3_4" , 'beta version 1.2.3_4 eq 1.2.3_4' );
26
27# test illegal formats
28diag "test illegal formats" unless $ENV{PERL_CORE};
29eval {my $version = new version "1.2_3_4";};
30like($@, qr/multiple underscores/,
31 "Invalid version format (multiple underscores)");
32
33eval {my $version = new version "1.2_3.4";};
34like($@, qr/underscores before decimal/,
35 "Invalid version format (underscores before decimal)");
36
37# Test boolean operator
38ok ($version, 'boolean');
39
40# Test ref operator
41ok (ref($version) eq 'version','ref operator');
42
43# Test comparison operators with self
44diag "tests with self" unless $ENV{PERL_CORE};
45ok ( $version eq $version, '$version eq $version' );
46is ( $version cmp $version, 0, '$version cmp $version == 0' );
47ok ( $version == $version, '$version == $version' );
48
49# test first with non-object
50$version = new version "5.006.001";
51$new_version = "5.8.0";
52diag "tests with non-objects" unless $ENV{PERL_CORE};
53ok ( $version ne $new_version, '$version ne $new_version' );
54ok ( $version lt $new_version, '$version lt $new_version' );
55ok ( $new_version gt $version, '$new_version gt $version' );
56ok ( ref(\$new_version) eq 'SCALAR', 'no auto-upgrade');
57$new_version = "$version";
58ok ( $version eq $new_version, '$version eq $new_version' );
59ok ( $new_version eq $version, '$new_version eq $version' );
60
61# now test with existing object
62$new_version = new version "5.8.0";
63diag "tests with objects" unless $ENV{PERL_CORE};
64ok ( $version ne $new_version, '$version ne $new_version' );
65ok ( $version lt $new_version, '$version lt $new_version' );
66ok ( $new_version gt $version, '$new_version gt $version' );
67$new_version = new version "$version";
68ok ( $version eq $new_version, '$version eq $new_version' );
69
70# Test Numeric Comparison operators
71# test first with non-object
72$new_version = "5.8.0";
73diag "numeric tests with non-objects" unless $ENV{PERL_CORE};
74ok ( $version == $version, '$version == $version' );
75ok ( $version < $new_version, '$version < $new_version' );
76ok ( $new_version > $version, '$new_version > $version' );
77ok ( $version != $new_version, '$version != $new_version' );
78
79# now test with existing object
80$new_version = new version $new_version;
81diag "numeric tests with objects" unless $ENV{PERL_CORE};
82ok ( $version < $new_version, '$version < $new_version' );
83ok ( $new_version > $version, '$new_version > $version' );
84ok ( $version != $new_version, '$version != $new_version' );
85
86# now test with actual numbers
87diag "numeric tests with numbers" unless $ENV{PERL_CORE};
88ok ( $version->numify() == 5.006001, '$version->numify() == 5.006001' );
89ok ( $version->numify() <= 5.006001, '$version->numify() <= 5.006001' );
90ok ( $version->numify() < 5.008, '$version->numify() < 5.008' );
91#ok ( $version->numify() > v5.005_02, '$version->numify() > 5.005_02' );
92
93# test with long decimals
94diag "Tests with extended decimal versions" unless $ENV{PERL_CORE};
95$version = new version 1.002003;
96ok ( $version eq "1.2.3", '$version eq "1.2.3"');
97ok ( $version->numify == 1.002003, '$version->numify == 1.002003');
98$version = new version "2002.09.30.1";
99ok ( $version eq "2002.9.30.1",'$version eq 2002.9.30.1');
100ok ( $version->numify == 2002.009030001,
101 '$version->numify == 2002.009030001');
102
103# now test with Beta version form with string
104$version = new version "1.2.3";
105$new_version = "1.2.3_4";
106diag "tests with beta-style non-objects" unless $ENV{PERL_CORE};
107ok ( $version lt $new_version, '$version lt $new_version' );
108ok ( $new_version gt $version, '$new_version gt $version' );
109ok ( $version ne $new_version, '$version ne $new_version' );
110
111$version = new version "1.2.4";
112diag "numeric tests with beta-style non-objects" unless $ENV{PERL_CORE};
113ok ( $version > $new_version, '$version > $new_version' );
114ok ( $new_version < $version, '$new_version < $version' );
115ok ( $version != $new_version, '$version != $new_version' );
116
117# now test with Beta version form with object
118$version = new version "1.2.3";
119$new_version = new version "1.2.3_4";
120diag "tests with beta-style objects" unless $ENV{PERL_CORE};
121ok ( $version < $new_version, '$version < $new_version' );
122ok ( $new_version > $version, '$new_version > $version' );
123ok ( $version != $new_version, '$version != $new_version' );
124
125$version = new version "1.2.4";
126diag "tests with beta-style objects" unless $ENV{PERL_CORE};
127ok ( $version > $new_version, '$version > $new_version' );
128ok ( $new_version < $version, '$new_version < $version' );
129ok ( $version != $new_version, '$version != $new_version' );
130
131$version = new version "1.2.4";
132$new_version = new version "1.2_4";
133diag "tests with beta-style objects with same subversion" unless $ENV{PERL_CORE};
134ok ( $version > $new_version, '$version > $new_version' );
135ok ( $new_version < $version, '$new_version < $version' );
136ok ( $version != $new_version, '$version != $new_version' );
137
138# that which is not expressly permitted is forbidden
139diag "forbidden operations" unless $ENV{PERL_CORE};
140ok ( !eval { $version++ }, "noop ++" );
141ok ( !eval { $version-- }, "noop --" );
142ok ( !eval { $version/1 }, "noop /" );
143ok ( !eval { $version*3 }, "noop *" );
144ok ( !eval { abs($version) }, "noop abs" );
145
146# test reformed UNIVERSAL::VERSION
147diag "Replacement UNIVERSAL::VERSION tests" unless $ENV{PERL_CORE};
148
149# we know this file is here since we require it ourselves
150$version = $Test::More::VERSION;
151eval "use Test::More $version";
152unlike($@, qr/Test::More version $version required/,
153 'Replacement eval works with exact version');
154
155$version += 0.01; # this should fail even with old UNIVERSAL::VERSION
156eval "use Test::More $version";
157like($@, qr/Test::More version $version required/,
158 'Replacement eval works with incremented version');
159
160chop($version); # shorten by 1 digit, should still succeed
161eval "use Test::More $version";
162unlike($@, qr/Test::More version $version required/,
163 'Replacement eval works with single digit');
164
165$version += 0.1; # this would fail with old UNIVERSAL::VERSION
166eval "use Test::More $version";
167unlike($@, qr/Test::More version $version required/,
168 'Replacement eval works with incremented digit');
169