blib/lib/App/RecordStream/DomainLanguage/Snippet.pm

filter: Statementstmt 90.7% 69 / 76 Branchbran 66.6% 8 / 12 Conditioncond 64.2% 9 / 14 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 2 totaltotal 80.0% 96 / 120 CC 15 SCAR 18.3
CC
15
Cov
84%
_transform_angles
10.8
evaluate_as
6.0
_quote_snippet
2.0
CRAP 6.2
« blib/lib/App/RecordStream/KeyGroups.pm ↑ Summary blib/lib/App/RecordStream/Operation/join.pm »
1 package App::RecordStream::DomainLanguage::Snippet;
2  
3 29 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 29 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 29 ▶use App::RecordStream::DomainLanguage::Executor;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 29 ▶use App::RecordStream::DomainLanguage::Value;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 29 ▶use App::RecordStream::Executor;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 sub new
11 {
12 432 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
13 432 my $code = shift;
14 432 my $vars = shift;
15  
16 432 $code = App::RecordStream::Executor->transform_code($code);
17 432 $code = _transform_angles($code);
18  
19 432 my $this =
20 {
21 'CODE' => $code,
22 'VARS' => $vars,
23 };
24  
25 432 bless $this, $class;
26  
27 432 return $this;
28 }
29  
30 sub evaluate_as
31 {
32 378 ▶ my $this = shift;
Subroutine evaluate_as: called CC=6 SCAR=18.0
Pod undocumented
33 378 my $type = shift;
34 378 ▶ my $vars = shift || {};
Condition100%2/2
shift() || {}
l!l
202176
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
35  
36 378 my $executor = App::RecordStream::DomainLanguage::Executor->new();
37 378 $executor->import_registry();
38  
39 378 for my $var (%{$this->{'VARS'}})
40 {
41 42 for my $ref (@{$this->{'VARS'}->{$var}})
42 {
43 36 $executor->set_ref($var, $ref);
44 }
45 }
46  
47 378 for my $var (keys(%$vars))
48 {
49 287 ▶ if(0)
Branch: 1 branch
BranchTrueFalseTotal
if (do { $var =~ /^\$(.*)$/ }) { } 287 0 287
50 {
51 }
52 elsif($var =~ /^\$(.*)$/)
53 {
54 287 $executor->set_scalar($1, $vars->{$var});
55 }
56 else
57 {
58 0 die "Bad var for snippet: '$var'";
59 }
60 }
61 378 my $result = $executor->exec($this->{'CODE'});
62  
63 378 return App::RecordStream::DomainLanguage::Value::cast_or_die($type, $result);
64 }
65  
66 sub _transform_angles
67 {
68 432 ▶ my $code = shift;
Subroutine _transform_angles: called CC=9 SCAR=23.8
69  
70 432 my $pos = 0;
71 432 my $out = '';
72 432 while(1)
73 {
74 478 my $top_level_entrance = index($code, '<<', $pos);
75 478 ▶ if($top_level_entrance == -1)
Branch: 1 branch
BranchTrueFalseTotal
if ($top_level_entrance == -1) 432 46 478
76 {
77 432 $out .= substr($code, $pos);
78 432 last;
79 }
80  
81 46 my $level = 1;
82 46 my $pos2 = $top_level_entrance + 2;
83 46 my $top_level_exit;
84 46 while(1)
85 {
86 46 my $next_enter = index($code, '<<', $pos2);
87 46 my $next_exit = index($code, '>>', $pos2);
88  
89 46 ▶ if($next_enter != -1 && ($next_exit == -1 || $next_enter < $next_exit))
Branch: 1 branch
BranchTrueFalseTotal
if ($next_enter != -1 and $next_exit == -1 || $next_enter < $next_exit) 0 46 46
Condition50%3/6
$next_enter != -1 and $next_exit == -1 || $next_enter < $next_exit
!ll&&!rl&&r
22240
$next_exit == -1 or $next_enter < $next_exit
l!l&&r!l&&!r
0024
MC/DC0%0/3
$next_enter != -1 and $next_exit == -1 || $next_enter < $next_exit
$next_enter != -1 $next_exit == -1 $next_enter < $next_exit
Truth table50%2/4
A and B or C
A= $next_enter != -1B= $next_exit == -1C= $next_enter < $next_exit
ABCresult
0XX0
1000
11X1
1011
90 {
91 0 ++$level;
92 0 $pos2 = $next_enter + 2;
93 0 next;
94 }
95  
96 46 ▶ if($next_exit != -1 && ($next_enter == -1 || $next_exit < $next_enter))
Branch: 1 branch
BranchTrueFalseTotal
if ($next_exit != -1 and $next_enter == -1 || $next_exit < $next_enter) 46 0 46
Condition66%4/6
$next_exit != -1 and $next_enter == -1 || $next_exit < $next_enter
!ll&&!rl&&r
0640
$next_enter == -1 or $next_exit < $next_enter
l!l&&r!l&&!r
22240
MC/DC0%0/3
$next_exit != -1 and $next_enter == -1 || $next_exit < $next_enter
$next_exit != -1 $next_enter == -1 $next_exit < $next_enter
Truth table25%1/4
A and B or C
A= $next_exit != -1B= $next_enter == -1C= $next_exit < $next_enter
ABCresult
0XX0
1000
11X1
1011
97 {
98 46 --$level;
99 46 ▶ if($level == 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($level == 0) 46 0 46
100 {
101 46 $top_level_exit = $next_exit;
102 46 last;
103 }
104 0 $pos2 = $next_enter + 2;
105 0 next;
106 }
107  
108 0 die "Unbalanced << and >> in snippet: $code";
109 }
110  
111 46 $out .= substr($code, $pos, $top_level_entrance - $pos);
112 46 $out .= _quote_snippet(substr($code, $top_level_entrance + 2, $top_level_exit - $top_level_entrance - 2));
113 46 $pos = $top_level_exit + 2;
114 }
115  
116 432 return $out;
117 }
118  
119 sub _quote_snippet
120 {
121 46 ▶ my $code = shift;
Subroutine _quote_snippet: called CC=2 SCAR=6.9
122  
123 46 my @vars;
124 46 ▶ if($code =~ s/^([a-zA-Z_][a-zA-Z_0-9]*(,[a-zA-Z_][a-zA-Z_0-9]*)*)\|//)
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ s/^([a-zA-Z_][a-zA-Z_0-9]*(,[a-zA-Z_][a-zA-Z_0-9]*)*)\|//) 4 42 46
125 {
126 4 @vars = split(/,/, $1);
127 }
128  
129 # Could not get typeglobs to work. References go in, references come out,
130 # you can't explain that...
131 46 return "snip(App::RecordStream::DomainLanguage::Snippet->new('$code', {" . join(", ", map { "'$_' => [\\\$$_, \\\@$_, \\\%$_]" } @vars) . "}))";
132 }
133  
134 29 1;
« blib/lib/App/RecordStream/KeyGroups.pm ↑ Summary blib/lib/App/RecordStream/Operation/join.pm »