/usr/share/perl5/Lire/Test/TestCase.pm is in lire 2:2.1.1-2.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 | package Lire::Test::TestCase;
use strict;
use base qw/ Test::Unit::TestCase /;
use POSIX qw/ setlocale LC_MESSAGES tzset /;
use File::Basename qw/ dirname /;
use Cwd 'realpath';
use Test::Unit::Failure;
use Test::Unit::Error;
use Lire::Config;
use Lire::Config::SpecParser;
use Lire::Test::HashConfig;
use Lire::PluginManager;
use Lire::Utils qw/check_param/;
=pod
=head1 NAME
Lire::Test::TestCase - Common base for Lire test cases.
=head1 SYNOPSIS
use base qw/Lire::Test::TestCase/;
sub set_up {
my $self = shift->SUPER::set_up();
$self->{'cfg'}{'lr_schemas_path'} = [ $self->{'tmpdir'} ];
}
=head1 DESCRIPTION
This package can be used to write test cases for Lire instead of
inheriting directly from Test::Unit::TestCase.
The default set_up method will create a Lire::Test::HashConfig object
and set it up in the configuration framework. This makes it easy to
set configuration variables to arbitrary values in the set_up()
method.
It also sets the LC_MESSAGES locale category to 'C'.
If you override set_up() and tear_down() be sure to call the SUPER::
implementation.
This subclass also defined a new kind of assertion which make sure
that a snippet of code died with a proper error message.
=head2 assert_died( $test, $regex, [ $msg ] )
Execute $test and fail unless it dies with a message matching $regex.
$test should be a reference to CODE. $regex should be a Regexp ref.
=head2 assert_dies( $regex, $test, [ $msg ] )
Like assert_died() but with a signature closer to the one defined in
Test::Unit::Assert.
=head2 assert_isa( $class, $instance, [ $msg ] )
Fails unless $instance is an instance of $class.
=head1 FIXTURE RELATED METHOD
=head2 lire_default_config_spec()
Returns a Lire::Config::ConfigSpec object initialized from the
default Lire configuration specification.
=head2 set_up_tz( $tz )
When you tests uses timelocal() or localtime(), you should
use that method to specify the TZ under which it should run.
The TZ will be reset to its previous value during tear_down().
=head2 set_up_plugin_mgr()
This will make the global PluginManager an instance that will only
live for the current test. The old PluginManager will be reinstated
during tear_down(). This way, you do not need to track calls to
register_plugin().
=head1 SEE ALSO
Test::Unit::TestCase(3pm)
=head1 VERSION
$Id: TestCase.pm,v 1.17 2006/07/23 13:16:32 vanbaal Exp $
=head1 AUTHORS
Francis J. Lacoste <flacoste@logreport.org>
=head1 COPYRIGHT
Copyright (C) 2003, 2004 Stichting LogReport Foundation LogReport@LogReport.org
This file is part of Lire.
Lire is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program (see COPYING); if not, check with
http://www.gnu.org/copyleft/gpl.html.
=cut
sub set_up {
my $self = $_[0];
$self->{'_saved_cfg'} = $Lire::Config::SINGLETON;
$self->{'cfg'} = new Lire::Test::HashConfig( {} );
$Lire::Config::SINGLETON = $self->{'cfg'};
$self->{'_old_lc_messages'} = setlocale( LC_MESSAGES );
setlocale( LC_MESSAGES, 'C' );
return $self;
}
sub tear_down {
my $self = $_[0];
$Lire::Config::SINGLETON = $self->{'_saved_cfg'};
setlocale( LC_MESSAGES, $self->{'_old_lc_messages'} );
$self->_tear_down_tz();
$self->_tear_down_plugin_mgr();
return $self;
}
sub set_up_tz {
my ( $self, $tz ) = @_;
check_param( $tz, 'tz' );
$self->{'_old_tz'} = $ENV{'TZ'}
unless defined $self->{'_old_tz'};
$ENV{'TZ'} = $tz;
tzset();
return;
}
sub _tear_down_tz {
my $self = $_[0];
return unless defined $self->{'_old_tz'};
$ENV{'TZ'} = $self->{'_old_tz'} || '';
tzset();
$self->{'_old_tz'} = undef;
return;
}
sub set_up_plugin_mgr {
my $self = $_[0];
unless ( defined $self->{'_old_plugin_mgr'} ) {
$self->{'_old_plugin_mgr'} = $Lire::PluginManager::instance;
$Lire::PluginManager::instance = new Lire::PluginManager();
}
return;
}
sub _tear_down_plugin_mgr {
my $self = $_[0];
if ( defined $self->{'_old_plugin_mgr'} ) {
$Lire::PluginManager::instance = $self->{'_old_plugin_mgr'};
$self->{'_old_plugin_mgr'} = undef;
}
return;
}
my $default_spec = undef;
sub lire_default_config_spec {
my $self = $_[0];
return $default_spec if defined $default_spec;
my $dir = realpath( dirname( __FILE__ ) . '/../../config-spec' );
my $parser = new Lire::Config::SpecParser();
$parser->merge_specification( $dir . '/lire.xml' );
return $default_spec = $parser->configspec();
}
sub assert_isa {
my ( $self, $class, $instance, $msg ) = @_;
$self->_error( "missing 'class' parameter" )
unless defined $class;
$self->_error( "missing 'instance' parameter" )
unless @_ > 2;
$self->_fail( defined $msg
? $msg
: "expected a '$class' instance, got undef" )
unless defined $instance;
$self->_fail( defined $msg
? $msg
: "expected a '$class' instance, got unblessed '$instance'" )
unless ref $instance;
$self->_fail( defined $msg
? $msg
: ( "expected a '$class' instance, got a '"
. ref( $instance ) . "' instance" ) )
unless UNIVERSAL::isa( $instance, $class );
}
sub assert_dies {
my ( $self, $regex, $test, $msg ) = @_;
$self->assert_died( $test, $regex, $msg );
}
sub assert_died {
my ( $self, $test, $regex, $msg ) = @_;
$self->_error( "missing 'test' parameter" )
unless defined $test;
$self->_error( "missing 'regex' parameter" )
unless defined $regex;
$self->_error( "'test' parameter should be a CODE reference" )
unless ref $test eq 'CODE';
$self->_error( "'regex' parameter should be a Regexp reference" )
unless ref $regex eq 'Regexp';
eval { $test->() };
$self->_fail( defined $msg ? $msg : "expected test to die" )
unless $@;
$self->_fail( defined $msg
? $msg
: "expected die message to match $regex, got '$@'" )
unless $@ =~ /$regex/;
}
sub _caller {
my $self = $_[0];
my $level = 2;
my ( $asserter, $file, $line ) = caller( $level );
while ( $file eq __FILE__ ) {
$level++;
( $asserter, $file, $line ) = caller( $level );
}
return ( $asserter, $file, $line );
}
sub _fail {
my $self = shift;
my ( $asserter, $file, $line ) = $self->_caller();
my $message = join '', @_;
Test::Unit::Failure->throw( '-text' => $message,
'-object' => $self,
'-file' => $file,
'-line' => $line );
}
sub _error {
my $self = shift;
my( $asserter, $file, $line ) = $self->_caller();
my $message = join '', @_;
Test::Unit::Error->throw( '-text' => $message,
'-object' => $self,
'-file' => $file,
'-line' => $line );
}
1;
|