lib/PXML/Element.pm

filter: Statementstmt 87.7% 93 / 106 Branchbran 56.8% 25 / 44 Conditioncond 20.0% 1 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 85.2% 29 / 34 Podpod 0.0% 0 / 19 totaltotal 70.1% 148 / 211 CC 25 SCAR 12.7
CC
25
Cov
77%
_text
9.8
attributes
6.0
name_set
6.0
CRAP 3.6
« meta/FunctionalPerl/Dependencies/ChjBin.pm ↑ Summary .htmlgen/FunctionalPerl/Htmlgen/Nav.pm »
1 #
2 # Copyright (c) 2013-2020 Christian Jaeger, copying@christianjaeger.ch
3 #
4 # This is free software, offered under either the same terms as perl 5
5 # or the terms of the Artistic License version 2 or the terms of the
6 # MIT License (Expat version). See the file COPYING.md that came
7 # bundled with this file.
8 #
9  
10 =head1 NAME
11  
12 PXML::Element - base class for PXML elements
13  
14 =head1 SYNOPSIS
15  
16 =head1 DESCRIPTION
17  
18  
19 =head1 NOTE
20  
21 This is alpha software! Read the status section in the package README
22 or on the L<website|http://functional-perl.org/>.
23  
24 =cut
25  
26 package PXML::Element;
27  
28 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 8 ▶use warnings FATAL => 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 8 ▶use FP::Stream qw(stream_mixed_flatten stream_map);
Subroutine BEGIN: called CC=1 SCAR=0.0
33 8 ▶use FP::Hash qw($empty_hash hash_set);
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 # XX Depend on these? PXML::Serialize uses these, so any app that
36 # serializes PXML would require them anyway.
37 8 ▶use FP::Lazy;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 8 ▶use FP::List;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 8 ▶use Chj::xIO qw(capture_stdout);
Subroutine BEGIN: called CC=1 SCAR=0.0
40 8 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 8 ▶use Chj::NamespaceCleanAbove;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 8 ▶use FP::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: not called CC=1 SCAR=0.0
44  
45 # [ name, attributes, body ]
46 BEGIN {
47 sub NAME () {0}
48 sub ATTRIBUTES () {1}
49 sub BODY () {2}
50 }
51  
52 282 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod undocumented
53 282 my $cl = shift;
54 282 ▶ @_ == 3 or fp_croak_arity 3;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or fp_croak_arity(3) 282 0 282
55 282 bless [@_], $cl
56 }
57  
58 382 ▶sub name {
Subroutine name: called CC=1 SCAR=0.0
Pod undocumented
59 382 $_[0][NAME]
60 }
61  
62 0 ▶sub lcname {
Subroutine lcname: not called CC=1 SCAR=6.9
Pod undocumented
63 0 lc($_[0][NAME])
64 }
65  
66 244 ▶sub maybe_attributes {
Subroutine maybe_attributes: called CC=1 SCAR=0.0
Pod undocumented
67 244 $_[0][ATTRIBUTES]
68 }
69  
70 # NOTE that $empty_hash gives exceptions for accesses to any field!
71 # (With the current implementation of locked / const hashes in perl.)
72 # `exists` works as expected though. But then, PXML (as all of FP) may
73 # move to restricted hashes and arrays everywhere anyway, so this
74 # should be consistent then.
75 0 ▶sub attributes {
Subroutine attributes: not called CC=2 SCAR=17.9
Pod undocumented
76 0 ▶ $_[0][ATTRIBUTES] // $empty_hash
Condition0%0/3
$_[0][1] // $PXML::Element::empty_hash
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[0][1] // $PXML::Element::empty_hash
$_[0][1] $PXML::Element::empty_hash
Truth table0%0/3
A // B
A= $_[0][1]B= $PXML::Element::empty_hash
ABresult
1X1
011
000
77 }
78  
79 296 ▶sub body {
Subroutine body: called CC=1 SCAR=0.0
Pod undocumented
80  
81 # could be undef, too, but then undef is the empty list when
82 # interpreted as a FP::List, thus no need for the maybe_
83 # prefix. -- XXX that's not true anymore, null is not undef anymore.
84 296 $_[0][BODY]
85 }
86  
87 26 ▶sub maybe_attribute {
Subroutine maybe_attribute: called CC=3 SCAR=11.0
Pod undocumented
88 26 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 26 0 26
89 26 my $s = shift;
90 26 my ($name) = @_;
91 26 ▶ defined($$s[ATTRIBUTES]) ? $$s[ATTRIBUTES]{$name} : undef
Branch: 1 branch
BranchTrueFalseTotal
defined $s->[1] ? : 25 1 26
92 }
93  
94 7 ▶sub perhaps_attribute {
Subroutine perhaps_attribute: called CC=4 SCAR=15.2
Pod undocumented
95 7 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 7 0 7
96 7 my $s = shift;
97 7 my ($name) = @_;
98 7 ▶ if (defined(my $h = $$s[ATTRIBUTES])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $h = $s->[1])) { } 7 0 7
99 7 ▶ exists $$h{$name} ? $$h{$name} : ()
Branch: 1 branch
BranchTrueFalseTotal
exists $h->{$name} ? : 0 7 7
100 } else {
101 ()
102 0 }
103 }
104  
105 # functional setters (following the convention I've started to use of
106 # "trailing _set means functional, leading set_ means mutation"))
107  
108 0 ▶sub name_set {
Subroutine name_set: not called CC=2 SCAR=17.9
Pod undocumented
109 0 my $s = shift;
110 0 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 0 0 0
111 0 bless [$_[0], $$s[ATTRIBUTES], $$s[BODY]], ref $s
112 }
113  
114 2 ▶sub attributes_set {
Subroutine attributes_set: called CC=2 SCAR=7.1
Pod undocumented
115 2 my $s = shift;
116 2 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 2 0 2
117 2 bless [$$s[NAME], $_[0], $$s[BODY]], ref $s
118 }
119  
120 3 ▶sub attribute_set {
Subroutine attribute_set: called CC=3 SCAR=11.4
Pod undocumented
121 3 my $s = shift;
122 3 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 3 0 3
123 3 my ($nam, $v) = @_;
124 3 ▶ bless [$$s[NAME], hash_set($$s[1] // {}, $nam, $v), $$s[BODY]], ref $s
Condition50%1/2
$s->[1] // {}
l!l
30
MC/DC0%0/1
$s->[1] // {}
$s->[1]
Truth table50%1/2
A
A= $s->[1]
Aresult
11
00
125 }
126  
127 23 ▶sub body_set {
Subroutine body_set: called CC=2 SCAR=7.1
Pod undocumented
128 23 my $s = shift;
129 23 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 23 0 23
130 23 bless [$$s[NAME], $$s[ATTRIBUTES], $_[0]], ref $s
131 }
132  
133 # functional updaters
134  
135 2 ▶sub name_update {
Subroutine name_update: called CC=2 SCAR=7.0
Pod undocumented
136 2 my $s = shift;
137 2 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 2 0 2
138 2 my ($fn) = @_;
139 2 bless [&$fn($$s[NAME]), $$s[ATTRIBUTES], $$s[BODY]], ref $s
140 }
141  
142 0 ▶sub attributes_update {
Subroutine attributes_update: not called CC=2 SCAR=17.9
Pod undocumented
143 0 my $s = shift;
144 0 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 0 0 0
145 0 my ($fn) = @_;
146 0 bless [$$s[NAME], &$fn($$s[ATTRIBUTES]), $$s[BODY]], ref $s
147 }
148  
149 50 ▶sub body_update {
Subroutine body_update: called CC=2 SCAR=7.0
Pod undocumented
150 50 my $s = shift;
151 50 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 50 0 50
152 50 my ($fn) = @_;
153 50 bless [$$s[NAME], $$s[ATTRIBUTES], &$fn($$s[BODY])], ref $s
154 }
155  
156 # mapping
157  
158 35 ▶sub body_map {
Subroutine body_map: called CC=2 SCAR=7.0
Pod undocumented
159 35 my $s = shift;
160 35 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 35 0 35
161 35 my ($fn) = @_;
162 35 ▶ $s->body_update(sub { stream_map $fn, stream_mixed_flatten $_[0] })
Subroutine __ANON__: called CC=1 SCAR=0.0
163 35 }
164  
165 # "body text", a string, dropping tags; not having knowledge about
166 # which XML tags have 'relevant body text', this returns all of it.
167  
168 # XX ugly: this is replicating part of the serializer. But don't want
169 # to touch the code there... so, here goes. Really, better languages
170 # have been created to write code in.
171  
172 78 ▶sub _text {
Subroutine _text: called CC=9 SCAR=22.8
173 78 my ($v) = @_;
174 78 ▶ if (defined $v) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $v) { } 77 1 78
175 77 ▶ if (length ref $v) {
Branch: 1 branch
BranchTrueFalseTotal
if (length ref $v) { } 45 32 77
176 45 ▶ if (defined blessed $v) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined builtin::blessed($v)) { } 27 18 45
177 27 ▶ if ($v->isa("PXML::Element")) {
Branch: 3 branches
BranchTrueFalseTotal
if ($v->isa('PXML::Element')) { } 2 25 27
elsif ($v->isa('FP::Abstract::Sequence')) { } 13 12 25
elsif (is_promise($v)) { } 12 0 12
178 2 $v->text
179 } elsif ($v->isa("FP::Abstract::Sequence")) {
180 13 join("", map { _text($_) } $v->values)
181 } elsif (is_promise $v) {
182 12 _text(force $v);
183 } else {
184 0 die "don't know how to get text of: $v";
185 }
186 } else {
187 18 ▶ if (UNIVERSAL::isa($v, "ARRAY")) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($v, 'ARRAY')) { } 18 0 18
elsif (UNIVERSAL::isa($v, 'CODE')) { } 0 0 0
188 18 join("", map { _text($_) } @$v);
189 } elsif (UNIVERSAL::isa($v, "CODE")) {
190  
191 # correct? XX why does A(string_to_stream("You're
192 # great."))->text trigger this case?
193 0 _text(&$v());
194 } else {
195 0 die "don't know how to get text of: $v";
196 }
197 }
198 } else {
199 32 $v
200 }
201 } else {
202 1 ""
203 }
204 }
205  
206 18 ▶sub text {
Subroutine text: called CC=1 SCAR=0.0
Pod undocumented
207 18 my $s = shift;
208 18 _text($s->body)
209 }
210  
211 # only for debugging? Doesn't emit XML/XHTML prologues! Also, ugly
212 # monkey-access to PXML::Serialize. Circular dependency, too.
213  
214 44 ▶sub string {
Subroutine string: called CC=1 SCAR=0.0
Pod undocumented
215 44 my $s = shift;
216 44 require PXML::Serialize;
217 44 ▶ capture_stdout {
Subroutine __ANON__: called CC=1 SCAR=0.0
218 44 PXML::Serialize::pxml_print_fragment_fast($s, *STDOUT);
219 }
220 44 }
221  
222 # XML does not distinguish between void elements and non-void ones in
223 # its syntactical representation; whether an element is printed in
224 # self-closing representation is orthogonal and can rely simply on
225 # whether the content of the particular element ('at runtime') is
226 # empty.
227  
228 4 ▶sub require_printing_nonvoid_elements_nonselfreferential {
Subroutine require_printing_nonvoid_elements_nonselfreferential: called CC=1 SCAR=0.0
Pod undocumented
229 4 0
230 }
231  
232 #sub void_element_h {
233 # undef
234 #}
235  
236 8 _END_
« meta/FunctionalPerl/Dependencies/ChjBin.pm ↑ Summary .htmlgen/FunctionalPerl/Htmlgen/Nav.pm »