This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Merge CPAN version.pm tests into CORE.
[perl5.git] / lib / version / t / 02derived.t
CommitLineData
543eec9e
JP
1#! /usr/local/perl -w
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'
4
5#########################
6
7use Test::More qw/no_plan/;
8use File::Temp qw/tempfile/;
9
10BEGIN {
11 (my $coretests = $0) =~ s'[^/]+\.t'coretests.pm';
12 require $coretests;
13 use_ok("version", 0.96);
14 # If we made it this far, we are ok.
15}
16
17use lib qw/./;
18
19package version::Bad;
20use base 'version';
21sub new { my($self,$n)=@_; bless \$n, $self }
22
23# Bad subclass for SemVer failures seen with pure Perl version.pm only
24package version::Bad2;
25use base 'version';
26sub new {
27 my ($class, $val) = @_;
28 die 'Invalid version string format' unless version::is_strict($val);
29 my $self = $class->SUPER::new($val);
30 return $self;
31}
32sub declare {
33 my ($class, $val) = @_;
34 my $self = $class->SUPER::declare($val);
35 return $self;
36}
37
38package main;
39
40my $warning;
41local $SIG{__WARN__} = sub { $warning = $_[0] };
42# dummy up a legal module for testing RT#19017
43my ($fh, $filename) = tempfile('tXXXXXXX', SUFFIX => '.pm', UNLINK => 1);
44(my $package = basename($filename)) =~ s/\.pm$//;
45print $fh <<"EOF";
46# This is an empty subclass
47package $package;
48use base 'version';
49use vars '\$VERSION';
50\$VERSION=0.001;
51EOF
52close $fh;
53
54sub main_reset {
55 delete $main::INC{'$package'};
56 undef &qv; undef *::qv; # avoid 'used once' warning
57 undef &declare; undef *::declare; # avoid 'used once' warning
58}
59
60diag "Tests with empty derived class" unless $ENV{PERL_CORE};
61
62use_ok($package, 0.001);
63my $testobj = $package->new(1.002_003);
64isa_ok( $testobj, $package );
65ok( $testobj->numify == 1.002003, "Numified correctly" );
66ok( $testobj->stringify eq "1.002003", "Stringified correctly" );
67ok( $testobj->normal eq "v1.2.3", "Normalified correctly" );
68
69my $verobj = version::->new("1.2.4");
70ok( $verobj > $testobj, "Comparison vs parent class" );
71
72BaseTests($package, "new", "qv");
73main_reset;
74use_ok($package, 0.001, "declare");
75BaseTests($package, "new", "declare");
76main_reset;
77use_ok($package, 0.001);
78BaseTests($package, "parse", "qv");
79main_reset;
80use_ok($package, 0.001, "declare");
81BaseTests($package, "parse", "declare");
82
83diag "tests with bad subclass" unless $ENV{PERL_CORE};
84$testobj = version::Bad->new(1.002_003);
85isa_ok( $testobj, "version::Bad" );
86eval { my $string = $testobj->numify };
87like($@, qr/Invalid version object/,
88 "Bad subclass numify");
89eval { my $string = $testobj->normal };
90like($@, qr/Invalid version object/,
91 "Bad subclass normal");
92eval { my $string = $testobj->stringify };
93like($@, qr/Invalid version object/,
94 "Bad subclass stringify");
95eval { my $test = ($testobj > 1.0) };
96like($@, qr/Invalid version object/,
97 "Bad subclass vcmp");
98
99# Bad subclassing for SemVer with pure Perl version.pm only
100eval { my $test = version::Bad2->new("01.1.2") };
101like($@, qr/Invalid version string format/,
102 "Correctly found invalid version");
103
104eval { my $test = version::Bad2->declare("01.1.2") };
105unlike($@, qr/Invalid version string format/,
106 "Correctly ignored invalid version");