blib/lib/Text/Nunjucks/Runtime/Frame.pm

filter: Statementstmt 65.2% 60 / 92 Branchbran 50.0% 15 / 30 Conditioncond 47.6% 10 / 21 MC/DCmcdc 42.8% 6 / 14 Subroutinesub 70.0% 14 / 20 Podpod 0.0% 0 / 12 totaltotal 55.5% 105 / 189 CC 25 SCAR 15.6
CC
25
Cov
59%
set
8.1
set
8.1
resolve
7.3
CRAP 4.7
« blib/lib/Text/Nunjucks/Nodes/Node.pm ↑ Summary blib/lib/Text/Nunjucks/EmitterObj.pm »
1 package Text::Nunjucks::Runtime::Frame;
2 # ABSTRACT: A scope of template variables (runtime.js class Frame)
3  
4 23 our $VERSION = '0.001';
5  
6 23 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 23 ▶use Text::Nunjucks::Runtime;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 23 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # upstream: class Frame in src/runtime.js.
11  
12 # constructor(parent, isolateWrites): variables is a bare bag (JS
13 # Object.create(null)); topLevel starts false and the compiler/environment
14 # flip it to true on the root frame, so it is rw.
15 136388 ▶has variables => ( is => 'ro', default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
16 23 has parent => ( is => 'ro' );
17 136388 ▶has top_level => ( is => 'rw', default => sub { 0 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
18 23 has isolate_writes => ( is => 'ro' );
19  
20 136388 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=3 SCAR=12.0
21 136388 my ( $orig, $class, @args ) = @_;
22 136388 ▶ return $class->$orig(@args) if @args == 1 && ref $args[0] eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if @args == 1 and ref $args[0] eq "HASH" 0 136388 136388
Condition33%1/3
@args == 1 and ref $args[0] eq "HASH"
!ll&&!rl&&r
13638800
MC/DC0%0/2
@args == 1 and ref $args[0] eq "HASH"
@args == 1 ref $args[0] eq "HASH"
Truth table33%1/3
A and B
A= @args == 1B= ref $args[0] eq "HASH"
ABresult
0X0
100
111
23 136388 my ( $parent, $isolate_writes ) = @args;
24 136388 return $class->$orig( parent => $parent, isolate_writes => $isolate_writes );
25 23 };
26  
27 464673 ▶sub set {
Subroutine set: called CC=5 SCAR=20.9
Pod undocumented
28 0 ▶ my ( $self, $name, $val, $resolve_up ) = @_;
Subroutine set: not called CC=5 SCAR=20.9
Pod undocumented
29  
30 # Allow variables with dots by automatically creating the nested structure.
31 0 my @parts = split /\./, $name, -1; # JS split('.') keeps trailing empties
32 0 my $obj = $self->variables;
33 0 my $frame = $self;
34  
35 0 ▶ if ($resolve_up) {
Branch: 2 branches
BranchTrueFalseTotal
if ($resolve_up) 36132 428541 464673
if ($resolve_up) 0 0 0
36 0 ▶ if ( $frame = $self->resolve( $parts[0], 1 ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($frame = $self->resolve($parts[0], 1)) 3387 32745 36132
if ($frame = $self->resolve($parts[0], 1)) 0 0 0
37 0 $frame->set( $name, $val );
38 0 return;
39 }
40 }
41  
42 0 for my $i ( 0 .. $#parts - 1 ) {
43 0 my $id = $parts[$i];
44 # JS: if (!obj[id]) obj[id] = {}; -- JS falsiness (null/None too), not Perl's
45 0 ▶ if ( Text::Nunjucks::Runtime->_falsy( $obj->{$id} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ("Text::Nunjucks::Runtime"->_falsy($obj->{$id})) 2118 178708 180826
if ("Text::Nunjucks::Runtime"->_falsy($obj->{$id})) 0 0 0
46 0 $obj->{$id} = {};
47 }
48 0 $obj = $obj->{$id};
49 }
50  
51 0 $obj->{ $parts[-1] } = $val;
52 }
53  
54 4 ▶sub get {
Subroutine get: called CC=2 SCAR=9.2
Pod undocumented
55 0 ▶ my ( $self, $name ) = @_;
Subroutine get: not called CC=2 SCAR=9.2
Pod undocumented
56 0 my $val = $self->variables->{$name};
57 0 ▶ return $val if defined $val; # val !== undefined
Branch: 2 branches
BranchTrueFalseTotal
if defined $val 3 1 4
if defined $val 0 0 0
58 0 return Text::Nunjucks::Runtime->None; # JS: return null
59 }
60  
61 196431 ▶sub lookup {
Subroutine lookup: called CC=3 SCAR=13.2
Pod undocumented
62 0 ▶ my ( $self, $name ) = @_;
Subroutine lookup: not called CC=3 SCAR=13.2
Pod undocumented
63 0 my $p = $self->parent;
64 0 my $val = $self->variables->{$name};
65 0 ▶ return $val if defined $val; # val !== undefined
Branch: 2 branches
BranchTrueFalseTotal
if defined $val 39523 156908 196431
if defined $val 0 0 0
66  
67 # Port-only (k83): one call per frame up the chain, as upstream's lookup
68 # recurses; a template that includes itself N deep stands on N frames or
69 # more. Perl warns at the 100th nested call of a sub, at the call site; the
70 # depth is bounded by memory, not by Perl (see Lib async_iter).
71 23 ▶ no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
72 0 ▶ return $p && $p->lookup($name); # p && p.lookup(name)
Condition50%3/6
$p && $p->lookup($name)
!ll&&!rl&&r
541392781574954
$p && $p->lookup($name)
!ll&&!rl&&r
000
MC/DC100%2/2
$p && $p->lookup($name)
$p $p->lookup($name)
MC/DC0%0/2
$p && $p->lookup($name)
$p $p->lookup($name)
Truth table100%3/3
A && B
A= $pB= $p->lookup($name)
ABresult
0X0
100
111
Truth table0%0/3
A && B
A= $pB= $p->lookup($name)
ABresult
0X0
100
111
73 }
74  
75 74358 ▶sub resolve {
Subroutine resolve: called CC=5 SCAR=19.9
Pod undocumented
76 0 ▶ my ( $self, $name, $for_write ) = @_;
Subroutine resolve: not called CC=5 SCAR=19.9
Pod undocumented
77 0 ▶ my $p = ( $for_write && $self->isolate_writes ) ? undef : $self->parent;
Branch: 2 branches
BranchTrueFalseTotal
$for_write && $self->isolate_writes ? : 3392 70966 74358
$for_write && $self->isolate_writes ? : 0 0 0
Condition50%3/6
$for_write and $self->isolate_writes
!ll&&!rl&&r
38226327403392
$for_write and $self->isolate_writes
!ll&&!rl&&r
000
MC/DC100%2/2
$for_write and $self->isolate_writes
$for_write $self->isolate_writes
MC/DC0%0/2
$for_write and $self->isolate_writes
$for_write $self->isolate_writes
Truth table100%3/3
A and B
A= $for_writeB= $self->isolate_writes
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $for_writeB= $self->isolate_writes
ABresult
0X0
100
111
78 0 my $val = $self->variables->{$name};
79 0 ▶ return $self if defined $val; # val !== undefined
Branch: 2 branches
BranchTrueFalseTotal
if defined $val 3389 70969 74358
if defined $val 0 0 0
80  
81 # one call per frame up the chain, as in lookup (k83)
82 23 ▶ no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
83 0 ▶ return $p && $p->resolve($name);
Condition50%3/6
$p && $p->resolve($name)
!ll&&!rl&&r
327456338161
$p && $p->resolve($name)
!ll&&!rl&&r
000
MC/DC100%2/2
$p && $p->resolve($name)
$p $p->resolve($name)
MC/DC0%0/2
$p && $p->resolve($name)
$p $p->resolve($name)
Truth table100%3/3
A && B
A= $pB= $p->resolve($name)
ABresult
0X0
100
111
Truth table0%0/3
A && B
A= $pB= $p->resolve($name)
ABresult
0X0
100
111
84 }
85  
86 10941 ▶sub push {
Subroutine push: called CC=1 SCAR=1.2
Pod undocumented
87 0 ▶ my ( $self, $isolate_writes ) = @_;
Subroutine push: not called CC=1 SCAR=1.2
Pod undocumented
88 0 return ref($self)->new( $self, $isolate_writes );
89 }
90  
91 2222 ▶sub pop {
Subroutine pop: called CC=1 SCAR=1.2
Pod undocumented
92 0 ▶ my ($self) = @_;
Subroutine pop: not called CC=1 SCAR=1.2
Pod undocumented
93 0 return $self->parent;
94 }
95  
96 23 1;
97  
98 __END__
« blib/lib/Text/Nunjucks/Nodes/Node.pm ↑ Summary blib/lib/Text/Nunjucks/EmitterObj.pm »