4 use Test::More tests => 16;
22 isa => 'HashRef[Int]',
27 isa => 'ArrayRef[Int]',
32 isa => 'ArrayRef[HashRef[Int]]'
37 isa => 'ArrayRef[My::Class]',
42 isa => 'ArrayRef[My::Role]',
46 ok(Foo->meta->has_attribute('foo'));
49 my $hash = { a => 1, b => 2, c => 3 };
50 my $array = [ 1, 2, 3 ];
51 my $complex = [ { a => 1, b => 1 }, { c => 2, d => 2} ];
52 my $foo = Foo->new(foo => $hash, bar => $array, complex => $complex);
54 is_deeply($foo->foo(), $hash, "foo is a proper hash");
55 is_deeply($foo->bar(), $array, "bar is a proper array");
56 is_deeply($foo->complex(), $complex, "complex is a proper ... structure");
58 $foo->my_class([My::Class->new]);
59 is ref($foo->my_class), 'ARRAY';
60 isa_ok $foo->my_class->[0], 'My::Class';
62 $foo->my_role([My::Class->new]);
63 is ref($foo->my_role), 'ARRAY';
65 } "Parameterized constraints work";
69 Foo->new( foo => { a => 'b' });
70 } qr/Attribute \(foo\) does not pass the type constraint because: Validation failed for 'HashRef\[Int\]' failed with value/, "Bad args for hash throws an exception";
73 Foo->new( bar => [ a => 'b' ]);
74 } qr/Attribute \(bar\) does not pass the type constraint because: Validation failed for 'ArrayRef\[Int\]' failed with value/, "Bad args for array throws an exception";
77 Foo->new( complex => [ { a => 1, b => 1 }, { c => "d", e => "f" } ] )
78 } qr/Attribute \(complex\) does not pass the type constraint because: Validation failed for 'ArrayRef\[HashRef\[Int\]\]' failed with value/, "Bad args for complex types throws an exception";
81 Foo->new( my_class => [ 10 ] );
82 } qr/Attribute \(my_class\) does not pass the type constraint because: Validation failed for 'ArrayRef\[My::Class\]' failed with value/;
84 Foo->new( my_class => [ {foo => 'bar'} ] );
85 } qr/Attribute \(my_class\) does not pass the type constraint because: Validation failed for 'ArrayRef\[My::Class\]' failed with value/;
89 Foo->new( my_role => [ 20 ] );
90 } qr/Attribute \(my_role\) does not pass the type constraint because: Validation failed for 'ArrayRef\[My::Role\]' failed with value/;
92 Foo->new( my_role => [ {foo => 'bar'} ] );
93 } qr/Attribute \(my_role\) does not pass the type constraint because: Validation failed for 'ArrayRef\[My::Role\]' failed with value/;
100 use Mouse::Util::TypeConstraints;
103 => as 'ArrayRef[HashRef]'
106 => from 'ArrayRef[Str]'
108 [ map { +{ $_ => 1 } } @$_ ]
119 my @list = ( {a => 1}, {b => 1}, {c => 1} );
120 my $bar = Bar->new(list => [ qw(a b c) ]);
122 is_deeply( $bar->list, \@list, "list is as expected");
126 Bar->new(list => [ { 1 => 2 }, 2, 3 ]);
127 } qr/Attribute \(list\) does not pass the type constraint because: Validation failed for 'Bar::List' failed with value/, "Bad coercion parameter throws an error";