File Coverage

blib/lib/Test/Proto/Compare.pm
Criterion Covered Total %
statement 30 31 96.7
branch 4 4 100.0
condition n/a
subroutine 14 15 93.3
pod 8 9 88.8
total 56 59 94.9


line stmt bran cond sub pod time code
1             package Test::Proto::Compare;
2 7     7   23896 use strict;
  7         17  
  7         252  
3 7     7   37 use warnings;
  7         13  
  7         176  
4 7     7   813 use Moo;
  7         15951  
  7         46  
5 7     7   3890 use Test::Proto::Common;
  7         16  
  7         1063  
6             use overload
7             '&{}' => \&compare,
8 7     7   1615 '""' => sub { $_[0]->summary };
  7     0   1018  
  7         102  
  0         0  
9              
10             has 'code',
11             is => 'rw',
12             default => sub {
13             sub { $_[0] cmp $_[1] }
14             };
15              
16             has 'reversed',
17             is => 'rw',
18             default => sub { 0 };
19              
20             has 'summary',
21             is => 'rw',
22             default => sub { 'cmp' };
23              
24             around 'code', 'reversed', 'summary', \&Test::Proto::Common::chainable;
25              
26             sub reverse {
27 7     7 1 79 my $self = shift;
28 7         204 $self->reversed( !$self->reversed );
29 7         27 return $self;
30             }
31              
32             sub compare {
33 138     138 1 330 my ( $self, $A, $B ) = @_;
34 138 100       3539 if ( $self->reversed ) {
35 12         327 return $self->code->( $B, $A );
36             }
37             else {
38 126         3059 return $self->code->( $A, $B );
39             }
40             }
41              
42 4     4 1 11 sub eq { shift->compare(@_) == 0 }
43 4     4 1 31 sub ne { shift->compare(@_) != 0 }
44              
45 3     3 1 34 sub gt { shift->compare(@_) > 0 }
46 3     3 1 32 sub ge { shift->compare(@_) >= 0 }
47              
48 3     3 1 39 sub lt { shift->compare(@_) < 0 }
49 12     12 1 67 sub le { shift->compare(@_) <= 0 }
50              
51             sub BUILDARGS {
52 67     67 0 7561 my $class = shift;
53 67 100       1706 return { ( exists $_[0] ? ( code => $_[0] ) : () ) };
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 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 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             1;