1 # -*-Perl-*- Test Harness script for Bioperl
9 test_begin(-tests => 154);
10 use_ok 'Bio::Root::IO';
14 ok my $obj = Bio::Root::IO->new();
15 isa_ok $obj, 'Bio::Root::IO';
18 #############################################
19 # tests for exceptions/debugging/verbosity
20 #############################################
22 throws_ok { $obj->throw('Testing throw') } qr/Testing throw/, 'Throw';
25 throws_ok { $obj->throw('Testing throw') } qr/Testing throw/;
27 eval { $obj->warn('Testing warn') };
31 throws_ok { $obj->throw('Testing throw') } qr/Testing throw/;
33 ok my @stack = $obj->stack_trace(), 'Stack trace';
36 ok my $verbobj = Bio::Root::IO->new( -verbose => 1, -strict => 1 ), 'Verbosity';
37 is $verbobj->verbose(), 1;
42 #############################################
43 # tests for finding executables
44 #############################################
46 ok my $io = Bio::Root::IO->new();
49 my $out_file = 'test_file.txt';
51 open $out_fh, '>', $out_file or die "Could not write file '$out_file': $!\n";
54 # -X test file will fail in Windows regardless of chmod,
55 # because it looks for the executable suffix (like ".exe")
56 if ($^O =~ m/mswin/i) {
58 my $exec_file = 'test_exec.exe';
59 open my $exe_fh, '>', $exec_file or die "Could not write file '$exec_file': $!\n";
61 ok $obj->exists_exe($exec_file), 'executable file';
62 unlink $exec_file or die "Could not delete file '$exec_file': $!\n";
64 # A not executable file
65 ok (! $obj->exists_exe($out_file), 'non-executable file');
66 unlink $out_file or die "Could not delete file '$out_file': $!\n";
70 chmod 0777, $out_file or die "Could not change permission of file '$out_file': $!\n";
71 ok $obj->exists_exe($out_file), 'executable file';
73 # A not executable file
74 chmod 0444, $out_file or die "Could not change permission of file '$out_file': $!\n";
75 ok (! $obj->exists_exe($out_file), 'non-executable file');
76 unlink $out_file or die "Could not delete file '$out_file': $!\n";
80 my $out_dir = 'test_dir';
81 mkdir $out_dir or die "Could not write dir '$out_dir': $!\n";
82 chmod 0777, $out_dir or die "Could not change permission of dir '$out_dir': $!\n";
83 ok (! $obj->exists_exe($out_dir), 'executable dir');
84 rmdir $out_dir or die "Could not delete dir '$out_dir': $!\n";
87 #############################################
88 # tests for handle read and write abilities
89 #############################################
93 ok my $in_file = Bio::Root::IO->catfile(qw(t data test.waba));
94 is $in_file, test_input_file('test.waba');
96 ok my $in_file_2 = Bio::Root::IO->catfile(qw(t data test.txt));
98 $out_file = test_output_file();
103 ok my $rio = Bio::Root::IO->new( -input => $in_file ), 'Read from file';
104 is $rio->file, $in_file;
105 is_deeply [$rio->cleanfile], [undef, $in_file];
109 ok $rio = Bio::Root::IO->new( -file => '<'.$in_file );
110 is $rio->file, '<'.$in_file;
111 is_deeply [$rio->cleanfile], ['<', $in_file];
112 1 while $rio->_readline; # read entire file content
116 ok my $wio = Bio::Root::IO->new( -file => ">$out_file" ), 'Write to file';
117 is $wio->file, ">$out_file";
118 is_deeply [$wio->cleanfile], ['>', $out_file];
122 ok $rio = Bio::Root::IO->new( -file => "+>$out_file" ), 'Read+write to file';
123 is $rio->file, "+>$out_file";
124 is_deeply [$rio->cleanfile], ['+>', $out_file];
132 open $in_fh , '<', $in_file or die "Could not read file '$in_file': $!\n", 'Read from GLOB handle';
133 ok $rio = Bio::Root::IO->new( -fh => $in_fh );
134 is $rio->_fh, $in_fh;
138 open $out_fh, '>', $out_file or die "Could not write file '$out_file': $!\n", 'Write to GLOB handle';
139 ok $wio = Bio::Root::IO->new( -fh => $out_fh );
140 is $wio->_fh, $out_fh;
145 eval { require File::Temp; }
146 or skip 'could not create File::Temp object, maybe your File::Temp is 10 years old', 4;
148 $out_fh = File::Temp->new;
149 ok $wio = Bio::Root::IO->new( -fh => $out_fh ), 'Read from File::Temp handle';
150 isa_ok $wio, 'Bio::Root::IO';
151 is $wio->mode, 'rw', 'is a write handle';
152 warnings_like sub { $wio->close }, '', 'no warnings in ->close()';
157 # Exclusive arguments
158 open $in_fh , '<', $in_file or die "Could not read file '$in_file': $!\n", 'Read from GLOB handle';
159 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';
160 throws_ok {$rio = Bio::Root::IO->new( -input => $in_file, -file => $in_file_2 )} qr/Input file given twice/;
161 throws_ok {$rio = Bio::Root::IO->new( -input => $in_file, -string => 'abcedf' )} qr/File or filehandle provided with -string/;
162 throws_ok {$rio = Bio::Root::IO->new( -fh => $in_fh , -file => $in_file )} qr/Providing both a file and a filehandle for reading/;
163 throws_ok {$rio = Bio::Root::IO->new( -fh => $in_fh , -string => 'abcedf' )} qr/File or filehandle provided with -string/;
164 throws_ok {$rio = Bio::Root::IO->new( -file => $in_file, -string => 'abcedf' )} qr/File or filehandle provided with -string/;
167 lives_ok {$rio = Bio::Root::IO->new( -input => $in_file, -file => $in_file )} 'Same file';
170 ##############################################
171 # tests _pushback for multi-line buffering
172 ##############################################
174 ok $rio = Bio::Root::IO->new( -file => $in_file ), 'Pushback';
176 ok my $line1 = $rio->_readline;
177 ok my $line2 = $rio->_readline;
179 ok $rio->_pushback($line2);
180 ok $rio->_pushback($line1);
182 ok my $line3 = $rio->_readline;
183 ok my $line4 = $rio->_readline;
184 ok my $line5 = $rio->_readline;
193 ##############################################
194 # test _print and _insert
195 ##############################################
197 ok my $fio = Bio::Root::IO->new( -file => ">$out_file" );
198 ok $fio->_print("line 1\n"), '_print';
199 ok $fio->_print("line 2\n");
200 ok $fio->_insert("insertion at line 2\n",2), '_insert at middle of file';
201 ok $fio->_print("line 3\n");
202 ok $fio->_print("line 4\n");
205 open my $checkio, '<', $out_file or die "Could not read file '$out_file': $!\n";
206 my @content = <$checkio>;
208 is_deeply \@content, ["line 1\n","insertion at line 2\n","line 2\n","line 3\n","line 4\n"];
210 ok $fio = Bio::Root::IO->new(-file=>">$out_file");
211 ok $fio->_insert("insertion at line 1\n",1), '_insert in empty file';
214 open $checkio, '<', $out_file or die "Could not read file '$out_file': $!\n";
215 @content = <$checkio>;
217 is_deeply \@content, ["insertion at line 1\n"];
220 ##############################################
221 # test Win vs UNIX line ending
222 ##############################################
225 ok my $unix_rio = Bio::Root::IO->new(-file => test_input_file('U71225.gb.unix'));
226 ok my $win_rio = Bio::Root::IO->new(-file => test_input_file('U71225.gb.win' ));
227 ok my $mac_rio = Bio::Root::IO->new(-file => test_input_file('U71225.gb.mac' ));
229 my $expected = "LOCUS U71225 1164 bp DNA linear VRT 27-NOV-2001\n";
230 is $unix_rio->_readline, $expected;
231 is $win_rio->_readline , $expected;
232 like $mac_rio->_readline, qr#^LOCUS.*//\n$#ms;
233 # line spans entire file because lines end with "\r" but $/ is "\n"
235 $expected = "DEFINITION Desmognathus quadramaculatus 12S ribosomal RNA gene, partial\n";
236 is $unix_rio->_readline, $expected;
237 is $win_rio->_readline , $expected;
238 is $mac_rio->_readline , undef;
240 $expected = " sequence; tRNA-Val gene, complete sequence; and 16S ribosomal RNA\n";
241 is $unix_rio->_readline, $expected;
242 is $win_rio->_readline , $expected;
243 is $mac_rio->_readline , undef;
245 $expected = " gene, partial sequence, mitochondrial genes for mitochondrial RNAs.\n";
246 is $unix_rio->_readline, $expected;
247 is $win_rio->_readline , $expected;
248 is $mac_rio->_readline , undef;
250 $expected = "ACCESSION U71225\n";
251 is $unix_rio->_readline, $expected;
252 is $win_rio->_readline , $expected;
253 is $mac_rio->_readline , undef;
255 # In Windows the "-raw" parameter has no effect, because Perl already discards
256 # the '\r' from the line when reading in text mode from the filehandle
257 # ($line = <$fh>), and put it back automatically when printing
258 if ($^O =~ m/mswin/i) {
259 is $win_rio->_readline( -raw => 1) , "VERSION U71225.1 GI:2804359\n";
262 is $win_rio->_readline( -raw => 1) , "VERSION U71225.1 GI:2804359\r\n";
264 is $win_rio->_readline( -raw => 0) , "KEYWORDS .\n";
268 ##############################################
269 # test Win vs UNIX line ending using PerlIO::eol
270 ##############################################
273 test_skip(-tests => 20, -requires_module => 'PerlIO::eol');
275 local $Bio::Root::IO::HAS_EOL = 1;
276 ok my $unix_rio = Bio::Root::IO->new(-file => test_input_file('U71225.gb.unix'));
277 ok my $win_rio = Bio::Root::IO->new(-file => test_input_file('U71225.gb.win' ));
278 ok my $mac_rio = Bio::Root::IO->new(-file => test_input_file('U71225.gb.mac' ));
280 my $expected = "LOCUS U71225 1164 bp DNA linear VRT 27-NOV-2001\n";
281 is $unix_rio->_readline, $expected;
282 is $win_rio->_readline , $expected;
283 is $mac_rio->_readline , $expected;
285 $expected = "DEFINITION Desmognathus quadramaculatus 12S ribosomal RNA gene, partial\n";
286 is $unix_rio->_readline, $expected;
288 local $TODO = "Sporadic test failures when running using PerlIO::eol on Linux w/".
289 "Windows line endings: #";
290 is $win_rio->_readline , $expected;
292 is $mac_rio->_readline , $expected;
294 $expected = " sequence; tRNA-Val gene, complete sequence; and 16S ribosomal RNA\n";
295 is $unix_rio->_readline, $expected;
297 local $TODO = "Sporadic test failures when running using PerlIO::eol on Linux w/".
298 "Windows line endings: #";
299 is $win_rio->_readline , $expected;
301 is $mac_rio->_readline , $expected;
303 $expected = " gene, partial sequence, mitochondrial genes for mitochondrial RNAs.\n";
304 is $unix_rio->_readline, $expected;
306 local $TODO = "Sporadic test failures when running using PerlIO::eol on Linux w/".
307 "Windows line endings: #";
308 is $win_rio->_readline , $expected;
310 is $mac_rio->_readline , $expected;
312 $expected = "ACCESSION U71225\n";
313 is $unix_rio->_readline, $expected;
315 local $TODO = "Sporadic test failures when running using PerlIO::eol on Linux w/".
316 "Windows line endings: #";
317 is $win_rio->_readline , $expected;
319 is $mac_rio->_readline , $expected;
321 # $HAS_EOL ignores -raw
322 is $win_rio->_readline( -raw => 1) , "VERSION U71225.1 GI:2804359\n";
323 is $win_rio->_readline( -raw => 0) , "KEYWORDS .\n";
327 ##############################################
328 # test Path::Class support
329 ##############################################
332 test_skip(-tests => 2, -requires_module => 'Path::Class');
333 my $f = sub { Bio::Root::IO->new( -file => Path::Class::file(test_input_file('U71225.gb.unix') ) ) };
334 lives_ok(sub { $f->() } , 'Bio::Root::IO->new can handle a Path::Class object');
335 isa_ok($f->(), 'Bio::Root::IO');
339 ##############################################
341 ##############################################
343 my $teststring = "Foo\nBar\nBaz";
344 ok $rio = Bio::Root::IO->new(-string => $teststring), 'Read string';
348 ok $line1 = $rio->_readline;
351 ok $line2 = $rio->_readline;
353 ok $rio->_pushback($line2);
355 ok $line3 = $rio->_readline;
357 ok $line3 = $rio->_readline;
361 ##############################################
363 ##############################################
365 ok my $obj = Bio::Root::IO->new(-verbose => 0);
367 isa_ok $obj, 'Bio::Root::IO';
369 my $TEST_STRING = "Bioperl rocks!\n";
374 ($tfh, $tfile) = $obj->tempfile();
376 print $tfh $TEST_STRING;
378 open my $IN, '<', $tfile or die "Could not read file '$tfile': $!\n";
379 my $val = join '', <$IN>;
380 is $val, $TEST_STRING;
389 ok ! -e $tfile, 'auto UNLINK => 1';
392 $obj = Bio::Root::IO->new();
395 my $tdir = $obj->tempdir(CLEANUP=>1);
397 ($tfh, $tfile) = $obj->tempfile(dir => $tdir);
400 undef $obj; # see Bio::Root::IO::_io_cleanup
406 ok ! -e $tfile, 'tempfile deleted';
410 $obj = Bio::Root::IO->new(-verbose => 0);
411 ($tfh, $tfile) = $obj->tempfile(UNLINK => 0);
415 undef $obj; # see Bio::Root::IO::_io_cleanup
421 ok -e $tfile, 'UNLINK => 0';
424 ok unlink( $tfile) == 1 ;
427 ok $obj = Bio::Root::IO->new;
429 # check suffix is applied
430 my ($fh1, $fn1) = $obj->tempfile(SUFFIX => '.bioperl');
432 like $fn1, qr/\.bioperl$/, 'tempfile suffix';
435 # check single return value mode of File::Temp
436 my $fh2 = $obj->tempfile;
438 ok $fh2, 'tempfile() in scalar context';