| 1 | { | ||||||||||
| 2 | package OOP::Perlish::Class::Accessor::UnitTests::Scalar; | ||||||||||
| 3 | 1 | ▶ | use OOP::Perlish::Class::Accessor::UnitTests::Base; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use base qw(OOP::Perlish::Class::Accessor::UnitTests::Base); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use OOP::Perlish::Class::Accessor; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use Test::More; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use Data::Dumper; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 7 | ▶ | sub setup : Test(setup) | ||||||||
|
Subroutine
setup: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 10 | { | ||||||||||
| 11 | 7 | my ($self) = @_; | |||||||||
| 12 | 7 | $self->{accessor} = OOP::Perlish::Class::Accessor->new( type => 'SCALAR', name => 'test', self => bless({}, __PACKAGE__) ); | |||||||||
| 13 | 1 | ▶ | } | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | sub get_value | ||||||||||
| 16 | 9 | ▶ | { | ||||||||
|
Subroutine
get_value: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 17 | 9 | my ($self) = @_; | |||||||||
| 18 | 9 | return $self->{accessor}->value(); | |||||||||
| 19 | } | ||||||||||
| 20 | |||||||||||
| 21 | |||||||||||
| 22 | # Utility function to test positive/negative assignment for validators | ||||||||||
| 23 | 4 | ▶ | sub use_validator(@) { | ||||||||
|
Subroutine
use_validator: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 24 | 4 | my ($self, $value) = @_; | |||||||||
| 25 | |||||||||||
| 26 | 4 | $self->{accessor}->value($value); | |||||||||
| 27 | 4 | is($self->get_value(), $value, 'we pass positive assertion for validation'); | |||||||||
| 28 | |||||||||||
| 29 | 4 | $self->{accessor}->value('invalid'); | |||||||||
| 30 | 4 | ok( ! $self->get_value(), 'we pass negative assertion for validation'); | |||||||||
| 31 | } | ||||||||||
| 32 | |||||||||||
| 33 | 2 | ▶ | sub test_negative_assertion_type : Test | ||||||||
|
Subroutine
test_negative_assertion_type: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 34 | { | ||||||||||
| 35 | 2 | my ($self) = @_; | |||||||||
| 36 | |||||||||||
| 37 | 2 | $self->{accessor}->value('foo' => 'bar'); | |||||||||
| 38 | 2 | ok( ! $self->get_value(), "Cannot set value with invalid type" ); | |||||||||
| 39 | 1 | ▶ | } | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 40 | |||||||||||
| 41 | 2 | ▶ | sub test_negative_assertion_type_ref : Test | ||||||||
|
Subroutine
test_negative_assertion_type_ref: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 42 | { | ||||||||||
| 43 | 2 | my ($self) = @_; | |||||||||
| 44 | |||||||||||
| 45 | 2 | $self->{accessor}->value([ 'foo' ]); | |||||||||
| 46 | 2 | ok( ! $self->get_value(), "Cannot set type with a reference to a non-scalar-type" ); | |||||||||
| 47 | 1 | ▶ | } | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 48 | |||||||||||
| 49 | 2 | ▶ | sub test_type_with_scalar: Test | ||||||||
|
Subroutine
test_type_with_scalar: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 50 | { | ||||||||||
| 51 | 2 | my ($self) = @_; | |||||||||
| 52 | |||||||||||
| 53 | 2 | $self->{accessor}->value('foo'); | |||||||||
| 54 | 2 | is( $self->get_value(), 'foo', 'Value is set with scalar' ); | |||||||||
| 55 | 1 | ▶ | } | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 56 | |||||||||||
| 57 | 2 | ▶ | sub test_type_with_ref : Test | ||||||||
|
Subroutine
test_type_with_ref: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 58 | { | ||||||||||
| 59 | 2 | my ($self) = @_; | |||||||||
| 60 | 2 | my $refscalar = 'foo'; | |||||||||
| 61 | |||||||||||
| 62 | 2 | $self->{accessor}->value(\$refscalar); | |||||||||
| 63 | 2 | is( $self->get_value(), 'foo', 'value is set with scalar ref'); | |||||||||
| 64 | 1 | ▶ | } | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 65 | |||||||||||
| 66 | 2 | ▶ | sub test_setting_with_regex_validator(@) : Test(2) | ||||||||
|
Subroutine
test_setting_with_regex_validator: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 67 | { | ||||||||||
| 68 | 2 | my ($self) = @_; | |||||||||
| 69 | 2 | $self->{accessor}->validator( qr/.*test.*/i ); | |||||||||
| 70 | |||||||||||
| 71 | 2 | $self->use_validator("test"); | |||||||||
| 72 | 1 | ▶ | } | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 73 | |||||||||||
| 74 | 2 | ▶ | sub test_setting_with_sub_validator(@) : Test(2) | ||||||||
|
Subroutine
test_setting_with_sub_validator: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 75 | { | ||||||||||
| 76 | 2 | my ($self) = @_; | |||||||||
| 77 | |||||||||||
| 78 | 2 | ▶ | $self->{accessor}->validator( sub { my ($self, $value) = @_; $value eq 'hello' && return $value; return } ); | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||
| 79 | 2 | $self->use_validator("hello"); | |||||||||
| 80 | 1 | ▶ | } | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 81 | |||||||||||
| 82 | 2 | ▶ | sub unset_value : Test | ||||||||
|
Subroutine
unset_value: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||
| 83 | { | ||||||||||
| 84 | 2 | my ($self) = @_; | |||||||||
| 85 | 2 | my $undef = $self->get_value(); | |||||||||
| 86 | 2 | ▶ | ok( ! $undef, 'when nothing has been defined, we get undef for scalar' ) || diag(Dumper($undef)); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | 1 | ▶ | } | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 88 | } | ||||||||||
| 89 | 1 | 1; | |||||||||
| 90 | =head1 NAME | ||||||||||
| 91 | |||||||||||
| 92 | =head1 VERSION | ||||||||||
| 93 | |||||||||||
| 94 | =head1 SYNOPSIS | ||||||||||
| 95 | |||||||||||
| 96 | =head1 METHODS | ||||||||||
| 97 | |||||||||||
| 98 | =head1 AUTHOR | ||||||||||
| 99 | |||||||||||
| 100 | Jamie Beverly, C<< <jbeverly at cpan.org> >> | ||||||||||
| 101 | |||||||||||
| 102 | =head1 BUGS | ||||||||||
| 103 | |||||||||||
| 104 | Please report any bugs or feature requests to C<bug-foo-bar at rt.cpan.org>, | ||||||||||
| 105 | or through | ||||||||||
| 106 | the web interface at | ||||||||||
| 107 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=OOP-Perlish-Class>. I will be | ||||||||||
| 108 | notified, and then you'll | ||||||||||
| 109 | automatically be notified of progress on your bug as I make changes. | ||||||||||
| 110 | |||||||||||
| 111 | =head1 SUPPORT | ||||||||||
| 112 | |||||||||||
| 113 | You can find documentation for this module with the perldoc command. | ||||||||||
| 114 | |||||||||||
| 115 | perldoc OOP::Perlish::Class | ||||||||||
| 116 | |||||||||||
| 117 | |||||||||||
| 118 | You can also look for information at: | ||||||||||
| 119 | |||||||||||
| 120 | =over 4 | ||||||||||
| 121 | |||||||||||
| 122 | =item * RT: CPAN's request tracker | ||||||||||
| 123 | |||||||||||
| 124 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=OOP-Perlish-Class> | ||||||||||
| 125 | |||||||||||
| 126 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||
| 127 | |||||||||||
| 128 | L<http://annocpan.org/dist/OOP-Perlish-Class> | ||||||||||
| 129 | |||||||||||
| 130 | =item * CPAN Ratings | ||||||||||
| 131 | |||||||||||
| 132 | L<http://cpanratings.perl.org/d/OOP-Perlish-Class> | ||||||||||
| 133 | |||||||||||
| 134 | =item * Search CPAN | ||||||||||
| 135 | |||||||||||
| 136 | L<http://search.cpan.org/dist/OOP-Perlish-Class/> | ||||||||||
| 137 | |||||||||||
| 138 | =back | ||||||||||
| 139 | |||||||||||
| 140 | |||||||||||
| 141 | =head1 ACKNOWLEDGEMENTS | ||||||||||
| 142 | |||||||||||
| 143 | =head1 COPYRIGHT & LICENSE | ||||||||||
| 144 | |||||||||||
| 145 | Copyright 2009 Jamie Beverly | ||||||||||
| 146 | |||||||||||
| 147 | This program is free software; you can redistribute it and/or modify it | ||||||||||
| 148 | under the terms of either: the GNU General Public License as published | ||||||||||
| 149 | by the Free Software Foundation; or the Artistic License. | ||||||||||
| 150 | |||||||||||
| 151 | See http://dev.perl.org/licenses/ for more information. | ||||||||||
| 152 | |||||||||||
| 153 | =cut | ||||||||||