Forest/Tree.pm

filter: Statementstmt 91.0% 81 / 89 Branchbran 78.5% 33 / 42 Conditioncond 70.5% 12 / 17 MC/DCmcdc 54.5% 6 / 11 Subroutinesub 81.8% 18 / 22 Podpod 92.8% 13 / 14 totaltotal 83.5% 163 / 195 CC 30 SCAR 13.1
CC
30
Cov
85%
set_child_at
6.6
insert_child_at
6.0
add_child
6.0
CRAP 3.7
« Forest/Tree/Writer/ASCIIWithBranches.pm ↑ Summary Forest/Tree/Pure.pm »
1 package Forest::Tree;
2 15 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 15 ▶use Scalar::Util 'reftype', 'refaddr';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 15 ▶use List::Util 'sum', 'max';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 15 our $VERSION = '0.10';
8 15 our $AUTHORITY = 'cpan:STEVAN';
9  
10 15 extends qw(Forest::Tree::Pure);
11  
12 #has '+node' => ( is => 'rw' );
13 15 has 'node' => (
14 traits => [qw(StorableClone)],
15 is => 'rw',
16 isa => 'Item',
17 );
18  
19 0 ▶sub set_node {
Subroutine set_node: not called CC=1 SCAR=6.9
Pod documented
20 0 my ( $self, $new ) = @_;
21 0 $self->node($new);
22 0 $self;
23 }
24  
25 15 has 'parent' => (
26 traits => [qw(NoClone)],
27 reader => 'parent',
28 writer => '_set_parent',
29 predicate => 'has_parent',
30 clearer => 'clear_parent',
31 isa => 'Maybe[Forest::Tree]',
32 weak_ref => 1,
33 handles => {
34 'add_sibling' => 'add_child',
35 'get_sibling_at' => 'get_child_at',
36 'insert_sibling_at' => 'insert_child_at',
37 },
38 );
39  
40 #has '+children' => (
41 # is => 'rw',
42 has 'children' => (
43 traits => [qw(Array Clone)],
44 is => 'rw',
45 isa => 'ArrayRef[Forest::Tree]',
46 lazy => 1,
47 74 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
48 handles => {
49 get_child_at => 'get',
50 child_count => 'count',
51 },
52 51 ▶ trigger => sub {
Subroutine __ANON__: called CC=4 SCAR=14.3
53 51 my ($self, $children) = @_;
54 51 foreach my $child (@$children) {
55 71 $child->_set_parent($self);
56 71 ▶ $self->clear_height if $self->has_height;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_height 0 71 71
57 71 ▶ $self->clear_size if $self->has_size;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_size 0 71 71
58 }
59 }
60 15 );
61  
62 13 ▶after 'clear_size' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
63 13 my $self = shift;
64 13 ▶ $self->parent->clear_size
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_parent and $self->parent->has_size 5 8 13
Condition66%2/3
$self->has_parent and $self->parent->has_size
!ll&&!rl&&r
805
MC/DC50%1/2
$self->has_parent and $self->parent->has_size
$self->has_parent $self->parent->has_size
Truth table66%2/3
A and B
A= $self->has_parentB= $self->parent->has_size
ABresult
0X0
100
111
65 if $self->has_parent && $self->parent->has_size;
66 15 };
67  
68 13 ▶after 'clear_height' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
69 13 my $self = shift;
70 13 ▶ $self->parent->clear_height
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_parent and $self->parent->has_height 5 8 13
Condition66%2/3
$self->has_parent and $self->parent->has_height
!ll&&!rl&&r
805
MC/DC50%1/2
$self->has_parent and $self->parent->has_height
$self->has_parent $self->parent->has_height
Truth table66%2/3
A and B
A= $self->has_parentB= $self->parent->has_height
ABresult
0X0
100
111
71 if $self->has_parent && $self->parent->has_height;
72 15 };
73  
74 ## informational
75 76 ▶sub is_root { !(shift)->has_parent }
Subroutine is_root: called CC=1 SCAR=0.0
Pod documented
76  
77 ## depth
78 118 ▶sub depth { ((shift)->parent || return -1)->depth + 1 }
Subroutine depth: called CC=2 SCAR=6.9
Pod documented
Condition100%2/2
(shift())->parent || (return -1)
l!l
7939
MC/DC100%1/1
(shift())->parent || (return -1)
(shift())->parent
Truth table100%2/2
A
A= (shift())->parent
Aresult
11
00
79  
80 ## child management
81  
82 61 ▶sub add_child {
Subroutine add_child: called CC=6 SCAR=17.9
Pod documented
83 61 my ($self, $child) = @_;
84 61 ▶ (blessed($child) && $child->isa(ref $self))
Branch: 2 branches
BranchTrueFalseTotal
builtin::blessed($child) and $child->isa(ref $self) or confess('Child parameter must be a ' . ref($self) . ' not (' . (defined $child ? $child : "undef") . ')') 57 4 61
defined $child ? : 3 1 4
Condition100%3/3
builtin::blessed($child) and $child->isa(ref $self)
!ll&&!rl&&r
3157
MC/DC100%2/2
builtin::blessed($child) and $child->isa(ref $self)
builtin::blessed($child) $child->isa(ref $self)
Truth table100%3/3
A and B
A= builtin::blessed($child)B= $child->isa(ref $self)
ABresult
0X0
100
111
85 || confess "Child parameter must be a " . ref($self) . " not (" . (defined $child ? $child : 'undef') . ")";
86 57 $child->_set_parent($self);
87 57 ▶ $self->clear_height if $self->has_height;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_height 4 53 57
88 57 ▶ $self->clear_size if $self->has_size;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_size 4 53 57
89 57 push @{ $self->children } => $child;
90 57 $self;
91 }
92  
93 1 ▶sub replace {
Subroutine replace: called CC=2 SCAR=7.0
Pod documented
94 1 my ( $self, $replacement ) = @_;
95  
96 1 ▶ confess "Can't replace root" if $self->is_root;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_root 0 1 1
97  
98 1 $self->parent->set_child_at( $self->get_index_in_siblings, $replacement );
99  
100 1 return $replacement;
101 }
102  
103 6 ▶sub add_children {
Subroutine add_children: called CC=2 SCAR=6.9
Pod documented
104 6 my ($self, @children) = @_;
105 6 $self->add_child($_) for @children;
106 6 return $self;
107 }
108  
109 2 ▶sub set_child_at {
Subroutine set_child_at: called CC=6 SCAR=18.8
Pod documented
110 2 my ( $self, $index, $child ) = @_;
111  
112 2 ▶ (blessed($child) && $child->isa(ref $self))
Branch: 2 branches
BranchTrueFalseTotal
builtin::blessed($child) and $child->isa(ref $self) or confess('Child parameter must be a ' . ref($self) . ' not (' . (defined $child ? $child : "undef") . ')') 2 0 2
defined $child ? : 0 0 0
Condition33%1/3
builtin::blessed($child) and $child->isa(ref $self)
!ll&&!rl&&r
002
MC/DC0%0/2
builtin::blessed($child) and $child->isa(ref $self)
builtin::blessed($child) $child->isa(ref $self)
Truth table33%1/3
A and B
A= builtin::blessed($child)B= $child->isa(ref $self)
ABresult
0X0
100
111
113 || confess "Child parameter must be a " . ref($self) . " not (" . (defined $child ? $child : 'undef') . ")";
114  
115 2 ▶ $self->clear_height if $self->has_height;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_height 1 1 2
116 2 ▶ $self->clear_size if $self->has_size;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_size 1 1 2
117  
118 2 my $children = $self->children;
119  
120 2 $children->[$index]->clear_parent;
121  
122 2 $children->[$index] = $child;
123 2 $child->_set_parent($self);
124  
125 2 $self;
126 }
127  
128 7 ▶sub insert_child_at {
Subroutine insert_child_at: called CC=6 SCAR=18.0
Pod documented
129 7 my ($self, $index, $child) = @_;
130 7 ▶ (blessed($child) && $child->isa(ref $self))
Branch: 2 branches
BranchTrueFalseTotal
builtin::blessed($child) and $child->isa(ref $self) or confess('Child parameter must be a ' . ref($self) . ' not (' . (defined $child ? $child : "undef") . ')') 3 4 7
defined $child ? : 0 4 4
Condition66%2/3
builtin::blessed($child) and $child->isa(ref $self)
!ll&&!rl&&r
403
MC/DC50%1/2
builtin::blessed($child) and $child->isa(ref $self)
builtin::blessed($child) $child->isa(ref $self)
Truth table66%2/3
A and B
A= builtin::blessed($child)B= $child->isa(ref $self)
ABresult
0X0
100
111
131 || confess "Child parameter must be a " . ref($self) . " not (" . (defined $child ? $child : 'undef') . ")";
132 3 $child->_set_parent($self);
133 3 ▶ $self->clear_height if $self->has_height;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_height 1 2 3
134 3 ▶ $self->clear_size if $self->has_size;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_size 1 2 3
135 3 splice @{ $self->children }, $index, 0, $child;
136 3 $self;
137 }
138  
139 2 ▶sub remove_child_at {
Subroutine remove_child_at: called CC=3 SCAR=11.0
Pod documented
140 2 my ($self, $index) = @_;
141 2 ▶ $self->clear_height if $self->has_height;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_height 1 1 2
142 2 ▶ $self->clear_size if $self->has_size;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_size 1 1 2
143 2 my $child = splice @{ $self->children }, $index, 1;
144 2 $child->clear_parent;
145 2 $child;
146 }
147  
148 ##siblings
149  
150 2 ▶sub siblings {
Subroutine siblings: called CC=2 SCAR=7.0
Pod documented
151 2 my $self = shift;
152 2 ▶ return [] unless $self->has_parent;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_parent 0 2 2
153 2 [ grep { $self->uid ne $_->uid } @{ $self->parent->children } ];
154 }
155  
156 23 ▶sub get_index_in_siblings {
Subroutine get_index_in_siblings: called CC=2 SCAR=7.1
Pod documented
157 23 my ($self) = @_;
158 23 ▶ return -1 if $self->is_root;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_root 0 23 23
159  
160 23 $self->parent->get_child_index($self);
161 }
162  
163 ## cloning
164  
165 0 ▶sub clone_and_detach { shift->clone(@_) }
Subroutine clone_and_detach: not called CC=1 SCAR=6.9
Pod undocumented
166  
167 0 ▶sub to_pure_tree {
Subroutine to_pure_tree: not called CC=1 SCAR=6.9
Pod documented
168 0 my $self = shift;
169  
170 0 $self->reconstruct_with_class("Forest::Tree::Pure");
171 }
172  
173 0 ▶sub to_mutable_tree {
Subroutine to_mutable_tree: not called CC=1 SCAR=6.9
Pod documented
174 0 my $self = shift;
175  
176 0 return $self;
177 }
178  
179 15 __PACKAGE__->meta->make_immutable;
180  
181 15 ▶no Moose; 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
182  
183 __END__
« Forest/Tree/Writer/ASCIIWithBranches.pm ↑ Summary Forest/Tree/Pure.pm »