| 1 | package Test::Proto::Compare; | ||||||||||
| 2 | 7 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 7 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 7 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 7 | ▶ | use Test::Proto::Common; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | use overload | ||||||||||
| 7 | 7 | ▶ | '&{}' => \&compare, | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.2
| |||||||||||
| 8 | 0 | ▶ | '""' => sub { $_[0]->summary }; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.2
| |||||||||||
| 9 | |||||||||||
| 10 | has 'code', | ||||||||||
| 11 | is => 'rw', | ||||||||||
| 12 | 40 | ▶ | default => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 57 | ▶ | sub { $_[0] cmp $_[1] } | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 40 | }; | |||||||||
| 15 | |||||||||||
| 16 | has 'reversed', | ||||||||||
| 17 | is => 'rw', | ||||||||||
| 18 | 89 | ▶ | default => sub { 0 }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 19 | |||||||||||
| 20 | has 'summary', | ||||||||||
| 21 | is => 'rw', | ||||||||||
| 22 | 67 | ▶ | default => sub { 'cmp' }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 23 | |||||||||||
| 24 | 7 | around 'code', 'reversed', 'summary', \&Test::Proto::Common::chainable; | |||||||||
| 25 | |||||||||||
| 26 | 7 | ▶ | sub reverse { | ||||||||
|
Subroutine
reverse: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 27 | 7 | my $self = shift; | |||||||||
| 28 | 7 | $self->reversed( !$self->reversed ); | |||||||||
| 29 | 7 | return $self; | |||||||||
| 30 | } | ||||||||||
| 31 | |||||||||||
| 32 | 138 | ▶ | sub compare { | ||||||||
|
Subroutine
compare: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 33 | 138 | my ( $self, $A, $B ) = @_; | |||||||||
| 34 | 138 | ▶ | if ( $self->reversed ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | 12 | return $self->code->( $B, $A ); | |||||||||
| 36 | } | ||||||||||
| 37 | else { | ||||||||||
| 38 | 126 | return $self->code->( $A, $B ); | |||||||||
| 39 | } | ||||||||||
| 40 | } | ||||||||||
| 41 | |||||||||||
| 42 | 4 | ▶ | sub eq { shift->compare(@_) == 0 } | ||||||||
|
Subroutine
eq: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 43 | 4 | ▶ | sub ne { shift->compare(@_) != 0 } | ||||||||
|
Subroutine
ne: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 44 | |||||||||||
| 45 | 3 | ▶ | sub gt { shift->compare(@_) > 0 } | ||||||||
|
Subroutine
gt: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 46 | 3 | ▶ | sub ge { shift->compare(@_) >= 0 } | ||||||||
|
Subroutine
ge: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 47 | |||||||||||
| 48 | 3 | ▶ | sub lt { shift->compare(@_) < 0 } | ||||||||
|
Subroutine
lt: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 49 | 12 | ▶ | sub le { shift->compare(@_) <= 0 } | ||||||||
|
Subroutine
le: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 50 | |||||||||||
| 51 | 67 | ▶ | sub BUILDARGS { | ||||||||
|
Subroutine
BUILDARGS: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 52 | 67 | my $class = shift; | |||||||||
| 53 | 67 | ▶ | return { ( exists $_[0] ? ( code => $_[0] ) : () ) }; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | =head1 NAME | ||||||||||
| 57 | |||||||||||
| 58 | Test::Proto::Compare - wrapper for comparison functions | ||||||||||
| 59 | |||||||||||
| 60 | =head1 SYNOPSIS | ||||||||||
| 61 | |||||||||||
| 62 | my $c = Test::Proto::Compare->new(sub {lc $_[0] cmp lc $_[1]}); | ||||||||||
| 63 | $c->summary('lc cmp'); | ||||||||||
| 64 | $c->compare($left, $right); # lc $left cmp $right | ||||||||||
| 65 | $c->reverse->compare($left, $right); # lc $right cmp lc $left | ||||||||||
| 66 | |||||||||||
| 67 | This class provides a wrapper for comparison functions so they can be identified by formatters. | ||||||||||
| 68 | |||||||||||
| 69 | =head1 METHODS | ||||||||||
| 70 | |||||||||||
| 71 | =head3 new | ||||||||||
| 72 | |||||||||||
| 73 | If an argument is passed, it replaces the C<code> attribute. | ||||||||||
| 74 | |||||||||||
| 75 | =head3 code | ||||||||||
| 76 | |||||||||||
| 77 | Chainable attribute containing the comparison code itself. | ||||||||||
| 78 | |||||||||||
| 79 | =head3 compare | ||||||||||
| 80 | |||||||||||
| 81 | Executes the comparison code, using reversed to determine whether to reverse the arguments. | ||||||||||
| 82 | |||||||||||
| 83 | =head3 summary | ||||||||||
| 84 | |||||||||||
| 85 | Chainable attribute; a brief human-readable description of the operation which will be performed. Default is 'cmp'. | ||||||||||
| 86 | |||||||||||
| 87 | =head3 reversed | ||||||||||
| 88 | |||||||||||
| 89 | Chainable attribute. 1 if the comparison is reversed, 0 otherwise. Default is 0. Also a chainable setter. | ||||||||||
| 90 | |||||||||||
| 91 | =head3 reverse | ||||||||||
| 92 | |||||||||||
| 93 | A chainable method which takes no arguments, and causes C<reversed> to be either 1 or 0 (whichever it previously wasn't). | ||||||||||
| 94 | |||||||||||
| 95 | =head3 eq, ne, gt, lt, ge, le | ||||||||||
| 96 | |||||||||||
| 97 | These run compare and return a true or false value depending on what compare returned. | ||||||||||
| 98 | |||||||||||
| 99 | =cut | ||||||||||
| 100 | |||||||||||
| 101 | 7 | 1; | |||||||||