Structure.pm

filter: Statementstmt 43.8% 71 / 162 Branchbran 35.7% 25 / 70 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 40.5% 15 / 37 Podpod 100% 15 / 15 totaltotal 43.9% 127 / 289 CC 43 SCAR 23.7
CC
43
Cov
41%
_clone
42.0
_sub_hash
30.0
_eq_array
16.1
CRAP 10.7
« XSD.pm ↑ Summary XSD/ParseXML.pm »
1 package Data::Validate::Structure;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 =head1 NAME
6  
7 Data::Validate::Structure - handle a structure in custom ways
8  
9 =head1 SYNOPSIS
10  
11 use Data::Validate::Structure;
12  
13 my $structure = Structure->new( $data );
14  
15 # Check sub structures matches exactly
16 $structure == $structure2
17  
18 # Check sub structures matches equaly (array order not important)
19 $structure eq $structure
20  
21 # Check structure contains all of structure2 at least
22 $structure >= $structure2
23  
24 # Check structure2 contains all of structure at least
25 $structure <= $structure2
26  
27 # structure much contain structure2 but not equal it
28 $structure > $structure2
29  
30 # structure2 must contain structure but not equal it
31 $structure < $structure2
32  
33 # Make sure structure does not exactly match structure2
34 $structure != $structure2
35  
36 # Remove all parts of structure2 from structure
37 $structure - $structure2
38 $structure -= $structure2
39  
40 # Merge two structures together
41 $structure + $structure2
42 $structure += $structure2
43  
44 =head1 DESCRIPTION
45
46 Take a structure and attempt to allow some basic structure
47 to structure testing.
48  
49 =head1 METHODS
50  
51 =cut
52
53 3 our $VERSION = "0.09";
54 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
55  
56 use overload
57 3 ▶ '""' => \&autoname,
Subroutine BEGIN: called CC=1 SCAR=0.0
58 '%{}' => \&autovalue,
59 '@{}' => \&autovalue,
60 'bool' => \&autobool,
61 '==' => \&identical,
62 'eq' => \&equal,
63 '!=' => \&notidentical,
64 'ne' => \&notequal,
65 '<=' => \&disabled,
66 '>=' => \&disabled,
67 '>' => \&disabled,
68 '<' => \&disabled,
69 '-' => \&disabled,
70 '-=' => \&disabled,
71 '+' => \&plus,
72 3 '+=' => \&pluseq;
73  
74 =head2 $class->new( $structure )
75  
76 Create a new structure.
77  
78 =cut
79 4 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
80 4 my ($class, $structure) = @_;
81 4 my $self = bless { structure => $structure }, $class;
82 4 return $self;
83 }
84  
85 =head2 $structure->disabled()
86  
87 Internal method, wht to do when a function is disabled.
88  
89 =cut
90 0 ▶sub disabled { carp "Structure method disabled";}
Subroutine disabled: not called CC=1 SCAR=6.9
Pod documented
91  
92 =head2 $structure->equal( $otherstructure )
93  
94 Test that structure is the same as other structure.
95  
96 =cut
97 2 ▶sub equal { return $_[0] if _autoself(); return _equal(@_); }
Subroutine equal: called CC=2 SCAR=7.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if _autoself() 0 2 2
98  
99 =head2 $structure->notequal( $otherstructure )
100  
101 Test that structure is not the same as other structure.
102  
103 =cut
104 0 ▶sub notequal { return $_[0] if _autoself(); return not _equal(@_); }
Subroutine notequal: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if _autoself() 0 0 0
105  
106 =head2 $structure->_equal( $otherstructure )
107  
108 Internal method for testing structural equiverlance.
109  
110 =cut
111 2 ▶sub _equal {
Subroutine _equal: called CC=1 SCAR=0.0
112 2 my ($self, $sct) = @_;
113 2 return _eq($self, $sct, StrictArray => 0 );
114 }
115  
116 =head2 $structure->identical( $otherstructure )
117  
118 Return true if structure is identical.
119  
120 =cut
121 0 ▶sub identical { return $_[0] if _autoself(); return _identical(@_); }
Subroutine identical: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if _autoself() 0 0 0
122  
123 =head2 $structure->notidentical( $otherstructure )
124  
125 Return true if structure is not identical.
126  
127 =cut
128 0 ▶sub notidentical { return $_[0] if _autoself(); return not _identical(@_); }
Subroutine notidentical: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if _autoself() 0 0 0
129  
130 =head2 $structure->_identical( $otherstructure )
131  
132 Return true if structure is identical (internal).
133  
134 =cut
135 0 ▶sub _identical {
Subroutine _identical: not called CC=1 SCAR=6.9
136 0 my ($self, $sct) = @_;
137 0 return _eq($self, $sct, StrictArray => 1 );
138 }
139  
140 =head2 $structure->_autoself()
141  
142 Return true if the caller was internal.
143  
144 =cut
145 6 ▶sub _autoself {
Subroutine _autoself: called CC=2 SCAR=6.9
146 6 my ($self) = @_;
147 6 my ($package) = caller(1);
148 6 ▶ if($package eq "Data::Validate::Structure") {
Branch: 1 branch
BranchTrueFalseTotal
if ($package eq "Data::Validate::Structure") 4 2 6
149 4 return 1;
150 }
151 2 return 0;
152 }
153  
154 =head2 $structure->autovalue()
155  
156 Return the structure
157  
158 =cut
159 4 ▶sub autovalue {
Subroutine autovalue: called CC=2 SCAR=8.1
Pod documented
160 4 my ($self) = @_;
161 4 ▶ return $self if _autoself;
Branch: 1 branch
BranchTrueFalseTotal
if _autoself() 4 0 4
162 #my (@a) = caller;
163 #warn join(', ', @a)."\n";
164 0 return $self->structure;
165 }
166  
167 =head2 $structure->autoname()
168  
169 Return the structure name
170  
171 =cut
172 0 ▶sub autoname {
Subroutine autoname: not called CC=2 SCAR=17.9
Pod documented
173 0 my ($self) = @_;
174 0 ▶ return $self if _autoself;
Branch: 1 branch
BranchTrueFalseTotal
if _autoself() 0 0 0
175 0 return $self->name;
176 }
177  
178 =head2 $structure->autobool()
179  
180 Returns the truth of the structure
181  
182 =cut
183 0 ▶sub autobool {
Subroutine autobool: not called CC=3 SCAR=24.8
Pod documented
184 0 my ($self) = @_;
185 0 ▶ if(ref($self->structure) eq "ARRAY") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $self->structure eq 'ARRAY') { } 0 0 0
elsif (ref $self->structure eq 'HASH') { } 0 0 0
186 0 return scalar(@{$self->structure});
187 } elsif(ref($self->structure) eq "HASH") {
188 0 return keys(%{$self->structure});
189 }
190 }
191  
192 =head2 $structure->structure()
193  
194 Return the structure directly
195  
196 =cut
197 4 ▶sub structure { return $_[0]->{'structure'}; }
Subroutine structure: called CC=1 SCAR=0.0
Pod documented
198  
199  
200 =head2 $structure->name()
201  
202 Return the name directly
203  
204 =cut
205 0 ▶sub name { return $_[0]->{'name'}; }
Subroutine name: not called CC=1 SCAR=6.9
Pod documented
206  
207 =head2 $structure->_eq( $otherstructure, %p )
208  
209 Return true if other structure is equle.
210  
211 =cut
212 27 ▶sub _eq {
Subroutine _eq: called CC=1 SCAR=0.0
213 27 my ($sct1, $sct2, %op) = @_;
214 return _sctdeal(
215 $sct1,
216 $sct2,
217 \&_eq_array,
218 \&_eq_hash,
219 22 ▶ sub { return 1 if $_[0] eq $_[1] },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq $_[1] 17 5 22
220 27 %op, SkipSame => 1,
221 );
222 0 return 0;
223 }
224  
225 =head2 $structure->_eq_hash( $otherhash, %p )
226  
227 Return true if other hash is equle.
228  
229 =cut
230 5 ▶sub _eq_hash {
Subroutine _eq_hash: called CC=4 SCAR=14.3
231 5 my ($sct1, $sct2, %op) = @_;
232 # check keys of hash to be the same via eqarray
233 5 ▶ return 0 if not _eq_array([keys(%{$sct1})], [keys(%{$sct2})], StrictArray => 0 );
Branch: 1 branch
BranchTrueFalseTotal
unless _eq_array([keys %{$sct1;}], [keys %{$sct2;}], "StrictArray", 0) 0 5 5
234 5 foreach my $key (keys(%{$sct1})) {
235 10 ▶ if(not _eq($sct1->{$key}, $sct2->{$key}, %op)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_eq($sct1->{$key}, $sct2->{$key}, %op)) 0 10 10
236 0 return 0;
237 }
238 }
239 5 return 1;
240 }
241  
242 =head2 $structure->_eq_array( $otherarray )
243  
244 Return true if other array is equle.
245  
246 =cut
247 sub _eq_array
248 5 ▶{
Subroutine _eq_array: called CC=12 SCAR=27.8
249 5 my ($sct1, $sct2, %op) = @_;
250 # Check size of array (because this will save time)
251 0 ▶ return 1 if @{$sct1} == 0 and @{$sct2} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @{$sct1;} == 0 and @{$sct2;} == 0 0 5 5
Condition33%1/3
@{$sct1;} == 0 and @{$sct2;} == 0
!ll&&!rl&&r
500
MC/DC0%0/2
@{$sct1;} == 0 and @{$sct2;} == 0
@{$sct1;} == 0 @{$sct2;} == 0
Truth table33%1/3
A and B
A= @{$sct1;} == 0B= @{$sct2;} == 0
ABresult
0X0
100
111
252 5 ▶ return 0 if not @{$sct1} == @{$sct2};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$sct1;} == @{$sct2;} 0 5 5
253 5 ▶ if($op{'StrictArray'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op{'StrictArray'}) { } 0 5 5
254 # This will look for strict arrays where the order
255 # is the same and so is the content.
256 0 ▶ for(my $i = 0; $i <= $#{$sct1}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#{$sct1;}) 0 0 0
257 0 ▶ return 0 if not _eq($sct1->[$i], $sct2->[$i], %op);
Branch: 1 branch
BranchTrueFalseTotal
unless _eq($sct1->[$i], $sct2->[$i], %op) 0 0 0
258 }
259 } else {
260 # This is less strict, it just wants the same content
261 # but not the same order (takes longer to run)
262 5 my %used;
263 5 ▶ for(my $i = 0; $i <= $#{$sct1}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#{$sct1;}) 10 5 15
264 10 my $ofsct1 = $sct1->[$i];
265 10 my $found = 0;
266 10 ▶ for(my $j = 0; $j <= $#{$sct2}; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j <= $#{$sct2;}) 21 0 21
267 21 ▶ next if $used{$j};
Branch: 1 branch
BranchTrueFalseTotal
if $used{$j} 6 15 21
268 15 my $ofsct2 = $sct2->[$j];
269 15 ▶ if(_eq($ofsct1, $ofsct2, %op)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_eq($ofsct1, $ofsct2, %op)) 10 5 15
270 10 $used{$j} = 1;
271 10 $found = 1;
272 10 last;
273 }
274 }
275 10 ▶ return 0 if not $found;
Branch: 1 branch
BranchTrueFalseTotal
unless $found 0 10 10
276 }
277 }
278 5 return 1;
279 }
280  
281 =head2 $structure->plus( $otherstructure )
282  
283 Return the current structure plus another structure
284  
285 =cut
286 0 ▶sub plus {
Subroutine plus: not called CC=1 SCAR=6.9
Pod documented
287 0 my ($self, $sct) = @_;
288 0 return _plus($self, $sct);
289 }
290  
291 =head2 $structure->pluseq( $otherstructure )
292  
293 Append another structure.
294  
295 =cut
296 0 ▶sub pluseq {
Subroutine pluseq: not called CC=1 SCAR=6.9
Pod documented
297 0 my ($self, $sct) = @_;
298 0 return _pluseq($self, $sct);
299 }
300  
301 =head2 $structure->_plus( $otherstructure )
302  
303 Internal method for merging two structures.
304  
305 =cut
306 0 ▶sub _plus {
Subroutine _plus: not called CC=1 SCAR=6.9
307 0 my ($sct1, $sct2, %op) = @_;
308 0 my $result = _sctclone($sct1);
309 0 _pluseq($result, $sct2);
310 0 return $result;
311 }
312  
313 =head2 $structure->_pluseq( $otherstructure )
314  
315 Internal method for returning two structures.
316  
317 =cut
318 0 ▶sub _pluseq {
Subroutine _pluseq: not called CC=1 SCAR=6.9
319 0 my ($sct1, $sct2, %op) = @_;
320 0 return _sctdeal(
321 $sct1,
322 $sct2,
323 \&_plus_array,
324 \&_plus_hash,
325 \&_plus_scalar,
326 %op,
327 );
328 }
329  
330 =head2 $structure->_plus_hash( $otherstructure )
331  
332 Return the current hash plus another hash
333  
334 =cut
335 0 ▶sub _plus_hash {
Subroutine _plus_hash: not called CC=3 SCAR=24.8
336 0 my ($sct1, $sct2, %op) = @_;
337 0 foreach (keys(%{$sct2})) {
338 0 ▶ if(defined($sct1->{$_})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sct1->{$_}) { } 0 0 0
339 0 _pluseq($sct1->{$_}, $sct2->{$_}, %op);
340 } else {
341 0 $sct1->{$_} = _clone($sct2->{$_});
342 }
343 }
344 0 return $sct1;
345 }
346  
347 =head2 $structure->_plus_array( $otherstructure )
348  
349 Return the current array plus another array
350  
351 =cut
352 0 ▶sub _plus_array {
Subroutine _plus_array: not called CC=2 SCAR=17.9
353 0 my ($sct1, $sct2, %op) = @_;
354 # Array would simply clone all the elements
355 0 foreach (@{$sct2}) {
356 0 push @{$sct1}, _clone($_);
357 }
358 0 return;
359 }
360  
361 =head2 $structure->_plus_scalar( $otherstructure )
362  
363 Deal with conflicting scalar data (atm we ignore)
364  
365 =cut
366 sub _plus_scalar
367 0 ▶{
Subroutine _plus_scalar: not called CC=1 SCAR=6.9
368 0 my ($sct1, $sct2, %op) = @_;
369 # We do not replace
370 0 return $sct1;
371 }
372  
373 =head2 $structure->subtract( $otherstructure )
374  
375 Return the current structure minus a sub structure
376  
377 =cut
378 0 ▶sub subtract {
Subroutine subtract: not called CC=1 SCAR=6.9
Pod documented
379 0 my ($sct1, $sct2, %op) = @_;
380 0 my $result = _sctclone($sct1);
381 0 _subeq($result, $sct2);
382 0 return $result;
383 }
384  
385 =head2 $structure->subeq( $otherstructure )
386  
387 Remove a sub structure from the current structure.
388  
389 =cut
390 0 ▶sub subeq {
Subroutine subeq: not called CC=1 SCAR=6.9
Pod documented
391 0 my ($sct1, $sct2, %op) = @_;
392 0 return _sctdeal(
393 $sct1,
394 $sct2,
395 \&_sub_array,
396 \&_sub_hash,
397 \&_sub_scalar,
398 %op,
399 );
400 }
401  
402 =head2 $structure->_sub_array( $otherstructure )
403  
404 Remove array elements using structure (NOT FINISHED).
405  
406 =cut
407 0 ▶sub _sub_array {
Subroutine _sub_array: not called CC=1 SCAR=6.9
408 0 my ($sct1, $sct2) = @_;
409 # Not finished, this will require
410 # The ability to remove array elements
411 # that are the same as those specified
412 # And this is more useful for hashes
413 # than arrays
414 0 return $sct1;
415 }
416  
417 =head2 $structure->_sub_hash( $otherstructure )
418  
419 Return the current hash minus a sub hash
420  
421 =cut
422 0 ▶sub _sub_hash {
Subroutine _sub_hash: not called CC=5 SCAR=34.0
423 0 my ($sct1, $sct2) = @_;
424 0 foreach (%{$sct2}) {
425 0 ▶ if($sct1->{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sct1->{$_}) 0 0 0
426 0 ▶ if(not defined(subeq($sct1->{$_}, $sct2->{$_}))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined subeq($sct1->{$_}, $sct2->{$_})) 0 0 0
427 0 delete($sct1->{$_});
428 }
429 }
430 }
431 0 ▶ return undef if not keys(%{$sct1});
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$sct1;} 0 0 0
432 0 return $sct1;
433 }
434  
435 =head2 $structure->_sub_scalar( $otherstructure )
436  
437 Remove a scalar so long as it's eq
438  
439 =cut
440 0 ▶sub _sub_scalar {
Subroutine _sub_scalar: not called CC=3 SCAR=24.8
441 0 my ($sct1, $sct2) = @_;
442 0 ▶ return undef if(not defined($sct2));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sct2 0 0 0
443 0 ▶ return undef if($sct1 eq $sct2);
Branch: 1 branch
BranchTrueFalseTotal
if $sct1 eq $sct2 0 0 0
444 0 return $sct1;
445 }
446  
447 =head2 $structure->_sctref( $otherstructure )
448  
449 Get the structure reference and the object.
450  
451 =cut
452 54 ▶sub _sctref {
Subroutine _sctref: called CC=2 SCAR=6.9
453 54 my ($sct) = @_;
454 54 my $st = $sct;
455 54 ▶ $st = $sct->structure() if(ref($sct) eq "Data::Validate::Structure");
Branch: 1 branch
BranchTrueFalseTotal
if ref $sct eq "Data::Validate::Structure" 4 50 54
456 54 my $ref = ref($st);
457 54 return ($st, $ref);
458 }
459  
460 =head2 $structure->_clone( $otherstructure )
461  
462 Make a clone of a structure.
463  
464 =cut
465 0 ▶sub _clone {
Subroutine _clone: not called CC=6 SCAR=37.4
466 0 my ($sct) = @_;
467 0 ▶ return $sct if not ref($sct);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $sct 0 0 0
468 0 my ($st, $ref) = _sctref($sct);
469 0 my $result;
470 0 ▶ if($ref eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq 'ARRAY') { } 0 0 0
elsif ($ref eq 'HASH') { } 0 0 0
471 0 $result = [];
472 0 foreach (@{$st}) {
473 0 push @{$result}, _clone($_);
474 }
475 } elsif($ref eq 'HASH') {
476 0 $result = {};
477 0 foreach (keys(%{$st})) {
478 0 $result->{$_} = _clone($st->{$_});
479 }
480 } else {
481 # This is for all other kinds of objects
482 0 $result = $st;
483 }
484 0 return $result;
485 }
486  
487 =head2 $structure->_sctclone( $otherstructure )
488  
489 Make a structure object clone.
490  
491 =cut
492 0 ▶sub _sctclone {
Subroutine _sctclone: not called CC=1 SCAR=6.9
493 0 my ($sct) = @_;
494 0 return Structure->new( Structure => _clone($sct) );
495 }
496  
497 =head2 $structure->_sctdeal( $otherstructure )
498  
499 Sort out each request so that it goes to the right place
500 and so that the comparisons are fair.
501  
502 =cut
503 sub _sctdeal
504 27 ▶{
Subroutine _sctdeal: called CC=4 SCAR=14.2
505 27 my ($sct1, $sct2, $arraysub, $hashsub, $othersub, %op) = @_;
506 27 my ($st1,$ref1) = _sctref($sct1);
507 27 my ($st2,$ref2) = _sctref($sct2);
508  
509 27 ▶ if($ref1 eq $ref2) { # and defined($st1) and defined($st2)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref1 eq $ref2) 27 0 27
510 #return $sct1 if $op{'SkipSame'} and $sct1 eq $sct2;
511 27 ▶ if($ref1 eq "ARRAY") {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref1 eq 'ARRAY') { } 0 27 27
elsif ($ref1 eq 'HASH') { } 5 22 27
512 0 return $arraysub->($st1, $st2, %op);
513 } elsif($ref1 eq "HASH") {
514 5 return $hashsub->($st1, $st2, %op);
515 } else {
516 22 return $othersub->($st1, $st2, %op);
517 }
518 }
519  
520 }
521  
522 =head1 AUTHOR
523  
524 Copyright, Martin Owens 2005-2008, Affero General Public License (AGPL)
525  
526 http://www.fsf.org/licensing/licenses/agpl-3.0.html
527  
528 =cut
529 3 1;
« XSD.pm ↑ Summary XSD/ParseXML.pm »