Code/Condition.pm

filter: Statementstmt 21.5% 11 / 51 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 50.0% 3 / 6 Podpod 100% 3 / 3 totaltotal 16.5% 17 / 103 CC 17 SCAR 38.0
CC
17
Cov
13%
add_if
72.0
output
42.0
else
30.0
CRAP 44.6
« Code/Array.pm ↑ Summary Code/Expression.pm »
1 package JavaScript::Code::Condition;
2
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use vars qw[ $VERSION ];
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use base qw[ JavaScript::Code::Element ];
Subroutine BEGIN: called CC=1 SCAR=0.0
6
7 2 __PACKAGE__->mk_ro_accessors(qw[ ifs ]);
8
9 2 $VERSION = '0.08';
10
11 =head1 NAME
12
13 JavaScript::Code::Condition - A JavaScript Condition
14
15 =head1 METHODS
16
17 =head2 new
18
19 =head2 $self->add_if( %args | \%args )
20
21 Adds a new if statement.
22
23 I<%args> must contain the following keys:
24
25 - expression: a L<JavaScript::Code::Expression::Boolean> object
26
27 - block: a L<JavaScript::Code::Block> object
28
29 =cut
30
31 0 ▶sub add_if {
Subroutine add_if: not called CC=8 SCAR=42.8
Pod documented
32 0 my $self = shift;
33
34 0 my $args = $_[0];
35 0 ▶ $args = [ $self->args(@_) ]
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args eq "ARRAY" 0 0 0
36 unless ref $args eq 'ARRAY';
37
38 0 foreach my $c ( @{$args} ) {
39
40 0 my $expr = $c->{expression};
41 0 ▶ die "Expression must be a 'JavaScript::Code::Expression::Boolean'."
Branch: 1 branch
BranchTrueFalseTotal
unless ref $expr and $expr->isa("JavaScript::Code::Expression::Boolean") 0 0 0
Condition0%0/3
ref $expr and $expr->isa("JavaScript::Code::Expression::Boolean")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $expr and $expr->isa("JavaScript::Code::Expression::Boolean")
ref $expr $expr->isa("JavaScript::Code::Expression::Boolean")
Truth table0%0/3
A and B
A= ref $exprB= $expr->isa("JavaScript::Code::Expression::Boolean")
ABresult
0X0
100
111
42 unless ref $expr
43 and $expr->isa('JavaScript::Code::Expression::Boolean');
44
45 0 my $block = $c->{block};
46 0 ▶ die "Block must be a 'JavaScript::Code::Block'."
Branch: 1 branch
BranchTrueFalseTotal
unless ref $block and $block->isa("JavaScript::Code::Block") 0 0 0
Condition0%0/3
ref $block and $block->isa("JavaScript::Code::Block")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $block and $block->isa("JavaScript::Code::Block")
ref $block $block->isa("JavaScript::Code::Block")
Truth table0%0/3
A and B
A= ref $blockB= $block->isa("JavaScript::Code::Block")
ABresult
0X0
100
111
47 unless ref $block
48 and $block->isa('JavaScript::Code::Block');
49
50 0 my $cond = {
51 expression => $expr,
52 block => $block->clone->parent($self),
53 };
54
55 $self->{ifs} = []
56 0 ▶ unless defined $self->{ifs};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"ifs"} 0 0 0
57
58 0 push @{ $self->{ifs} }, $cond;
59 }
60
61 0 return $self;
62 }
63
64 =head2 $self->else( $block )
65
66 Sets the else statement.
67
68 I<$block> must be a L<JavaScript::Code::Block>
69
70 =cut
71
72 0 ▶sub else {
Subroutine else: not called CC=5 SCAR=34.0
Pod documented
73 0 my $self = shift;
74 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
75 0 my $args = $self->args(@_);
76
77 0 ▶ my $block = $args->{block} || shift;
Condition0%0/3
$args->{'block'} || shift()
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{'block'} || shift()
$args->{'block'} shift()
Truth table0%0/3
A || B
A= $args->{'block'}B= shift()
ABresult
1X1
011
000
78 0 ▶ die "Block must be a 'JavaScript::Code::Block'."
Branch: 1 branch
BranchTrueFalseTotal
unless ref $block and $block->isa("JavaScript::Code::Block") 0 0 0
Condition0%0/3
ref $block and $block->isa("JavaScript::Code::Block")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $block and $block->isa("JavaScript::Code::Block")
ref $block $block->isa("JavaScript::Code::Block")
Truth table0%0/3
A and B
A= ref $blockB= $block->isa("JavaScript::Code::Block")
ABresult
0X0
100
111
79 unless ref $block
80 and $block->isa('JavaScript::Code::Block');
81
82 0 $self->{else} = $block->clone->parent($self);
83
84 0 return $self;
85 }
86 else {
87 0 return $self->{else};
88 }
89 }
90
91 =head2 $self->output( )
92
93 =cut
94
95 0 ▶sub output {
Subroutine output: not called CC=6 SCAR=37.4
Pod documented
96 0 my $self = shift;
97 0 ▶ my $scope = shift || 1;
Condition0%0/2
shift() || 1
l!l
00
MC/DC0%0/1
shift() || 1
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
98
99 0 ▶ die "At least one if-statement is needed."
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->ifs 0 0 0
100 unless defined $self->ifs;
101
102 0 my $indenting = $self->get_indenting($scope);
103 0 my $output = '';
104
105 0 my $max = @{ $self->ifs };
106 0 ▶ for ( my $i = 0 ; $i < $max ; ++$i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $max) 0 0 0
107 0 $output .= $indenting;
108 0 ▶ $output .= "else " if $i;
Branch: 1 branch
BranchTrueFalseTotal
if $i 0 0 0
109
110 0 my $c = $self->ifs->[$i];
111 0 $output .= "if ( " . $c->{expression}->output($scope) . " )\n";
112 0 $output .= $c->{block}->output($scope);
113 }
114
115 0 ▶ if ( defined $self->else ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->else) 0 0 0
116 0 $output .= $indenting;
117 0 $output .= "else\n";
118 0 $output .= $self->else->output;
119 }
120
121 0 return $output;
122 }
123
124 =head1 SEE ALSO
125
126 L<JavaScript::Code>
127
128 =head1 AUTHOR
129
130 Sascha Kiefer, C<esskar@cpan.org>
131
132 =head1 LICENSE
133
134 This library is free software, you can redistribute it and/or modify it under
135 the same terms as Perl itself.
136
137 1;
« Code/Array.pm ↑ Summary Code/Expression.pm »