Class/Accessor/UnitTests/Hash.pm

filter: Statementstmt 100% 94 / 94 Branchbran 87.5% 7 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 27 / 27 Podpod 0.0% 0 / 10 totaltotal 92.0% 128 / 139 CC 5 SCAR 2.8
CC
5
Cov
99%
__ANON__
3.0
unset_value
2.0
compare_values_to_hash
2.0
CRAP 1.3
« Class/Accessor/UnitTests/HashRef.pm ↑ Summary Class/Accessor/UnitTests/Array.pm »
1 #!/usr/bin/perl
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 {
5 package OOP::Perlish::Class::Accessor::UnitTests::Hash;
6 1 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶ use OOP::Perlish::Class::Accessor::UnitTests::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶ use base 'OOP::Perlish::Class::Accessor::UnitTests::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶ use OOP::Perlish::Class::Accessor;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶ use Test::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 sub get_value
15 8 ▶ {
Subroutine get_value: called CC=1 SCAR=0.0
Pod undocumented
16 8 my ($self) = @_;
17  
18 8 my %values = $self->{accessor}->value();
19 8 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 4 ▶ {
Subroutine use_validator: called CC=1 SCAR=0.0
Pod undocumented
26 4 my ( $self, %values ) = @_;
27  
28 4 $self->{accessor}->value(%values);
29 4 $self->compare_values_to_hash(%values);
30  
31 4 $self->{accessor}->value( 'invalid1' => 'invalid1', 'invalid2' => 'invalid2', 'invalid3' => 'invalid3' );
32 4 my %compare_values = $self->get_value();
33 4 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 8 ▶ {
Subroutine compare_values_to_hash: called CC=2 SCAR=6.9
Pod undocumented
40 8 my ( $self, %values ) = @_;
41  
42 8 my %test_values = $self->get_value();
43  
44 8 is( scalar keys %test_values, scalar keys %values, 'we pass positive assertion of validation' );
45 8 ▶ while( my ( $k, $v ) = each %values ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %values) 32 8 40
46 32 is( $test_values{$k}, $v, "possitive assertion that \$test_values{$k} == \$values{$k}" );
47 }
48 }
49  
50 6 ▶ sub setup : Test(setup)
Subroutine setup: called CC=1 SCAR=0.0
Pod undocumented
51 {
52 6 my ($self) = @_;
53 6 $self->{accessor} = OOP::Perlish::Class::Accessor->new( type => 'HASH', name => 'test', self => bless( {}, __PACKAGE__ ) );
54 1 ▶ }
Subroutine BEGIN: called CC=1 SCAR=0.0
55  
56 2 ▶ sub test_type_with_hash(@) : Test(5)
Subroutine test_type_with_hash: called CC=1 SCAR=0.0
Pod undocumented
57 {
58 2 my ($self) = @_;
59 2 my %values = ( 'foo' => 'bar', 'bar' => 'baz', 'baz' => 'bup', 'bup' => 'quux' );
60  
61 2 $self->{accessor}->value(%values);
62 2 $self->compare_values_to_hash(%values);
63 1 ▶ }
Subroutine BEGIN: called CC=1 SCAR=0.0
64  
65 2 ▶ sub test_type_with_hashref(@) : Test(5)
Subroutine test_type_with_hashref: called CC=1 SCAR=0.0
Pod undocumented
66 {
67 2 my ($self) = @_;
68 2 my %values = ( 'foo' => 'bar', 'bar' => 'baz', 'baz' => 'bup', 'bup' => 'quux' );
69  
70 2 $self->{accessor}->value( \%values );
71 2 $self->compare_values_to_hash(%values);
72 1 ▶ }
Subroutine BEGIN: called CC=1 SCAR=0.0
73  
74 2 ▶ sub test_negative_assertion_type : Test
Subroutine test_negative_assertion_type: called CC=1 SCAR=0.0
Pod undocumented
75 {
76 2 my ($self) = @_;
77
78 2 $self->{accessor}->value( [ 'foo', 'bar' ] );
79 2 ok( ! $self->get_value(), "negative assertion for type with non-hash" );
80 1 ▶ }
Subroutine BEGIN: called CC=1 SCAR=0.0
81  
82 2 ▶ sub test_setting_hash_with_regex_validator(@) : Test(6)
Subroutine test_setting_hash_with_regex_validator: called CC=1 SCAR=0.0
Pod undocumented
83 {
84 2 my ($self) = @_;
85 2 my %values = ( 'foo' => 'test1', 'bar' => 'test2', 'baz' => 'test3', 'bup' => 'test4' );
86  
87 2 $self->{accessor}->validator(qr/.*test.*/i);
88 2 $self->use_validator(%values);
89 1 ▶ }
Subroutine BEGIN: called CC=1 SCAR=0.0
90  
91 2 ▶ sub test_setting_hash_with_sub_validator(@) : Test(6)
Subroutine test_setting_hash_with_sub_validator: called CC=1 SCAR=0.0
Pod undocumented
92 {
93 2 my ($self) = @_;
94 2 my %values = ( 'foo' => 'test1', 'bar' => 'test2', 'baz' => 'test3', 'bup' => 'test4' );
95  
96 $self->{accessor}->validator(
97 8 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
98 8 my ( $self, %values ) = @_;
99 8 my %checked = ();
100 8 ▶ while( my ( $k, $v ) = each %values ) { return unless( $v =~ m/^(.*test.*)$/ ); $checked{$k} = $1 }
Branch: 2 branches
BranchTrueFalseTotal
if (() = each %values) 20 4 24
unless $v =~ /^(.*test.*)$/ 4 16 20
101 4 return %checked;
102 }
103 2 );
104 2 $self->use_validator(%values);
105 1 ▶ }
Subroutine BEGIN: called CC=1 SCAR=0.0
106  
107 2 ▶ sub unset_value : Test
Subroutine unset_value: called CC=2 SCAR=7.1
Pod undocumented
108 {
109 2 my ($self) = @_;
110  
111 2 my %test = $self->get_value();
112 2 ▶ ok( ! keys %test, 'we get an empty list when nothing has been set' ) || diag( Dumper( { %test } ) );
Branch: 1 branch
BranchTrueFalseTotal
&ok(!keys(%test), "we get an empty list when nothing has been set") or diag(Dumper({%test})) 2 0 2
113 1 ▶ }
Subroutine BEGIN: called CC=1 SCAR=0.0
114  
115 }
116 1 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<< <jbeverly at cpan.org> >>
128  
129 =head1 BUGS
130  
131 Please report any bugs or feature requests to C<bug-foo-bar at rt.cpan.org>,
132 or through
133 the web interface at
134 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=OOP-Perlish-Class>. 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<http://rt.cpan.org/NoAuth/Bugs.html?Dist=OOP-Perlish-Class>
152  
153 =item * AnnoCPAN: Annotated CPAN documentation
154  
155 L<http://annocpan.org/dist/OOP-Perlish-Class>
156  
157 =item * CPAN Ratings
158  
159 L<http://cpanratings.perl.org/d/OOP-Perlish-Class>
160  
161 =item * Search CPAN
162  
163 L<http://search.cpan.org/dist/OOP-Perlish-Class/>
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
« Class/Accessor/UnitTests/HashRef.pm ↑ Summary Class/Accessor/UnitTests/Array.pm »