1#!/usr/bin/perl -w
2# $Id: 16version.t 1511 2010-08-21 23:24:49Z ian $
3
4# version.t
5#
6# Ensure VERSION() and REVISION() behave appropriately.
7
8use strict;
9use Test::More tests => 15;
10use Test::Exception;
11
12# create a package with just version information
13package Test::Version::One;
14
15use base qw( Class::Declare );
16use vars qw( $VERSION       );
17             $VERSION = '0.04';
18
191;
20
21# return to the default package
22package main;
23
24# make sure this still reports 0.04 through the call to REVISION()
25ok( Test::Version::One->VERSION eq '0.04' ,
26    'normal version information reported correctly' );
27
28
29# create a package with just revision information
30#   NB: have to hack this to make sure CVS doesn't expand the
31#       revision string (so that we can compare with a constant value)
32
33package Test::Version::Two;
34
35use base qw( Class::Declare );
36use vars qw( $REVISION      );
37             $REVISION  = '$Rev' . 'ision: 1.2.3 $';
38
391;
40
41# return to the default package
42package main;
43
44# make sure the REVISION() method returns the correct revision number
45ok( Test::Version::Two->REVISION eq '1.2.3' ,
46    'revision information reported correctly' );
47
48# make sure the version is the same as the revision
49ok( Test::Version::Two->REVISION eq Test::Version::Two->VERSION ,
50    'version numbers from revision strings reports correctly' );
51
52
53# create a package with revision and version information
54#   NB: have to hack this to make sure CVS doesn't expand the
55#       revision string (so that we can compare with a constant value)
56
57package Test::Version::Three;
58
59use base qw( Class::Declare     );
60use vars qw( $REVISION $VERSION );
61             $REVISION  = '$Rev' . 'ision: 1.2.3 $';
62             $VERSION   = '0.4';
63
641;
65
66# return to the default package
67package main;
68
69# make sure the REVISION() method returns the correct revision number
70ok( Test::Version::Three->REVISION eq '1.2.3' ,
71    'revision information reported correctly with version information' );
72
73# make sure the version is the reported correctly
74ok( Test::Version::Three->VERSION  eq '0.4'   ,
75    'version numbers overriding revision strings reports correctly' );
76
77# ensure required versioning is supported
78#   - packages with a defined version
79ok( Test::Version::Three->VERSION(  '0.3' )  eq '0.4'   ,
80    'required plain version supported' );
81ok( Test::Version::Three->VERSION( 'v0.3' )  eq '0.4'   ,
82    'required qualified version supported' );
83#   - packages with a defined revision only
84ok( Test::Version::Two->VERSION(  '1.2.1' )  eq '1.2.3' ,
85    'required plain version supported' );
86ok( Test::Version::Two->VERSION( 'v1.2.1' )  eq '1.2.3' ,
87    'required qualified version supported' );
88#   - require a version/revision ahead of that provided by the package
89throws_ok { Test::Version::Three->VERSION( 'v2' ) }
90          qr/version \S+ required--this is only/ ,
91          'require qualified future version fails as expected';
92throws_ok { Test::Version::Two->VERSION( '2.3.4' ) }
93          qr/version \S+ required--this is only/ ,
94          'require plain future version fails as expected';
95
96# ensure an invalid required version is reported correctly
97throws_ok { Test::Version::Three->VERSION( 'abcd' ) }
98          qr/Invalid version format/ ,
99          'require invalid version fails as expected';
100
101
102# create a package without version or revision information
103
104package Test::Version::Four;
105
106use base qw( Class::Declare );
107
1081;
109
110# return to the default package
111package main;
112
113ok( ! defined( Test::Version::Four->REVISION ) ,
114    'undefined revision reported correctly' );
115ok( ! defined( Test::Version::Four->VERSION  ) ,
116    'undefined version reported correctly' );
117
118# ensure required version checks not supported for packages that do not
119# provide a version or revision
120throws_ok { Test::Version::Four->VERSION( '1.2' ) }
121          qr/does not define \S+::VERSION--version check failed/ ,
122          'require version fails as expected for non-versioned packages';
123