#########################
-use Test::More tests => 183;
+use Test::More tests => 200;
diag "Tests with base class" unless $ENV{PERL_CORE};
-use_ok("version"); # If we made it this far, we are ok.
+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 0.30;
+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 = 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;
ok ($version, 'boolean');
# Test class membership
- isa_ok ( $version, "version" );
+ isa_ok ( $version, $CLASS );
# Test comparison operators with self
diag "tests with self" unless $ENV{PERL_CORE};
# test creation from existing version object
diag "create new from existing version" unless $ENV{PERL_CORE};
- ok (eval {$new_version = version->new($version)},
+ ok (eval {$new_version = $CLASS->new($version)},
"new from existing object");
ok ($new_version == $version, "class->new($version) identical");
$new_version = $version->new();
# test the CVS revision mode
diag "testing CVS Revision" unless $ENV{PERL_CORE};
- $version = new version qw$Revision: 1.2$;
+ $version = new $CLASS qw$Revision: 1.2$;
ok ( $version eq "1.2.0", 'qw$Revision: 1.2$ eq 1.2.0' );
- $version = new version qw$Revision: 1.2.3.4$;
+ $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
$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;