PML/Schema/Derive.pm

filter: Statementstmt 80.2% 110 / 137 Branchbran 66.1% 45 / 68 Conditioncond 54.5% 18 / 33 MC/DCmcdc 15.7% 3 / 19 Subroutinesub 83.3% 10 / 12 Podpod 75.0% 3 / 4 totaltotal 69.2% 189 / 273 CC 53 SCAR 45.7
CC
53
Cov
73%
simplify
120.5
init
3.1
get_decl_type
2.0
CRAP 96.4
« PML/Backend/PML.pm ↑ Summary PML/Schema/Seq.pm »
1 package Treex::PML::Schema::Derive;
2  
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 9 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 $VERSION='2.29'; # version template
9 }
10 9 ▶no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use Treex::PML::Schema::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use base qw(Treex::PML::Schema::XMLNode);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 0 ▶sub get_decl_type { return(PML_DERIVE_DECL); }
Subroutine get_decl_type: not called CC=1 SCAR=6.9
Pod documented
16 0 ▶sub get_decl_type_str { return('derive'); }
Subroutine get_decl_type_str: not called CC=1 SCAR=6.9
Pod documented
17  
18 18 ▶sub init {
Subroutine init: called CC=3 SCAR=11.3
Pod undocumented
19 18 my ($derive,$opts)=@_;
20 18 ▶ if (!exists($derive->{type})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $derive->{"type"}) 0 18 18
21 0 die "<derive> must have a type attribute\n";
22 }
23 18 ▶ if (!exists($derive->{name})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $derive->{"name"}) 16 2 18
24 16 $derive->{name}=$derive->{type};
25 }
26 }
27  
28 18 ▶sub simplify {
Subroutine simplify: called CC=51 SCAR=47.9
Pod documented
29 18 my ($derive,$opts)=@_;
30 18 ▶ $derive->{name} ||= $derive->{-name};
Condition33%1/3
$derive->{"name"} ||= $derive->{"-name"}
l!l&&r!l&&!r
0180
MC/DC0%0/2
$derive->{"name"} ||= $derive->{"-name"}
$derive->{"name"} $derive->{"-name"}
Truth table33%1/3
A ||= B
A= $derive->{"name"}B= $derive->{"-name"}
ABresult
1X1
011
000
31 18 my $schema = $derive->{-parent};
32 return if
33 (($schema->get_decl_type == PML_TEMPLATE_DECL and $opts->{no_template_derive}) or
34 18 ▶ ($schema->get_decl_type == PML_SCHEMA_DECL and $opts->{no_derive}));
Branch: 1 branch
BranchTrueFalseTotal
if $schema->get_decl_type == 22 and $opts->{"no_template_derive"} or $schema->get_decl_type == 14 and $opts->{"no_derive"} 0 18 18
Condition55%5/9
$schema->get_decl_type == 22 and $opts->{"no_template_derive"} or $schema->get_decl_type == 14 and $opts->{"no_derive"}
l!l&&r!l&&!r
0018
$schema->get_decl_type == 22 and $opts->{"no_template_derive"}
!ll&&!rl&&r
1710
$schema->get_decl_type == 14 and $opts->{"no_derive"}
!ll&&!rl&&r
1170
MC/DC0%0/4
$schema->get_decl_type == 22 and $opts->{"no_template_derive"} or $schema->get_decl_type == 14 and $opts->{"no_derive"}
$schema->get_decl_type == 22 $opts->{"no_template_derive"} $schema->get_decl_type == 14 $opts->{"no_derive"}
Truth table28%2/7
A and B or C and D
A= $schema->get_decl_type == 22B= $opts->{"no_template_derive"}C= $schema->get_decl_type == 14D= $opts->{"no_derive"}
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
35  
36 18 my $name = $derive->{name};
37 18 my $type;
38 18 my $source = $derive->{type};
39 18 ▶ unless (defined($source) and length($source)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $source and length $source) 0 18 18
Condition33%1/3
defined $source and length $source
!ll&&!rl&&r
0018
MC/DC0%0/2
defined $source and length $source
defined $source length $source
Truth table33%1/3
A and B
A= defined $sourceB= length $source
ABresult
0X0
100
111
40 0 croak "Derive must specify source type in the attribute 'type' in $schema->{URL}\n";
41 }
42 18 ▶ if (defined($name) and length($name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name and length $name) { } 18 0 18
Condition33%1/3
defined $name and length $name
!ll&&!rl&&r
0018
MC/DC0%0/2
defined $name and length $name
defined $name length $name
Truth table33%1/3
A and B
A= defined $nameB= length $name
ABresult
0X0
100
111
43 18 ▶ if (exists ($schema->{type}{$name}) and $source ne $name) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"type"}{$name} and $source ne $name) 0 18 18
Condition66%2/3
exists $schema->{"type"}{$name} and $source ne $name
!ll&&!rl&&r
2160
MC/DC0%0/2
exists $schema->{"type"}{$name} and $source ne $name
exists $schema->{"type"}{$name} $source ne $name
Truth table66%2/3
A and B
A= exists $schema->{"type"}{$name}B= $source ne $name
ABresult
0X0
100
111
44 0 croak "Refusing to derive already existing type '$name' from '$source' in $schema->{URL}\n";
45 }
46 18 $type = $schema->{type}{$name} = $schema->copy_decl($schema->{type}{$source});
47 18 $type->{-name} = $name;
48 } else {
49 0 $name = $source;
50 0 $type = $schema->{type}{$name};
51 }
52  
53 # deriving possible for structures, sequences and choices
54 18 ▶ if ($derive->{structure}) {
Branch: 4 branches
BranchTrueFalseTotal
if ($derive->{'structure'}) { } 7 11 18
elsif ($derive->{'sequence'}) { } 2 9 11
elsif ($derive->{'container'}) { } 7 2 9
elsif ($derive->{'choice'}) { } 2 0 2
55 7 ▶ if ($type->{structure}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type->{'structure'}) { } 7 0 7
56 7 my $derive_structure = $derive->{structure};
57 7 my $target_structure = $type->{structure};
58 7 foreach my $attr (qw(role name)) {
59 14 ▶ if (exists $derive_structure->{$attr}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $derive_structure->{$attr}) 3 11 14
60 3 $target_structure->{$attr} = $derive_structure->{$attr};
61 0 push @{$target_structure->{-attributes}},$attr
62 3 ▶ unless grep { $_ eq $attr } @{$target_structure->{-attributes}};
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $attr;} @{$$target_structure{"-attributes"};} 0 3 3
63 }
64 }
65 7 ▶ $target_structure->{member} ||= {};
Condition50%1/2
$target_structure->{"member"} ||= {}
l!l
70
MC/DC0%0/1
$target_structure->{"member"} ||= {}
$target_structure->{"member"}
Truth table50%1/2
A
A= $target_structure->{"member"}
Aresult
11
00
66 7 my $members = $target_structure->{member};
67 7 ▶ while (my ($member,$value) = each %{$derive_structure->{member}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$derive_structure{"member"};}) 9 7 16
68 9 $members->{$member} = $target_structure->copy_decl($value); # FIXME: no need if we remove derives in the end
69 }
70 7 ▶ if (ref $derive_structure->{delete}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $derive_structure->{"delete"}) 0 7 7
71 0 for my $member (@{$derive_structure->{delete}}) {
72 0 delete $members->{$member};
73 }
74 }
75 } else {
76  
77 0 croak "Cannot derive structure type '$name' from a non-structure '$source'\n";
78 }
79 } elsif ($derive->{sequence}) {
80 2 ▶ if ($type->{sequence}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type->{'sequence'}) { } 2 0 2
81 2 my $derive_sequence = $derive->{sequence};
82 2 my $target_sequence = $type->{sequence};
83 2 ▶ if (exists $derive_sequence->{role}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $derive_sequence->{"role"}) 0 2 2
84 0 $target_sequence->{role} = $derive_sequence->{role};
85 0 push @{$target_sequence->{-attributes}},'role'
86 0 ▶ unless grep { $_ eq 'role' } @{$target_sequence->{-attributes}};
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq "role";} @{$$target_sequence{"-attributes"};} 0 0 0
87 }
88 2 ▶ if (exists $derive_sequence->{content_pattern}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $derive_sequence->{"content_pattern"}) 2 0 2
89 2 $target_sequence->{content_pattern} = $derive_sequence->{content_pattern};
90 2 push @{$target_sequence->{-attributes}},'content_pattern'
91 2 ▶ unless grep { $_ eq 'content_pattern' } @{$target_sequence->{-attributes}};
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq "content_pattern";} @{$$target_sequence{"-attributes"};} 2 0 2
92 }
93 2 ▶ $target_sequence->{element} ||= {};
Condition50%1/2
$target_sequence->{"element"} ||= {}
l!l
20
MC/DC0%0/1
$target_sequence->{"element"} ||= {}
$target_sequence->{"element"}
Truth table50%1/2
A
A= $target_sequence->{"element"}
Aresult
11
00
94 2 my $elements = $target_sequence->{element};
95 2 ▶ while (my ($element,$value) = each %{$derive_sequence->{element}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$derive_sequence{"element"};}) 2 2 4
96 2 $elements->{$element} = $target_sequence->copy_decl($value); # FIXME: no need if we remove derives in the end
97 }
98 2 ▶ if (ref $derive_sequence->{delete}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $derive_sequence->{"delete"}) 0 2 2
99 0 for my $element (@{$derive_sequence->{delete}}) {
100 0 delete $elements->{$element};
101 }
102 }
103 } else {
104 0 require Data::Dumper;
105 # print STDERR Data::Dumper::Dumper([$type]);
106 0 croak "Cannot derive sequence type '$name' from a non-sequence '$source'\n";
107 }
108 } elsif ($derive->{container}) {
109 7 ▶ if ($type->{container}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type->{'container'}) { } 7 0 7
110 7 my $derive_container = $derive->{container};
111 7 my $target_container = $type->{container};
112 7 for my $attr (qw(type role)) {
113 14 ▶ next unless exists $derive_container->{$attr};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $derive_container->{$attr} 12 2 14
114 2 ▶ if ($attr eq 'type' and !exists($target_container->{type})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr eq "type" and not exists $target_container->{"type"}) 1 1 2
Condition66%2/3
$attr eq "type" and not exists $target_container->{"type"}
!ll&&!rl&&r
101
MC/DC50%1/2
$attr eq "type" and not exists $target_container->{"type"}
$attr eq "type" not exists $target_container->{"type"}
Truth table66%2/3
A and not(B)
A= $attr eq "type"B= not exists $target_container->{"type"}
ABresult
0X0
100
111
115 1 foreach my $d (qw(list alt structure container sequence cdata)) {
116 6 ▶ if (exists $target_container->{$d}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $target_container->{$d}) 1 5 6
117 1 delete $target_container->{$d};
118 1 last;
119 }
120 }
121 1 delete $target_container->{-decl};
122 1 delete $target_container->{-resolved};
123 }
124 2 $target_container->{$attr} = $derive_container->{$attr};
125 1 push @{$target_container->{-attributes}},$attr
126 2 ▶ unless grep { $_ eq $attr } @{$target_container->{-attributes}};
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $attr;} @{$$target_container{"-attributes"};} 1 1 2
127 }
128 7 ▶ $target_container->{attribute} ||= {};
Condition100%2/2
$target_container->{"attribute"} ||= {}
l!l
61
MC/DC100%1/1
$target_container->{"attribute"} ||= {}
$target_container->{"attribute"}
Truth table100%2/2
A
A= $target_container->{"attribute"}
Aresult
11
00
129 7 my $attributes = $target_container->{attribute};
130 7 ▶ while (my ($attribute,$value) = each %{$derive_container->{attribute}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$derive_container{"attribute"};}) 7 7 14
131 7 $attributes->{$attribute} = $target_container->copy_decl($value); # FIXME: no need if we remove derives in the end
132 }
133 7 ▶ if (ref $derive_container->{delete}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $derive_container->{"delete"}) 1 6 7
134 1 for my $attribute (@{$derive_container->{delete}}) {
135 1 delete $attributes->{$attribute};
136 }
137 }
138 } else {
139 0 croak "Cannot derive a container '$name' from a different type '$source'\n";
140 }
141 } elsif ($derive->{choice}) {
142 2 my $choice = $derive->{choice};
143 2 ▶ if ($type->{choice}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type->{'choice'}) { } 2 0 2
144 2 my (@add,%delete);
145 2 ▶ if (UNIVERSAL::isa($choice,'HASH')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($choice, 'HASH')) { } 2 0 2
146 2 ▶ @add = @{$choice->{values}} if ref $choice->{values};
Branch: 1 branch
BranchTrueFalseTotal
if ref $choice->{"values"} 2 0 2
147 2 ▶ @delete{ @{$choice->{delete}} }=() if ref $choice->{delete};
Branch: 1 branch
BranchTrueFalseTotal
if ref $choice->{"delete"} 2 0 2
148 } else {
149 0 @add = @$choice;
150 }
151 2 my %seen;
152 2 @{$type->{choice}{values}} =
153 2 ▶ grep { !($seen{$_}++) and ! exists $delete{$_} } (@{$type->{choice}{values}},@add);
Condition66%2/3
$seen{$_}++ || exists $delete{$_}
l!l&&r!l&&!r
0214
MC/DC50%1/2
$seen{$_}++ || exists $delete{$_}
$seen{$_}++ exists $delete{$_}
Truth table66%2/3
A || B
A= $seen{$_}++B= exists $delete{$_}
ABresult
1X1
011
000
154 } else {
155 0 croak "Cannot derive a choice type '$name' from a non-choice type '$source'\n";
156 }
157 } else {
158 0 ▶ unless ($name ne $source) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name ne $source) 0 0 0
159 0 croak "<derive type='$source'> has no effect in $schema->{URL}\n";
160 }
161 }
162 }
163  
164 9 1;
165 __END__
« PML/Backend/PML.pm ↑ Summary PML/Schema/Seq.pm »