summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJohn Peacock <jpeacock@rowman.com>2005-08-23 23:41:11 +0300
committerRafael Garcia-Suarez <rgarciasuarez@gmail.com>2005-08-24 15:53:01 +0000
commite0218a61b599e8e5c97718ac68ef92ad34b20839 (patch)
treed49dbe4e2b1b595f58f31c9ee9067317b242f321 /lib
parent80dc6883dac79eac16d48a9478d1423a03bd4025 (diff)
downloadperl-e0218a61b599e8e5c97718ac68ef92ad34b20839.tar.gz
[Fwd: CPAN Upload: J/JP/JPEACOCK/version-0.47.tar.gz]
From: "John Peacock" <jpeacock@rowman.com> Message-ID: <2444.85.65.24.143.1124818871.squirrel@webmail.rowman.com> p4raw-id: //depot/perl@25325
Diffstat (limited to 'lib')
-rw-r--r--lib/version.pm24
-rw-r--r--lib/version.t49
2 files changed, 68 insertions, 5 deletions
diff --git a/lib/version.pm b/lib/version.pm
index e6e4f3ee1f..1e3cabbb0d 100644
--- a/lib/version.pm
+++ b/lib/version.pm
@@ -12,7 +12,7 @@ use vars qw(@ISA $VERSION $CLASS @EXPORT);
@EXPORT = qw(qv);
-$VERSION = "0.44";
+$VERSION = "0.47";
$CLASS = 'version';
@@ -538,6 +538,28 @@ will also exclusively return the numified form. Technically, the
$module->VERSION function returns a string (PV) that can be converted to a
number following the normal Perl rules, when used in a numeric context.
+=head1 SUBCLASSING
+
+This module is specifically designed and tested to be easily subclassed.
+In practice, you only need to override the methods you want to change, but
+you have to take some care when overriding new() (since that is where all
+of the parsing takes place). For example, this is a perfect acceptable
+derived class:
+
+ package myversion;
+ use base version;
+ sub new {
+ my($self,$n)=@_;
+ my $obj;
+ # perform any special input handling here
+ $obj = $self->SUPER::new($n);
+ # and/or add additional hash elements here
+ return $obj;
+ }
+
+See also L<version::AlphaBeta> on CPAN for an alternate representation of
+version strings.
+
=head1 EXPORT
qv - quoted version initialization operator
diff --git a/lib/version.t b/lib/version.t
index bfb9c463a7..e387095ec2 100644
--- a/lib/version.t
+++ b/lib/version.t
@@ -4,12 +4,12 @@
#########################
-use Test::More tests => 183;
+use Test::More tests => 200;
diag "Tests with base class" unless $ENV{PERL_CORE};
BEGIN {
- use_ok("version", 0.30); # If we made it this far, we are ok.
+ use_ok("version", 0.47); # If we made it this far, we are ok.
}
BaseTests("version");
@@ -22,18 +22,38 @@ use version 0.30;
@ISA = qw(version);
$VERSION = 0.01;
+package version::Bad;
+use base version;
+sub new { my($self,$n)=@_; bless \$n, $self }
+
package main;
-my $testobj = new version::Empty 1.002_003;
+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 = new version "1.2.4";
+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;
@@ -278,4 +298,25 @@ SKIP: {
$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");
+
}