web/cgi-bin/yatt.lib/YATT/ArgMacro.pm

filter: Statementstmt 85.4% 147 / 172 Branchbran 66.6% 36 / 54 Conditioncond 43.4% 10 / 23 MC/DCmcdc 30.7% 4 / 13 Subroutinesub 92.3% 24 / 26 Podpod 0.0% 0 / 14 totaltotal 73.1% 221 / 302 CC 45 SCAR 23.1
CC
45
Cov
78%
create_from
56.0
spec_clone_with_renaming
10.5
spec_revert_into
7.5
CRAP 10.1
« web/cgi-bin/yatt.lib/YATT/Util/VarExporter.pm ↑ Summary blib/lib/YATT/Util/RLimit.pm »
1 # -*- mode: perl; coding: utf-8 -*-
2 3 ▶package YATT::ArgMacro; use YATT::Inc;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings qw(FATAL all NONFATAL misc);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use base qw(YATT::Class::Configurable);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use YATT::Util qw(checked_eval);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use YATT::Util::Symbol qw(globref fields_hash_of_class define_const);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use YATT::LRXML::Node qw(copy_array copy_node_renamed_as
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 create_node_from node_name node_size);
12  
13 3 ▶use YATT::Fields 'spec', [disabled => 0];
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 12 ▶sub initargs { qw(spec) }
Subroutine initargs: called CC=1 SCAR=0.0
Pod undocumented
16  
17 use YATT::Types
18 3 ▶ [Spec => [qw(cf_name cf_classname
Subroutine BEGIN: called CC=1 SCAR=0.0
19 cf_base
20 cf_in
21 cf_out
22 cf_edit
23 cf_rename_spec
24 cf_disabled
25 trigger
26 output
27 output_map
28 prototype
29 )]]
30 , [Slot => [qw(cf_name cf_classname cf_call_spec
31 cf_spec cf_mode cf_type cf_doc)]]
32 3 ;
33  
34 #========================================
35  
36 0 ▶sub handle {
Subroutine handle: not called CC=1 SCAR=6.9
Pod undocumented
37 0 my ($macro, $trans, $scope, $node, $widget) = @_;
38 0 $macro->accept($trans, $scope, $node, $widget);
39 0 $node;
40 }
41  
42 11 ▶sub expand_all_macros {
Subroutine expand_all_macros: called CC=5 SCAR=16.1
Pod undocumented
43 11 my ($pack, $trans, $scope, $node, $widget, $trigger, $order) = @_;
44 11 my $copy = $node->variant_builder;
45 11 $copy->add_filtered_copy($node->clone, [\&filter, $trigger, \ my %found]);
46 11 ▶ if (%found) {
Branch: 1 branch
BranchTrueFalseTotal
if (%found) { } 10 1 11
47 10 foreach my Spec $spec (@$order) {
48 12 ▶ my MY $macro = $found{$spec->refid} or next;
Branch: 1 branch
BranchTrueFalseTotal
my YATT::ArgMacro $macro = $found{$spec->refid} or next 12 0 12
49 # XXX: disabled だけれど、他にも config がある場合は、エラーにすべき。
50 12 ▶ if ($macro->{disabled}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($macro->{"disabled"}) 5 7 12
51 5 $spec->revert_into($copy, $macro);
52 5 next;
53 }
54 7 $copy = $macro->handle($trans, $scope, $copy, $widget);
55 }
56 10 $copy;
57 } else {
58 1 $node;
59 }
60 }
61  
62 18 ▶sub filter {
Subroutine filter: called CC=7 SCAR=19.6
Pod undocumented
63 18 my ($trigger, $unique, $name, $value) = @_;
64 18 ▶ if (my Slot $slot = $trigger->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my YATT::ArgMacro::Slot $slot = $trigger->{$name}) { } 17 1 18
65 # ここで、rename が関係する
66 my MY $macro = $unique->{$slot->{cf_spec}->refid}
67 17 ▶ ||= $slot->{cf_classname}->new($slot->{cf_spec});
Condition66%2/3
$$unique{$slot->{"cf_spec"}->refid} ||= $slot->{"cf_classname"}->new($slot->{"cf_spec"})
l!l&&r!l&&!r
5120
MC/DC0%0/2
$$unique{$slot->{"cf_spec"}->refid} ||= $slot->{"cf_classname"}->new($slot->{"cf_spec"})
$$unique{$slot->{"cf_spec"}->refid} $slot->{"cf_classname"}->new($slot->{"cf_spec"})
Truth table66%2/3
A ||= B
A= $$unique{$slot->{"cf_spec"}->refid}B= $slot->{"cf_classname"}->new($slot->{"cf_spec"})
ABresult
1X1
011
000
68 17 ▶ if ($macro->{disabled} or my $out = $slot->is_output) {
Branch: 1 branch
BranchTrueFalseTotal
if ($macro->{'disabled'} or my $out = $slot->is_output) { } 6 11 17
Condition100%3/3
$macro->{"disabled"} or my $out = $slot->is_output
l!l&&r!l&&!r
1511
MC/DC100%2/2
$macro->{"disabled"} or my $out = $slot->is_output
$macro->{"disabled"} my $out = $slot->is_output
Truth table100%3/3
A or B
A= $macro->{"disabled"}B= my $out = $slot->is_output
ABresult
1X1
011
000
69 # 出力引数が明示的に与えられていた場合は、disabled モードにする。
70 6 $macro->{disabled} = 1;
71 # 元の引数を残す
72 # rename 済みの override を返す。
73 6 ▶ unless ($out) {
Branch: 2 branches
BranchTrueFalseTotal
if ($out) { } 5 1 6
if (do { node_size($value) }) { } 5 0 5
74 1 copy_array($value);
75 } elsif (node_size($value)) {
76 # label_list=lh ===> label=lh
77 5 MY->copy_node_renamed_as($macro->output_name, $value);
78 } else {
79 # header_list ===> header=header_list
80 0 MY->create_node_from($value
81 , $macro->output_name
82 , node_name($value));
83 }
84 } else {
85 # text になってないと、不便では?
86 # ← でも、<:att>....</:att> の場合も有る。
87 11 $macro->configure($slot->{cf_name} => copy_array($value));
88 11 ();
89 }
90 } else {
91 1 copy_array($value);
92 }
93 }
94  
95 #========================================
96  
97 #
98 # use YATT::ArgMacro AM => out => ['name=type'], in => [qw(x y z ...)];
99 # => creates new class AM.
100 #
101 2 ▶sub import {
Subroutine import: called CC=2 SCAR=6.9
102 2 my ($pack, $macro_name) = splice @_, 0, 2;
103 2 my ($callpack) = caller;
104 2 my $class_name = "${callpack}::$macro_name";
105 2 my Spec $spec = Spec->new(name => $macro_name, classname => $class_name
106 , @_);
107  
108 2 ▶ my $base = $spec->{cf_base} || __PACKAGE__;
Condition50%1/2
$spec->{'cf_base'} || __PACKAGE__
l!l
02
MC/DC0%0/1
$spec->{'cf_base'} || __PACKAGE__
$spec->{'cf_base'}
Truth table50%1/2
A
A= $spec->{'cf_base'}
Aresult
11
00
109 2 my @fields = $spec->fields;
110  
111 2 my $script = <<END;
112 package $class_name;
113 use strict;
114 use base qw($base);
115 use YATT::Fields qw(@fields);
116  
117 sub $class_name () {'$class_name'}
118 END
119  
120 # print STDERR $script;
121 2 $pack->checked_eval($script);
122  
123 2 define_const(globref($class_name, 'macro_spec'), $spec);
124 }
125  
126 #
127 # Instanciate and register ArgMacro Spec in given widget's argument list.
128 #
129  
130 16 ▶sub register_in {
Subroutine register_in: called CC=5 SCAR=16.2
Pod undocumented
131 16 my ($pack, $registry, $node, $widget, $rename_spec) = @_;
132 16 my Spec $spec = $pack->macro_spec
133 ->clone_with_renaming($rename_spec, $registry, $node);
134  
135 16 my ($dict, $order) = $widget->macro_specs;
136 16 push @$order, $spec;
137  
138 16 ▶ foreach my Slot $slot ($spec->{output} ? $spec->{output}
Branch: 1 branch
BranchTrueFalseTotal
$spec->{'output'} ? : 9 7 16
139 7 : @{$spec->{cf_out}}) {
140 $widget->add_arg($slot->{cf_name} => $registry->create_var
141 23 ($slot->{cf_type}, undef, varname => $slot->{cf_name}));
142 }
143  
144 16 foreach my $name (keys %{$spec->{trigger}}) {
145 54 my Slot $slot = $spec->{trigger}{$name};
146 54 ▶ if (my Slot $old = $dict->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my YATT::ArgMacro::Slot $old = $dict->{$name}) 0 54 54
147 die $registry->node_error
148 ($node, "ArgMacro %s conflicts with %s for %s"
149 , $spec->call_spec(1)
150 0 , $old->{cf_call_spec}, $name);
151 }
152 54 $dict->{$name} = $slot;
153 }
154 }
155  
156 #
157 # Directly instanciate ArgMacro spec.
158 #
159  
160 0 ▶sub create_from {
Subroutine create_from: not called CC=7 SCAR=40.3
Pod undocumented
161 0 my ($pack, $trans, $scope, $orig, $rename_spec) = @_;
162 0 my Spec $spec = $pack->macro_spec
163 ->clone_with_renaming($rename_spec, $trans, $orig);
164  
165 0 my $copy = $orig->variant_builder;
166 0 my ($name, $slot, @config);
167 0 ▶ for (my $n = $orig->clone; $n->readable; $n->next) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n->readable) 0 0 0
168 0 ▶ unless ($n->is_attribute and $name = $n->node_name
Branch: 1 branch
BranchTrueFalseTotal
unless ($n->is_attribute and $name = $n->node_name and $slot = $spec->{"trigger"}{$name} and not $slot->is_output) 0 0 0
Condition0%0/9
$n->is_attribute and $name = $n->node_name and $slot = $spec->{"trigger"}{$name} and not $slot->is_output
!ll&&!rl&&r
000
$n->is_attribute and $name = $n->node_name and $slot = $spec->{"trigger"}{$name}
!ll&&!rl&&r
000
$n->is_attribute and $name = $n->node_name
!ll&&!rl&&r
000
MC/DC0%0/4
$n->is_attribute and $name = $n->node_name and $slot = $spec->{"trigger"}{$name} and not $slot->is_output
$n->is_attribute $name = $n->node_name $slot = $spec->{"trigger"}{$name} not $slot->is_output
Truth table0%0/5
A and B and C and not(D)
A= $n->is_attributeB= $name = $n->node_nameC= $slot = $spec->{"trigger"}{$name}D= not $slot->is_output
ABCDresult
0XXX0
10XX0
110X0
11100
11111
169 and $slot = $spec->{trigger}{$name}
170 and not $slot->is_output) {
171 0 $copy->add_node(copy_array($n->current));
172 0 next;
173 }
174 0 push @config, $slot->{cf_name} => $n->current;
175 }
176 0 ▶ if (@config) {
Branch: 1 branch
BranchTrueFalseTotal
if (@config) { } 0 0 0
177 0 my $macro = $pack->new($spec, @config);
178 0 $macro->accept($trans, $scope, $copy); # To avoid return value confusion.
179 0 ($macro, $copy)
180 } else {
181 0 (undef, $orig->rewind);
182 }
183 }
184  
185 #========================================
186  
187 3 foreach my $mode (qw(in out edit)) {
188 52 ▶ Spec->define("configure_$mode", sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
189 52 spec_configure_slot(shift, "cf_$mode", $mode, @_);
190 9 });
191 }
192  
193 # 分かった、これが use YATT::ArgMacro と clone の両方の configure
194 # から呼ばれる。一方では生の list, 他方では Spec の list だ。
195 52 ▶sub spec_configure_slot {
Subroutine spec_configure_slot: called CC=3 SCAR=11.0
Pod undocumented
196 52 (my Spec $spec, my ($name, $mode, $list)) = @_;
197 $spec->{$name} = [map {
198 52 Slot->create($_, mode => $mode, classname => $spec->{cf_classname})
199 54 } @$list];
200 52 ▶ if ($mode eq 'out' && @{$spec->{$name}} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "out" and @{$spec->{$name};} == 1) 10 42 52
Condition100%3/3
$mode eq "out" and @{$spec->{$name};} == 1
!ll&&!rl&&r
34810
MC/DC100%2/2
$mode eq "out" and @{$spec->{$name};} == 1
$mode eq "out" @{$spec->{$name};} == 1
Truth table100%3/3
A and B
A= $mode eq "out"B= @{$spec->{$name};} == 1
ABresult
0X0
100
111
201 # Only if 1 output var exists.
202 10 $spec->{output} = $spec->{$name}[0];
203 }
204 }
205  
206 3 Spec->define(fields => \&spec_fields);
207 2 ▶sub spec_fields {
Subroutine spec_fields: called CC=5 SCAR=16.3
Pod undocumented
208 2 my Spec $spec = shift;
209 2 ▶ if ($spec->{cf_base}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"cf_base"}) 0 2 2
210 0 croak "ArgMacro base= is not yet implemented";
211 }
212 2 my @fields;
213 2 foreach my $list ($spec->{cf_in}, $spec->{cf_edit}) {
214 4 foreach my Slot $slot (@$list) {
215 3 push @fields, 'cf_' . $slot->{cf_name};
216 }
217 }
218 2 foreach my Slot $slot (@{$spec->{cf_out}}) {
219 3 push @fields, 'out_' . $slot->{cf_name};
220 }
221 2 @fields;
222 }
223  
224 3 Spec->define(call_spec => \&spec_call_spec);
225 35 ▶sub spec_call_spec {
Subroutine spec_call_spec: called CC=4 SCAR=13.9
Pod undocumented
226 35 (my Spec $spec, my ($user)) = @_;
227 47 my @args = grep {defined $_}
228 35 ▶ map { ref $_ ? @$_ : $_ } $spec->{cf_rename_spec};
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 12 23 35
229 47 ▶ '%'.join("", grep {defined $_}
Branch: 2 branches
BranchTrueFalseTotal
$user ? : 16 19 35
@args ? : 12 23 35
230 ($user ? $spec->{cf_name} : $spec->{cf_classname})
231 35 , (@args ? ('('.join("=", @args).')') : ())).';';
232 }
233  
234 3 Spec->define(clone_with_renaming => \&spec_clone_with_renaming);
235 16 ▶sub spec_clone_with_renaming {
Subroutine spec_clone_with_renaming: called CC=10 SCAR=23.5
Pod undocumented
236 16 (my Spec $orig, my ($rename, $registry, $node)) = @_;
237 16 my Spec $new = $orig->clone(rename_spec => $rename);
238 16 $new->{prototype} = $orig;
239 16 $new->{trigger} = \ my %trigger;
240  
241 16 my ($prefix, $short_name, $from) = ('');
242 16 ▶ if ($rename) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rename) 6 10 16
243 die $registry->node_error($node, "ArgMacro: No output is defined")
244 6 ▶ unless $new->{cf_out} && @{$new->{cf_out}};
Branch: 1 branch
BranchTrueFalseTotal
unless $new->{"cf_out"} and @{$new->{"cf_out"};} 0 6 6
Condition33%1/3
$new->{"cf_out"} and @{$new->{"cf_out"};}
!ll&&!rl&&r
006
MC/DC0%0/2
$new->{"cf_out"} and @{$new->{"cf_out"};}
$new->{"cf_out"} @{$new->{"cf_out"};}
Truth table33%1/3
A and B
A= $new->{"cf_out"}B= @{$new->{"cf_out"};}
ABresult
0X0
100
111
245 die $registry->node_error($node, "ArgMacro: Can't rename multiple output")
246 6 ▶ if @{$new->{cf_out}} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @{$new->{"cf_out"};} > 1 0 6 6
247  
248 6 ▶ ($short_name, $from) = ref $rename ? @$rename : $rename;
Branch: 1 branch
BranchTrueFalseTotal
ref $rename ? : 6 0 6
249 6 $prefix = $short_name . '_';
250  
251 6 my Slot $orig = $new->{cf_out}[0];
252 6 $new->{output} = $orig->clone(name => $short_name);
253 6 $new->{output_map}{$orig->{cf_name}} = $short_name;
254 }
255  
256 16 my $call_spec = $new->call_spec(1);
257 16 foreach my $list (grep {defined $_}
258 $new->{cf_in}, $new->{cf_edit}, $new->{cf_out}) {
259 48 foreach my Slot $slot (@$list) {
260 $trigger{$prefix . $slot->{cf_name}}
261 48 = $slot->clone(spec => $new, call_spec => $call_spec);
262 }
263 }
264  
265 16 ▶ if ($from) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from) 6 10 16
266 6 ▶ unless (my Slot $major = $trigger{$prefix . $from}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my YATT::ArgMacro::Slot $major = $trigger{$prefix . $from}) { } 6 0 6
267 0 die $registry->node_error($node, "Unknown parameter: %s", $from);
268 } else {
269 6 $trigger{$short_name} = $major;
270 }
271 }
272 16 $new;
273 }
274  
275 3 Spec->define(revert_into => \&spec_revert_into);
276 5 ▶sub spec_revert_into {
Subroutine spec_revert_into: called CC=4 SCAR=20.1
Pod undocumented
277 5 (my Spec $spec, my $node, my MY $macro) = @_;
278 5 ▶ foreach my Slot $slot (map {ref $_ ? @$_ : ()} $spec->{cf_edit}) {
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 5 0 5
279 0 my $name = "cf_$slot->{cf_name}";
280 0 ▶ defined(my $expr = $macro->{$name})
Branch: 1 branch
BranchTrueFalseTotal
defined(my $expr = $macro->{$name}) or next 0 0 0
281 or next;
282 0 $node->add_node($expr);
283 }
284 }
285  
286 9 ▶sub output_name {
Subroutine output_name: called CC=1 SCAR=0.0
Pod undocumented
287 9 (my MY $macro) = @_;
288 9 my Spec $spec = $macro->{spec};
289 9 my Slot $out = $spec->{output};
290 9 $out->{cf_name};
291 }
292  
293 #========================================
294 3 Slot->define(create => \&slot_create);
295 54 ▶sub slot_create {
Subroutine slot_create: called CC=3 SCAR=11.1
Pod undocumented
296 54 my ($pack, $item, @rest) = @_;
297 54 my ($name, @args) = do {
298 54 ▶ unless (ref $item) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $item) { } 48 6 54
if (do { UNIVERSAL::isa($item, 'YATT::ArgMacro::Slot') }) { } 48 0 48
299 6 my ($n, $t) = split /=/, $item, 2;
300 6 ($n, type => $t);
301 } elsif (UNIVERSAL::isa($item, Slot)) {
302 48 return $item->clone;
303 } else {
304 0 @$item
305 }
306 };
307 6 $pack->new(name => $name, @args, @rest);
308 }
309  
310 3 Slot->define(is_output => \&slot_is_output);
311 16 ▶sub slot_is_output {
Subroutine slot_is_output: called CC=1 SCAR=0.0
Pod undocumented
312 16 my Slot $slot = shift;
313 16 $slot->{cf_mode} eq 'out';
314 }
315  
316 3 1;
« web/cgi-bin/yatt.lib/YATT/Util/VarExporter.pm ↑ Summary blib/lib/YATT/Util/RLimit.pm »