line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Test::Proto::Role::HashRef; |
2
|
11
|
|
|
11
|
|
11237
|
use 5.008; |
|
11
|
|
|
|
|
41
|
|
|
11
|
|
|
|
|
558
|
|
3
|
11
|
|
|
11
|
|
64
|
use strict; |
|
11
|
|
|
|
|
23
|
|
|
11
|
|
|
|
|
363
|
|
4
|
11
|
|
|
11
|
|
60
|
use warnings; |
|
11
|
|
|
|
|
23
|
|
|
11
|
|
|
|
|
284
|
|
5
|
11
|
|
|
11
|
|
62
|
use Test::Proto::Common; |
|
11
|
|
|
|
|
23
|
|
|
11
|
|
|
|
|
1003
|
|
6
|
11
|
|
|
11
|
|
63
|
use Moo::Role; |
|
11
|
|
|
|
|
22
|
|
|
11
|
|
|
|
|
93
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
=head1 NAME |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
Test::Proto::Role::HashRef - Role containing test case methods for hash refs. |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=head1 SYNOPSIS |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
package MyProtoClass; |
15
|
|
|
|
|
|
|
use Moo; |
16
|
|
|
|
|
|
|
with 'Test::Proto::Role::HashRef'; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
This Moo Role provides methods to Test::Proto::HashRef for test case methods that apply to hashrefs such as C. It can also be used for objects which use overload or otherwise respond to hashref syntax. |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=head1 METHODS |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=head3 key_exists |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
pHash->key_exists('a')->ok({a=>1, b=>2}); |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
Returns true if the key exists (even if the value is undefined). |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=cut |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub key_exists { |
32
|
3
|
|
|
3
|
1
|
22
|
my ( $self, $key, $reason ) = @_; |
33
|
3
|
|
|
|
|
17
|
$self->add_test( 'key_exists', { key => $key }, $reason ); |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
define_test key_exists => sub { |
36
|
3
|
|
|
3
|
|
5
|
my ( $self, $data, $reason ) = @_; # self is the runner, NOT the prototype |
37
|
3
|
100
|
|
|
|
65
|
return $self->pass if exists $self->subject->{ $data->{key} }; |
38
|
1
|
|
|
|
|
5
|
return $self->fail; |
39
|
|
|
|
|
|
|
}; |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
=head3 key_has_value |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
pHash->key_has_value('a',1)->ok({a=>1, b=>2}); |
44
|
|
|
|
|
|
|
pHash->key_has_value('b',p->num_gt(0))->ok({a=>1, b=>2}); |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
Returns the value of corresponding to the key provided within the subject, and tests it against the prototype provided in the argument. |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=cut |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub key_has_value { |
51
|
8
|
|
|
8
|
1
|
44
|
my ( $self, $key, $expected, $reason ) = @_; |
52
|
8
|
|
|
|
|
63
|
$self->add_test( |
53
|
|
|
|
|
|
|
'key_has_value', |
54
|
|
|
|
|
|
|
{ |
55
|
|
|
|
|
|
|
key => $key, |
56
|
|
|
|
|
|
|
expected => $expected |
57
|
|
|
|
|
|
|
}, |
58
|
|
|
|
|
|
|
$reason |
59
|
|
|
|
|
|
|
); |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
define_test key_has_value => sub { |
62
|
8
|
|
|
8
|
|
20
|
my ( $self, $data, $reason ) = @_; # self is the runner, NOT the prototype |
63
|
8
|
|
|
|
|
216
|
my $subject = $self->subject->{ $data->{key} }; |
64
|
8
|
|
|
|
|
41
|
return upgrade( $data->{expected} )->validate( $subject, $self ); |
65
|
|
|
|
|
|
|
}; |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=head3 superhash_of |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
pHash->superhash_of({'a'=>1})->ok({a=>1, b=>2}); |
70
|
|
|
|
|
|
|
pHash->superhash_of({'b'=>p->num_gt(0)})->ok({a=>1, b=>2}); |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
Tests whether each of the key-value pairs in the second argument are present and validate the test subject's equivalent pair. |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=cut |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub superhash_of { |
77
|
8
|
|
|
8
|
1
|
66
|
my ( $self, $expected, $reason ) = @_; |
78
|
8
|
|
|
|
|
44
|
$self->add_test( 'superhash_of', { expected => $expected }, $reason ); |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
define_test superhash_of => sub { |
81
|
6
|
|
|
6
|
|
17
|
my ( $self, $data, $reason ) = @_; # self is the runner, NOT the prototype |
82
|
6
|
|
|
|
|
162
|
my $subproto = Test::Proto::HashRef->new(); |
83
|
6
|
|
|
|
|
39
|
foreach my $key ( keys %{ $data->{expected} } ) { |
|
6
|
|
|
|
|
33
|
|
84
|
5
|
|
|
|
|
19
|
my $expected = $data->{expected}->{$key}; |
85
|
5
|
|
|
|
|
29
|
$subproto->key_has_value( $key, $expected ); |
86
|
|
|
|
|
|
|
} |
87
|
6
|
|
|
|
|
187
|
return $subproto->validate( $self->subject, $self ); |
88
|
|
|
|
|
|
|
}; |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=head3 count_keys |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
pHash->count_keys(2)->ok({a=>1, b=>2}); |
93
|
|
|
|
|
|
|
pHash->count_keys(p->num_gt(0))->ok({a=>1, b=>2}); |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
Counts the keys of the hashref and compares them to the prototype provided. There is no equivalent count_values - the number should be identical! |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=cut |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
sub count_keys { |
100
|
3
|
|
|
3
|
1
|
22
|
my ( $self, $expected, $reason ) = @_; |
101
|
3
|
|
|
|
|
14
|
$self->add_test( 'count_keys', { expected => $expected }, $reason ); |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
define_test count_keys => sub { |
105
|
3
|
|
|
3
|
|
9
|
my ( $self, $data, $reason ) = @_; # self is the runner, NOT the prototype |
106
|
3
|
|
|
|
|
4
|
my $subject = scalar CORE::keys %{ $self->subject }; |
|
3
|
|
|
|
|
66
|
|
107
|
3
|
|
|
|
|
13
|
return upgrade( $data->{expected} )->validate( $subject, $self ); |
108
|
|
|
|
|
|
|
}; |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=head3 keys |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
pHash->keys($tests_keys)->ok({a=>1, b=>2}); |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
Returns the hash keys of the subject as an array reference (in an undetermined order), and tests them against the prototype provided in the argument. |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
In the above example, the C passes if the prototype C<$tests_keys> returns a pass for C<['a','b']> or C<['b','a']>. |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=cut |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
sub keys { |
121
|
4
|
|
|
4
|
1
|
37
|
my ( $self, $expected, $reason ) = @_; |
122
|
4
|
|
|
|
|
26
|
$self->add_test( 'keys', { expected => $expected }, $reason ); |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
define_test 'keys' => sub { |
126
|
4
|
|
|
4
|
|
10
|
my ( $self, $data, $reason ) = @_; # self is the runner, NOT the prototype |
127
|
4
|
|
|
|
|
9
|
my $subject = [ CORE::keys %{ $self->subject } ]; |
|
4
|
|
|
|
|
110
|
|
128
|
4
|
|
|
|
|
22
|
return upgrade( $data->{expected} )->validate( $subject, $self ); |
129
|
|
|
|
|
|
|
}; |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=head3 values |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
pHash->values($tests_values)->ok({a=>1, b=>2}); |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
Produces the hash values of the subject as an array reference (in an undetermined order), and tests them against the prototype provided in the argument. |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
In the above example, the C passes if the prototype C<$tests_values> returns a pass for C<[1,2]> or C<[2,1]>. |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=cut |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
sub values { |
142
|
4
|
|
|
4
|
1
|
34
|
my ( $self, $expected, $reason ) = @_; |
143
|
4
|
|
|
|
|
25
|
$self->add_test( 'values', { expected => $expected }, $reason ); |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
define_test 'values' => sub { |
147
|
4
|
|
|
4
|
|
11
|
my ( $self, $data, $reason ) = @_; # self is the runner, NOT the prototype |
148
|
4
|
|
|
|
|
7
|
my $subject = [ CORE::values %{ $self->subject } ]; |
|
4
|
|
|
|
|
105
|
|
149
|
4
|
|
|
|
|
21
|
return upgrade( $data->{expected} )->validate( $subject, $self ); |
150
|
|
|
|
|
|
|
}; |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=head3 enumerated |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
pHash->enumerated($tests_key_value_pairs)->ok({a=>1, b=>2}); |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
Produces the hash keys and values of the subject as an array reference (in an undetermined order), and tests them against the prototype provided in the argument. |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
In the above example, the prototype C<$tests_key_value_pairs> should return a pass for C<[['a','1'],['b','2']]> or C<[['b','2'],['a','1']]>. |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=cut |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
sub enumerated { |
163
|
4
|
|
|
4
|
1
|
41
|
my ( $self, $expected, $reason ) = @_; |
164
|
4
|
|
|
|
|
24
|
$self->add_test( 'enumerated', { expected => $expected }, $reason ); |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
define_test 'enumerated' => sub { |
168
|
4
|
|
|
4
|
|
10
|
my ( $self, $data, $reason ) = @_; # self is the runner, NOT the prototype |
169
|
4
|
|
|
|
|
9
|
my $subject = []; |
170
|
4
|
|
|
|
|
9
|
push @$subject, [ $_, $self->subject->{$_} ] foreach ( CORE::keys %{ $self->subject } ); |
|
4
|
|
|
|
|
102
|
|
171
|
4
|
|
|
|
|
23
|
return upgrade( $data->{expected} )->validate( $subject, $self ); |
172
|
|
|
|
|
|
|
}; |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
# simple_test count_keys => sub { |
175
|
|
|
|
|
|
|
# my ($subject, $expected) = @_; # self is the runner, NOT the prototype |
176
|
|
|
|
|
|
|
# return $expected == scalar keys %$subject; |
177
|
|
|
|
|
|
|
# }; |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
=head1 OTHER INFORMATION |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
For author, version, bug reports, support, etc, please see L. |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=cut |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
1; |