MechanicalTurk/DataStructure.pm

filter: Statementstmt 6.2% 8 / 129 Branchbran 0.0% 0 / 80 Conditioncond 0.0% 0 / 33 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 13.3% 2 / 15 Podpod 0.0% 0 / 8 totaltotal 3.4% 10 / 287 CC 54 SCAR 45.0
CC
54
Cov
3%
get
240.0
fromProperties
110.0
_visit
72.0
CRAP 89.8
« MechanicalTurk/Command/UpdateHITs.pm ↑ Summary MechanicalTurk/Response.pm »
1 package Net::Amazon::MechanicalTurk::DataStructure;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 our $VERSION = '1.00';
6  
7 0 ▶sub wrap {
Subroutine wrap: not called CC=1 SCAR=6.9
Pod undocumented
8 0 my ($class, $data) = @_;
9 0 ▶ visit($data, sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
10 0 my ($key, $value, $nodes) = @_;
11 0 ▶ if (ref($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) 0 0 0
12 0 bless($value, $class);
13 }
14 0 });
15 }
16  
17 0 ▶sub fromProperties {
Subroutine fromProperties: not called CC=10 SCAR=47.0
Pod undocumented
18 # Assume static call if 1st arg is not this class
19 0 ▶ shift if ($#_ >= 0 and $_[0] eq "Net::Amazon::MechanicalTurk::DataStructure");
Branch: 1 branch
BranchTrueFalseTotal
if $#_ >= 0 and $_[0] eq "Net::Amazon::MechanicalTurk::DataStructure" 0 0 0
Condition0%0/3
$#_ >= 0 and $_[0] eq "Net::Amazon::MechanicalTurk::DataStructure"
!ll&&!rl&&r
000
MC/DC0%0/2
$#_ >= 0 and $_[0] eq "Net::Amazon::MechanicalTurk::DataStructure"
$#_ >= 0 $_[0] eq "Net::Amazon::MechanicalTurk::DataStructure"
Truth table0%0/3
A and B
A= $#_ >= 0B= $_[0] eq "Net::Amazon::MechanicalTurk::DataStructure"
ABresult
0X0
100
111
20 0 my $data = {};
21 0 my $props = shift;
22
23 0 ▶ while (my ($fullKey,$value) = each %$props) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$props) 0 0 0
24 0 my $nodeRef = \$data;
25 0 foreach my $key (split(/\./, $fullKey)) {
26 0 ▶ if (UNIVERSAL::isa(${$nodeRef}, "HASH")) {
Branch: 3 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa(${$nodeRef;}, 'HASH')) { } 0 0 0
elsif (UNIVERSAL::isa(${$nodeRef;}, 'ARRAY')) { } 0 0 0
elsif ($key =~ /^\d+$/) { } 0 0 0
27 0 $nodeRef = \${$nodeRef}->{$key};
28 }
29 0 elsif (UNIVERSAL::isa(${$nodeRef}, "ARRAY")) {
30 0 ▶ if ($key !~ /^\d+$/ or $key < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $key =~ /^\d+$/ or $key < 1) 0 0 0
Condition0%0/3
not $key =~ /^\d+$/ or $key < 1
l!l&&r!l&&!r
000
MC/DC0%0/2
not $key =~ /^\d+$/ or $key < 1
not $key =~ /^\d+$/ $key < 1
Truth table0%0/3
not(A) or B
A= not $key =~ /^\d+$/B= $key < 1
ABresult
1X1
011
000
31 0 Carp::croak("Can't convert key $fullKey to data structure.");
32 }
33 0 $nodeRef = \${$nodeRef}->[$key-1];
34 }
35 elsif ($key =~ /^\d+$/) {
36 0 ${$nodeRef} = [];
37 0 $nodeRef = \${$nodeRef}->[$key-1];
38 }
39 else {
40 0 ${$nodeRef} = {};
41 0 $nodeRef = \${$nodeRef}->{$key};
42 }
43 }
44 0 ${$nodeRef} = $value;
45 }
46
47 0 return $data;
48 }
49  
50 0 ▶sub toProperties {
Subroutine toProperties: not called CC=3 SCAR=24.8
Pod undocumented
51 # Assume static call if 1st arg is not this class
52 0 ▶ shift if ($#_ >= 0 and $_[0] eq "Net::Amazon::MechanicalTurk::DataStructure");
Branch: 1 branch
BranchTrueFalseTotal
if $#_ >= 0 and $_[0] eq "Net::Amazon::MechanicalTurk::DataStructure" 0 0 0
Condition0%0/3
$#_ >= 0 and $_[0] eq "Net::Amazon::MechanicalTurk::DataStructure"
!ll&&!rl&&r
000
MC/DC0%0/2
$#_ >= 0 and $_[0] eq "Net::Amazon::MechanicalTurk::DataStructure"
$#_ >= 0 $_[0] eq "Net::Amazon::MechanicalTurk::DataStructure"
Truth table0%0/3
A and B
A= $#_ >= 0B= $_[0] eq "Net::Amazon::MechanicalTurk::DataStructure"
ABresult
0X0
100
111
53 0 my $self = shift;
54 0 my $props = {};
55 0 ▶ eachFlattenedProperty($self, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
56 0 my ($key, $value) = @_;
57 0 $props->{$key} = $value;
58 0 });
59 0 return $props;
60 }
61  
62 0 ▶sub eachFlattenedProperty {
Subroutine eachFlattenedProperty: not called CC=4 SCAR=30.0
Pod undocumented
63 # Assume static call if 1st arg is not this class
64 0 ▶ shift if ($#_ >= 0 and $_[0] eq "Net::Amazon::MechanicalTurk::DataStructure");
Branch: 1 branch
BranchTrueFalseTotal
if $#_ >= 0 and $_[0] eq "Net::Amazon::MechanicalTurk::DataStructure" 0 0 0
Condition0%0/3
$#_ >= 0 and $_[0] eq "Net::Amazon::MechanicalTurk::DataStructure"
!ll&&!rl&&r
000
MC/DC0%0/2
$#_ >= 0 and $_[0] eq "Net::Amazon::MechanicalTurk::DataStructure"
$#_ >= 0 $_[0] eq "Net::Amazon::MechanicalTurk::DataStructure"
Truth table0%0/3
A and B
A= $#_ >= 0B= $_[0] eq "Net::Amazon::MechanicalTurk::DataStructure"
ABresult
0X0
100
111
65 0 my ($self, $block) = @_;
66 0 ▶ return unless defined($self);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self 0 0 0
67 0 _eachFlattenedProperty(undef, $self, 0, $block);
68 }
69  
70 0 ▶sub _eachFlattenedProperty {
Subroutine _eachFlattenedProperty: not called CC=7 SCAR=40.3
71 0 my ($key, $value, $parentIsHash, $block) = @_;
72 0 ▶ if (UNIVERSAL::isa($value, "ARRAY")) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($value, 'ARRAY')) { } 0 0 0
elsif (UNIVERSAL::isa($value, 'HASH')) { } 0 0 0
73 0 ▶ for (my $i=0; $i<=$#{$value}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#{$value;}) 0 0 0
74 0 _eachFlattenedProperty($key.".".($i+1), $value->[$i], 0, $block);
75 }
76 }
77 elsif (UNIVERSAL::isa($value, "HASH")) {
78 0 ▶ while (my ($subKey,$subValue) = each %$value) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$value) 0 0 0
79 0 my $newKey = $subKey;
80 0 ▶ if (defined($key)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) 0 0 0
81 0 ▶ $newKey = ($parentIsHash) ? "${key}.1.${subKey}" : "${key}.${subKey}";
Branch: 1 branch
BranchTrueFalseTotal
$parentIsHash ? : 0 0 0
82 }
83 0 _eachFlattenedProperty($newKey, $subValue, 1, $block);
84 }
85 }
86 else {
87 0 $block->($key, $value);
88 }
89 }
90  
91 0 ▶sub visit {
Subroutine visit: not called CC=3 SCAR=24.8
Pod undocumented
92 # Assume static call if 1st arg is not this class
93 0 ▶ shift if ($#_ >= 0 and $_[0] eq "Net::Amazon::MechanicalTurk::DataStructure");
Branch: 1 branch
BranchTrueFalseTotal
if $#_ >= 0 and $_[0] eq "Net::Amazon::MechanicalTurk::DataStructure" 0 0 0
Condition0%0/3
$#_ >= 0 and $_[0] eq "Net::Amazon::MechanicalTurk::DataStructure"
!ll&&!rl&&r
000
MC/DC0%0/2
$#_ >= 0 and $_[0] eq "Net::Amazon::MechanicalTurk::DataStructure"
$#_ >= 0 $_[0] eq "Net::Amazon::MechanicalTurk::DataStructure"
Truth table0%0/3
A and B
A= $#_ >= 0B= $_[0] eq "Net::Amazon::MechanicalTurk::DataStructure"
ABresult
0X0
100
111
94 0 my ($self, $block, $orderKeys) = @_;
95 0 _visit(undef, $self, [], $block, $orderKeys);
96 }
97  
98 0 ▶sub _visit {
Subroutine _visit: not called CC=8 SCAR=42.8
99 0 my ($key, $value, $nodes, $block, $orderKeys) = @_;
100 0 ▶ return unless defined($value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
101
102 0 $block->($key, $value, $nodes);
103 0 push(@$nodes, $value);
104 0 ▶ if (UNIVERSAL::isa($value, "HASH")) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($value, 'HASH')) { } 0 0 0
elsif (UNIVERSAL::isa($value, 'ARRAY')) { } 0 0 0
105 0 ▶ if ($orderKeys) {
Branch: 1 branch
BranchTrueFalseTotal
if ($orderKeys) { } 0 0 0
106 0 foreach my $k (sort keys %$value) {
107 0 _visit($k, $value->{$k}, $nodes, $block, $orderKeys);
108 }
109 }
110 else {
111 0 ▶ while (my ($k,$v) = each %{$value}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$value;}) 0 0 0
112 0 _visit($k, $v, $nodes, $block, $orderKeys);
113 }
114 }
115 }
116 elsif (UNIVERSAL::isa($value, "ARRAY")) {
117 0 ▶ for (my $i=0; $i<=$#{$value}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#{$value;}) 0 0 0
118 0 _visit($i, $value->[$i], $nodes, $block, $orderKeys);
119 }
120 }
121 0 pop(@$nodes);
122 }
123  
124 0 ▶sub toString {
Subroutine toString: not called CC=3 SCAR=24.8
Pod undocumented
125 # Assume static call if 1st arg is not this class
126 0 ▶ shift if ($#_ >= 0 and $_[0] eq "Net::Amazon::MechanicalTurk::DataStructure");
Branch: 1 branch
BranchTrueFalseTotal
if $#_ >= 0 and $_[0] eq "Net::Amazon::MechanicalTurk::DataStructure" 0 0 0
Condition0%0/3
$#_ >= 0 and $_[0] eq "Net::Amazon::MechanicalTurk::DataStructure"
!ll&&!rl&&r
000
MC/DC0%0/2
$#_ >= 0 and $_[0] eq "Net::Amazon::MechanicalTurk::DataStructure"
$#_ >= 0 $_[0] eq "Net::Amazon::MechanicalTurk::DataStructure"
Truth table0%0/3
A and B
A= $#_ >= 0B= $_[0] eq "Net::Amazon::MechanicalTurk::DataStructure"
ABresult
0X0
100
111
127 0 my $self = shift;
128 0 my $message = "<<" . ref($self) . ">>";
129 0 ▶ visit($self, sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
130 0 my ($key, $value, $nodes) = @_;
131 0 ▶ if (!defined($key)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $key) 0 0 0
132 0 return;
133 }
134 0 ▶ if (!UNIVERSAL::isa($value, "ARRAY") && !UNIVERSAL::isa($value, "HASH")) {
Branch: 1 branch
BranchTrueFalseTotal
if (not UNIVERSAL::isa($value, 'ARRAY') || UNIVERSAL::isa($value, 'HASH')) { } 0 0 0
Condition0%0/3
UNIVERSAL::isa($value, 'ARRAY') || UNIVERSAL::isa($value, 'HASH')
l!l&&r!l&&!r
000
MC/DC0%0/2
UNIVERSAL::isa($value, 'ARRAY') || UNIVERSAL::isa($value, 'HASH')
UNIVERSAL::isa($value, 'ARRAY') UNIVERSAL::isa($value, 'HASH')
Truth table0%0/3
A || B
A= UNIVERSAL::isa($value, 'ARRAY')B= UNIVERSAL::isa($value, 'HASH')
ABresult
1X1
011
000
135 0 $message .= "\n" . (" " x ($#{$nodes}*2)) . "[$key]" . " " . $value;
136 }
137 else {
138 0 $message .= "\n" . (" " x ($#{$nodes}*2)) . "[$key]";
139 }
140 0 }, 1);
141 0 return $message;
142 }
143  
144 0 ▶sub getFirst {
Subroutine getFirst: not called CC=5 SCAR=34.0
Pod undocumented
145 # Assume static call if 1st arg is not this class
146 0 ▶ shift if ($#_ >= 0 and $_[0] eq "Net::Amazon::MechanicalTurk::DataStructure");
Branch: 1 branch
BranchTrueFalseTotal
if $#_ >= 0 and $_[0] eq "Net::Amazon::MechanicalTurk::DataStructure" 0 0 0
Condition0%0/3
$#_ >= 0 and $_[0] eq "Net::Amazon::MechanicalTurk::DataStructure"
!ll&&!rl&&r
000
MC/DC0%0/2
$#_ >= 0 and $_[0] eq "Net::Amazon::MechanicalTurk::DataStructure"
$#_ >= 0 $_[0] eq "Net::Amazon::MechanicalTurk::DataStructure"
Truth table0%0/3
A and B
A= $#_ >= 0B= $_[0] eq "Net::Amazon::MechanicalTurk::DataStructure"
ABresult
0X0
100
111
147 0 my $self = shift;
148 0 my $result = get($self, @_);
149 0 ▶ if (UNIVERSAL::isa($result, "ARRAY")) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($result, 'ARRAY')) { } 0 0 0
150 0 ▶ return ($#{$result} >= 0) ? $result->[0] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$#{$result;} >= 0 ? : 0 0 0
151 }
152 else {
153 0 return $result;
154 }
155 }
156  
157 0 ▶sub get {
Subroutine get: not called CC=15 SCAR=54.8
Pod undocumented
158 # Assume static call if 1st arg is not this class
159 0 ▶ shift if ($#_ >= 0 and $_[0] eq "Net::Amazon::MechanicalTurk::DataStructure");
Branch: 1 branch
BranchTrueFalseTotal
if $#_ >= 0 and $_[0] eq "Net::Amazon::MechanicalTurk::DataStructure" 0 0 0
Condition0%0/3
$#_ >= 0 and $_[0] eq "Net::Amazon::MechanicalTurk::DataStructure"
!ll&&!rl&&r
000
MC/DC0%0/2
$#_ >= 0 and $_[0] eq "Net::Amazon::MechanicalTurk::DataStructure"
$#_ >= 0 $_[0] eq "Net::Amazon::MechanicalTurk::DataStructure"
Truth table0%0/3
A and B
A= $#_ >= 0B= $_[0] eq "Net::Amazon::MechanicalTurk::DataStructure"
ABresult
0X0
100
111
160 0 my $self = shift;
161  
162 0 my @matches;
163 0 ▶ if ($#_ == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#_ == 0) { } 0 0 0
164 0 ▶ if (UNIVERSAL::isa($_[0], "ARRAY")) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($_[0], 'ARRAY')) { } 0 0 0
165 0 @matches = @$_[0];
166 }
167 else {
168 0 @matches = split /\./, $_[0];
169 }
170 }
171 else {
172 0 @matches = @_;
173 }
174  
175 0 my $node = $self;
176 0 my $i = 0;
177 0 ▶ while ($i <= $#matches) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#matches) 0 0 0
178 0 my $match = $matches[$i];
179 0 ▶ if (UNIVERSAL::isa($node, "ARRAY")) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($node, 'ARRAY')) { } 0 0 0
elsif (UNIVERSAL::isa($node, 'HASH')) { } 0 0 0
180 # numeric indices are 1 based
181 0 ▶ if ($match =~ /^\d+$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($match =~ /^\d+$/) { } 0 0 0
elsif ($#{$node;} >= 0) { } 0 0 0
182 0 ▶ if ($match < 1 or $match > ($#{$node}+1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match < 1 or $match > $#{$node;} + 1) 0 0 0
Condition0%0/3
$match < 1 or $match > $#{$node;} + 1
l!l&&r!l&&!r
000
MC/DC0%0/2
$match < 1 or $match > $#{$node;} + 1
$match < 1 $match > $#{$node;} + 1
Truth table0%0/3
A or B
A= $match < 1B= $match > $#{$node;} + 1
ABresult
1X1
011
000
183 0 return undef;
184 }
185 0 $node = $node->[$match-1];
186 0 $i++;
187 }
188 0 elsif ($#{$node} >= 0) {
189 0 $node = $node->[0];
190 }
191 else {
192 0 return undef;
193 }
194 }
195 elsif (UNIVERSAL::isa($node, "HASH")) {
196 0 ▶ if (!exists $node->{$match}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $node->{$match}) { } 0 0 0
197 0 ▶ if ($match =~ /^\d+$/ and $match == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match =~ /^\d+$/ and $match == 1) { } 0 0 0
Condition0%0/3
$match =~ /^\d+$/ and $match == 1
!ll&&!rl&&r
000
MC/DC0%0/2
$match =~ /^\d+$/ and $match == 1
$match =~ /^\d+$/ $match == 1
Truth table0%0/3
A and B
A= $match =~ /^\d+$/B= $match == 1
ABresult
0X0
100
111
198 # handle case where data structure has
199 # a hash containing a hash
200 # but get supplied an index of 1
201 # family.1.kid.1
202 # { family => { kid => ['k1', 'k2' ] }
203 # allows get to read properties produced
204 # by toProperties
205 0 $i++;
206 }
207 else {
208 0 return undef;
209 }
210 }
211 else {
212 0 $node = $node->{$match};
213 0 $i++;
214 }
215 }
216 else {
217 0 return undef;
218 }
219 }
220  
221 0 return $node;
222 }
223  
224 1 return 1;
« MechanicalTurk/Command/UpdateHITs.pm ↑ Summary MechanicalTurk/Response.pm »