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
|
use strict;
use warnings;
use Test::More;
use Test::Fatal;
{
use Moose::Util::TypeConstraints;
use List::Util qw( sum );
subtype 'H1', as 'HashRef[Int]';
subtype 'H2', as 'HashRef', where { scalar keys %{$_} < 2 };
subtype 'H3', as 'HashRef[Int]',
where { ( sum( values %{$_} ) || 0 ) < 5 };
subtype 'H5', as 'HashRef';
coerce 'H5', from 'Str', via { { key => $_ } };
no Moose::Util::TypeConstraints;
}
{
package Foo;
use Moose;
has hash_int => (
traits => ['Hash'],
is => 'rw',
isa => 'HashRef[Int]',
handles => {
set_hash_int => 'set',
},
);
has h1 => (
traits => ['Hash'],
is => 'rw',
isa => 'H1',
handles => {
set_h1 => 'set',
},
);
has h2 => (
traits => ['Hash'],
is => 'rw',
isa => 'H2',
handles => {
set_h2 => 'set',
},
);
has h3 => (
traits => ['Hash'],
is => 'rw',
isa => 'H3',
handles => {
set_h3 => 'set',
},
);
has h4 => (
traits => ['Hash'],
is => 'rw',
isa => 'HashRef',
lazy => 1,
default => 'invalid',
clearer => '_clear_h4',
handles => {
get_h4 => 'get',
accessor_h4 => 'accessor',
},
);
has h5 => (
traits => ['Hash'],
is => 'rw',
isa => 'H5',
coerce => 1,
lazy => 1,
default => 'invalid',
clearer => '_clear_h5',
handles => {
get_h5 => 'get',
accessor_h5 => 'accessor',
},
);
}
my $foo = Foo->new;
{
$foo->hash_int( {} );
is_deeply( $foo->hash_int, {}, "hash_int - correct contents" );
isnt( exception { $foo->set_hash_int( x => 'foo' ) }, undef, "hash_int - can't set wrong type" );
is_deeply( $foo->hash_int, {}, "hash_int - correct contents" );
$foo->set_hash_int( x => 1 );
is_deeply( $foo->hash_int, { x => 1 }, "hash_int - correct contents" );
}
{
isnt( exception { $foo->set_h1('foo') }, undef, "h1 - can't set onto undef" );
$foo->h1( {} );
is_deeply( $foo->h1, {}, "h1 - correct contents" );
isnt( exception { $foo->set_h1( x => 'foo' ) }, undef, "h1 - can't set wrong type" );
is_deeply( $foo->h1, {}, "h1 - correct contents" );
$foo->set_h1( x => 1 );
is_deeply( $foo->h1, { x => 1 }, "h1 - correct contents" );
}
{
isnt( exception { $foo->set_h2('foo') }, undef, "h2 - can't set onto undef" );
$foo->h2( {} );
is_deeply( $foo->h2, {}, "h2 - correct contents" );
$foo->set_h2( x => 'foo' );
is_deeply( $foo->h2, { x => 'foo' }, "h2 - correct contents" );
isnt( exception { $foo->set_h2( y => 'bar' ) }, undef, "h2 - can't set more than one element" );
is_deeply( $foo->h2, { x => 'foo' }, "h2 - correct contents" );
}
{
isnt( exception { $foo->set_h3(1) }, undef, "h3 - can't set onto undef" );
$foo->h3( {} );
is_deeply( $foo->h3, {}, "h3 - correct contents" );
isnt( exception { $foo->set_h3( x => 'foo' ) }, undef, "h3 - can't set non-int" );
isnt( exception { $foo->set_h3( x => 100 ) }, undef, "h3 - can't violate overall type constraint" );
is_deeply( $foo->h3, {}, "h3 - correct contents" );
$foo->set_h3( x => 1 );
is_deeply( $foo->h3, { x => 1 }, "h3 - correct contents" );
isnt( exception { $foo->set_h3( x => 100 ) }, undef, "h3 - can't violate overall type constraint" );
is_deeply( $foo->h3, { x => 1 }, "h3 - correct contents" );
$foo->set_h3( y => 3 );
is_deeply( $foo->h3, { x => 1, y => 3 }, "h3 - correct contents" );
}
{
my $expect
= qr/\QAttribute (h4) does not pass the type constraint because: Validation failed for 'HashRef' with value \E.*invalid.*/;
like(
exception { $foo->accessor_h4('key'); },
$expect,
'invalid default is caught when trying to read via accessor'
);
like(
exception { $foo->accessor_h4( size => 42 ); },
$expect,
'invalid default is caught when trying to write via accessor'
);
like(
exception { $foo->get_h4(42); },
$expect,
'invalid default is caught when trying to get'
);
}
{
my $foo = Foo->new;
is(
$foo->accessor_h5('key'), 'invalid',
'lazy default is coerced when trying to read via accessor'
);
$foo->_clear_h5;
$foo->accessor_h5( size => 42 );
is_deeply(
$foo->h5,
{ key => 'invalid', size => 42 },
'lazy default is coerced when trying to write via accessor'
);
$foo->_clear_h5;
is(
$foo->get_h5('key'), 'invalid',
'lazy default is coerced when trying to get'
);
}
done_testing;
|