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 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
|
# -*-Perl-*- Test Harness script for Bioperl
use strict;
use warnings;
BEGIN {
use Bio::Root::Test;
test_begin(-tests => 154);
use_ok 'Bio::Root::IO';
}
ok my $obj = Bio::Root::IO->new();
isa_ok $obj, 'Bio::Root::IO';
#############################################
# tests for exceptions/debugging/verbosity
#############################################
throws_ok { $obj->throw('Testing throw') } qr/Testing throw/, 'Throw';
$obj->verbose(-1);
throws_ok { $obj->throw('Testing throw') } qr/Testing throw/;
eval { $obj->warn('Testing warn') };
ok !$@, 'Warn';
$obj->verbose(1);
throws_ok { $obj->throw('Testing throw') } qr/Testing throw/;
ok my @stack = $obj->stack_trace(), 'Stack trace';
is scalar @stack, 2;
ok my $verbobj = Bio::Root::IO->new( -verbose => 1, -strict => 1 ), 'Verbosity';
is $verbobj->verbose(), 1;
ok $obj->verbose(-1);
#############################################
# tests for finding executables
#############################################
ok my $io = Bio::Root::IO->new();
# An executable file
my $out_file = 'test_file.txt';
my $out_fh;
open $out_fh, '>', $out_file or die "Could not write file '$out_file': $!\n";
print $out_fh 'test';
close $out_fh;
# -X test file will fail in Windows regardless of chmod,
# because it looks for the executable suffix (like ".exe")
if ($^O =~ m/mswin/i) {
# An executable file
my $exec_file = 'test_exec.exe';
open my $exe_fh, '>', $exec_file or die "Could not write file '$exec_file': $!\n";
close $exe_fh;
ok $obj->exists_exe($exec_file), 'executable file';
unlink $exec_file or die "Could not delete file '$exec_file': $!\n";
# A not executable file
ok (! $obj->exists_exe($out_file), 'non-executable file');
unlink $out_file or die "Could not delete file '$out_file': $!\n";
}
else {
# An executable file
chmod 0777, $out_file or die "Could not change permission of file '$out_file': $!\n";
ok $obj->exists_exe($out_file), 'executable file';
# A not executable file
chmod 0444, $out_file or die "Could not change permission of file '$out_file': $!\n";
ok (! $obj->exists_exe($out_file), 'non-executable file');
unlink $out_file or die "Could not delete file '$out_file': $!\n";
}
# An executable dir
my $out_dir = 'test_dir';
mkdir $out_dir or die "Could not write dir '$out_dir': $!\n";
chmod 0777, $out_dir or die "Could not change permission of dir '$out_dir': $!\n";
ok (! $obj->exists_exe($out_dir), 'executable dir');
rmdir $out_dir or die "Could not delete dir '$out_dir': $!\n";
#############################################
# tests for handle read and write abilities
#############################################
# Test catfile
ok my $in_file = Bio::Root::IO->catfile(qw(t data test.waba));
is $in_file, test_input_file('test.waba');
ok my $in_file_2 = Bio::Root::IO->catfile(qw(t data test.txt));
$out_file = test_output_file();
# Test with files
ok my $rio = Bio::Root::IO->new( -input => $in_file ), 'Read from file';
is $rio->file, $in_file;
is_deeply [$rio->cleanfile], [undef, $in_file];
is $rio->mode, 'r';
ok $rio->close;
ok $rio = Bio::Root::IO->new( -file => '<'.$in_file );
is $rio->file, '<'.$in_file;
is_deeply [$rio->cleanfile], ['<', $in_file];
1 while $rio->_readline; # read entire file content
is $rio->mode, 'r';
ok $rio->close;
ok my $wio = Bio::Root::IO->new( -file => ">$out_file" ), 'Write to file';
is $wio->file, ">$out_file";
is_deeply [$wio->cleanfile], ['>', $out_file];
is $wio->mode, 'w';
ok $wio->close;
ok $rio = Bio::Root::IO->new( -file => "+>$out_file" ), 'Read+write to file';
is $rio->file, "+>$out_file";
is_deeply [$rio->cleanfile], ['+>', $out_file];
is $rio->mode, 'rw';
ok $rio->close;
# Test with handles
my $in_fh;
open $in_fh , '<', $in_file or die "Could not read file '$in_file': $!\n", 'Read from GLOB handle';
ok $rio = Bio::Root::IO->new( -fh => $in_fh );
is $rio->_fh, $in_fh;
is $rio->mode, 'r';
close $in_fh;
open $out_fh, '>', $out_file or die "Could not write file '$out_file': $!\n", 'Write to GLOB handle';
ok $wio = Bio::Root::IO->new( -fh => $out_fh );
is $wio->_fh, $out_fh;
is $wio->mode, 'w';
close $out_fh;
SKIP: {
eval { require File::Temp; }
or skip 'could not create File::Temp object, maybe your File::Temp is 10 years old', 4;
$out_fh = File::Temp->new;
ok $wio = Bio::Root::IO->new( -fh => $out_fh ), 'Read from File::Temp handle';
isa_ok $wio, 'Bio::Root::IO';
is $wio->mode, 'rw', 'is a write handle';
warnings_like sub { $wio->close }, '', 'no warnings in ->close()';
ok $wio->close;
}
# Exclusive arguments
open $in_fh , '<', $in_file or die "Could not read file '$in_file': $!\n", 'Read from GLOB handle';
throws_ok {$rio = Bio::Root::IO->new( -input => $in_file, -fh => $in_fh )} qr/Providing both a file and a filehandle for reading/, 'Exclusive arguments';
throws_ok {$rio = Bio::Root::IO->new( -input => $in_file, -file => $in_file_2 )} qr/Input file given twice/;
throws_ok {$rio = Bio::Root::IO->new( -input => $in_file, -string => 'abcedf' )} qr/File or filehandle provided with -string/;
throws_ok {$rio = Bio::Root::IO->new( -fh => $in_fh , -file => $in_file )} qr/Providing both a file and a filehandle for reading/;
throws_ok {$rio = Bio::Root::IO->new( -fh => $in_fh , -string => 'abcedf' )} qr/File or filehandle provided with -string/;
throws_ok {$rio = Bio::Root::IO->new( -file => $in_file, -string => 'abcedf' )} qr/File or filehandle provided with -string/;
close $in_fh;
lives_ok {$rio = Bio::Root::IO->new( -input => $in_file, -file => $in_file )} 'Same file';
##############################################
# tests _pushback for multi-line buffering
##############################################
ok $rio = Bio::Root::IO->new( -file => $in_file ), 'Pushback';
ok my $line1 = $rio->_readline;
ok my $line2 = $rio->_readline;
ok $rio->_pushback($line2);
ok $rio->_pushback($line1);
ok my $line3 = $rio->_readline;
ok my $line4 = $rio->_readline;
ok my $line5 = $rio->_readline;
is $line1, $line3;
is $line2, $line4;
isnt $line5, $line4;
ok $rio->close;
##############################################
# test _print and _insert
##############################################
ok my $fio = Bio::Root::IO->new( -file => ">$out_file" );
ok $fio->_print("line 1\n"), '_print';
ok $fio->_print("line 2\n");
ok $fio->_insert("insertion at line 2\n",2), '_insert at middle of file';
ok $fio->_print("line 3\n");
ok $fio->_print("line 4\n");
ok $fio->close;
open my $checkio, '<', $out_file or die "Could not read file '$out_file': $!\n";
my @content = <$checkio>;
close $checkio;
is_deeply \@content, ["line 1\n","insertion at line 2\n","line 2\n","line 3\n","line 4\n"];
ok $fio = Bio::Root::IO->new(-file=>">$out_file");
ok $fio->_insert("insertion at line 1\n",1), '_insert in empty file';
ok $fio->close;
open $checkio, '<', $out_file or die "Could not read file '$out_file': $!\n";
@content = <$checkio>;
close $checkio;
is_deeply \@content, ["insertion at line 1\n"];
##############################################
# test Win vs UNIX line ending
##############################################
{
ok my $unix_rio = Bio::Root::IO->new(-file => test_input_file('U71225.gb.unix'));
ok my $win_rio = Bio::Root::IO->new(-file => test_input_file('U71225.gb.win' ));
ok my $mac_rio = Bio::Root::IO->new(-file => test_input_file('U71225.gb.mac' ));
my $expected = "LOCUS U71225 1164 bp DNA linear VRT 27-NOV-2001\n";
is $unix_rio->_readline, $expected;
is $win_rio->_readline , $expected;
like $mac_rio->_readline, qr#^LOCUS.*//\n$#ms;
# line spans entire file because lines end with "\r" but $/ is "\n"
$expected = "DEFINITION Desmognathus quadramaculatus 12S ribosomal RNA gene, partial\n";
is $unix_rio->_readline, $expected;
is $win_rio->_readline , $expected;
is $mac_rio->_readline , undef;
$expected = " sequence; tRNA-Val gene, complete sequence; and 16S ribosomal RNA\n";
is $unix_rio->_readline, $expected;
is $win_rio->_readline , $expected;
is $mac_rio->_readline , undef;
$expected = " gene, partial sequence, mitochondrial genes for mitochondrial RNAs.\n";
is $unix_rio->_readline, $expected;
is $win_rio->_readline , $expected;
is $mac_rio->_readline , undef;
$expected = "ACCESSION U71225\n";
is $unix_rio->_readline, $expected;
is $win_rio->_readline , $expected;
is $mac_rio->_readline , undef;
# In Windows the "-raw" parameter has no effect, because Perl already discards
# the '\r' from the line when reading in text mode from the filehandle
# ($line = <$fh>), and put it back automatically when printing
if ($^O =~ m/mswin/i) {
is $win_rio->_readline( -raw => 1) , "VERSION U71225.1 GI:2804359\n";
}
else {
is $win_rio->_readline( -raw => 1) , "VERSION U71225.1 GI:2804359\r\n";
}
is $win_rio->_readline( -raw => 0) , "KEYWORDS .\n";
}
##############################################
# test Win vs UNIX line ending using PerlIO::eol
##############################################
SKIP: {
test_skip(-tests => 20, -requires_module => 'PerlIO::eol');
local $Bio::Root::IO::HAS_EOL = 1;
ok my $unix_rio = Bio::Root::IO->new(-file => test_input_file('U71225.gb.unix'));
ok my $win_rio = Bio::Root::IO->new(-file => test_input_file('U71225.gb.win' ));
ok my $mac_rio = Bio::Root::IO->new(-file => test_input_file('U71225.gb.mac' ));
my $expected = "LOCUS U71225 1164 bp DNA linear VRT 27-NOV-2001\n";
is $unix_rio->_readline, $expected;
is $win_rio->_readline , $expected;
is $mac_rio->_readline , $expected;
$expected = "DEFINITION Desmognathus quadramaculatus 12S ribosomal RNA gene, partial\n";
is $unix_rio->_readline, $expected;
TODO: {
local $TODO = "Sporadic test failures when running using PerlIO::eol on Linux w/".
"Windows line endings: #";
is $win_rio->_readline , $expected;
};
is $mac_rio->_readline , $expected;
$expected = " sequence; tRNA-Val gene, complete sequence; and 16S ribosomal RNA\n";
is $unix_rio->_readline, $expected;
TODO: {
local $TODO = "Sporadic test failures when running using PerlIO::eol on Linux w/".
"Windows line endings: #";
is $win_rio->_readline , $expected;
};
is $mac_rio->_readline , $expected;
$expected = " gene, partial sequence, mitochondrial genes for mitochondrial RNAs.\n";
is $unix_rio->_readline, $expected;
TODO: {
local $TODO = "Sporadic test failures when running using PerlIO::eol on Linux w/".
"Windows line endings: #";
is $win_rio->_readline , $expected;
};
is $mac_rio->_readline , $expected;
$expected = "ACCESSION U71225\n";
is $unix_rio->_readline, $expected;
TODO: {
local $TODO = "Sporadic test failures when running using PerlIO::eol on Linux w/".
"Windows line endings: #";
is $win_rio->_readline , $expected;
};
is $mac_rio->_readline , $expected;
# $HAS_EOL ignores -raw
is $win_rio->_readline( -raw => 1) , "VERSION U71225.1 GI:2804359\n";
is $win_rio->_readline( -raw => 0) , "KEYWORDS .\n";
}
##############################################
# test Path::Class support
##############################################
SKIP: {
test_skip(-tests => 2, -requires_module => 'Path::Class');
my $f = sub { Bio::Root::IO->new( -file => Path::Class::file(test_input_file('U71225.gb.unix') ) ) };
lives_ok(sub { $f->() } , 'Bio::Root::IO->new can handle a Path::Class object');
isa_ok($f->(), 'Bio::Root::IO');
}
##############################################
# test -string
##############################################
my $teststring = "Foo\nBar\nBaz";
ok $rio = Bio::Root::IO->new(-string => $teststring), 'Read string';
is $rio->mode, 'r';
ok $line1 = $rio->_readline;
is $line1, "Foo\n";
ok $line2 = $rio->_readline;
is $line2, "Bar\n";
ok $rio->_pushback($line2);
ok $line3 = $rio->_readline;
is $line3, "Bar\n";
ok $line3 = $rio->_readline;
is $line3, 'Baz';
##############################################
# test tempfile()
##############################################
{
ok my $obj = Bio::Root::IO->new(-verbose => 0);
isa_ok $obj, 'Bio::Root::IO';
my $TEST_STRING = "Bioperl rocks!\n";
my ($tfh,$tfile);
eval {
($tfh, $tfile) = $obj->tempfile();
isa_ok $tfh, 'GLOB';
print $tfh $TEST_STRING;
close $tfh;
open my $IN, '<', $tfile or die "Could not read file '$tfile': $!\n";
my $val = join '', <$IN>;
is $val, $TEST_STRING;
close $IN;
ok -e $tfile;
undef $obj;
};
undef $obj;
if ( $@ ) {
ok 0;
} else {
ok ! -e $tfile, 'auto UNLINK => 1';
}
$obj = Bio::Root::IO->new();
eval {
my $tdir = $obj->tempdir(CLEANUP=>1);
ok -d $tdir;
($tfh, $tfile) = $obj->tempfile(dir => $tdir);
close $tfh;
ok -e $tfile;
undef $obj; # see Bio::Root::IO::_io_cleanup
};
if ( $@ ) {
ok 0;
} else {
ok ! -e $tfile, 'tempfile deleted';
}
eval {
$obj = Bio::Root::IO->new(-verbose => 0);
($tfh, $tfile) = $obj->tempfile(UNLINK => 0);
isa_ok $tfh, 'GLOB';
close $tfh;
ok -e $tfile;
undef $obj; # see Bio::Root::IO::_io_cleanup
};
if ( $@ ) {
ok 0;
} else {
ok -e $tfile, 'UNLINK => 0';
}
ok unlink( $tfile) == 1 ;
ok $obj = Bio::Root::IO->new;
# check suffix is applied
my ($fh1, $fn1) = $obj->tempfile(SUFFIX => '.bioperl');
isa_ok $fh1, 'GLOB';
like $fn1, qr/\.bioperl$/, 'tempfile suffix';
ok close $fh1;
# check single return value mode of File::Temp
my $fh2 = $obj->tempfile;
isa_ok $fh2, 'GLOB';
ok $fh2, 'tempfile() in scalar context';
ok close $fh2;
}
|