I want parameterized types... I want it!
[gitmo/Mouse.git] / t / 043-parameterized-type.t
1 #!/usr/bin/env perl
2 use strict;
3 use warnings;
4 use Test::More tests => 7;
5 use Test::Exception;
6
7 {
8     {
9         package Foo;
10         use Mouse;
11
12         has foo => (
13             is  => 'ro',
14             isa => 'HashRef[Int]',
15         );
16
17         has bar => (
18             is  => 'ro',
19             isa => 'ArrayRef[Int]',
20         );
21
22         has 'complex' => (
23             is => 'rw',
24             isa => 'ArrayRef[HashRef[Int]]'
25         );
26     };
27
28     ok(Foo->meta->has_attribute('foo'));
29
30     lives_and {
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);
35
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";
40
41     # check bad args
42     throws_ok {
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";
45
46     throws_ok {
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";
49
50     throws_ok {
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";
53 }
54
55
56