| 1 | package Test::Proto::Role::HashRef; | ||||||||||||||
| 2 | 11 | ▶ | use 5.008; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | 11 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 11 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 11 | ▶ | use Test::Proto::Common; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 11 | ▶ | use Moo::Role; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 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<key_exists>. 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 | 3 | ▶ | sub key_exists { | ||||||||||||
|
Subroutine
key_exists: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 32 | 3 | my ( $self, $key, $reason ) = @_; | |||||||||||||
| 33 | 3 | $self->add_test( 'key_exists', { key => $key }, $reason ); | |||||||||||||
| 34 | } | ||||||||||||||
| 35 | 0 | ▶ | define_test key_exists => sub { | ||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=9.2
Subroutine
_TEST_key_exists: called CC=2 SCAR=9.2
| |||||||||||||||
| 36 | 0 | my ( $self, $data, $reason ) = @_; # self is the runner, NOT the prototype | |||||||||||||
| 37 | 0 | ▶ | return $self->pass if exists $self->subject->{ $data->{key} }; | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 38 | 0 | return $self->fail; | |||||||||||||
| 39 | 11 | }; | |||||||||||||
| 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 | 8 | ▶ | sub key_has_value { | ||||||||||||
|
Subroutine
key_has_value: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 51 | 8 | my ( $self, $key, $expected, $reason ) = @_; | |||||||||||||
| 52 | 8 | $self->add_test( | |||||||||||||
| 53 | 'key_has_value', | ||||||||||||||
| 54 | { | ||||||||||||||
| 55 | key => $key, | ||||||||||||||
| 56 | expected => $expected | ||||||||||||||
| 57 | }, | ||||||||||||||
| 58 | $reason | ||||||||||||||
| 59 | ); | ||||||||||||||
| 60 | } | ||||||||||||||
| 61 | 8 | ▶ | define_test key_has_value => sub { | ||||||||||||
|
Subroutine
_TEST_key_has_value: called CC=1 SCAR=0.0
| |||||||||||||||
| 62 | 8 | my ( $self, $data, $reason ) = @_; # self is the runner, NOT the prototype | |||||||||||||
| 63 | 8 | my $subject = $self->subject->{ $data->{key} }; | |||||||||||||
| 64 | 8 | return upgrade( $data->{expected} )->validate( $subject, $self ); | |||||||||||||
| 65 | 11 | }; | |||||||||||||
| 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 | 8 | ▶ | sub superhash_of { | ||||||||||||
|
Subroutine
superhash_of: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 77 | 8 | my ( $self, $expected, $reason ) = @_; | |||||||||||||
| 78 | 8 | $self->add_test( 'superhash_of', { expected => $expected }, $reason ); | |||||||||||||
| 79 | } | ||||||||||||||
| 80 | 6 | ▶ | define_test superhash_of => sub { | ||||||||||||
|
Subroutine
_TEST_superhash_of: called CC=2 SCAR=6.9
| |||||||||||||||
| 81 | 6 | my ( $self, $data, $reason ) = @_; # self is the runner, NOT the prototype | |||||||||||||
| 82 | 6 | my $subproto = Test::Proto::HashRef->new(); | |||||||||||||
| 83 | 6 | foreach my $key ( keys %{ $data->{expected} } ) { | |||||||||||||
| 84 | 5 | my $expected = $data->{expected}->{$key}; | |||||||||||||
| 85 | 5 | $subproto->key_has_value( $key, $expected ); | |||||||||||||
| 86 | } | ||||||||||||||
| 87 | 6 | return $subproto->validate( $self->subject, $self ); | |||||||||||||
| 88 | 11 | }; | |||||||||||||
| 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 | 3 | ▶ | sub count_keys { | ||||||||||||
|
Subroutine
count_keys: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 100 | 3 | my ( $self, $expected, $reason ) = @_; | |||||||||||||
| 101 | 3 | $self->add_test( 'count_keys', { expected => $expected }, $reason ); | |||||||||||||
| 102 | } | ||||||||||||||
| 103 | |||||||||||||||
| 104 | 3 | ▶ | define_test count_keys => sub { | ||||||||||||
|
Subroutine
_TEST_count_keys: called CC=1 SCAR=0.0
| |||||||||||||||
| 105 | 3 | my ( $self, $data, $reason ) = @_; # self is the runner, NOT the prototype | |||||||||||||
| 106 | 3 | my $subject = scalar CORE::keys %{ $self->subject }; | |||||||||||||
| 107 | 3 | return upgrade( $data->{expected} )->validate( $subject, $self ); | |||||||||||||
| 108 | 11 | }; | |||||||||||||
| 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<ok> passes if the prototype C<$tests_keys> returns a pass for C<['a','b']> or C<['b','a']>. | ||||||||||||||
| 117 | |||||||||||||||
| 118 | =cut | ||||||||||||||
| 119 | |||||||||||||||
| 120 | 4 | ▶ | sub keys { | ||||||||||||
|
Subroutine
keys: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 121 | 4 | my ( $self, $expected, $reason ) = @_; | |||||||||||||
| 122 | 4 | $self->add_test( 'keys', { expected => $expected }, $reason ); | |||||||||||||
| 123 | } | ||||||||||||||
| 124 | |||||||||||||||
| 125 | 4 | ▶ | define_test 'keys' => sub { | ||||||||||||
|
Subroutine
_TEST_keys: called CC=1 SCAR=0.0
| |||||||||||||||
| 126 | 4 | my ( $self, $data, $reason ) = @_; # self is the runner, NOT the prototype | |||||||||||||
| 127 | 4 | my $subject = [ CORE::keys %{ $self->subject } ]; | |||||||||||||
| 128 | 4 | return upgrade( $data->{expected} )->validate( $subject, $self ); | |||||||||||||
| 129 | 11 | }; | |||||||||||||
| 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<ok> passes if the prototype C<$tests_values> returns a pass for C<[1,2]> or C<[2,1]>. | ||||||||||||||
| 138 | |||||||||||||||
| 139 | =cut | ||||||||||||||
| 140 | |||||||||||||||
| 141 | 4 | ▶ | sub values { | ||||||||||||
|
Subroutine
values: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 142 | 4 | my ( $self, $expected, $reason ) = @_; | |||||||||||||
| 143 | 4 | $self->add_test( 'values', { expected => $expected }, $reason ); | |||||||||||||
| 144 | } | ||||||||||||||
| 145 | |||||||||||||||
| 146 | 4 | ▶ | define_test 'values' => sub { | ||||||||||||
|
Subroutine
_TEST_values: called CC=1 SCAR=1.2
| |||||||||||||||
| 147 | 0 | my ( $self, $data, $reason ) = @_; # self is the runner, NOT the prototype | |||||||||||||
| 148 | 0 | my $subject = [ CORE::values %{ $self->subject } ]; | |||||||||||||
| 149 | 0 | return upgrade( $data->{expected} )->validate( $subject, $self ); | |||||||||||||
| 150 | 11 | }; | |||||||||||||
| 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 | 4 | ▶ | sub enumerated { | ||||||||||||
|
Subroutine
enumerated: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 163 | 4 | my ( $self, $expected, $reason ) = @_; | |||||||||||||
| 164 | 4 | $self->add_test( 'enumerated', { expected => $expected }, $reason ); | |||||||||||||
| 165 | } | ||||||||||||||
| 166 | |||||||||||||||
| 167 | 4 | ▶ | define_test 'enumerated' => sub { | ||||||||||||
|
Subroutine
_TEST_enumerated: called CC=2 SCAR=6.9
| |||||||||||||||
| 168 | 4 | my ( $self, $data, $reason ) = @_; # self is the runner, NOT the prototype | |||||||||||||
| 169 | 4 | my $subject = []; | |||||||||||||
| 170 | 4 | push @$subject, [ $_, $self->subject->{$_} ] foreach ( CORE::keys %{ $self->subject } ); | |||||||||||||
| 171 | 4 | return upgrade( $data->{expected} )->validate( $subject, $self ); | |||||||||||||
| 172 | 11 | }; | |||||||||||||
| 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<Test::Proto>. | ||||||||||||||
| 182 | |||||||||||||||
| 183 | =cut | ||||||||||||||
| 184 | |||||||||||||||
| 185 | 11 | 1; | |||||||||||||