Toolkit/Pattern.pm

filter: Statementstmt 93.8% 91 / 97 Branchbran 80.0% 48 / 60 Conditioncond 40.0% 4 / 10 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 93.3% 14 / 15 Podpod 0.0% 0 / 11 totaltotal 79.2% 157 / 198 CC 38 SCAR 21.2
CC
38
Cov
86%
_dump_tree
17.1
english_unit
6.1
type
6.0
CRAP 8.3
« Toolkit/Pattern/NearestWeekday.pm ↑ Summary Toolkit/Pattern/Step.pm »
1 package Cron::Toolkit::Pattern;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Cron::Toolkit::Utils qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 777 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
7 777 my ($class, %args) = @_;
8 my $self = bless {
9 children => [],
10 field_type => $args{field_type}
11 777 }, $class;
12 777 ▶ $self->{value} = $args{value} if defined $args{value};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"value"} 736 41 777
13  
14 777 return $self;
15 }
16 91 ▶sub add_child {
Subroutine add_child: called CC=1 SCAR=0.0
Pod undocumented
17 91 my ($self, $child) = @_;
18 91 push @{$self->{children}}, $child;
19 }
20  
21 140 ▶sub children {
Subroutine children: called CC=1 SCAR=0.0
Pod undocumented
22 140 my ($self) = @_;
23 140 return $self->{children};
24 }
25  
26 127 ▶sub has_children {
Subroutine has_children: called CC=2 SCAR=6.9
Pod undocumented
27 127 my $self = shift;
28 127 ▶ return scalar @{ $self->{children} } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
scalar @{$self->{'children'};} ? : 14 113 127
29 }
30  
31 0 ▶sub type {
Subroutine type: not called CC=2 SCAR=17.9
Pod undocumented
32 0 my ($self, $value) = @_;
33 0 ▶ $self->{type} = $value if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 0 0 0
34 0 return $self->{type}; # Return the value (either set or current)
35 }
36  
37 2083474 ▶sub field_type {
Subroutine field_type: called CC=2 SCAR=7.1
Pod undocumented
38 2083474 my ($self, $value) = @_;
39 2083474 ▶ $self->{field_type} = $value if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 0 2083474 2083474
40 2083474 return $self->{field_type};
41 }
42  
43 1952065 ▶sub value {
Subroutine value: called CC=2 SCAR=7.1
Pod undocumented
44 1952065 my ($self, $value) = @_;
45 1952065 ▶ $self->{value} = $value if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 0 1952065 1952065
46 1952065 return $self->{value};
47 }
48  
49 479 ▶sub lowest {
Subroutine lowest: called CC=5 SCAR=16.5
Pod undocumented
50 479 my ( $self, $tm ) = @_;
51  
52 479 my ( $min, $max ) = @{ $LIMITS{ $self->field_type } };
53 479 ▶ $max = $tm->length_of_month if $self->field_type eq 'dom';
Branch: 1 branch
BranchTrueFalseTotal
if $self->field_type eq "dom" 0 479 479
54  
55 479 for my $v ( $min .. $max ) {
56 1551 my $test_tm = $tm;
57 1551 ▶ $test_tm = $test_tm->with_day_of_month($v) if $self->field_type eq 'dom';
Branch: 1 branch
BranchTrueFalseTotal
if $self->field_type eq "dom" 0 1551 1551
58 1551 ▶ return $v if $self->match($v);
Branch: 1 branch
BranchTrueFalseTotal
if $self->match($v) 479 1072 1551
59 }
60 0 return;
61 }
62  
63 479 ▶sub highest {
Subroutine highest: called CC=5 SCAR=16.5
Pod undocumented
64 479 my ( $self, $tm ) = @_;
65 479 my ( $min, $max ) = @{ $LIMITS{ $self->field_type } };
66 479 ▶ $max = $tm->length_of_month if $self->field_type eq 'dom';
Branch: 1 branch
BranchTrueFalseTotal
if $self->field_type eq "dom" 0 479 479
67  
68 479 for my $v ( reverse $min .. $max ) {
69 20943 my $test_tm = $tm;
70 20943 ▶ $test_tm = $test_tm->with_day_of_month($v) if $self->field_type eq 'dom';
Branch: 1 branch
BranchTrueFalseTotal
if $self->field_type eq "dom" 0 20943 20943
71 20943 ▶ return $v if $self->match($v);
Branch: 1 branch
BranchTrueFalseTotal
if $self->match($v) 479 20464 20943
72 }
73 0 return;
74 }
75  
76 94 ▶sub english_unit {
Subroutine english_unit: called CC=5 SCAR=18.1
Pod undocumented
77 94 my $self = shift;
78 94 ▶ my $unit = $self->field_type =~ /^(dow|dom)$/ ? 'day' : $self->field_type;
Branch: 1 branch
BranchTrueFalseTotal
$self->field_type =~ /^(dow|dom)$/ ? : 20 74 94
79 94 ▶ $unit .= 's' if ($self->type eq 'single' && $self->value != 1 && $self->field_type ne 'year');
Branch: 1 branch
BranchTrueFalseTotal
if $self->type eq "single" and $self->value != 1 and $self->field_type ne "year" 0 94 94
Condition33%2/6
$self->type eq "single" and $self->value != 1 and $self->field_type ne "year"
!ll&&!rl&&r
9400
$self->type eq "single" and $self->value != 1
!ll&&!rl&&r
9400
MC/DC0%0/3
$self->type eq "single" and $self->value != 1 and $self->field_type ne "year"
$self->type eq "single" $self->value != 1 $self->field_type ne "year"
Truth table25%1/4
A and B and C
A= $self->type eq "single"B= $self->value != 1C= $self->field_type ne "year"
ABCresult
0XX0
10X0
1100
1111
80 94 return $unit;
81 }
82  
83 136 ▶sub english_value {
Subroutine english_value: called CC=5 SCAR=16.1
Pod undocumented
84 136 my ( $self ) = @_;
85 136 my $value = $self->{value};
86 136 ▶ die "missing value" unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 136 136
87 136 ▶ return 'the ' . num_to_ordinal($value) if $self->field_type eq 'dom';
Branch: 1 branch
BranchTrueFalseTotal
if $self->field_type eq "dom" 28 108 136
88 108 ▶ return $MONTH_NAMES{$value} if $self->field_type eq 'month';
Branch: 1 branch
BranchTrueFalseTotal
if $self->field_type eq "month" 29 79 108
89 79 ▶ return $DAY_NAMES{$value} if $self->field_type eq 'dow';
Branch: 1 branch
BranchTrueFalseTotal
if $self->field_type eq "dow" 24 55 79
90 55 return $value;
91 }
92  
93 664 ▶sub _dump_tree {
Subroutine _dump_tree: called CC=17 SCAR=28.4
94 664 my ($self, $indent) = @_;
95 664 ▶ $indent //= '';
Condition50%1/2
$indent //= ""
l!l
6640
MC/DC0%0/1
$indent //= ""
$indent
Truth table50%1/2
A
A= $indent
Aresult
11
00
96  
97 664 my $type = $self->type;
98  
99 # Simple leaf values — inline
100 664 ▶ if ($type eq 'wildcard') { return '*'; }
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "wildcard") 170 494 664
101 494 ▶ if ($type eq 'unspecified') { return '?'; }
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "unspecified") 84 410 494
102 410 ▶ if ($type eq 'single') { return $self->{value}; }
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "single") 348 62 410
103 62 ▶ if ($type eq 'last') { return $self->{value} // 'L'; }
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "last") 10 52 62
Condition50%1/2
$self->{"value"} // "L"
l!l
100
MC/DC0%0/1
$self->{"value"} // "L"
$self->{"value"}
Truth table50%1/2
A
A= $self->{"value"}
Aresult
11
00
104 52 ▶ if ($type eq 'lastW') { return 'LW'; }
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "lastW") 1 51 52
105 51 ▶ if ($type eq 'nearest_weekday') { return $self->{value}; }
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "nearest_weekday") 3 48 51
106 48 ▶ if ($type eq 'nth') { return $self->{value}; }
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "nth") 9 39 48
107  
108 39 my @children = @{$self->{children}};
109 39 ▶ return '' unless @children;
Branch: 1 branch
BranchTrueFalseTotal
unless @children 0 39 39
110  
111 39 my $out = "";
112  
113 39 ▶ if ($type eq 'range') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "range") 19 20 39
114 19 return $children[0]->_dump_tree($indent) . '-' . $children[1]->_dump_tree($indent);
115 }
116  
117 20 ▶ if ($type eq 'step') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "step") 11 9 20
118 11 my $base = $children[0]->_dump_tree($indent . ' ');
119 11 $out .= $base . "\n";
120 11 $out .= "${indent}└─ /$children[1]{value}";
121 11 return $out;
122 }
123  
124 9 ▶ if ($type eq 'list') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "list") 9 0 9
125 9 $out = "\n";
126 9 for my $i (0 .. $#children) {
127 27 ▶ my $prefix = ($i == $#children) ? '└─ ' : '├─ ';
Branch: 1 branch
BranchTrueFalseTotal
$i == $#children ? : 9 18 27
128 27 ▶ my $next_indent = $indent . ($i == $#children ? ' ' : '│ ');
Branch: 1 branch
BranchTrueFalseTotal
$i == $#children ? : 9 18 27
129 27 $out .= "${indent}$prefix" . $children[$i]->_dump_tree($next_indent) . "\n";
130 }
131 9 chomp $out;
132 9 return $out;
133 }
134  
135 0 return $type;
136 }
137  
138 2 1;
« Toolkit/Pattern/NearestWeekday.pm ↑ Summary Toolkit/Pattern/Step.pm »