blib/lib/YATT/Lite/PageConsts.pm

filter: Statementstmt 94.9% 189 / 199 Branchbran 70.0% 56 / 80 Conditioncond 71.4% 25 / 35 MC/DCmcdc 50.0% 10 / 20 Subroutinesub 100% 39 / 39 Podpod 80.0% 12 / 15 totaltotal 85.3% 331 / 388 CC 56 SCAR 21.2
CC
56
Cov
86%
inject_into
25.9
locate_const
9.1
locate_page
8.7
CRAP 8.3
« blib/lib/YATT/Lite/PSGIEnv.pm ↑ Summary blib/lib/YATT/Lite/WebMVC0/DBSchema.pm »
1 package YATT::Lite::PageConsts;
2 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use warnings qw(FATAL all NONFATAL misc);
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 6 ▶use MOP4Import::Base::CLI_JSON -as_base
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 , [fields => qw(_pages _page_loc _file _line)];
7  
8 6 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 6 ▶use YATT::Lite::Util qw(globref);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 require YATT::Lite::MFields;
13  
14 # The instance registered by define_page(s). The receiver can be a class name,
15 # or an object created elsewhere (eg. by cli_run) which has no pages.
16 80 ▶sub _registered {
Subroutine _registered: called CC=3 SCAR=11.0
17 80 my ($self_or_class) = @_;
18 80 ▶ if (ref $self_or_class and $self_or_class->{_pages}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self_or_class and $self_or_class->{'_pages'}) { } 11 69 80
Condition100%3/3
ref $self_or_class and $self_or_class->{"_pages"}
!ll&&!rl&&r
67211
MC/DC100%2/2
ref $self_or_class and $self_or_class->{"_pages"}
ref $self_or_class $self_or_class->{"_pages"}
Truth table100%3/3
A and B
A= ref $self_or_classB= $self_or_class->{"_pages"}
ABresult
0X0
100
111
19 11 $self_or_class;
20 } else {
21 69 $self_or_class->instance;
22 }
23 }
24  
25 # The instance registered into $callpack itself (not inherited one),
26 # created and registered on the first call.
27 12 ▶sub _instance_for {
Subroutine _instance_for: called CC=2 SCAR=6.9
28 12 my ($pack, $callpack, $file, $line) = @_;
29 12 ▶ if (my $sub = *{globref($callpack, 'instance')}{CODE}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sub = *{globref($callpack, "instance");}{"CODE"}) 4 8 12
30 4 return $sub->();
31 }
32 8 my MY $self = $pack->new;
33 8 $self->{_file} = File::Spec->rel2abs($file);
34 8 $self->{_line} = $line;
35 8 $self->register_into($callpack);
36 8 $self;
37 }
38  
39 8 ▶sub define_page :MetaOnly {
Subroutine define_page: called CC=1 SCAR=0.0
Pod documented
40 8 my ($pack, $page, $consts) = @_;
41 8 my ($callpack, $file, $line) = caller;
42 8 my MY $self = $pack->_instance_for($callpack, $file, $line);
43 # Remember where each page is defined, for locate_page/locate_const.
44 8 $self->add_page($page, $consts, File::Spec->rel2abs($file), $line);
45 6 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 5 ▶sub define_pages_from_hash :MetaOnly {
Subroutine define_pages_from_hash: called CC=3 SCAR=11.0
Pod documented
48 5 my ($pack, $pages) = @_;
49 5 my ($callpack, $file, $line) = caller;
50 5 ▶ ref $pages eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
ref $pages eq "HASH" or Carp::croak("define_pages_from_hash takes a HASH ref of pages") 4 1 5
51 or Carp::croak("define_pages_from_hash takes a HASH ref of pages");
52 4 my MY $self = $pack->_instance_for($callpack, $file, $line);
53 # Locations of these pages are unknown; locate_page guesses them.
54 4 $self->add_page($_, $pages->{$_}) for sort keys %$pages;
55 4 $self;
56 6 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 24 ▶sub add_page {
Subroutine add_page: called CC=6 SCAR=18.3
Pod undocumented
59 24 (my MY $self, my ($page, $consts, $file, $line)) = @_;
60 24 ▶ ref $consts eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
ref $consts eq "HASH" or Carp::croak("Constants of page '${page}' must be a HASH ref") 24 0 24
61 or Carp::croak("Constants of page '$page' must be a HASH ref");
62 24 ▶ if ($self->{_pages}{$page}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_pages"}{$page}) 1 23 24
63 1 my $where = do {
64 1 ▶ if (my $loc = $self->{_page_loc}{$page}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $loc = $self->{'_page_loc'}{$page}) { } 1 0 1
65 1 " at $loc->[0] line $loc->[1]";
66 } else {
67 0 "";
68 }
69 };
70 1 ▶ my $redef = defined $file ? ", redefined at $file line $line" : "";
Branch: 1 branch
BranchTrueFalseTotal
defined $file ? : 1 0 1
71 1 die "Page '$page' is already defined$where$redef\n";
72 }
73 23 $self->{_pages}{$page} = $consts;
74 23 ▶ $self->{_page_loc}{$page} = [$file, $line] if defined $file;
Branch: 1 branch
BranchTrueFalseTotal
if defined $file 7 16 23
75 23 $self;
76 }
77  
78 8 ▶sub register_into :MetaOnly {
Subroutine register_into: called CC=1 SCAR=0.0
Pod undocumented
79 8 (my MY $self, my $pkg) = @_;
80 8 YATT::Lite::MFields->add_isa_to($pkg, MY);
81 8 ▶ *{globref($pkg, 'instance')} = sub { $self };
Subroutine __ANON__: called CC=1 SCAR=0.0
82 8 ▶ *{globref($pkg, 'import')} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
83 10 my $callpack = caller;
84 10 $self->inject_into($callpack, @_[1..$#_]);
85 8 };
86 6 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
87  
88 10 ▶sub inject_into {
Subroutine inject_into: called CC=18 SCAR=32.6
Pod documented
89 10 (my MY $self, my ($destpkg, $page, %opts)) = @_;
90 10 my $failok = delete $opts{missing_ok};
91 10 ▶ if (keys %opts) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %opts) 0 10 10
92 0 Carp::croak "Unknown options: ".join(", ", sort keys %opts);
93 }
94 10 ▶ $page ||= do {
Condition33%1/3
$page ||= do { if (my $sub = $self->can("page_name")) { &$sub($self); } }
l!l&&r!l&&!r
1000
MC/DC0%0/2
$page ||= do { if (my $sub = $self->can("page_name")) { &$sub($self); } }
$page do { if (my $sub = $self->can("page_name")) { &$sub($self); } }
Truth table33%1/3
A ||= B
A= $pageB= do { if (my $sub = $self->can("page_name")) { &$sub($self); } }
ABresult
1X1
011
000
95 0 ▶ if (my $sub = $self->can("page_name")) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sub = $self->can("page_name")) 0 0 0
96 0 $sub->($self)
97 }
98 };
99 10 ▶ unless (defined $page and $page ne '') {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $page and $page ne "") 0 10 10
Condition33%1/3
defined $page and $page ne ""
!ll&&!rl&&r
0010
MC/DC0%0/2
defined $page and $page ne ""
defined $page $page ne ""
Truth table33%1/3
A and B
A= defined $pageB= $page ne ""
ABresult
0X0
100
111
100 0 Carp::croak("page name is not specified");
101 }
102 10 ▶ my $vars = $self->find_consts($page)
Branch: 1 branch
BranchTrueFalseTotal
my $vars = $self->find_consts($page) or $failok or Carp::croak("No such page: $page") 10 0 10
Condition33%1/3
my $vars = $self->find_consts($page) or $failok
l!l&&r!l&&!r
1000
MC/DC0%0/2
my $vars = $self->find_consts($page) or $failok
my $vars = $self->find_consts($page) $failok
Truth table33%1/3
A or B
A= my $vars = $self->find_consts($page)B= $failok
ABresult
1X1
011
000
103 or $failok or Carp::croak("No such page: $page");
104  
105 # print STDERR "# injecting vars (@{[keys %$vars]})in $page\n"
106 # , YATT::Lite::Util::terse_dump($self);
107  
108 10 foreach my $name (keys %$vars) {
109 24 my $value = $vars->{$name};
110 24 ▶ if ($failok and ref $value and UNIVERSAL::can($value, 'varname')
Branch: 1 branch
BranchTrueFalseTotal
if ($failok and ref $value and UNIVERSAL::can($value, 'varname') and UNIVERSAL::can($value, 'value')) { } 3 21 24
Condition88%8/9
$failok and ref $value and UNIVERSAL::can($value, "varname") and UNIVERSAL::can($value, "value")
!ll&&!rl&&r
2103
$failok and ref $value and UNIVERSAL::can($value, "varname")
!ll&&!rl&&r
1743
$failok and ref $value
!ll&&!rl&&r
3147
MC/DC75%3/4
$failok and ref $value and UNIVERSAL::can($value, "varname") and UNIVERSAL::can($value, "value")
$failok ref $value UNIVERSAL::can($value, "varname") UNIVERSAL::can($value, "value")
Truth table80%4/5
A and B and C and D
A= $failokB= ref $valueC= UNIVERSAL::can($value, "varname")D= UNIVERSAL::can($value, "value")
ABCDresult
0XXX0
10XX0
110X0
11100
11111
111 and UNIVERSAL::can($value, 'value')) {
112 # For $failok case (== from yatt)
113 3 my $glob = globref($destpkg, $value->varname($name));
114 3 ▶ (*$glob) = map {ref $_ ? $_ : \ $_} $value->value;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 3 3
115 } else {
116 21 my $glob = globref($destpkg, $name);
117 21 *$glob = do {
118 21 ▶ if (not ref $value or ref $value eq 'ARRAY' or ref $value eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $value or ref $value eq 'ARRAY' or ref $value eq 'HASH') { } 18 3 21
Condition83%5/6
not ref $value or ref $value eq "ARRAY" or ref $value eq "HASH"
l!l&&r!l&&!r
1803
not ref $value or ref $value eq "ARRAY"
l!l&&r!l&&!r
1713
MC/DC66%2/3
not ref $value or ref $value eq "ARRAY" or ref $value eq "HASH"
not ref $value ref $value eq "ARRAY" ref $value eq "HASH"
Truth table75%3/4
not(A) or B or C
A= not ref $valueB= ref $value eq "ARRAY"C= ref $value eq "HASH"
ABCresult
1XX1
01X1
0011
0000
119 18 \ $value
120 } else {
121 3 $value;
122 }
123 };
124 # 関数の場合は、関数だけでなく、スカラ変数にも入れておく。
125 21 ▶ *$glob = \ $value if ref $value eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "CODE" 3 18 21
126 }
127 }
128 }
129  
130  
131 6 *find_vars = *find_consts;
132 6 *find_vars = *find_consts;
133 30 ▶sub find_consts :Doc(Find constant(s) for specified PAGE/NAME) {
Subroutine find_consts: called CC=3 SCAR=11.8
Pod documented
134 30 my MY $self = shift->_registered;
135 30 my ($page, $varname) = @_;
136 30 ▶ my $page_vars = $self->{_pages}{$page}
Branch: 1 branch
BranchTrueFalseTotal
my $page_vars = $self->{"_pages"}{$page} or return 30 0 30
137 or return;
138 30 ▶ if (not defined $varname) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $varname) { } 30 0 30
139 30 $page_vars;
140 } else {
141 0 $page_vars->{$varname};
142 }
143 6 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
144  
145 63 ▶sub const_type :Doc(Type of yatt variable for the constant VALUE) {
Subroutine const_type: called CC=6 SCAR=17.9
Pod documented
146 63 my ($self_or_class, $value) = @_;
147 63 ▶ if (not ref $value) {
Branch: 4 branches
BranchTrueFalseTotal
if (not ref $value) { } 36 27 63
elsif (ref $value eq 'ARRAY') { } 6 21 27
elsif (ref $value eq 'CODE') { } 10 11 21
elsif (UNIVERSAL::can($value, 'varname') and UNIVERSAL::can($value, 'value')) { } 10 1 11
148 36 'text';
149 } elsif (ref $value eq 'ARRAY') {
150 6 'list'
151 } elsif (ref $value eq 'CODE') {
152 10 'expr'
153 ▶ } elsif (UNIVERSAL::can($value, 'varname')
Condition66%2/3
UNIVERSAL::can($value, "varname") and UNIVERSAL::can($value, "value")
!ll&&!rl&&r
1010
MC/DC50%1/2
UNIVERSAL::can($value, "varname") and UNIVERSAL::can($value, "value")
UNIVERSAL::can($value, "varname") UNIVERSAL::can($value, "value")
Truth table66%2/3
A and B
A= UNIVERSAL::can($value, "varname")B= UNIVERSAL::can($value, "value")
ABresult
0X0
100
111
154 and UNIVERSAL::can($value, 'value')) {
155 10 'html';
156 } else {
157 1 'scalar';
158 }
159 6 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
160  
161 #========================================
162 # Locations in the source of the pagevars module (for LanguageServer).
163 # const_key_regexp and page_key_regexp can be overridden in the
164 # pagevars module to support local notations.
165  
166 49 ▶sub source_lines {
Subroutine source_lines: called CC=2 SCAR=7.1
Pod undocumented
167 49 (my MY $self, my $file) = @_;
168 49 ▶ open my $fh, '<', $file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or return 49 0 49
169 or return;
170 49 <$fh>;
171 }
172  
173 # "NAME =>" of a constant.
174 45 ▶sub const_key_regexp {
Subroutine const_key_regexp: called CC=1 SCAR=0.0
Pod documented
175 45 (my MY $self, my $name) = @_;
176 45 qr/(?:^|[\s,{(])(['"]?)\Q$name\E\1\s*=>/;
177 }
178  
179 # Key of PAGE in a hash of pages ("$PAGES{PAGE} =" or "PAGE =>").
180 # Only used for pages registered by define_pages_from_hash.
181 20 ▶sub page_key_regexp {
Subroutine page_key_regexp: called CC=1 SCAR=0.0
Pod documented
182 20 (my MY $self, my $page) = @_;
183 20 qr/(?:^|[\s,{(])(['"]?)\Q$page\E\1\s*(?:=>|\}\s*=)/;
184 }
185  
186 # The line which starts a define_page call.
187 60 ▶sub page_call_regexp {
Subroutine page_call_regexp: called CC=1 SCAR=0.0
Pod documented
188 60 (my MY $self) = @_;
189 60 qr/\bdefine_page\b/;
190 }
191  
192 # Block of PAGE in @$lines as 0-based ($start, $end), or empty list.
193 49 ▶sub page_block_range {
Subroutine page_block_range: called CC=6 SCAR=17.9
Pod documented
194 49 (my MY $self, my ($page, $lines)) = @_;
195 49 ▶ if (my $loc = $self->{_page_loc}{$page}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $loc = $self->{"_page_loc"}{$page}) 44 5 49
196 # From define_page of PAGE to just before the next define_page.
197 44 my ($file, $line) = @$loc;
198 50 my @recorded = sort {$a <=> $b}
199 44 map {$_->[1]} grep {$_->[0] eq $file} values %{$self->{_page_loc}};
200 44 my ($prev) = reverse grep {$_ < $line} @recorded;
201 44 my ($next) = grep {$_ > $line} @recorded;
202 44 ▶ my $start = $self->_page_call_start($lines, $line - 1, $prev // 0);
Condition100%2/2
$prev // 0
l!l
2816
MC/DC100%1/1
$prev // 0
$prev
Truth table100%2/2
A
A= $prev
Aresult
11
00
203 44 ▶ my $end = defined $next
Branch: 1 branch
BranchTrueFalseTotal
defined $next ? : 16 28 44
204 ? $self->_page_call_start($lines, $next - 1, $line) - 1
205 : $#$lines;
206 44 return ($start, $end);
207 }
208 5 my $page_re = $self->page_key_regexp($page);
209 5 ▶ my $start = first {$lines->[$_] =~ $page_re} 0 .. $#$lines;
Subroutine __ANON__: called CC=1 SCAR=0.0
210 5 ▶ return unless defined $start;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $start 0 5 5
211 15 my @other_re = map {$self->page_key_regexp($_)}
212 5 grep {$_ ne $page} keys %{$self->{_pages}};
213 22 ▶ my $end = first {
Subroutine __ANON__: called CC=1 SCAR=0.0
214 22 my $text = $lines->[$_];
215 22 grep {$text =~ $_} @other_re;
216 5 } $start+1 .. $#$lines;
217 5 ▶ ($start, defined $end ? $end - 1 : $#$lines);
Branch: 1 branch
BranchTrueFalseTotal
defined $end ? : 5 0 5
218 }
219  
220 # caller reports the line of the statement executed last before the call,
221 # which can be inside a block (map, grep, do) in the arguments.
222 # So look back (down to index $lower) for the line which starts the call.
223 60 ▶sub _page_call_start {
Subroutine _page_call_start: called CC=4 SCAR=14.5
224 60 (my MY $self, my ($lines, $i, $lower)) = @_;
225 60 my $call_re = $self->page_call_regexp;
226 60 ▶ for (my $j = $i; $j >= $lower; $j--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j >= $lower) 244 0 244
227 244 ▶ return $j if defined $lines->[$j] and $lines->[$j] =~ $call_re;
Branch: 1 branch
BranchTrueFalseTotal
if defined $lines->[$j] and $lines->[$j] =~ /$call_re/ 60 184 244
Condition66%2/3
defined $lines->[$j] and $lines->[$j] =~ /$call_re/
!ll&&!rl&&r
018460
MC/DC50%1/2
defined $lines->[$j] and $lines->[$j] =~ /$call_re/
defined $lines->[$j] $lines->[$j] =~ /$call_re/
Truth table66%2/3
A and B
A= defined $lines->[$j]B= $lines->[$j] =~ /$call_re/
ABresult
0X0
100
111
228 }
229 0 $i;
230 }
231  
232 3 ▶sub locate_page :Doc(Locate the definition of PAGE as file and line) {
Subroutine locate_page: called CC=5 SCAR=21.6
Pod documented
233 3 my MY $self = shift->_registered;
234 3 my ($page) = @_;
235 3 ▶ $self->{_pages}{$page}
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_pages"}{$page} or return 3 0 3
236 or return;
237 3 ▶ if (my $loc = $self->{_page_loc}{$page}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $loc = $self->{"_page_loc"}{$page}) 3 0 3
238 3 my ($start) = $self->page_block_range($page
239 , [$self->source_lines($loc->[0])]);
240 3 return ($loc->[0], $start + 1);
241 }
242 my $file = $self->{_file}
243 0 ▶ or return;
Branch: 1 branch
BranchTrueFalseTotal
my $file = $self->{"_file"} or return 0 0 0
244 0 my ($start) = $self->page_block_range($page, [$self->source_lines($file)]);
245 0 ▶ ($file, defined $start ? $start + 1 : $self->{_line});
Branch: 1 branch
BranchTrueFalseTotal
defined $start ? : 0 0 0
246 6 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
247  
248 # Looks for the key of NAME in the block of PAGE first, then anywhere
249 # in the file (for common values), then falls back to the page itself.
250 47 ▶sub locate_const :Doc(Locate the definition of constant NAME for PAGE as file and line) {
Subroutine locate_const: called CC=9 SCAR=22.1
Pod documented
251 47 my MY $self = shift->_registered;
252 47 my ($page, $name) = @_;
253 47 ▶ my $page_vars = $self->{_pages}{$page}
Branch: 1 branch
BranchTrueFalseTotal
my $page_vars = $self->{"_pages"}{$page} or return 47 0 47
254 or return;
255 47 ▶ exists $page_vars->{$name}
Branch: 1 branch
BranchTrueFalseTotal
exists $page_vars->{$name} or return 46 1 47
256 or return;
257 46 ▶ my $file = do {
Branch: 1 branch
BranchTrueFalseTotal
my $file = do { if (my $loc = $self->{"_page_loc"}{$page}) { $loc->[0]; } else { $self->{"_file"}; } } or return 46 0 46
258 46 ▶ if (my $loc = $self->{_page_loc}{$page}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $loc = $self->{'_page_loc'}{$page}) { } 41 5 46
259 41 $loc->[0];
260 } else {
261 5 $self->{_file};
262 }
263 } or return;
264 46 my @lines = $self->source_lines($file);
265 46 my $const_re = $self->const_key_regexp($name);
266  
267 46 ▶ if (my ($start, $end) = $self->page_block_range($page, \@lines)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $self->page_block_range($page, \@lines)) 46 0 46
268 46 foreach my $i ($start .. $end) {
269 160 ▶ return ($file, $i+1) if $lines[$i] =~ $const_re;
Branch: 1 branch
BranchTrueFalseTotal
if $lines[$i] =~ /$const_re/ 39 121 160
270 }
271 }
272 7 ▶ if (defined(my $i = first {$lines[$_] =~ $const_re} 0 .. $#lines)) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $i = &first(sub { $lines[$_] =~ /$const_re/; } , 0 .. $#lines))) 6 1 7
273 6 return ($file, $i+1);
274 }
275 1 $self->locate_page($page);
276 6 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
277  
278 6 ▶sub as_html {
Subroutine as_html: called CC=1 SCAR=0.0
Pod documented
279 6 my ($text) = @_;
280 6 bless \ $text, 'YATT::Lite::PageConsts::html';
281 }
282  
283 package
284 YATT::Lite::PageConsts::html;
285 6 ▶use overload '""' => 'value';
Subroutine BEGIN: called CC=1 SCAR=0.0
286 3 ▶sub varname {shift; 'html_'. shift}
Subroutine varname: called CC=1 SCAR=0.0
287 9 ▶sub value {${shift()}}
Subroutine value: called CC=1 SCAR=0.0
288  
289  
290 6 1;
« blib/lib/YATT/Lite/PSGIEnv.pm ↑ Summary blib/lib/YATT/Lite/WebMVC0/DBSchema.pm »