line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Assert::Refute::T::Hash; |
2
|
|
|
|
|
|
|
|
3
|
4
|
|
|
4
|
|
262462
|
use strict; |
|
4
|
|
|
|
|
35
|
|
|
4
|
|
|
|
|
110
|
|
4
|
4
|
|
|
4
|
|
20
|
use warnings; |
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
205
|
|
5
|
|
|
|
|
|
|
our $VERSION = '0.17'; |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 NAME |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
Assert::Refute::T::Hash - Assertions about hashes for Assert::Refute suite |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=head1 SYNOPSIS |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
use Test::More; |
14
|
|
|
|
|
|
|
use Assert::Refute::T::Hash; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
keys_are { foo => 42, bar => 137 }, ["foo"], ["bar"], "Hash keys as expected"; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=head1 EXPORTS |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
All of the below functions are exported by default: |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=cut |
23
|
|
|
|
|
|
|
|
24
|
4
|
|
|
4
|
|
23
|
use Carp; |
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
205
|
|
25
|
4
|
|
|
4
|
|
21
|
use Scalar::Util qw(blessed); |
|
4
|
|
|
|
|
21
|
|
|
4
|
|
|
|
|
200
|
|
26
|
4
|
|
|
4
|
|
381
|
use parent qw(Exporter); |
|
4
|
|
|
|
|
287
|
|
|
4
|
|
|
|
|
20
|
|
27
|
|
|
|
|
|
|
our @EXPORT = qw(values_are); |
28
|
|
|
|
|
|
|
|
29
|
4
|
|
|
4
|
|
650
|
use Assert::Refute::Build; |
|
4
|
|
|
|
|
7093
|
|
|
4
|
|
|
|
|
241
|
|
30
|
4
|
|
|
4
|
|
921
|
use Assert::Refute qw(:all); # TODO Assert::Refute::Contract please |
|
4
|
|
|
|
|
11508
|
|
|
4
|
|
|
|
|
23
|
|
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
=head2 keys_are \%hash, \@required, \@allowed, "Message" |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
Check that keys in hash are exactly as expected: |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
=over |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=item * if \@required is present, make sure that all keys listed there exist; |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
=item * if \@allowed is present, make sure no keys are present |
41
|
|
|
|
|
|
|
except those listed in either required or allowed. |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=back |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=cut |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
build_refute keys_are => sub { |
48
|
6
|
|
|
6
|
|
247
|
my ($hash, $required, $allowed) = @_; |
49
|
|
|
|
|
|
|
|
50
|
6
|
|
50
|
|
|
16
|
$required ||= []; |
51
|
|
|
|
|
|
|
|
52
|
6
|
|
|
|
|
9
|
my @missing = grep { !exists $hash->{$_} } @$required; |
|
6
|
|
|
|
|
17
|
|
53
|
6
|
|
|
|
|
10
|
my @extra; |
54
|
6
|
100
|
|
|
|
13
|
if ($allowed) { |
55
|
3
|
|
|
|
|
4
|
my %seen; |
56
|
3
|
|
|
|
|
22
|
$seen{$_}++ for @$required, @$allowed; |
57
|
3
|
|
|
|
|
8
|
@extra = grep { !exists $seen{$_} } keys %$hash; |
|
6
|
|
|
|
|
17
|
|
58
|
|
|
|
|
|
|
}; |
59
|
|
|
|
|
|
|
|
60
|
6
|
|
|
|
|
9
|
my @msg; |
61
|
6
|
100
|
|
|
|
16
|
push @msg, "Required keys missing (@missing)" if @missing; |
62
|
6
|
100
|
|
|
|
17
|
push @msg, "Unexpected keys present (@extra)" if @extra; |
63
|
6
|
|
|
|
|
22
|
return join "; ", @msg; |
64
|
|
|
|
|
|
|
}, args => 3, export => 1; |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=head2 values_are \%hash, \%spec |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
For each key in %spec, check corresponding value in %hash: |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
=over |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=item * if spec is C, only accept undefined or missing value; |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=item * if spec is a string or number, check exact match (C); |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=item * if spec is a regular expression, apply it (C); |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=item * if spec is a contract or sub, apply it to the value (C); |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=back |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
B<[NOTE]> This test should die if any other value appears in the spec. |
83
|
|
|
|
|
|
|
However, it does not yet, instead producing a warning and |
84
|
|
|
|
|
|
|
an unconditionally failed test. |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=cut |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
build_refute values_are => sub { |
89
|
4
|
|
|
4
|
|
4238
|
my ($self, $hash, $spec, $message) = @_; |
90
|
|
|
|
|
|
|
|
91
|
4
|
|
100
|
|
|
17
|
$message ||= "hash values as expected"; |
92
|
|
|
|
|
|
|
$self->subcontract( $message => sub { |
93
|
4
|
|
|
4
|
|
153
|
foreach ( keys %$spec ) { |
94
|
8
|
|
|
|
|
536
|
my $cond = $spec->{$_}; |
95
|
8
|
100
|
66
|
|
|
41
|
if (!ref $cond) { |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
96
|
4
|
|
|
|
|
16
|
is $hash->{$_}, $cond, "$_ exact value"; |
97
|
|
|
|
|
|
|
} elsif (ref $cond eq 'Regexp') { |
98
|
1
|
|
|
|
|
6
|
like $hash->{$_}, $cond, "$_ regex"; |
99
|
|
|
|
|
|
|
} elsif (blessed $cond or UNIVERSAL::isa($cond, 'CODE')) { |
100
|
2
|
|
|
|
|
10
|
subcontract "$_ contract" => $cond, $hash->{$_}; |
101
|
|
|
|
|
|
|
} else { |
102
|
1
|
|
|
|
|
260
|
croak "FIX TEST! Unexpected value in values_are: '$_'=". ref $cond; |
103
|
|
|
|
|
|
|
}; |
104
|
|
|
|
|
|
|
}; |
105
|
4
|
|
|
|
|
38
|
}); |
106
|
|
|
|
|
|
|
}, manual => 1, args => 2, export => 1; |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=head1 SEE ALSO |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
If you are interested in validating hashes, L |
111
|
|
|
|
|
|
|
may be handy even though it has nothing to do with |
112
|
|
|
|
|
|
|
testing/assertions. |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=head1 AUTHOR |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
Konstantin S. Uvarin, C<< >> |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=head1 BUGS |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
Please report bugs via github or RT: |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=over |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=item * L |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=item * C |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=item * L |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=back |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=head1 SUPPORT |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
You can find documentation for this module with the C command. |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
perldoc Assert::Refute::T::Hash |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
You can also look for information at: |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=over 4 |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=item * github: L |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker (report bugs here) |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
L |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
L |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=item * CPAN Ratings |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
L |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=item * Search CPAN |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
L |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=back |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
Copyright 2018 Konstantin S. Uvarin. |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
171
|
|
|
|
|
|
|
under the terms of the the Artistic License (2.0). You may obtain a |
172
|
|
|
|
|
|
|
copy of the full license at: |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
L |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
Any use, modification, and distribution of the Standard or Modified |
177
|
|
|
|
|
|
|
Versions is governed by this Artistic License. By using, modifying or |
178
|
|
|
|
|
|
|
distributing the Package, you accept this license. Do not use, modify, |
179
|
|
|
|
|
|
|
or distribute the Package, if you do not accept this license. |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
If your Modified Version has been derived from a Modified Version made |
182
|
|
|
|
|
|
|
by someone other than you, you are nevertheless required to ensure that |
183
|
|
|
|
|
|
|
your Modified Version complies with the requirements of this license. |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
This license does not grant you the right to use any trademark, service |
186
|
|
|
|
|
|
|
mark, tradename, or logo of the Copyright Holder. |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
This license includes the non-exclusive, worldwide, free-of-charge |
189
|
|
|
|
|
|
|
patent license to make, have made, use, offer to sell, sell, import and |
190
|
|
|
|
|
|
|
otherwise transfer the Package with respect to any patent claims |
191
|
|
|
|
|
|
|
licensable by the Copyright Holder that are necessarily infringed by the |
192
|
|
|
|
|
|
|
Package. If you institute patent litigation (including a cross-claim or |
193
|
|
|
|
|
|
|
counterclaim) against any party alleging that the Package constitutes |
194
|
|
|
|
|
|
|
direct or contributory patent infringement, then this Artistic License |
195
|
|
|
|
|
|
|
to you shall terminate on the date that such litigation is filed. |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER |
198
|
|
|
|
|
|
|
AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. |
199
|
|
|
|
|
|
|
THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
200
|
|
|
|
|
|
|
PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY |
201
|
|
|
|
|
|
|
YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR |
202
|
|
|
|
|
|
|
CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR |
203
|
|
|
|
|
|
|
CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, |
204
|
|
|
|
|
|
|
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=cut |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
1; |