File Coverage

blib/lib/Bio/MUST/Core/Utils.pm
Criterion Covered Total %
statement 55 55 100.0
branch 7 8 87.5
condition 4 5 80.0
subroutine 14 14 100.0
pod 0 6 0.0
total 80 88 90.9


line stmt bran cond sub pod time code
1             package Bio::MUST::Core::Utils;
2             # ABSTRACT: Utility functions for enabling multiple file processing
3             $Bio::MUST::Core::Utils::VERSION = '0.212670';
4 18     18   182791 use strict;
  18         55  
  18         559  
5 18     18   94 use warnings;
  18         39  
  18         469  
6 18     18   94 use autodie;
  18         41  
  18         136  
7              
8 18     18   96868 use File::Basename;
  18         62  
  18         1476  
9 18     18   129 use Path::Class qw(file);
  18         56  
  18         877  
10 18     18   8817 use Test::Files;
  18         33381  
  18         1618  
11 18     18   172 use Test::Most;
  18         47  
  18         162  
12              
13             # TODO: use :filenames in some binaries
14              
15             use Exporter::Easy (
16 18         205 OK => [ qw(secure_outfile :filenames :tests) ],
17             TAGS => [
18             filenames => [ qw(insert_suffix change_suffix append_suffix) ],
19             tests => [ qw(cmp_store cmp_float) ],
20             ],
21 18     18   35941 );
  18         1305  
22              
23              
24             sub secure_outfile {
25 2     2 0 4144 my $infile = shift;
26 2         5 my $suffix = shift;
27              
28 2 100       10 return insert_suffix($infile, $suffix) if defined $suffix;
29              
30 1 50       4 rename $infile, append_suffix($infile, '.bak') if -e $infile;
31 1         1451 return $infile;
32             }
33              
34              
35             sub insert_suffix {
36 6     6 0 2738 my $infile = shift;
37 6         11 my $string = shift;
38              
39 6         176 my ($filename, $directories, $suffix) = fileparse($infile, qr{\.[^.]*}xms);
40 6         99 return $directories . $filename . $string . $suffix;
41             }
42              
43              
44             sub change_suffix {
45 5     5 0 9 my $infile = shift;
46 5         7 my $suffix = shift;
47              
48 5         67 my ($filename, $directories) = fileparse($infile, qr{\.[^.]*}xms);
49 5         20 return $directories . $filename . $suffix;
50             }
51              
52              
53             sub append_suffix {
54 6     6 0 116 my $infile = shift;
55 6         9 my $suffix = shift;
56              
57 6         46 my ($filename, $directories) = fileparse($infile);
58 6         79 return $directories . $filename . $suffix;
59             }
60              
61              
62             sub cmp_store {
63 84     84 0 4607 my %args = @_;
64             my ($obj, $method, $file, $test, $args)
65 84         451 = @args{ qw(obj method file test args) };
66              
67 84   100     550 $args //= {}; # optional hash reference
68              
69             # named output file
70 84         190 my $outfile;
71 84 100       392 unless ($method =~ m/\A temp_/xms) {
72 80         557 $outfile = file('test', "my_$file");
73 80 100       10067 ( file($outfile) )->remove if -e $outfile;
74 80         9171 $obj->$method($outfile, $args);
75             }
76              
77             # anonymous temporary file
78 84   66     677 $outfile //= $obj->$method($args);
79              
80             # compare file contents
81 84         775 compare_ok($outfile, file('test', $file), "$test: $file");
82              
83 84         6177098 return;
84             }
85              
86              
87             sub cmp_float {
88 84     84 0 827 my ($got, $expect, $epsilon, $test) = @_;
89              
90             # compare got and expect to epsilon precision
91 84         391 cmp_ok abs($got - $expect), '<', $epsilon, $test;
92              
93 84         27343 return;
94             }
95              
96             1;
97              
98             __END__
99              
100             =pod
101              
102             =head1 NAME
103              
104             Bio::MUST::Core::Utils - Utility functions for enabling multiple file processing
105              
106             =head1 VERSION
107              
108             version 0.212670
109              
110             =head1 SYNOPSIS
111              
112             # TODO
113              
114             =head1 DESCRIPTION
115              
116             # TODO
117              
118             =head1 AUTHOR
119              
120             Denis BAURAIN <denis.baurain@uliege.be>
121              
122             =head1 COPYRIGHT AND LICENSE
123              
124             This software is copyright (c) 2013 by University of Liege / Unit of Eukaryotic Phylogenomics / Denis BAURAIN.
125              
126             This is free software; you can redistribute it and/or modify it under
127             the same terms as the Perl 5 programming language system itself.
128              
129             =cut