Venus/Role/Comparable.pm

filter: Statementstmt 86.4% 64 / 74 Branchbran 75.0% 48 / 64 Conditioncond 71.4% 15 / 21 MC/DCmcdc 57.1% 8 / 14 Subroutinesub 100% 14 / 14 Podpod 90.0% 9 / 10 totaltotal 80.2% 158 / 197 CC 40 SCAR 22.4
CC
40
Cov
80%
lt
18.1
gt
14.9
eq
10.8
CRAP 9.4
« Venus/Throw.pm ↑ Summary Venus/Role.pm »
1 package Venus::Role::Comparable;
2  
3 61 ▶use 5.018;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 61 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 61 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 61 ▶use Venus::Role 'with';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 61 require Scalar::Util;
11 61 require Venus::Type;
12  
13 # METHODS
14  
15 sub eq {
16 440 ▶ my ($self, $data) = @_;
Subroutine eq: called CC=10 SCAR=23.8
Pod documented
17  
18 440 $data = Venus::Type->new(value => $data)->deduce;
19  
20 440 ▶ if (Scalar::Util::refaddr($self) eq Scalar::Util::refaddr($data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::refaddr($self) eq builtin::refaddr($data)) 2 438 440
21 2 return 1;
22 }
23 438 ▶ if (Scalar::Util::blessed($data) && !$data->isa('Venus::Kind')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($data) and not $data->isa("Venus::Kind")) 0 438 438
Condition33%1/3
builtin::blessed($data) and not $data->isa("Venus::Kind")
!ll&&!rl&&r
04380
MC/DC0%0/2
builtin::blessed($data) and not $data->isa("Venus::Kind")
builtin::blessed($data) not $data->isa("Venus::Kind")
Truth table33%1/3
A and not(B)
A= builtin::blessed($data)B= not $data->isa("Venus::Kind")
ABresult
0X0
100
111
24 0 return 0;
25 }
26 438 ▶ if ($self->comparer('eq') eq 'numified') {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->comparer('eq') eq 'numified') { } 186 252 438
elsif ($self->comparer('eq') eq 'stringified') { } 80 172 252
elsif (my $method = $self->comparer('eq')) { } 172 0 172
27 186 ▶ return $self->numified == $data->numified ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->numified == $data->numified ? : 106 80 186
28 }
29 elsif ($self->comparer('eq') eq 'stringified') {
30 80 ▶ return $self->stringified eq $data->stringified ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->stringified eq $data->stringified ? : 19 61 80
31 }
32 elsif (my $method = $self->comparer('eq')) {
33 172 ▶ return $self->$method eq $data->$method ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->$method eq $data->$method ? : 25 147 172
34 }
35 else {
36 0 return 0;
37 }
38 }
39  
40 sub ge {
41 187 ▶ my ($self, $data) = @_;
Subroutine ge: called CC=3 SCAR=11.0
Pod documented
42  
43 187 ▶ if ($self->gt($data) || $self->eq($data)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->gt($data) or $self->eq($data)) { } 102 85 187
Condition100%3/3
$self->gt($data) or $self->eq($data)
l!l&&r!l&&!r
544885
MC/DC100%2/2
$self->gt($data) or $self->eq($data)
$self->gt($data) $self->eq($data)
Truth table100%3/3
A or B
A= $self->gt($data)B= $self->eq($data)
ABresult
1X1
011
000
44 102 return 1;
45 }
46 else {
47 85 return 0;
48 }
49 }
50  
51 sub gele {
52 94 ▶ my ($self, $ge, $le) = @_;
Subroutine gele: called CC=3 SCAR=11.0
Pod documented
53  
54 94 ▶ if ($self->ge($ge) && $self->le($le)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ge($ge) and $self->le($le)) { } 21 73 94
Condition100%3/3
$self->ge($ge) and $self->le($le)
!ll&&!rl&&r
423121
MC/DC100%2/2
$self->ge($ge) and $self->le($le)
$self->ge($ge) $self->le($le)
Truth table100%3/3
A and B
A= $self->ge($ge)B= $self->le($le)
ABresult
0X0
100
111
55 21 return 1;
56 }
57 else {
58 73 return 0;
59 }
60 }
61  
62 sub gt {
63 375 ▶ my ($self, $data) = @_;
Subroutine gt: called CC=10 SCAR=27.0
Pod documented
64  
65 375 $data = Venus::Type->new(value => $data)->deduce;
66  
67 375 ▶ if (Scalar::Util::refaddr($self) eq Scalar::Util::refaddr($data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::refaddr($self) eq builtin::refaddr($data)) 1 374 375
68 1 return 0;
69 }
70 374 ▶ if (Scalar::Util::blessed($data) && !$data->isa('Venus::Kind')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($data) and not $data->isa("Venus::Kind")) 0 374 374
Condition33%1/3
builtin::blessed($data) and not $data->isa("Venus::Kind")
!ll&&!rl&&r
03740
MC/DC0%0/2
builtin::blessed($data) and not $data->isa("Venus::Kind")
builtin::blessed($data) not $data->isa("Venus::Kind")
Truth table33%1/3
A and not(B)
A= builtin::blessed($data)B= not $data->isa("Venus::Kind")
ABresult
0X0
100
111
71 0 return 0;
72 }
73 374 ▶ if ($self->comparer('gt') eq 'numified') {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->comparer('gt') eq 'numified') { } 302 72 374
elsif ($self->comparer('gt') eq 'stringified') { } 72 0 72
elsif (my $method = $self->comparer('gt')) { } 0 0 0
74 302 ▶ return $self->numified > $data->numified ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->numified > $data->numified ? : 85 217 302
75 }
76 elsif ($self->comparer('gt') eq 'stringified') {
77 72 ▶ return $self->stringified gt $data->stringified ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->stringified gt $data->stringified ? : 24 48 72
78 }
79 elsif (my $method = $self->comparer('gt')) {
80 0 ▶ return $self->$method gt $data->$method ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->$method gt $data->$method ? : 0 0 0
81 }
82 else {
83 0 return 0;
84 }
85 }
86  
87 sub gtlt {
88 94 ▶ my ($self, $gt, $lt) = @_;
Subroutine gtlt: called CC=3 SCAR=11.0
Pod documented
89  
90 94 ▶ if ($self->gt($gt) && $self->lt($lt)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->gt($gt) and $self->lt($lt)) { } 2 92 94
Condition100%3/3
$self->gt($gt) and $self->lt($lt)
!ll&&!rl&&r
66262
MC/DC100%2/2
$self->gt($gt) and $self->lt($lt)
$self->gt($gt) $self->lt($lt)
Truth table100%3/3
A and B
A= $self->gt($gt)B= $self->lt($lt)
ABresult
0X0
100
111
91 2 return 1;
92 }
93 else {
94 92 return 0;
95 }
96 }
97  
98 sub lt {
99 268 ▶ my ($self, $data) = @_;
Subroutine lt: called CC=10 SCAR=29.0
Pod documented
100  
101 268 $data = Venus::Type->new(value => $data)->deduce;
102  
103 268 ▶ if (Scalar::Util::refaddr($self) eq Scalar::Util::refaddr($data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::refaddr($self) eq builtin::refaddr($data)) 0 268 268
104 0 return 0;
105 }
106 268 ▶ if (Scalar::Util::blessed($data) && !$data->isa('Venus::Kind')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($data) and not $data->isa("Venus::Kind")) 0 268 268
Condition33%1/3
builtin::blessed($data) and not $data->isa("Venus::Kind")
!ll&&!rl&&r
02680
MC/DC0%0/2
builtin::blessed($data) and not $data->isa("Venus::Kind")
builtin::blessed($data) not $data->isa("Venus::Kind")
Truth table33%1/3
A and not(B)
A= builtin::blessed($data)B= not $data->isa("Venus::Kind")
ABresult
0X0
100
111
107 0 return 0;
108 }
109 268 ▶ if ($self->comparer('lt') eq 'numified') {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->comparer('lt') eq 'numified') { } 217 51 268
elsif ($self->comparer('lt') eq 'stringified') { } 51 0 51
elsif (my $method = $self->comparer('lt')) { } 0 0 0
110 217 ▶ return $self->numified < $data->numified ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->numified < $data->numified ? : 60 157 217
111 }
112 elsif ($self->comparer('lt') eq 'stringified') {
113 51 ▶ return $self->stringified lt $data->stringified ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->stringified lt $data->stringified ? : 18 33 51
114 }
115 elsif (my $method = $self->comparer('lt')) {
116 0 ▶ return $self->$method lt $data->$method ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->$method lt $data->$method ? : 0 0 0
117 }
118 else {
119 0 return 0;
120 }
121 }
122  
123 sub le {
124 146 ▶ my ($self, $data) = @_;
Subroutine le: called CC=3 SCAR=11.0
Pod documented
125  
126 146 ▶ if ($self->lt($data) || $self->eq($data)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->lt($data) or $self->eq($data)) { } 82 64 146
Condition100%3/3
$self->lt($data) or $self->eq($data)
l!l&&r!l&&!r
394364
MC/DC100%2/2
$self->lt($data) or $self->eq($data)
$self->lt($data) $self->eq($data)
Truth table100%3/3
A or B
A= $self->lt($data)B= $self->eq($data)
ABresult
1X1
011
000
127 82 return 1;
128 }
129 else {
130 64 return 0;
131 }
132 }
133  
134 sub ne {
135 94 ▶ my ($self, $data) = @_;
Subroutine ne: called CC=2 SCAR=6.9
Pod documented
136  
137 94 ▶ return $self->eq($data) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->eq($data) ? : 24 70 94
138 }
139  
140 sub tv {
141 95 ▶ my ($self, $data) = @_;
Subroutine tv: called CC=4 SCAR=14.0
Pod documented
142  
143 95 ▶ if (!Scalar::Util::blessed($data)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($data)) 0 95 95
144 0 return 0;
145 }
146 95 ▶ if (Scalar::Util::refaddr($self) eq Scalar::Util::refaddr($data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::refaddr($self) eq builtin::refaddr($data)) 1 94 95
147 1 return 1;
148 }
149 94 ▶ if ($data->isa($self->class)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->isa($self->class)) { } 12 82 94
150 12 return $self->eq($data);
151 }
152 else {
153 82 return 0;
154 }
155 }
156  
157 # EXPORTS
158  
159 sub EXPORT {
160 62 ▶ ['eq', 'ge', 'gele', 'gt', 'gtlt', 'lt', 'le', 'ne', 'tv']
Subroutine EXPORT: called CC=1 SCAR=0.0
Pod undocumented
161 }
162  
163 61 1;
164  
165  
166  
167 =head1 NAME
168  
169 Venus::Role::Comparable - Comparable Role
170  
171 =cut
172  
173 =head1 ABSTRACT
174  
175 Comparable Role for Perl 5
176  
177 =cut
178  
179 =head1 SYNOPSIS
180  
181 package Example;
182  
183 use Venus::Class;
184  
185 base 'Venus::Kind';
186  
187 with 'Venus::Role::Comparable';
188  
189 sub numified {
190 return 2;
191 }
192  
193 package main;
194  
195 my $example = Example->new;
196  
197 # my $result = $example->eq(2);
198  
199 =cut
200  
201 =head1 DESCRIPTION
202  
203 This package modifies the consuming package and provides methods for performing
204 numerical and stringwise comparision operations or any object or raw data type.
205  
206 =cut
207  
208 =head1 METHODS
209  
210 This package provides the following methods:
211  
212 =cut
213  
214 =head2 eq
215  
216 eq(Any $arg) (Bool)
217  
218 The eq method performs an I<"equals"> operation using the invocant and the
219 argument provided. The operation will be performed as either a numerical or
220 stringwise operation based upon the preference (i.e. the return value of the
221 L</comparer> method) of the invocant.
222  
223 I<Since C<0.08>>
224  
225 =over 4
226  
227 =item eq example 1
228  
229 package main;
230  
231 my $example = Example->new;
232  
233 my $result = $example->eq($example);
234  
235 # 1
236  
237 =back
238  
239 =over 4
240  
241 =item eq example 2
242  
243 package main;
244  
245 my $example = Example->new;
246  
247 my $result = $example->eq([1,2]);
248  
249 # 0
250  
251 =back
252  
253 =over 4
254  
255 =item eq example 3
256  
257 package main;
258  
259 my $example = Example->new;
260  
261 my $result = $example->eq({1..4});
262  
263 # 0
264  
265 =back
266  
267 =cut
268  
269 =head2 ge
270  
271 ge(Any $arg) (Bool)
272  
273 The ge method performs a I<"greater-than-or-equal-to"> operation using the
274 invocant and argument provided. The operation will be performed as either a
275 numerical or stringwise operation based upon the preference (i.e. the return
276 value of the L</comparer> method) of the invocant.
277  
278 I<Since C<0.08>>
279  
280 =over 4
281  
282 =item ge example 1
283  
284 package main;
285  
286 my $example = Example->new;
287  
288 my $result = $example->ge(3);
289  
290 # 0
291  
292 =back
293  
294 =over 4
295  
296 =item ge example 2
297  
298 package main;
299  
300 my $example = Example->new;
301  
302 my $result = $example->ge($example);
303  
304 # 1
305  
306 =back
307  
308 =over 4
309  
310 =item ge example 3
311  
312 package main;
313  
314 my $example = Example->new;
315  
316 my $result = $example->ge([1,2,3]);
317  
318 # 0
319  
320 =back
321  
322 =cut
323  
324 =head2 gele
325  
326 gele(Any $arg1, Any $arg2) (Bool)
327  
328 The gele method performs a I<"greater-than-or-equal-to"> operation on the 1st
329 argument, and I<"lesser-than-or-equal-to"> operation on the 2nd argument. The
330 operation will be performed as either a numerical or stringwise operation based
331 upon the preference (i.e. the return value of the L</comparer> method) of the
332 invocant.
333  
334 I<Since C<0.08>>
335  
336 =over 4
337  
338 =item gele example 1
339  
340 package main;
341  
342 my $example = Example->new;
343  
344 my $result = $example->gele(1, 3);
345  
346 # 1
347  
348 =back
349  
350 =over 4
351  
352 =item gele example 2
353  
354 package main;
355  
356 my $example = Example->new;
357  
358 my $result = $example->gele(2, []);
359  
360 # 0
361  
362 =back
363  
364 =over 4
365  
366 =item gele example 3
367  
368 package main;
369  
370 my $example = Example->new;
371  
372 my $result = $example->gele(0, '3');
373  
374 # 1
375  
376 =back
377  
378 =cut
379  
380 =head2 gt
381  
382 gt(Any $arg) (Bool)
383  
384 The gt method performs a I<"greater-than"> operation using the invocant and
385 argument provided. The operation will be performed as either a numerical or
386 stringwise operation based upon the preference (i.e. the return value of the
387 L</comparer> method) of the invocant.
388  
389 I<Since C<0.08>>
390  
391 =over 4
392  
393 =item gt example 1
394  
395 package main;
396  
397 my $example = Example->new;
398  
399 my $result = $example->gt({1..2});
400  
401 # 0
402  
403 =back
404  
405 =over 4
406  
407 =item gt example 2
408  
409 package main;
410  
411 my $example = Example->new;
412  
413 my $result = $example->gt(1.9998);
414  
415 # 1
416  
417 =back
418  
419 =over 4
420  
421 =item gt example 3
422  
423 package main;
424  
425 my $example = Example->new;
426  
427 my $result = $example->gt(\1_000_000);
428  
429 # 0
430  
431 =back
432  
433 =cut
434  
435 =head2 gtlt
436  
437 gtlt(Any $arg1, Any $arg2) (Bool)
438  
439 The gtlt method performs a I<"greater-than"> operation on the 1st argument, and
440 I<"lesser-than"> operation on the 2nd argument. The operation will be performed
441 as either a numerical or stringwise operation based upon the preference (i.e.
442 the return value of the L</comparer> method) of the invocant.
443  
444 I<Since C<0.08>>
445  
446 =over 4
447  
448 =item gtlt example 1
449  
450 package main;
451  
452 my $example = Example->new;
453  
454 my $result = $example->gtlt('1', 3);
455  
456 # 1
457  
458 =back
459  
460 =over 4
461  
462 =item gtlt example 2
463  
464 package main;
465  
466 my $example = Example->new;
467  
468 my $result = $example->gtlt({1..2}, {1..4});
469  
470 # 0
471  
472 =back
473  
474 =over 4
475  
476 =item gtlt example 3
477  
478 package main;
479  
480 my $example = Example->new;
481  
482 my $result = $example->gtlt('.', ['.']);
483  
484 # 1
485  
486 =back
487  
488 =cut
489  
490 =head2 le
491  
492 le(Any $arg) (Bool)
493  
494 The le method performs a I<"lesser-than-or-equal-to"> operation using the
495 invocant and argument provided. The operation will be performed as either a
496 numerical or stringwise operation based upon the preference (i.e. the return
497 value of the L</comparer> method) of the invocant.
498  
499 I<Since C<0.08>>
500  
501 =over 4
502  
503 =item le example 1
504  
505 package main;
506  
507 my $example = Example->new;
508  
509 my $result = $example->le('9');
510  
511 # 1
512  
513 =back
514  
515 =over 4
516  
517 =item le example 2
518  
519 package main;
520  
521 my $example = Example->new;
522  
523 my $result = $example->le([1..2]);
524  
525 # 1
526  
527 =back
528  
529 =over 4
530  
531 =item le example 3
532  
533 package main;
534  
535 my $example = Example->new;
536  
537 my $result = $example->le(\1);
538  
539 # 0
540  
541 =back
542  
543 =cut
544  
545 =head2 lt
546  
547 lt(Any $arg) (Bool)
548  
549 The lt method performs a I<"lesser-than"> operation using the invocant and
550 argument provided. The operation will be performed as either a numerical or
551 stringwise operation based upon the preference (i.e. the return value of the
552 L</comparer> method) of the invocant.
553  
554 I<Since C<0.08>>
555  
556 =over 4
557  
558 =item lt example 1
559  
560 package main;
561  
562 my $example = Example->new;
563  
564 my $result = $example->lt(qr/.*/);
565  
566 # 1
567  
568 =back
569  
570 =over 4
571  
572 =item lt example 2
573  
574 package main;
575  
576 my $example = Example->new;
577  
578 my $result = $example->lt('.*');
579  
580 # 0
581  
582 =back
583  
584 =over 4
585  
586 =item lt example 3
587  
588 package main;
589  
590 my $example = Example->new;
591  
592 my $result = $example->lt('5');
593  
594 # 1
595  
596 =back
597  
598 =cut
599  
600 =head2 ne
601  
602 ne(Any $arg) (Bool)
603  
604 The ne method performs a I<"not-equal-to"> operation using the invocant and
605 argument provided. The operation will be performed as either a numerical or
606 stringwise operation based upon the preference (i.e. the return value of the
607 L</comparer> method) of the invocant.
608  
609 I<Since C<0.08>>
610  
611 =over 4
612  
613 =item ne example 1
614  
615 package main;
616  
617 my $example = Example->new;
618  
619 my $result = $example->ne([1,2]);
620  
621 # 1
622  
623 =back
624  
625 =over 4
626  
627 =item ne example 2
628  
629 package main;
630  
631 my $example = Example->new;
632  
633 my $result = $example->ne([2]);
634  
635 # 1
636  
637 =back
638  
639 =over 4
640  
641 =item ne example 3
642  
643 package main;
644  
645 my $example = Example->new;
646  
647 my $result = $example->ne(qr/2/);
648  
649 # 1
650  
651 =back
652  
653 =cut
654  
655 =head2 tv
656  
657 tv(Any $arg) (Bool)
658  
659 The tv method performs a I<"type-and-value-equal-to"> operation using the
660 invocant and argument provided. The operation will be performed as either a
661 numerical or stringwise operation based upon the preference (i.e. the return
662 value of the L</comparer> method) of the invocant.
663  
664 I<Since C<0.08>>
665  
666 =over 4
667  
668 =item tv example 1
669  
670 package main;
671  
672 my $example = Example->new;
673  
674 my $result = $example->tv($example);
675  
676 # 1
677  
678 =back
679  
680 =over 4
681  
682 =item tv example 2
683  
684 package main;
685  
686 use Venus::Number;
687  
688 my $example = Example->new;
689  
690 my $result = $example->tv(Venus::Number->new(2));
691  
692 # 0
693  
694 =back
695  
696 =over 4
697  
698 =item tv example 3
699  
700 package main;
701  
702 use Venus::String;
703  
704 my $example = Example->new;
705  
706 my $result = $example->tv(Venus::String->new('2'));
707  
708 # 0
709  
710 =back
711  
712 =over 4
713  
714 =item tv example 4
715  
716 package main;
717  
718 use Venus::String;
719  
720 my $example = Example->new;
721  
722 my $result = $example->tv(Example->new);
723  
724 # 1
725  
726 =back
727  
728 =cut
« Venus/Throw.pm ↑ Summary Venus/Role.pm »