blib/lib/Zuzu/Util/NativeHelpers.pm

filter: Statementstmt 84.1% 106 / 126 Branchbran 68.9% 40 / 58 Conditioncond 67.2% 39 / 58 MC/DCmcdc 37.5% 12 / 32 Subroutinesub 100% 19 / 19 Podpod 0.0% 0 / 7 totaltotal 72.0% 216 / 300 CC 58 SCAR 33.3
CC
58
Cov
76%
zuzu_to_perl
69.3
perl_to_zuzu
12.5
native_class
8.1
CRAP 27.9
« blib/lib/Zuzu/Module/YAML.pm ↑ Summary blib/lib/Zuzu/Runtime/Async/Scheduler.pm »
1 package Zuzu::Util::NativeHelpers;
2  
3 63 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 63 our $VERSION = '0.007002';
6  
7 63 ▶use Exporter qw( import );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 63 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 63 ▶use Zuzu::Value::Array;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 63 ▶use Zuzu::Value::Boolean;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 63 ▶use Zuzu::Value::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 63 ▶use Zuzu::Value::Dict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 63 ▶use Zuzu::Value::Function;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 63 ▶use Zuzu::Value::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 63 ▶use Zuzu::Value::PairList;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 63 ▶use Zuzu::Value::Set;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 63 ▶use Zuzu::Value::Bag;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 63 our @EXPORT_OK = qw(
21 native_class
22 native_function
23 native_functions
24 native_object
25 zuzu_to_perl
26 perl_to_zuzu
27 zuzu_bool
28 );
29  
30 3418 ▶sub native_class {
Subroutine native_class: called CC=7 SCAR=21.0
Pod undocumented
31 3418 my ( %args ) = @_;
32  
33 return Zuzu::Value::Class->new(
34 name => $args{name},
35 parent => $args{parent},
36 traits => $args{traits} // [],
37 field_specs => $args{field_specs} // [],
38 methods => $args{methods} // {},
39 trait_methods => $args{trait_methods} // {},
40 static_methods => $args{static_methods} // {},
41 nested_classes => $args{nested_classes} // {},
42 closure_env => $args{closure_env},
43 builtin_kind => $args{builtin_kind},
44 3418 ▶ );
Condition66%8/12
$args{"traits"} // []
l!l
03418
$args{"field_specs"} // []
l!l
03418
$args{"methods"} // {}
l!l
303388
$args{"trait_methods"} // {}
l!l
03418
$args{"static_methods"} // {}
l!l
483370
$args{"nested_classes"} // {}
l!l
03418
MC/DC0%0/1
$args{"traits"} // []
$args{"traits"}
MC/DC0%0/1
$args{"field_specs"} // []
$args{"field_specs"}
MC/DC100%1/1
$args{"methods"} // {}
$args{"methods"}
MC/DC0%0/1
$args{"trait_methods"} // {}
$args{"trait_methods"}
MC/DC100%1/1
$args{"static_methods"} // {}
$args{"static_methods"}
MC/DC0%0/1
$args{"nested_classes"} // {}
$args{"nested_classes"}
Truth table50%1/2
A
A= $args{"traits"}
Aresult
11
00
Truth table50%1/2
A
A= $args{"field_specs"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"methods"}
Aresult
11
00
Truth table50%1/2
A
A= $args{"trait_methods"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"static_methods"}
Aresult
11
00
Truth table50%1/2
A
A= $args{"nested_classes"}
Aresult
11
00
45 }
46  
47 50716 ▶sub native_function {
Subroutine native_function: called CC=4 SCAR=14.1
Pod undocumented
48 50716 my ( %args ) = @_;
49  
50 my $fn = Zuzu::Value::Function->new(
51 name => $args{name},
52 params => $args{params} // [],
53 vararg => exists $args{vararg} ? $args{vararg} : '__args',
54 body => undef,
55 closure_env => $args{closure_env},
56 50716 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'vararg'} ? : 0 50716 50716
Condition50%1/2
$args{"params"} // []
l!l
050716
MC/DC0%0/1
$args{"params"} // []
$args{"params"}
Truth table50%1/2
A
A= $args{"params"}
Aresult
11
00
57 50716 $fn->{_native} = $args{native};
58 50716 ▶ $fn->{_native_accepts_named} = $args{accepts_named} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$args{'accepts_named'} ? : 296 50420 50716
59  
60 50716 return $fn;
61 }
62  
63 663 ▶sub native_functions {
Subroutine native_functions: called CC=3 SCAR=11.0
Pod undocumented
64 663 my ( %args ) = @_;
65  
66 663 ▶ my $names = $args{names} // [];
Condition50%1/2
$args{"names"} // []
l!l
6630
MC/DC0%0/1
$args{"names"} // []
$args{"names"}
Truth table50%1/2
A
A= $args{"names"}
Aresult
11
00
67 663 my $builder = $args{builder};
68 663 my %out;
69 663 for my $name ( @{ $names } ) {
70 $out{$name} = native_function(
71 name => $name,
72 closure_env => $args{closure_env},
73 10245 native => $builder->( $name ),
74 );
75 }
76  
77 663 return \%out;
78 }
79  
80 2817 ▶sub native_object {
Subroutine native_object: called CC=7 SCAR=19.7
Pod undocumented
81 2817 my ( %args ) = @_;
82  
83 2817 ▶ my $slots = $args{slots} // {};
Condition50%1/2
$args{"slots"} // {}
l!l
28170
MC/DC0%0/1
$args{"slots"} // {}
$args{"slots"}
Truth table50%1/2
A
A= $args{"slots"}
Aresult
11
00
84 2817 ▶ my $const = $args{const} // {};
Condition100%2/2
$args{"const"} // {}
l!l
28098
MC/DC100%1/1
$args{"const"} // {}
$args{"const"}
Truth table100%2/2
A
A= $args{"const"}
Aresult
11
00
85 2817 ▶ my $types = $args{types} // {};
Condition50%1/2
$args{"types"} // {}
l!l
02817
MC/DC0%0/1
$args{"types"} // {}
$args{"types"}
Truth table50%1/2
A
A= $args{"types"}
Aresult
11
00
86 2817 for my $key ( CORE::keys %{ $slots } ) {
87 5440 ▶ $const->{$key} = 0 if not exists $const->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $const->{$key} 102 5338 5440
88 5440 ▶ $types->{$key} = 'Any' if not exists $types->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $types->{$key} 5440 0 5440
89 }
90  
91 return Zuzu::Value::Object->new(
92 class => $args{class},
93 2817 slots => $slots,
94 const => $const,
95 types => $types,
96 );
97 }
98  
99 64 ▶sub zuzu_to_perl {
Subroutine zuzu_to_perl: called CC=26 SCAR=42.4
Pod undocumented
100 64 my ( $value, %args ) = @_;
101  
102 64 my $bool_mapper = $args{boolean_mapper};
103 my $unwrap = exists $args{unwrap_object_value}
104 ? $args{unwrap_object_value}
105 64 ▶ : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'unwrap_object_value'} ? : 0 64 64
106  
107 64 ▶ return undef if not defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 3 61 64
108 61 ▶ if ( blessed($value) and $value->isa('Zuzu::Value::Boolean') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($value) and $value->isa("Zuzu::Value::Boolean")) 3 58 61
Condition100%3/3
builtin::blessed($value) and $value->isa("Zuzu::Value::Boolean")
!ll&&!rl&&r
39193
MC/DC100%2/2
builtin::blessed($value) and $value->isa("Zuzu::Value::Boolean")
builtin::blessed($value) $value->isa("Zuzu::Value::Boolean")
Truth table100%3/3
A and B
A= builtin::blessed($value)B= $value->isa("Zuzu::Value::Boolean")
ABresult
0X0
100
111
109 3 ▶ return $bool_mapper->( $value->value ? 1 : 0 )
Branch: 2 branches
BranchTrueFalseTotal
if defined $bool_mapper 3 0 3
$value->value ? : 2 1 3
110 if defined $bool_mapper;
111 0 ▶ return $value->value ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$value->value ? : 0 0 0
112 }
113 58 ▶ if ( blessed($value) and $value->isa('Zuzu::Value::Array') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($value) and $value->isa("Zuzu::Value::Array")) 11 47 58
Condition100%3/3
builtin::blessed($value) and $value->isa("Zuzu::Value::Array")
!ll&&!rl&&r
39811
MC/DC100%2/2
builtin::blessed($value) and $value->isa("Zuzu::Value::Array")
builtin::blessed($value) $value->isa("Zuzu::Value::Array")
Truth table100%3/3
A and B
A= builtin::blessed($value)B= $value->isa("Zuzu::Value::Array")
ABresult
0X0
100
111
114 11 return [ map { zuzu_to_perl( $_, %args ) } @{ $value->items } ];
115 }
116 47 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($value) and $value->isa("Zuzu::Value::Set") || $value->isa("Zuzu::Value::Bag")) 0 47 47
Condition50%3/6
builtin::blessed($value) and $value->isa("Zuzu::Value::Set") || $value->isa("Zuzu::Value::Bag")
!ll&&!rl&&r
3980
$value->isa("Zuzu::Value::Set") or $value->isa("Zuzu::Value::Bag")
l!l&&r!l&&!r
008
MC/DC0%0/3
builtin::blessed($value) and $value->isa("Zuzu::Value::Set") || $value->isa("Zuzu::Value::Bag")
builtin::blessed($value) $value->isa("Zuzu::Value::Set") $value->isa("Zuzu::Value::Bag")
Truth table50%2/4
A and B or C
A= builtin::blessed($value)B= $value->isa("Zuzu::Value::Set")C= $value->isa("Zuzu::Value::Bag")
ABCresult
0XX0
1000
11X1
1011
117 blessed($value)
118 and (
119 $value->isa('Zuzu::Value::Set')
120 or $value->isa('Zuzu::Value::Bag')
121 )
122 ) {
123 my @sorted = CORE::sort {
124 0 ▶ ( defined $a ? "$a" : '' ) cmp ( defined $b ? "$b" : '' )
Branch: 2 branches
BranchTrueFalseTotal
defined $a ? : 0 0 0
defined $b ? : 0 0 0
125 0 } @{ $value->items };
126 0 return [ map { zuzu_to_perl( $_, %args ) } @sorted ];
127 }
128 47 ▶ if ( blessed($value) and $value->isa('Zuzu::Value::PairList') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($value) and $value->isa("Zuzu::Value::PairList")) 0 47 47
Condition66%2/3
builtin::blessed($value) and $value->isa("Zuzu::Value::PairList")
!ll&&!rl&&r
3980
MC/DC0%0/2
builtin::blessed($value) and $value->isa("Zuzu::Value::PairList")
builtin::blessed($value) $value->isa("Zuzu::Value::PairList")
Truth table66%2/3
A and B
A= builtin::blessed($value)B= $value->isa("Zuzu::Value::PairList")
ABresult
0X0
100
111
129 0 require Tie::Hash::MultiValueOrdered;
130 0 tie my %out, 'Tie::Hash::MultiValueOrdered';
131 0 for my $pair ( @{ $value->list } ) {
132 0 ▶ my $key = defined $pair->[0] ? "$pair->[0]" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $pair->[0] ? : 0 0 0
133 0 $out{$key} = zuzu_to_perl( $pair->[1], %args );
134 }
135 0 tied(%out)->fetch_first;
136 0 return \%out;
137 }
138 47 ▶ if ( blessed($value) and $value->isa('Zuzu::Value::Dict') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($value) and $value->isa("Zuzu::Value::Dict")) 7 40 47
Condition100%3/3
builtin::blessed($value) and $value->isa("Zuzu::Value::Dict")
!ll&&!rl&&r
3917
MC/DC100%2/2
builtin::blessed($value) and $value->isa("Zuzu::Value::Dict")
builtin::blessed($value) $value->isa("Zuzu::Value::Dict")
Truth table100%3/3
A and B
A= builtin::blessed($value)B= $value->isa("Zuzu::Value::Dict")
ABresult
0X0
100
111
139 7 my %out;
140 7 for my $key ( CORE::keys %{ $value->map } ) {
141 15 $out{$key} = zuzu_to_perl( $value->map->{$key}, %args );
142 }
143 7 return \%out;
144 }
145 40 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($unwrap and builtin::blessed($value) and $value->isa("Zuzu::Value::Object") and exists $value->slots->{"__value"}) 0 40 40
Condition55%5/9
$unwrap and builtin::blessed($value) and $value->isa("Zuzu::Value::Object") and exists $value->slots->{"__value"}
!ll&&!rl&&r
4000
$unwrap and builtin::blessed($value) and $value->isa("Zuzu::Value::Object")
!ll&&!rl&&r
3910
$unwrap and builtin::blessed($value)
!ll&&!rl&&r
0391
MC/DC0%0/4
$unwrap and builtin::blessed($value) and $value->isa("Zuzu::Value::Object") and exists $value->slots->{"__value"}
$unwrap builtin::blessed($value) $value->isa("Zuzu::Value::Object") exists $value->slots->{"__value"}
Truth table40%2/5
A and B and C and D
A= $unwrapB= builtin::blessed($value)C= $value->isa("Zuzu::Value::Object")D= exists $value->slots->{"__value"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
146 $unwrap
147 and blessed($value)
148 and $value->isa('Zuzu::Value::Object')
149 and exists $value->slots->{__value}
150 ) {
151 0 return zuzu_to_perl( $value->slots->{__value}, %args );
152 }
153  
154 40 return $value;
155 }
156  
157 121 ▶sub perl_to_zuzu {
Subroutine perl_to_zuzu: called CC=11 SCAR=25.3
Pod undocumented
158 121 my ( $value, %args ) = @_;
159  
160 121 my $is_bool = $args{is_boolean};
161  
162 121 ▶ return undef if not defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 9 112 121
163 112 ▶ if ( ref($value) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "ARRAY") 10 102 112
164 return Zuzu::Value::Array->new(
165 10 items => [ map { perl_to_zuzu( $_, %args ) } @{ $value } ],
166 );
167 }
168 102 ▶ if ( ref($value) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "HASH") 15 87 102
169 15 my $tied = tied(%$value);
170 15 ▶ if ( $tied and $tied->DOES('Tie::Hash::MultiValueOrdered') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tied and $tied->DOES('Tie::Hash::MultiValueOrdered')) { } 0 15 15
Condition33%1/3
$tied and $tied->DOES("Tie::Hash::MultiValueOrdered")
!ll&&!rl&&r
1500
MC/DC0%0/2
$tied and $tied->DOES("Tie::Hash::MultiValueOrdered")
$tied $tied->DOES("Tie::Hash::MultiValueOrdered")
Truth table33%1/3
A and B
A= $tiedB= $tied->DOES("Tie::Hash::MultiValueOrdered")
ABresult
0X0
100
111
171 0 my @pairs;
172 0 my @list = $tied->pairs;
173 0 ▶ for ( my $i = 0; $i < @list; $i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @list) 0 0 0
174 0 push @pairs, [
175 $list[$i],
176 perl_to_zuzu( $list[$i + 1], %args ),
177 ];
178 }
179 0 return Zuzu::Value::PairList->new( list => \@pairs );
180 }
181 else {
182 15 my %out;
183 15 for my $key ( CORE::keys %{ $value } ) {
184 80 $out{$key} = perl_to_zuzu( $value->{$key}, %args );
185 }
186 15 return Zuzu::Value::Dict->new( map => \%out );
187 }
188 }
189 87 ▶ if ( defined $is_bool and $is_bool->($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $is_bool and &$is_bool($value)) 3 84 87
Condition100%3/3
defined $is_bool and &$is_bool($value)
!ll&&!rl&&r
72123
MC/DC100%2/2
defined $is_bool and &$is_bool($value)
defined $is_bool &$is_bool($value)
Truth table100%3/3
A and B
A= defined $is_boolB= &$is_bool($value)
ABresult
0X0
100
111
190 3 ▶ return Zuzu::Value::Boolean->new( value => $value ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 2 1 3
191 }
192  
193 84 return $value;
194 }
195  
196 277 ▶sub zuzu_bool {
Subroutine zuzu_bool: called CC=6 SCAR=17.9
Pod undocumented
197 277 my ( $value, $default ) = @_;
198  
199 277 ▶ return $default if not defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 249 28 277
200 28 ▶ if ( blessed($value) and $value->isa('Zuzu::Value::Boolean') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($value) and $value->isa("Zuzu::Value::Boolean")) 20 8 28
Condition66%2/3
builtin::blessed($value) and $value->isa("Zuzu::Value::Boolean")
!ll&&!rl&&r
8020
MC/DC50%1/2
builtin::blessed($value) and $value->isa("Zuzu::Value::Boolean")
builtin::blessed($value) $value->isa("Zuzu::Value::Boolean")
Truth table66%2/3
A and B
A= builtin::blessed($value)B= $value->isa("Zuzu::Value::Boolean")
ABresult
0X0
100
111
201 20 ▶ return $value->value ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$value->value ? : 14 6 20
202 }
203  
204 8 ▶ return $value ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 2 6 8
205 }
206  
207 63 1;
208  
209 =pod
210  
211 =head1 COPYRIGHT AND LICENCE
212  
213 B<< Zuzu::Util::NativeHelpers >> is copyright Toby Inkster.
214  
215 It is free software; you may redistribute it and/or modify it under
216 the terms of either the Artistic License 1.0 or the GNU General Public
217 License version 2.
218  
219 =cut
« blib/lib/Zuzu/Module/YAML.pm ↑ Summary blib/lib/Zuzu/Runtime/Async/Scheduler.pm »