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.212530';
4 18     18   224527 use strict;
  18         50  
  18         674  
5 18     18   106 use warnings;
  18         43  
  18         565  
6 18     18   107 use autodie;
  18         37  
  18         155  
7              
8 18     18   97362 use File::Basename;
  18         77  
  18         1590  
9 18     18   137 use Path::Class qw(file);
  18         44  
  18         922  
10 18     18   9455 use Test::Files;
  18         34314  
  18         1649  
11 18     18   169 use Test::Most;
  18         46  
  18         186  
12              
13             # TODO: use :filenames in some binaries
14              
15             use Exporter::Easy (
16 18         223 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   36361 );
  18         1697  
22              
23              
24             sub secure_outfile {
25 2     2 0 5340 my $infile = shift;
26 2         5 my $suffix = shift;
27              
28 2 100       11 return insert_suffix($infile, $suffix) if defined $suffix;
29              
30 1 50       5 rename $infile, append_suffix($infile, '.bak') if -e $infile;
31 1         2181 return $infile;
32             }
33              
34              
35             sub insert_suffix {
36 6     6 0 3745 my $infile = shift;
37 6         11 my $string = shift;
38              
39 6         218 my ($filename, $directories, $suffix) = fileparse($infile, qr{\.[^.]*}xms);
40 6         108 return $directories . $filename . $string . $suffix;
41             }
42              
43              
44             sub change_suffix {
45 5     5 0 9 my $infile = shift;
46 5         8 my $suffix = shift;
47              
48 5         84 my ($filename, $directories) = fileparse($infile, qr{\.[^.]*}xms);
49 5         23 return $directories . $filename . $suffix;
50             }
51              
52              
53             sub append_suffix {
54 6     6 0 114 my $infile = shift;
55 6         14 my $suffix = shift;
56              
57 6         53 my ($filename, $directories) = fileparse($infile);
58 6         108 return $directories . $filename . $suffix;
59             }
60              
61              
62             sub cmp_store {
63 84     84 0 5477 my %args = @_;
64             my ($obj, $method, $file, $test, $args)
65 84         523 = @args{ qw(obj method file test args) };
66              
67 84   100     626 $args //= {}; # optional hash reference
68              
69             # named output file
70 84         192 my $outfile;
71 84 100       405 unless ($method =~ m/\A temp_/xms) {
72 80         750 $outfile = file('test', "my_$file");
73 80 100       13177 ( file($outfile) )->remove if -e $outfile;
74 80         10862 $obj->$method($outfile, $args);
75             }
76              
77             # anonymous temporary file
78 84   66     806 $outfile //= $obj->$method($args);
79              
80             # compare file contents
81 84         967 compare_ok($outfile, file('test', $file), "$test: $file");
82              
83 84         5424199 return;
84             }
85              
86              
87             sub cmp_float {
88 84     84 0 989 my ($got, $expect, $epsilon, $test) = @_;
89              
90             # compare got and expect to epsilon precision
91 84         381 cmp_ok abs($got - $expect), '<', $epsilon, $test;
92              
93 84         29829 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.212530
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