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;
287 is $win_rio->_readline , $expected;
288 is $mac_rio->_readline , $expected;
290 $expected = " sequence; tRNA-Val gene, complete sequence; and 16S ribosomal RNA\n";
291 is $unix_rio->_readline, $expected;
292 is $win_rio->_readline , $expected;
293 is $mac_rio->_readline , $expected;
295 $expected = " gene, partial sequence, mitochondrial genes for mitochondrial RNAs.\n";
296 is $unix_rio->_readline, $expected;
297 is $win_rio->_readline , $expected;
298 is $mac_rio->_readline , $expected;
300 $expected = "ACCESSION U71225\n";
301 is $unix_rio->_readline, $expected;
302 is $win_rio->_readline , $expected;
303 is $mac_rio->_readline , $expected;
305 # $HAS_EOL ignores -raw
306 is $win_rio->_readline( -raw => 1) , "VERSION U71225.1 GI:2804359\n";
307 is $win_rio->_readline( -raw => 0) , "KEYWORDS .\n";
311 ##############################################
312 # test Path::Class support
313 ##############################################
316 test_skip(-tests => 2, -requires_module => 'Path::Class');
317 my $f = sub { Bio::Root::IO->new( -file => Path::Class::file(test_input_file('U71225.gb.unix') ) ) };
318 lives_ok(sub { $f->() } , 'Bio::Root::IO->new can handle a Path::Class object');
319 isa_ok($f->(), 'Bio::Root::IO');
323 ##############################################
325 ##############################################
327 my $teststring = "Foo\nBar\nBaz";
328 ok $rio = Bio::Root::IO->new(-string => $teststring), 'Read string';
332 ok $line1 = $rio->_readline;
335 ok $line2 = $rio->_readline;
337 ok $rio->_pushback($line2);
339 ok $line3 = $rio->_readline;
341 ok $line3 = $rio->_readline;
345 ##############################################
347 ##############################################
349 ok my $obj = Bio::Root::IO->new(-verbose => 0);
351 isa_ok $obj, 'Bio::Root::IO';
353 my $TEST_STRING = "Bioperl rocks!\n";
358 ($tfh, $tfile) = $obj->tempfile();
360 print $tfh $TEST_STRING;
362 open my $IN, '<', $tfile or die "Could not read file '$tfile': $!\n";
363 my $val = join '', <$IN>;
364 is $val, $TEST_STRING;
373 ok ! -e $tfile, 'auto UNLINK => 1';
376 $obj = Bio::Root::IO->new();
379 my $tdir = $obj->tempdir(CLEANUP=>1);
381 ($tfh, $tfile) = $obj->tempfile(dir => $tdir);
384 undef $obj; # see Bio::Root::IO::_io_cleanup
390 ok ! -e $tfile, 'tempfile deleted';
394 $obj = Bio::Root::IO->new(-verbose => 0);
395 ($tfh, $tfile) = $obj->tempfile(UNLINK => 0);
399 undef $obj; # see Bio::Root::IO::_io_cleanup
405 ok -e $tfile, 'UNLINK => 0';
408 ok unlink( $tfile) == 1 ;
411 ok $obj = Bio::Root::IO->new;
413 # check suffix is applied
414 my ($fh1, $fn1) = $obj->tempfile(SUFFIX => '.bioperl');
416 like $fn1, qr/\.bioperl$/, 'tempfile suffix';
419 # check single return value mode of File::Temp
420 my $fh2 = $obj->tempfile;
422 ok $fh2, 'tempfile() in scalar context';