File Coverage

blib/lib/OOP/Perlish/Class/Accessor/UnitTests/Hash.pm
Criterion Covered Total %
statement 92 92 100.0
branch 3 4 75.0
condition n/a
subroutine 27 27 100.0
pod 0 10 0.0
total 122 133 91.7


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2 1     1   5 use warnings;
  1         2  
  1         35  
3 1     1   6 use strict;
  1         2  
  1         44  
4             {
5             package OOP::Perlish::Class::Accessor::UnitTests::Hash;
6 1     1   5 use warnings;
  1         2  
  1         21  
7 1     1   4 use strict;
  1         2  
  1         19  
8 1     1   5 use OOP::Perlish::Class::Accessor::UnitTests::Base;
  1         2  
  1         35  
9 1     1   6 use base 'OOP::Perlish::Class::Accessor::UnitTests::Base';
  1         2  
  1         90  
10 1     1   6 use OOP::Perlish::Class::Accessor;
  1         1  
  1         7  
11 1     1   5 use Test::More;
  1         2  
  1         6  
12 1     1   271 use Data::Dumper;
  1         1  
  1         345  
13              
14             sub get_value
15             {
16 8     8 0 12 my ($self) = @_;
17              
18 8         32 my %values = $self->{accessor}->value();
19 8         50 return( %values );
20             }
21              
22             # Utility function to test positive/negative assignment for validators
23             # Adds 2 + N to your test count
24             sub use_validator(@)
25             {
26 4     4 0 16 my ( $self, %values ) = @_;
27              
28 4         26 $self->{accessor}->value(%values);
29 4         54 $self->compare_values_to_hash(%values);
30              
31 4         1700 $self->{accessor}->value( 'invalid1' => 'invalid1', 'invalid2' => 'invalid2', 'invalid3' => 'invalid3' );
32 4         28 my %compare_values = $self->get_value();
33 4         76 is( scalar keys(%compare_values), 0, 'we pass negative assertion of validation' );
34             }
35              
36             # Utility function for testing equality
37             # Adds 1 + N to test count
38             sub compare_values_to_hash(@)
39             {
40 8     8 0 32 my ( $self, %values ) = @_;
41              
42 8         38 my %test_values = $self->get_value();
43              
44 8         81 is( scalar keys %test_values, scalar keys %values, 'we pass positive assertion of validation' );
45 8         4358 while( my ( $k, $v ) = each %values ) {
46 32         11479 is( $test_values{$k}, $v, "possitive assertion that \$test_values{$k} == \$values{$k}" );
47             }
48             }
49              
50             sub setup : Test(setup)
51             {
52 6     6 0 3279 my ($self) = @_;
53 6         69 $self->{accessor} = OOP::Perlish::Class::Accessor->new( type => 'HASH', name => 'test', self => bless( {}, __PACKAGE__ ) );
54 1     1   6 }
  1         1  
  1         5  
55              
56             sub test_type_with_hash(@) : Test(5)
57             {
58 2     2 0 377 my ($self) = @_;
59 2         15 my %values = ( 'foo' => 'bar', 'bar' => 'baz', 'baz' => 'bup', 'bup' => 'quux' );
60              
61 2         14 $self->{accessor}->value(%values);
62 2         26 $self->compare_values_to_hash(%values);
63 1     1   280 }
  1         2  
  1         4  
64              
65             sub test_type_with_hashref(@) : Test(5)
66             {
67 2     2 0 332 my ($self) = @_;
68 2         15 my %values = ( 'foo' => 'bar', 'bar' => 'baz', 'baz' => 'bup', 'bup' => 'quux' );
69              
70 2         11 $self->{accessor}->value( \%values );
71 2         19 $self->compare_values_to_hash(%values);
72 1     1   225 }
  1         2  
  1         4  
73              
74             sub test_negative_assertion_type : Test
75             {
76 2     2 0 343 my ($self) = @_;
77            
78 2         16 $self->{accessor}->value( [ 'foo', 'bar' ] );
79 2         13 ok( ! $self->get_value(), "negative assertion for type with non-hash" );
80 1     1   264 }
  1         2  
  1         3  
81              
82             sub test_setting_hash_with_regex_validator(@) : Test(6)
83             {
84 2     2 0 390 my ($self) = @_;
85 2         20 my %values = ( 'foo' => 'test1', 'bar' => 'test2', 'baz' => 'test3', 'bup' => 'test4' );
86              
87 2         19 $self->{accessor}->validator(qr/.*test.*/i);
88 2         21 $self->use_validator(%values);
89 1     1   233 }
  1         1  
  1         4  
90              
91             sub test_setting_hash_with_sub_validator(@) : Test(6)
92             {
93 2     2 0 352 my ($self) = @_;
94 2         16 my %values = ( 'foo' => 'test1', 'bar' => 'test2', 'baz' => 'test3', 'bup' => 'test4' );
95              
96             $self->{accessor}->validator(
97             sub {
98 8     8   32 my ( $self, %values ) = @_;
99 8         11 my %checked = ();
100 8 100       69 while( my ( $k, $v ) = each %values ) { return unless( $v =~ m/^(.*test.*)$/ ); $checked{$k} = $1 }
  20         105  
  16         74  
101 4         36 return %checked;
102             }
103 2         17 );
104 2         13 $self->use_validator(%values);
105 1     1   318 }
  1         2  
  1         4  
106              
107             sub unset_value : Test
108             {
109 2     2 0 1025 my ($self) = @_;
110              
111 2         12 my %test = $self->get_value();
112 2 50       14 ok( ! keys %test, 'we get an empty list when nothing has been set' ) || diag( Dumper( { %test } ) );
113 1     1   230 }
  1         98  
  1         5  
114              
115             }
116             1;
117             =head1 NAME
118              
119             =head1 VERSION
120              
121             =head1 SYNOPSIS
122              
123             =head1 METHODS
124              
125             =head1 AUTHOR
126              
127             Jamie Beverly, C<< >>
128              
129             =head1 BUGS
130              
131             Please report any bugs or feature requests to C,
132             or through
133             the web interface at
134             L. I will be
135             notified, and then you'll
136             automatically be notified of progress on your bug as I make changes.
137              
138             =head1 SUPPORT
139              
140             You can find documentation for this module with the perldoc command.
141              
142             perldoc OOP::Perlish::Class
143              
144              
145             You can also look for information at:
146              
147             =over 4
148              
149             =item * RT: CPAN's request tracker
150              
151             L
152              
153             =item * AnnoCPAN: Annotated CPAN documentation
154              
155             L
156              
157             =item * CPAN Ratings
158              
159             L
160              
161             =item * Search CPAN
162              
163             L
164              
165             =back
166              
167              
168             =head1 ACKNOWLEDGEMENTS
169              
170             =head1 COPYRIGHT & LICENSE
171              
172             Copyright 2009 Jamie Beverly
173              
174             This program is free software; you can redistribute it and/or modify it
175             under the terms of either: the GNU General Public License as published
176             by the Free Software Foundation; or the Artistic License.
177              
178             See http://dev.perl.org/licenses/ for more information.
179              
180             =cut