Infrastructure/Utils.pm

filter: Statementstmt 36.0% 22 / 61 Branchbran 0.0% 0 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 45.4% 5 / 11 Podpod 100% 5 / 5 totaltotal 33.6% 32 / 95 CC 10 SCAR 23.7
CC
10
Cov
28%
_parse_var
30.0
convert_cmp_operator
6.0
convert_unit
6.0
CRAP 10.8
« Infrastructure/Config/Hash.pm ↑ Summary Infrastructure/Logfile.pm »
1 package Test::BDD::Infrastructure::Utils;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # ABSTRACT: class with collection of some utility functions
7 1 our $VERSION = '1.005'; # VERSION
8  
9 1 our (@ISA, @EXPORT_OK);
10  
11 1 ▶use Test::BDD::Infrastructure::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use Time::Seconds;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 require Exporter;
17  
18 1 @ISA = qw(Exporter);
19 1 @EXPORT_OK = qw( &convert_unit &convert_cmp_operator $CMP_OPERATOR_RE
20 convert_interval lookup_config lookup_config_node );
21 }
22  
23  
24 1 our $CMP_OPERATORS = {
25 'at least' => '>=',
26 'a maximum of' => '<=',
27 'not more than' => '<=',
28 'more than' => '>',
29 'bigger than' => '>',
30 'greater than' => '>',
31 'less than' => '<',
32 'smaller than' => '<',
33 'equal' => '==',
34 'exactly' => '==',
35 'newer than' => '<',
36 'older than' => '>',
37 'the string' => 'eq',
38 'the value' => 'eq',
39 'not the string' => 'ne',
40 'not the value' => 'ne',
41 };
42  
43 1 our $CMP_OPERATOR_RE = '('.join('|', keys %$CMP_OPERATORS ).')';
44  
45 0 ▶sub convert_cmp_operator {
Subroutine convert_cmp_operator: not called CC=2 SCAR=17.9
Pod documented
46 0 my ( $str ) = @_;
47 0 $str = lc $str;
48 0 ▶ if( ! defined $CMP_OPERATORS->{ $str } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $Test::BDD::Infrastructure::Utils::CMP_OPERATORS->{$str}) 0 0 0
49 0 die('unknown operator: '.$str);
50 }
51 0 return $CMP_OPERATORS->{ $str };
52 }
53  
54  
55 1 our $UNITS = {
56 '%' => 0.01,
57 percent => 0.01,
58  
59 byte => 1,
60 b => 1,
61  
62 kilobyte => 1000,
63 kb => 1000,
64 megabyte => 1000000,
65 mb => 1000000,
66 gigabyte => 1000000000,
67 gb => 1000000000,
68 terrabyte => 1000000000000,
69 tb => 1000000000000,
70  
71 kibibyte => 1024,
72 kib => 1024,
73 mebibyte => 1024*1024,
74 mib => 1024*1024,
75 gibibyte => 1024*1024*1024,
76 gib => 1024*1024*1024,
77 tebibyte => 1024*1024*1024*1024,
78 tib => 1024*1024*1024*1024,
79 };
80  
81 0 ▶sub convert_unit {
Subroutine convert_unit: not called CC=2 SCAR=17.9
Pod documented
82 0 my ( $size, $unit ) = @_;
83 0 $unit = lc $unit;
84 0 ▶ if( ! defined $UNITS->{$unit} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $Test::BDD::Infrastructure::Utils::UNITS->{$unit}) 0 0 0
85 0 die('unknown unit '.$unit);
86 }
87 0 return( $size * $UNITS->{$unit} );
88 }
89  
90  
91 1 our $INTERVALS = {
92 second => 1,
93 seconds => 1,
94 day => ONE_DAY,
95 days => ONE_DAY,
96 week => ONE_WEEK,
97 weeks => ONE_WEEK,
98 hour => ONE_HOUR,
99 hours => ONE_HOUR,
100 minute => ONE_MINUTE,
101 minutes => ONE_MINUTE,
102 month => ONE_MONTH,
103 months => ONE_MONTH,
104 year => ONE_YEAR,
105 years => ONE_YEAR,
106 };
107  
108 0 ▶sub convert_interval {
Subroutine convert_interval: not called CC=2 SCAR=17.9
Pod documented
109 0 my ( $count, $unit ) = @_;
110 0 $unit = lc $unit;
111 0 ▶ if( ! defined $INTERVALS->{$unit} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $Test::BDD::Infrastructure::Utils::INTERVALS->{$unit}) 0 0 0
112 0 die('unknown interval '.$unit);
113 }
114 0 return( $count * $INTERVALS->{$unit} );
115 }
116  
117 0 ▶sub _parse_var {
Subroutine _parse_var: not called CC=5 SCAR=34.0
118 0 my $str = shift;
119 0 ▶ if( $str =~ /^\\\$/ ) { # escaped \$
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^\\\$/) 0 0 0
120 0 $str =~ s/^\\\$/\$/;
121 0 return;
122 }
123 0 ▶ if( $str !~ /^\$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($str =~ /^\$/) 0 0 0
124 0 return;
125 }
126 0 $str =~ s/^\$//;
127 0 my ( $scope, $path );
128 0 ▶ if( ( $scope, $path ) = $str =~ /^([^:]*):(.*)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $str =~ /^([^:]*):(.*)$/) { } 0 0 0
129 0 ▶ if( $scope eq '' ) { $scope = 'c' }
Branch: 1 branch
BranchTrueFalseTotal
if ($scope eq "") 0 0 0
130 } else {
131 0 $scope = 'c';
132 0 $path = $str;
133 }
134 0 return( $scope, $path );
135 }
136  
137  
138 0 ▶sub lookup_config {
Subroutine lookup_config: not called CC=2 SCAR=17.9
Pod documented
139 0 my $str = shift;
140  
141 0 ▶ if( my ( $scope, $path ) = _parse_var( $str ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = _parse_var($str)) 0 0 0
142 0 my $c = Test::BDD::Infrastructure::Config->new;
143 0 return $c->get( $scope, $path );
144 }
145  
146 0 return $str;
147 }
148  
149  
150 0 ▶sub lookup_config_node {
Subroutine lookup_config_node: not called CC=2 SCAR=17.9
Pod documented
151 0 my $str = shift;
152  
153 0 ▶ if( my ( $scope, $path ) = _parse_var( $str ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = _parse_var($str)) 0 0 0
154 0 my $c = Test::BDD::Infrastructure::Config->new;
155 0 return $c->get_node( $scope, $path );
156 }
157  
158 0 return;
159 }
160  
161 1 1;
162  
163 __END__
« Infrastructure/Config/Hash.pm ↑ Summary Infrastructure/Logfile.pm »