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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
|
use strict;
use warnings;
use Test::More;
use Test::Fatal;
{
use Moose::Util::TypeConstraints;
use List::Util qw(sum);
subtype 'A1', as 'ArrayRef[Int]';
subtype 'A2', as 'ArrayRef', where { @$_ < 2 };
subtype 'A3', as 'ArrayRef[Int]', where { ( sum(@$_) || 0 ) < 5 };
subtype 'A5', as 'ArrayRef';
coerce 'A5', from 'Str', via { [ $_ ] };
no Moose::Util::TypeConstraints;
}
{
package Foo;
use Moose;
has array => (
traits => ['Array'],
is => 'rw',
isa => 'ArrayRef',
handles => {
push_array => 'push',
},
);
has array_int => (
traits => ['Array'],
is => 'rw',
isa => 'ArrayRef[Int]',
handles => {
push_array_int => 'push',
},
);
has a1 => (
traits => ['Array'],
is => 'rw',
isa => 'A1',
handles => {
push_a1 => 'push',
},
);
has a2 => (
traits => ['Array'],
is => 'rw',
isa => 'A2',
handles => {
push_a2 => 'push',
},
);
has a3 => (
traits => ['Array'],
is => 'rw',
isa => 'A3',
handles => {
push_a3 => 'push',
},
);
has a4 => (
traits => ['Array'],
is => 'rw',
isa => 'ArrayRef',
lazy => 1,
default => 'invalid',
clearer => '_clear_a4',
handles => {
get_a4 => 'get',
push_a4 => 'push',
accessor_a4 => 'accessor',
},
);
has a5 => (
traits => ['Array'],
is => 'rw',
isa => 'A5',
coerce => 1,
lazy => 1,
default => 'invalid',
clearer => '_clear_a5',
handles => {
get_a5 => 'get',
push_a5 => 'push',
accessor_a5 => 'accessor',
},
);
}
my $foo = Foo->new;
{
$foo->array( [] );
is_deeply( $foo->array, [], "array - correct contents" );
$foo->push_array('foo');
is_deeply( $foo->array, ['foo'], "array - correct contents" );
}
{
$foo->array_int( [] );
is_deeply( $foo->array_int, [], "array_int - correct contents" );
isnt( exception { $foo->push_array_int('foo') }, undef, "array_int - can't push wrong type" );
is_deeply( $foo->array_int, [], "array_int - correct contents" );
$foo->push_array_int(1);
is_deeply( $foo->array_int, [1], "array_int - correct contents" );
}
{
isnt( exception { $foo->push_a1('foo') }, undef, "a1 - can't push onto undef" );
$foo->a1( [] );
is_deeply( $foo->a1, [], "a1 - correct contents" );
isnt( exception { $foo->push_a1('foo') }, undef, "a1 - can't push wrong type" );
is_deeply( $foo->a1, [], "a1 - correct contents" );
$foo->push_a1(1);
is_deeply( $foo->a1, [1], "a1 - correct contents" );
}
{
isnt( exception { $foo->push_a2('foo') }, undef, "a2 - can't push onto undef" );
$foo->a2( [] );
is_deeply( $foo->a2, [], "a2 - correct contents" );
$foo->push_a2('foo');
is_deeply( $foo->a2, ['foo'], "a2 - correct contents" );
isnt( exception { $foo->push_a2('bar') }, undef, "a2 - can't push more than one element" );
is_deeply( $foo->a2, ['foo'], "a2 - correct contents" );
}
{
isnt( exception { $foo->push_a3(1) }, undef, "a3 - can't push onto undef" );
$foo->a3( [] );
is_deeply( $foo->a3, [], "a3 - correct contents" );
isnt( exception { $foo->push_a3('foo') }, undef, "a3 - can't push non-int" );
isnt( exception { $foo->push_a3(100) }, undef, "a3 - can't violate overall type constraint" );
is_deeply( $foo->a3, [], "a3 - correct contents" );
$foo->push_a3(1);
is_deeply( $foo->a3, [1], "a3 - correct contents" );
isnt( exception { $foo->push_a3(100) }, undef, "a3 - can't violate overall type constraint" );
is_deeply( $foo->a3, [1], "a3 - correct contents" );
$foo->push_a3(3);
is_deeply( $foo->a3, [ 1, 3 ], "a3 - correct contents" );
}
{
my $expect
= qr/\QAttribute (a4) does not pass the type constraint because: Validation failed for 'ArrayRef' with value \E.*invalid.*/;
like(
exception { $foo->accessor_a4(0); },
$expect,
'invalid default is caught when trying to read via accessor'
);
like(
exception { $foo->accessor_a4( 0 => 42 ); },
$expect,
'invalid default is caught when trying to write via accessor'
);
like(
exception { $foo->push_a4(42); },
$expect,
'invalid default is caught when trying to push'
);
like(
exception { $foo->get_a4(42); },
$expect,
'invalid default is caught when trying to get'
);
}
{
my $foo = Foo->new;
is(
$foo->accessor_a5(0), 'invalid',
'lazy default is coerced when trying to read via accessor'
);
$foo->_clear_a5;
$foo->accessor_a5( 1 => 'thing' );
is_deeply(
$foo->a5,
[ 'invalid', 'thing' ],
'lazy default is coerced when trying to write via accessor'
);
$foo->_clear_a5;
$foo->push_a5('thing');
is_deeply(
$foo->a5,
[ 'invalid', 'thing' ],
'lazy default is coerced when trying to push'
);
$foo->_clear_a5;
is(
$foo->get_a5(0), 'invalid',
'lazy default is coerced when trying to get'
);
}
{
package Bar;
use Moose;
}
{
package HasArray;
use Moose;
has objects => (
isa => 'ArrayRef[Foo]',
traits => ['Array'],
handles => {
push_objects => 'push',
},
);
}
{
my $ha = HasArray->new();
like(
exception { $ha->push_objects( Bar->new ) },
qr/\QValidation failed for 'Foo'/,
'got expected error when pushing an object of the wrong class onto an array ref'
);
}
done_testing;
|