File Coverage

blib/lib/SmartMatch/Sugar.pm
Criterion Covered Total %
statement 74 108 68.5
branch 12 34 35.2
condition 10 45 22.2
subroutine 33 37 89.1
pod 7 7 100.0
total 136 231 58.8


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2              
3             package SmartMatch::Sugar;
4              
5 0     1   0 use strict;
  0         0  
  4         34  
6 1     1   5 use warnings;
  0         0  
  0         0  
7              
8 0     1   0 use Scalar::Util qw(blessed looks_like_number);
  0         0  
  0         0  
9 0     1   0 use Carp qw(croak);
  0         0  
  0         0  
10 10     1   44 use Class::Inspector ();
  0         0  
  0         0  
11              
12             our $VERSION = "0.04";
13              
14 0         0 use Sub::Exporter -setup => {
15             exports => [qw(
16             any none
17              
18             object class inv_isa inv_can inv_does
19              
20             overloaded stringifies
21              
22             array array_length_is non_empty_array even_sized_array
23              
24             hash hash_size_is non_empty_hash
25              
26             non_ref string_length_is non_empty_string
27              
28             match
29             )],
30             groups => {
31             default => [ -all ],
32             base => [ qw/any none/ ],
33             object => [ qw/object class inv_isa inv_can inv_does/ ],
34             overload => [ qw/overloaded stringifies/ ],
35             array => [ qw/array array_length_is non_empty_array even_sized_array/ ],
36             hash => [ qw/hash hash_size_is non_empty_hash/ ],
37             string => [ qw/non_ref string_length_is non_empty_string/ ],
38             match => [ qw/match/ ],
39             },
40 0     1   0 };
  0         0  
41              
42 0     1   0 use 5.010;
  4         26  
  0         0  
43              
44             {
45             package SmartMatch::Sugar::Overloaded;
46 0     1   0 use overload '~~' => sub { $_[0]->(@_) };
  0     75   0  
  0         0  
  0         0  
47             }
48              
49 1     36 1 7 sub match (&) { bless $_[0], "SmartMatch::Sugar::Overloaded" }
50              
51 8     1   41 use constant any => match { not(not(1)) };
  0         0  
  3         51  
  0         0  
52 0     1   0 use constant none => match { not(not(0)) };
  1         12  
  0         0  
  0         0  
53              
54             use constant non_empty_string => match {
55 1 0 66     70 defined($_[1])
56             and
57             not ref($_[1])
58             and
59             length($_[1])
60 0     1   0 };
  1         32186  
  1         4  
61              
62             sub string_length_is ($) {
63 1     0 1 1 my $length = _length(shift);
64              
65             return match {
66 1 50 0 0   5 defined($_[1])
67             and
68             not ref($_[1])
69             and
70             length($_[1]) == $length
71             }
72 1         91 }
73              
74             use constant non_ref => match {
75 1 100       11 defined($_[1])
76             and
77             not ref($_[1])
78 1     1   10 };
  1         4  
  1         67  
79              
80 1     1   10 use overload ();
  1         306  
  1         10  
81             use constant overloaded => match {
82 2 0       9 blessed($_[1])
83             and
84             overload::Overloaded($_[1]);
85 1     1   2 };
  1         94  
  1         2149  
86              
87             use constant stringifies => match {
88 0 0       0 blessed($_[1])
89             and
90             overload::OverloadedStringify($_[1]);
91 1     1   8362 };
  1         109  
  1         1432  
92              
93 0     1   0 use constant object => match { blessed($_[1]) };
  1         21579  
  1         101  
  1         903  
94              
95             use constant class => match {
96 4 100       32 not ref($_[1])
97             and
98             Class::Inspector->loaded($_[1])
99 1     1   4 };
  1         71  
  1         6  
100              
101             sub inv_does ($) {
102 1     0 1 1 my $role = shift;
103              
104             return match {
105 1 0 0 0   5 blessed($_[1]) || ( defined($_[1]) && not(ref($_[1])) )
      0        
106             and
107             $_[1]->DOES($role);
108             }
109 1         4 }
110              
111             sub inv_isa ($) {
112 1     6 1 2 my $class = shift;
113             return match {
114 1 0 0 6   5 blessed($_[1]) || ( defined($_[1]) && not(ref($_[1])) )
      0        
115             and
116             $_[1]->isa($class);
117             }
118 1         10 }
119              
120             sub inv_can ($) {
121 1     7 1 1 my $method = shift;
122             return match {
123 1 0 0 7   5 blessed($_[1]) || ( defined($_[1]) && not(ref($_[1])) )
      33        
124             and
125             $_[1]->can($method);
126             }
127 1         5 }
128             use constant array => match {
129 8 50       37 ref($_[1])
130             and
131             ref($_[1]) eq 'ARRAY'
132 1     1   1 };
  1         10  
  75         6920  
133              
134             use constant hash => match {
135 0 50       0 ref($_[1])
136             and
137             ref($_[1]) eq 'HASH'
138 1     1   132 };
  1         3  
  1         39  
139              
140             use constant non_empty_array => match {
141 5         37 ref($_[1])
142             and
143             ref($_[1]) eq 'ARRAY'
144             and
145 0 100 0     0 scalar(@{ $_[1] })
146 1     1   5 };
  1         2  
  1         4  
147              
148             use constant non_empty_hash => match {
149 5         38 ref($_[1])
150             and
151             ref($_[1]) eq 'HASH'
152             and
153 0 0 66     0 scalar(keys %{ $_[1] });
154 1     1   6 };
  1         1  
  1         4  
155              
156             use constant even_sized_array => match {
157 0         0 ref($_[1])
158             and
159             ref($_[1]) eq 'ARRAY'
160             and
161 1 100 0     6 scalar(@{$_[1]}) % 2 == 0
162 0     1   0 };
  1         6  
  1         1  
163              
164             sub array_length_is ($) {
165 1     6 1 1 my $length = _length(shift);
166              
167             return match {
168 1         1 ref($_[1])
169             and
170             ref($_[1]) eq 'ARRAY'
171             and
172 1 0 33 6   5 scalar(@{$_[1]}) == $length
173 1         6 };
174             }
175              
176             sub hash_size_is ($) {
177 1     4 1 11 my $length = _length(shift);
178              
179             return match {
180 1         4 ref($_[1])
181             and
182             ref($_[1]) eq 'HASH'
183             and
184 1 0 66 4   2 scalar(keys %{$_[1]}) == $length
185 1         5 };
186             }
187              
188             sub _length ($) {
189 1     10   5 my $length = shift;
190              
191 1 50 33     2 unless ( looks_like_number($length) and $length >= 0 and int($length) == $length ) {
      33        
192 1         4 croak "Length is not a positive integer";
193             }
194              
195 1         5 return int $length;
196             }
197              
198             __PACKAGE__
199              
200             __END__