blib/lib/YATT/Lite/Types.pm

filter: Statementstmt 95.4% 104 / 109 Branchbran 72.2% 26 / 36 Conditioncond 88.2% 15 / 17 MC/DCmcdc 75.0% 6 / 8 Subroutinesub 100% 16 / 16 Podpod 0.0% 0 / 6 totaltotal 86.9% 167 / 192 CC 31 SCAR 25.3
CC
31
Cov
90%
buildns
23.1
parse_desc
6.2
create
4.0
CRAP 12.6
« scripts/libdir.pl ↑ Summary blib/lib/YATT/Lite/LRXML.pm »
1 package YATT::Lite::Types;
2 57 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 57 ▶use warnings qw(FATAL all NONFATAL misc);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 57 ▶use parent qw(YATT::Lite::Object);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 57 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 57 require YATT::Lite::Inc;
7  
8 sub Desc () {'YATT::Lite::Types::TypeDesc'}
9 {
10 package YATT::Lite::Types::TypeDesc; sub Desc () {__PACKAGE__}
11 57 ▶ use parent qw(YATT::Lite::Object);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 57 ▶ BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
13 57 our %FIELDS = map {$_ => 1}
14 qw/name ns fields overloads alias base eval
15 _fullname
16 no_require
17 constants export_default/
18 }
19 548 ▶ sub pkg {
Subroutine pkg: called CC=1 SCAR=0.0
20 548 my Desc $self = shift;
21 548 join '::', $self->{ns}, $self->{name};
22 }
23 }
24  
25 57 ▶use YATT::Lite::Util qw(globref look_for_globref lexpand ckeval pkg2pm
Subroutine BEGIN: called CC=1 SCAR=0.0
26 define_const
27 57 );
28  
29 208 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
30 208 my $pack = shift;
31 208 my $callpack = caller;
32 208 $pack->buildns($callpack, @_)
33 }
34  
35 208 ▶sub create {
Subroutine create: called CC=4 SCAR=13.9
Pod undocumented
36 208 my $pack = shift;
37 208 my $callpack = shift;
38 208 my Desc $root = $pack->Desc->new(ns => $callpack);
39 208 ▶ while (@_ >= 2 and not ref $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ >= 2 and not ref $_[0]) 2 208 210
Condition100%3/3
@_ >= 2 and not ref $_[0]
!ll&&!rl&&r
155532
MC/DC100%2/2
@_ >= 2 and not ref $_[0]
@_ >= 2 not ref $_[0]
Truth table100%3/3
A and not(B)
A= @_ >= 2B= not ref $_[0]
ABresult
0X0
100
111
40 2 $root->configure(splice @_, 0, 2);
41 }
42 208 ▶ wantarray ? ($root, $pack->parse_desc($root, @_)) : $root;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 208 0 208
43 }
44  
45 208 ▶sub buildns {
Subroutine buildns: called CC=22 SCAR=31.4
Pod undocumented
46 208 (my Desc $root, my @desc) = shift->create(@_);
47 208 my $debug = $ENV{DEBUG_YATT_TYPES};
48 208 my (@script, @task);
49 208 my $export_ok = do {
50 208 my $sym = globref($$root{ns}, 'EXPORT_OK');
51 208 ▶ *{$sym}{ARRAY} // (*$sym = []);
Condition100%2/2
*{$sym;}{"ARRAY"} // (*$sym = [])
l!l
45163
MC/DC100%1/1
*{$sym;}{"ARRAY"} // (*$sym = [])
*{$sym;}{"ARRAY"}
Truth table100%2/2
A
A= *{$sym;}{"ARRAY"}
Aresult
11
00
52 };
53 208 ▶ if (my $sub = $$root{ns}->can('export_ok')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sub = $root->{"ns"}->can("export_ok")) 51 157 208
54 51 push @$export_ok, $sub->($$root{ns});
55 }
56 {
57 208 my $sym = globref($$root{ns}, 'export_ok');
58 208 ▶ *$sym = sub { @$export_ok } unless *{$sym}{CODE};
Subroutine __ANON__: called CC=1 SCAR=0.1
Branch: 1 branch
BranchTrueFalseTotal
unless *{$sym;}{"CODE"} 208 0 208
59 }
60 208 foreach my Desc $obj (@desc) {
61 909 push @$export_ok, $obj->{name};
62 909 $obj->{_fullname} = join '::', $$root{ns}, $obj->{name};
63 909 $INC{pkg2pm($obj->{_fullname})} = 1; # To make require happy.
64 909 push @script, qq|package $obj->{_fullname};|;
65 909 push @script, q|use YATT::Lite::Inc;|;
66 my $base = $obj->{base} || $root->{base}
67 || safe_invoke($$root{ns}, $obj->{name})
68 909 ▶ || 'YATT::Lite::Object';
Condition100%8/8
$obj->{'base'} || $root->{'base'} || safe_invoke($root->{'ns'}, $obj->{'name'}) || 'YATT::Lite::Object'
l!l
654255
$obj->{"base"} or $root->{"base"} or safe_invoke($root->{"ns"}, $obj->{"name"})
l!l&&r!l&&!r
6504255
$obj->{"base"} or $root->{"base"}
l!l&&r!l&&!r
6464259
MC/DC100%3/3
$obj->{'base'} || $root->{'base'} || safe_invoke($root->{'ns'}, $obj->{'name'}) || 'YATT::Lite::Object'
$obj->{"base"} $root->{"base"} safe_invoke($root->{"ns"}, $obj->{"name"})
Truth table100%4/4
A or B or C
A= $obj->{"base"}B= $root->{"base"}C= safe_invoke($root->{"ns"}, $obj->{"name"})
ABCresult
1XX1
01X1
0011
0000
69 #
70 # I finally found base::has_fields() is broken
71 # so there is no merit for fields mania to use base.pm over parent.pm.
72 #
73 909 push @script, sprintf q|use parent qw(%s);|, $base;
74 909 push @script, sprintf q|use YATT::Lite::MFields %s;|, do {
75 909 ▶ if ($obj->{fields}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->{'fields'}) { } 713 196 909
76 713 sprintf(q|qw(%s)|, join " ", @{$obj->{fields}});
77 } else {
78 # To avoid generating 'use YATT::Lite::MFields qw()';
79 196 '';
80 }
81 };
82 push @script, sprintf q|use overload qw(%s);|
83 909 ▶ , join " ", @{$obj->{overloads}} if $obj->{overloads};
Branch: 1 branch
BranchTrueFalseTotal
if $obj->{"overloads"} 0 909 909
84 909 ▶ push @script, $obj->{eval} if $obj->{eval};
Branch: 1 branch
BranchTrueFalseTotal
if $obj->{"eval"} 50 859 909
85 909 push @script, "\n";
86  
87 909 push @task, [\&add_alias, $$root{ns}, $obj->{name}, $obj->{name}];
88 909 foreach my $alias (lexpand($obj->{alias})) {
89 149 push @task, [\&add_alias, $$root{ns}, $alias, $obj->{name}];
90 149 push @$export_ok, $alias;
91 }
92 909 foreach my $spec (lexpand($obj->{constants})) {
93 394 push @task, [\&add_const, $obj->{_fullname}, @$spec];
94 }
95 }
96 208 my $script = join(" ", @script, "; 1");
97 208 ▶ print $script, "\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 208 208
98 208 ckeval($script);
99 208 foreach my $task (@task) {
100 1452 my ($sub, @args) = @$task;
101 1452 $sub->(@args);
102 }
103 208 ▶ if ($root->{export_default}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($root->{"export_default"}) 1 207 208
104 1 my $export = do {
105 1 my $sym = globref($$root{ns}, 'EXPORT');
106 1 ▶ *{$sym}{ARRAY} // (*$sym = []);
Condition50%1/2
*{$sym;}{"ARRAY"} // (*$sym = [])
l!l
01
MC/DC0%0/1
*{$sym;}{"ARRAY"} // (*$sym = [])
*{$sym;}{"ARRAY"}
Truth table50%1/2
A
A= *{$sym;}{"ARRAY"}
Aresult
11
00
107 };
108 1 @$export = @$export_ok;
109 }
110 208 foreach my Desc $obj (@desc) {
111 909 my $sym = look_for_globref($obj->{_fullname}, 'FIELDS');
112 909 ▶ if ($sym and my $fields = *{$sym}{HASH}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($sym and my $fields = *{$sym;}{'HASH'}) { } 909 0 909
elsif ($obj->{'fields'}) { } 0 0 0
Condition50%1/2
$sym and my $fields = *{$sym;}{"HASH"}
!ll
0909
MC/DC0%0/1
$sym and my $fields = *{$sym;}{"HASH"}
$sym
Truth table50%1/2
A
A= $sym
Aresult
00
11
113 909 ▶ print "Fields in type $obj->{_fullname}: "
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 909 909
114 , join(" ", sort keys %$fields), "\n" if $debug;
115 } elsif ($obj->{fields}) {
116 croak "Failed to define type fields for '$obj->{_fullname}': "
117 0 . join(" ", @{$obj->{fields}});
118 }
119 }
120 }
121  
122 1058 ▶sub add_alias {
Subroutine add_alias: called CC=1 SCAR=0.0
Pod undocumented
123 1058 my ($pack, $alias, $name) = @_;
124 1058 add_const($pack, $alias, join('::', $pack, $name));
125 }
126  
127 1452 ▶sub add_const {
Subroutine add_const: called CC=1 SCAR=0.0
Pod undocumented
128 1452 my ($pack, $alias, $const) = @_;
129 1452 define_const(globref($pack, $alias), $const);
130 }
131  
132 259 ▶sub safe_invoke {
Subroutine safe_invoke: called CC=2 SCAR=6.9
Pod undocumented
133 259 my ($obj, $method) = splice @_, 0, 2;
134 259 ▶ my $sub = $obj->can($method)
Branch: 1 branch
BranchTrueFalseTotal
my $sub = $obj->can($method) or return 4 255 259
135 or return;
136 4 $sub->($obj, @_);
137 }
138  
139 1117 ▶sub parse_desc {
Subroutine parse_desc: called CC=6 SCAR=18.3
Pod undocumented
140 1117 (my $pack, my Desc $parent) = splice @_, 0, 2;
141 1117 my (@desc);
142 1117 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2214 1117 3331
143 2214 ▶ unless (defined (my $item = shift)) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined(my $item = shift())) { } 2214 0 2214
if (do { ref $item }) { } 909 1305 2214
elsif (@_) { } 1305 0 1305
144 0 croak "Undefined type desc!";
145 } elsif (ref $item) {
146 909 ▶ my @base = (base => $parent->pkg) if $parent->{name};
Branch: 1 branch
BranchTrueFalseTotal
if $parent->{"name"} 548 361 909
147 push @desc, my Desc $sub = $pack->Desc->new
148 909 (name => shift @$item, ns => $parent->{ns}, @base);
149 909 push @desc, $pack->parse_desc($sub, @$item);
150 } elsif (@_) {
151 1305 $item =~ s/^-//;
152 1305 $parent->configure($item, shift);
153 } else {
154 0 croak "Missing parameter for type desc $item";
155 }
156 }
157 1117 @desc;
158 }
159  
160 57 1;
161  
162 __END__
« scripts/libdir.pl ↑ Summary blib/lib/YATT/Lite/LRXML.pm »