1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
|
=pod
=encoding utf-8
=head1 PURPOSE
Check type constraints work with L<Class::InsideOut>.
=head1 DEPENDENCIES
Test is skipped if Class::InsideOut 1.13 is not available.
=head1 AUTHOR
Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
Based on C<< t/14_accessor_hooks.t >> from the Class::InsideOut test suite,
by David Golden.
=head1 COPYRIGHT AND LICENCE
This software is copyright (c) 2013-2014 by David Golden, Toby Inkster.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
use strict;
use warnings;
use lib qw( ./lib ./t/lib ../inc ./inc );
use Test::Requires { "Class::InsideOut" => 1.13 };
use Test::More;
BEGIN {
package Object::HookedTT;
use Class::InsideOut ':std';
use Types::Standard -types;
# $_ has the first argument in it for convenience
public integer => my %integer, { set_hook => Int };
# first argument is also available directly
public word => my %word, { set_hook => StrMatch[qr/\A\w+\z/] };
# Changing $_ changes what gets stored
my $UC = (StrMatch[qr/\A[A-Z]+\z/])->plus_coercions(Str, q{uc $_});
public uppercase => my %uppercase, {
set_hook => sub {
$_ = $UC->coercion->($_)
},
};
# Full @_ is available, but only first gets stored
public list => my %list, {
set_hook => sub { $_ = ArrayRef->check($_) ? $_ : [ @_ ] },
get_hook => sub { @$_ },
};
public reverser => my %reverser, {
set_hook => sub { $_ = ArrayRef->check($_) ? $_ : [ @_ ] },
get_hook => sub { reverse @$_ }
};
public write_only => my %only_only, {
get_hook => sub { die "is write-only\n" }
};
sub new {
register( bless {}, shift );
}
};
#--------------------------------------------------------------------------#
my $class = "Object::HookedTT";
my $properties = {
$class => {
integer => "public",
uppercase => "public",
word => "public",
list => "public",
reverser => "public",
write_only => "public",
},
};
my ($o, @got, $got);
#--------------------------------------------------------------------------#
is_deeply(
Class::InsideOut::_properties( $class ),
$properties,
"$class has/inherited its expected properties",
);
ok(
($o = $class->new()) && $o->isa($class),
"Creating a $class object",
);
#--------------------------------------------------------------------------#
eval { $o->integer(3.14) };
my $err = $@;
like(
$err,
'/integer\(\) Value "3.14" did not pass type constraint "Int"/i',
"integer(3.14) dies",
);
eval { $o->integer(42) };
is(
$@,
q{},
"integer(42) lives",
);
is(
$o->integer,
42,
"integer() == 42",
);
#--------------------------------------------------------------------------#
eval { $o->word("^^^^") };
like(
$@,
'/word\(\) value "\^\^\^\^" did not pass type constraint/i',
"word(^^^^) dies",
);
eval { $o->word("apple") };
is(
$@,
q{},
"word(apple) lives",
);
is(
$o->word,
'apple',
"word() eq 'apple'",
);
#--------------------------------------------------------------------------#
eval { $o->uppercase("banana") };
is(
$@,
q{},
"uppercase(banana) lives",
);
is(
$o->uppercase,
'BANANA',
"uppercase() eq 'BANANA'",
);
#--------------------------------------------------------------------------#
# list(@array)
eval { $o->list(qw(foo bar bam)) };
is(
$@,
q{},
"list(qw(foo bar bam)) lives",
);
is_deeply(
[ $o->list ],
[qw(foo bar bam)],
"list() gives qw(foo bar bam)",
);
# list(\@array)
eval { $o->list( [qw(foo bar bam)] ) };
is(
$@,
q{},
"list( [qw(foo bar bam)] ) lives",
);
is_deeply(
[ $o->list ],
[qw(foo bar bam)],
"list() gives qw(foo bar bam)",
);
#--------------------------------------------------------------------------#
eval { $o->reverser(qw(foo bar bam)) };
is(
$@,
q{},
"reverser(qw(foo bar bam)) lives",
);
# reverser in list context
@got = $o->reverser;
is_deeply(
\@got,
[qw(bam bar foo)],
"reverser() in list context gives qw(bam bar foo)",
);
# reverser in scalar context
$got = $o->reverser;
is(
$got,
'mabraboof',
"reverser() in scalar context gives mabraboof",
);
#--------------------------------------------------------------------------#
eval { $o->write_only( 23 ) };
is(
$@,
q{},
"write_only lives on write",
);
eval { $got = $o->write_only() };
like(
$@,
'/write_only\(\) is write-only at/i',
"write only dies on write (and was caught)",
);
done_testing;
|