Markdent/Role/Event.pm

filter: Statementstmt 68.5% 74 / 108 Branchbran 38.8% 7 / 18 Conditioncond 40.0% 10 / 25 MC/DCmcdc 26.6% 4 / 15 Subroutinesub 81.2% 13 / 16 Podpod 100% 1 / 1 totaltotal 59.5% 109 / 183 CC 27 SCAR 34.0
CC
27
Cov
60%
debug_dump
110.0
__ANON__
9.1
_debug_value
6.0
CRAP 30.0
↑ Summary Markdent/CLI.pm »
1 package Markdent::Role::Event;
2  
3 35 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 35 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 35 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 35 our $VERSION = '0.39';
8  
9 35 ▶use Markdent::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 35 ▶use MooseX::Role::Parameterized;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 35 parameter event_class => (
14 is => 'ro',
15 isa => t('Str'),
16 required => 1,
17 );
18  
19 role {
20 1280 ▶ my $p = shift;
Subroutine __ANON__: called CC=9 SCAR=22.1
21  
22 1280 my $class = $p->event_class;
23 1280 my ( $action, $event ) = $class =~ /::(Start|End)?(\w+)$/;
24  
25 # It's easier to hack this in rather than trying to find a general
26 # case for upper-case abbreviations in class names.
27 1280 $event =~ s/HTML/html/;
28  
29 1280 ▶ $event =~ s/(^|.)([A-Z])/$1 ? "$1\L_$2" : "\L$2"/ge;
Branch: 1 branch
BranchTrueFalseTotal
$1 ? : 632 1035 1667
30  
31 1280 my $event_name = join q{_}, map {lc} grep {defined} $action, $event;
32 1280 ▶ method event_name => sub {$event_name};
Subroutine event_name: called CC=1 SCAR=0.0
33  
34 1280 ▶ method name => sub {$event};
Subroutine name: called CC=1 SCAR=0.0
35  
36 1280 ▶ my $is_start = ( $action || q{} ) eq 'Start';
Condition100%2/2
$action || ''
l!l
890390
MC/DC100%1/1
$action || ''
$action
Truth table100%2/2
A
A= $action
Aresult
11
00
37 1280 ▶ method is_start => sub {$is_start};
Subroutine is_start: called CC=1 SCAR=0.0
38  
39 1280 ▶ my $is_end = ( $action || q{} ) eq 'End';
Condition100%2/2
$action || ''
l!l
890390
MC/DC100%1/1
$action || ''
$action
Truth table100%2/2
A
A= $action
Aresult
11
00
40 1280 ▶ method is_end => sub {$is_end};
Subroutine is_end: called CC=1 SCAR=0.0
41  
42 1280 my $is_inline = !defined $action;
43 1280 ▶ method is_inline => sub {$is_inline};
Subroutine is_inline: not called CC=1 SCAR=1.2
44  
45 1280 my @required;
46 my @optional;
47  
48 1280 for my $attr ( grep { $_->name !~ /^_/ }
49 $class->meta->get_all_attributes ) {
50  
51 1090 my $name = $attr->name;
52  
53 1090 ▶ if ( $attr->is_required ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr->is_required) { } 793 297 1090
54 793 push @required, [ $name, $attr->get_read_method ];
55 }
56 else {
57 297 ▶ die
Branch: 1 branch
BranchTrueFalseTotal
unless $attr->has_predicate or $attr->has_default or $attr->has_builder 0 297 297
Condition50%3/6
$attr->has_predicate or $attr->has_default or $attr->has_builder
l!l&&r!l&&!r
29700
$attr->has_predicate or $attr->has_default
l!l&&r!l&&!r
1491480
MC/DC0%0/3
$attr->has_predicate or $attr->has_default or $attr->has_builder
$attr->has_predicate $attr->has_default $attr->has_builder
Truth table50%2/4
A or B or C
A= $attr->has_predicateB= $attr->has_defaultC= $attr->has_builder
ABCresult
1XX1
01X1
0011
0000
58 "All optional attributes for an event must have a predicate or default value ($class - $name)"
59 unless $attr->has_predicate
60 || $attr->has_default
61 || $attr->has_builder;
62  
63 297 push @optional,
64 [
65 $name,
66 $attr->get_read_method,
67 $attr->predicate
68 ];
69 }
70 }
71  
72 1280 _make_kv_pairs_for_attribute_method( \@required, \@optional );
73 35 };
74  
75 sub _make_kv_pairs_for_attribute_method {
76 1280 ▶ my @required = @{ shift() };
Subroutine _make_kv_pairs_for_attribute_method: called CC=1 SCAR=0.1
77 1280 my @optional = @{ shift() };
78  
79 method kv_pairs_for_attributes => sub {
80 5396 ▶ my $event = shift;
Subroutine kv_pairs_for_attributes: called CC=5 SCAR=17.0
Subroutine __ANON__: called CC=5 SCAR=17.0
81  
82 5396 my %p;
83  
84 5396 for my $pair (@required) {
85 3599 my ( $name, $reader ) = @{$pair};
86  
87 3599 $p{$name} = $event->$reader();
88 }
89  
90 5396 for my $triplet (@optional) {
91 1423 my ( $name, $reader, $pred ) = @{$triplet};
92  
93 1423 ▶ next if $pred && !$event->$pred();
Branch: 2 branches
BranchTrueFalseTotal
if $pred and not $event->$pred 56 1367 1423
if $pred and not $event->$pred 0 0 0
Condition50%3/6
$pred and not $event->$pred
!ll&&!rl&&r
13095856
$pred and not $event->$pred
!ll&&!rl&&r
000
MC/DC100%2/2
$pred and not $event->$pred
$pred not $event->$pred
MC/DC0%0/2
$pred and not $event->$pred
$pred not $event->$pred
Truth table100%3/3
A and not(B)
A= $predB= not $event->$pred
ABresult
0X0
100
111
Truth table0%0/3
A and not(B)
A= $predB= not $event->$pred
ABresult
0X0
100
111
94  
95 1367 $p{$name} = $event->$name();
96 }
97  
98 5396 return %p;
99 1280 };
100 }
101  
102 sub debug_dump {
103 0 ▶ my $self = shift;
Subroutine debug_dump: not called CC=10 SCAR=47.0
Pod documented
104  
105 0 my $dump = ' - ' . $self->event_name . "\n";
106  
107 0 for my $attr ( sort { $a->name cmp $b->name }
108 $self->meta->get_all_attributes ) {
109 0 my $name = $attr->name;
110 0 my $reader = $attr->get_read_method;
111 0 my $pred = $attr->predicate;
112  
113 0 ▶ next if $pred && !$self->$pred();
Branch: 1 branch
BranchTrueFalseTotal
if $pred and not $self->$pred 0 0 0
Condition0%0/3
$pred and not $self->$pred
!ll&&!rl&&r
000
MC/DC0%0/2
$pred and not $self->$pred
$pred not $self->$pred
Truth table0%0/3
A and not(B)
A= $predB= not $self->$pred
ABresult
0X0
100
111
114  
115 0 my $val = $self->$reader();
116  
117 0 ▶ if ( ref $val && ref $val eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $val and ref $val eq 'ARRAY') { } 0 0 0
elsif (ref $val and ref $val eq 'HASH') { } 0 0 0
Condition0%0/3
ref $val and ref $val eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $val and ref $val eq "ARRAY"
ref $val ref $val eq "ARRAY"
Truth table0%0/3
A and B
A= ref $valB= ref $val eq "ARRAY"
ABresult
0X0
100
111
118 0 $dump .= sprintf( ' %-16s: |%s|', $name, $val->[0] );
119 0 $dump .= "\n";
120  
121 0 for my $v ( @{$val}[ 1 .. $#{$val} ] ) {
122 0 $self->_debug_value($v);
123  
124 0 $dump .= q{ } x 22;
125 0 $dump .= "|$v|\n";
126 }
127 }
128 ▶ elsif ( ref $val && ref $val eq 'HASH' ) {
Condition0%0/3
ref $val and ref $val eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $val and ref $val eq "HASH"
ref $val ref $val eq "HASH"
Truth table0%0/3
A and B
A= ref $valB= ref $val eq "HASH"
ABresult
0X0
100
111
129 0 $dump .= sprintf( ' %-16s:', $name );
130 0 $dump .= "\n";
131  
132 0 for my $k ( sort keys %{$val} ) {
133 0 $dump .= q{ } x 22;
134 $dump .= sprintf(
135 '%-16s: %s', $k,
136 0 $self->_debug_value( $val->{$k} )
137 );
138 0 $dump .= "\n";
139 }
140 }
141 else {
142 0 $dump .= sprintf(
143 ' %-16s: %s', $name,
144 $self->_debug_value($val)
145 );
146 0 $dump .= "\n";
147 }
148 }
149  
150 0 return $dump;
151 }
152  
153 sub _debug_value {
154 0 ▶ return defined $_[1] ? $_[1] : '<undef>';
Subroutine _debug_value: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 0 0 0
155 }
156  
157 35 1;
158  
159 # ABSTRACT: Implements behavior shared by all events
160  
161 __END__
162  
163 =pod
164  
165 =encoding UTF-8
166  
167 =head1 NAME
168  
169 Markdent::Role::Event - Implements behavior shared by all events
170  
171 =head1 VERSION
172  
173 version 0.39
174  
175 =head1 DESCRIPTION
176  
177 This role provides shared behavior for all event classes. It is actually
178 somewhat of a hack, as it is a parameterized role that generates methods for
179 each class that consumes it.
180  
181 =head1 METHODS
182  
183 This role provides the following methods:
184  
185 =head2 $event->is_start
186  
187 =head2 $event->is_end
188  
189 =head2 $event->is_inline
190  
191 These all returns booleans indicating whether the event is of the specified
192 type.
193  
194 =head2 $event->event_name
195  
196 This returns a name like "start_blockquote", "end_strong", or "text".
197  
198 =head2 $event->kv_pairs_for_attributes
199  
200 This returns a hash representing the data stored in the object's
201 attributes. If an attribute is not required and has not been set, it will not
202 be present in the hash.
203  
204 =head2 $event->debug_dump
205  
206 Returns a string representation of the event suitable for debugging output.
207  
208 =head1 BUGS
209  
210 See L<Markdent> for bug reporting details.
211  
212 Bugs may be submitted at L<https://github.com/houseabsolute/Markdent/issues>.
213  
214 I am also usually active on IRC as 'autarch' on C<irc://irc.perl.org>.
215  
216 =head1 SOURCE
217  
218 The source code repository for Markdent can be found at L<https://github.com/houseabsolute/Markdent>.
219  
220 =head1 AUTHOR
221  
222 Dave Rolsky <autarch@urth.org>
223  
224 =head1 COPYRIGHT AND LICENSE
225  
226 This software is copyright (c) 2021 by Dave Rolsky.
227  
228 This is free software; you can redistribute it and/or modify it under
229 the same terms as the Perl 5 programming language system itself.
230  
231 The full text of the license can be found in the
232 F<LICENSE> file included with this distribution.
233  
234 =cut
↑ Summary Markdent/CLI.pm »