4 use Test::More tests => 9;
14 isa => 'HashRef[Int]',
19 isa => 'ArrayRef[Int]',
24 isa => 'ArrayRef[HashRef[Int]]'
28 ok(Foo->meta->has_attribute('foo'));
31 my $hash = { a => 1, b => 2, c => 3 };
32 my $array = [ 1, 2, 3 ];
33 my $complex = [ { a => 1, b => 1 }, { c => 2, d => 2} ];
34 my $foo = Foo->new(foo => $hash, bar => $array, complex => $complex);
36 is_deeply($foo->foo(), $hash, "foo is a proper hash");
37 is_deeply($foo->bar(), $array, "bar is a proper array");
38 is_deeply($foo->complex(), $complex, "complex is a proper ... structure");
39 } "Parameterized constraints work";
43 Foo->new( foo => { a => 'b' });
44 } 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";
47 Foo->new( bar => [ a => 'b' ]);
48 } 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";
51 Foo->new( complex => [ { a => 1, b => 1 }, { c => "d", e => "f" } ] )
52 } 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";
59 use Mouse::Util::TypeConstraints;
62 => as 'ArrayRef[HashRef]'
65 => from 'ArrayRef[Str]'
67 [ map { +{ $_ => 1 } } @$_ ]
78 my @list = ( {a => 1}, {b => 1}, {c => 1} );
79 my $bar = Bar->new(list => [ qw(a b c) ]);
81 is_deeply( $bar->list, \@list, "list is as expected");
85 Bar->new(list => [ { 1 => 2 }, 2, 3 ]);
86 } qr/Attribute \(list\) does not pass the type constraint because: Validation failed for 'Bar::List' failed with value/, "Bad coercion parameter throws an error";