blib/lib/DTL/Fast/Tag/Ifchanged.pm

filter: Statementstmt 98.7% 80 / 81 Branchbran 95.4% 21 / 22 Conditioncond 100% 5 / 5 MC/DCmcdc 100% 3 / 3 Subroutinesub 100% 13 / 13 Podpod 0.0% 0 / 8 totaltotal 92.4% 122 / 132 SCAR 27.1
CC
15
Cov
98%
render
9.0
watches_changed
3.0
parse_parameters
2.0
CRAP 15.0
« blib/lib/DTL/Fast/Filter/Unorderedlist.pm ↑ Summary blib/lib/DTL/Fast/Tag/Regroup.pm »
1 package DTL::Fast::Tag::Ifchanged;
2 2 ▶use strict; use utf8; use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use parent 'DTL::Fast::Tag';
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 $DTL::Fast::TAG_HANDLERS{'ifchanged'} = __PACKAGE__;
6  
7 2 ▶use DTL::Fast::Expression::Operator::Binary::Eq;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 #@Override
10 8 ▶sub get_close_tag{ return 'endifchanged'; }
Subroutine get_close_tag: called CC=1 SCAR=0.0
Pod undocumented
11  
12 #@Override
13 sub parse_parameters
14 {
15 8 ▶ my $self = shift;
Subroutine parse_parameters: called CC=2 SCAR=6.9
Pod undocumented
16  
17 8 $self->add_branch();
18  
19 8 ▶ if ( $self->{'parameter'})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'parameter'}) { } 5 3 8
20 {
21 5 $self->{'watches'} = $self->parse_sources($self->{'parameter'});
22 }
23 else
24 {
25 3 $self->{'watch_content'} = 1;
26 }
27  
28 8 return $self;
29 }
30  
31  
32 #@Override
33 sub add_chunk
34 {
35 34 ▶ my $self = shift;
Subroutine add_chunk: called CC=1 SCAR=0.0
Pod undocumented
36 34 my $chunk = shift;
37  
38 34 $self->{'branches'}->[-1]->add_chunk($chunk);
39  
40 34 return $self;
41 }
42  
43 #@Override
44 sub parse_tag_chunk
45 {
46 12 ▶ my( $self, $tag_name, $tag_param, $chunk_lines ) = @_;
Subroutine parse_tag_chunk: called CC=2 SCAR=6.9
Pod undocumented
47  
48 12 my $result = undef;
49  
50 12 ▶ if( $tag_name eq 'else' )
Branch: 1 branch
BranchTrueFalseTotal
if ($tag_name eq 'else') { } 4 8 12
51 {
52 4 $self->add_branch();
53 4 $DTL::Fast::Template::CURRENT_TEMPLATE_LINE += $chunk_lines;
54 }
55 else
56 {
57 8 $result = $self->SUPER::parse_tag_chunk($tag_name, $tag_param, $chunk_lines);
58 }
59  
60 12 return $result;
61 }
62  
63 #@Override
64 sub render
65 {
66 71 ▶ my $self = shift;
Subroutine render: called CC=9 SCAR=22.0
Pod undocumented
67 71 my $context = shift;
68 71 my $result = '';
69  
70 71 my $forloop = $context->{'ns'}->[-1]->{'forloop'};
71  
72 71 ▶ if( defined $forloop )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $forloop) { } 71 0 71
73 {
74 71 ▶ if( $self->{'watch_content'} ) # slow behavior
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'watch_content'}) { } 27 44 71
75 {
76 27 $result = $self->{'branches'}->[0]->render($context);
77 27 ▶ if(
Branch: 2 branches
BranchTrueFalseTotal
if ($forloop->{'first'} or $result ne $self->{'last_iteration_content'}) { } 9 18 27
elsif (scalar @{$self->{'branches'};} > 1) { } 9 9 18
Condition100%3/3
$forloop->{"first"} or $result ne $self->{"last_iteration_content"}
l!l&&r!l&&!r
3618
MC/DC100%2/2
$forloop->{"first"} or $result ne $self->{"last_iteration_content"}
$forloop->{"first"} $result ne $self->{"last_iteration_content"}
Truth table100%3/3
A or B
A= $forloop->{"first"}B= $result ne $self->{"last_iteration_content"}
ABresult
1X1
011
000
78 $forloop->{'first'} # first pass
79 or $result ne $self->{'last_iteration_content'} # content changed
80 )
81 {
82 9 $self->{'last_iteration_content'} = $result;
83 }
84 18 elsif( scalar @{$self->{'branches'}} > 1 )
85 {
86 9 $result = $self->{'branches'}->[1]->render($context);
87 }
88 else
89 {
90 9 $result = '';
91 }
92 }
93 else
94 {
95 44 ▶ if( $forloop->{'first'} ) # first pass
Branch: 1 branch
BranchTrueFalseTotal
if ($forloop->{'first'}) { } 5 39 44
96 {
97 5 $self->update_preserved($context);
98 5 $result = $self->{'branches'}->[0]->render($context);
99 }
100 else
101 {
102 39 ▶ if( $self->watches_changed($context) )
Branch: 2 branches
BranchTrueFalseTotal
if ($self->watches_changed($context)) { } 14 25 39
elsif (scalar @{$self->{'branches'};} > 1) { } 9 16 25
103 {
104 14 $result = $self->{'branches'}->[0]->render($context);
105 14 $self->update_preserved($context);
106 }
107 25 elsif( scalar @{$self->{'branches'}} > 1 )
108 {
109 9 $result = $self->{'branches'}->[1]->render($context);
110 }
111 }
112 }
113 }
114 else
115 {
116 0 warn "ifchanged tag can be rendered only inside for loop";
117 }
118  
119 71 return $result;
120 }
121  
122 sub watches_changed
123 {
124 39 ▶ my $self = shift;
Subroutine watches_changed: called CC=3 SCAR=11.0
Pod undocumented
125 39 my $context = shift;
126 39 my $result = 0;
127  
128 39 ▶ for( my $i = 0; $i < scalar @{$self->{'watches'}}; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @{$self->{"watches"};}) 39 25 64
129 {
130 39 my $watch = $self->{'watches'}->[$i]->render($context);
131 39 my $preserve = $self->{'preserved'}->[$i];
132  
133 39 ▶ if( not DTL::Fast::Expression::Operator::Binary::Eq::dispatch($self, $watch, $preserve))
Branch: 1 branch
BranchTrueFalseTotal
unless (DTL::Fast::Expression::Operator::Binary::Eq::dispatch($self, $watch, $preserve)) 14 25 39
134 {
135 14 $result = 1;
136 14 last;
137 }
138 }
139 39 return $result;
140 }
141  
142  
143 sub update_preserved
144 {
145 19 ▶ my $self = shift;
Subroutine update_preserved: called CC=2 SCAR=6.9
Pod undocumented
146 19 my $context = shift;
147  
148 19 $self->{'preserved'} = [];
149
150 19 foreach my $watch ( @{$self->{'watches'}} )
151 {
152 19 push @{$self->{'preserved'}}, $watch->render($context);
153 }
154  
155 19 return $self;
156 }
157  
158 sub add_branch
159 {
160 12 ▶ my $self = shift;
Subroutine add_branch: called CC=2 SCAR=6.9
Pod undocumented
161  
162 12 ▶ $self->{'branches'} //= [];
Condition100%2/2
$self->{"branches"} //= []
l!l
48
MC/DC100%1/1
$self->{"branches"} //= []
$self->{"branches"}
Truth table100%2/2
A
A= $self->{"branches"}
Aresult
11
00
163 12 push @{$self->{'branches'}}, DTL::Fast::Renderer->new();
164 12 return $self;
165 }
166  
167 2 1;
« blib/lib/DTL/Fast/Filter/Unorderedlist.pm ↑ Summary blib/lib/DTL/Fast/Tag/Regroup.pm »