merge trunk to pluggable errors
[gitmo/Moose.git] / t / 200_examples / 004_example_w_DCS.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7
8 =pod
9
10 This tests how well Moose type constraints 
11 play with Declare::Constraints::Simple. 
12
13 Pretty well if I do say so myself :)
14
15 =cut
16
17 BEGIN {
18     eval "use Declare::Constraints::Simple;";
19     plan skip_all => "Declare::Constraints::Simple is required for this test" if $@;        
20     plan tests => 9;    
21 }
22
23 use Test::Exception;
24
25 {
26     package Foo;
27     use Moose;
28     use Moose::Util::TypeConstraints;
29     use Declare::Constraints::Simple -All;
30     
31     # define your own type ...
32     type 'HashOfArrayOfObjects' 
33         => IsHashRef(
34             -keys   => HasLength,
35             -values => IsArrayRef( IsObject ));    
36     
37     has 'bar' => (
38         is  => 'rw',
39         isa => 'HashOfArrayOfObjects',
40     );
41     
42     # inline the constraints as anon-subtypes
43     has 'baz' => (
44         is  => 'rw',
45         isa => subtype('ArrayRef' => IsArrayRef(IsInt)),
46     );
47
48     package Bar;
49     use Moose;
50 }
51
52 my $hash_of_arrays_of_objs = {
53    foo1 => [ Bar->new ],
54    foo2 => [ Bar->new, Bar->new ],       
55 };
56
57 my $array_of_ints = [ 1 .. 10 ];
58
59 my $foo;
60 lives_ok {
61     $foo = Foo->new(
62        'bar' => $hash_of_arrays_of_objs,
63        'baz' => $array_of_ints,
64     ); 
65 } '... construction succeeded';
66 isa_ok($foo, 'Foo');
67
68 is_deeply($foo->bar, $hash_of_arrays_of_objs, '... got our value correctly');
69 is_deeply($foo->baz, $array_of_ints, '... got our value correctly');
70
71 dies_ok {
72     $foo->bar([]);
73 } '... validation failed correctly';
74
75 dies_ok {
76     $foo->bar({ foo => 3 });
77 } '... validation failed correctly';
78
79 dies_ok {
80     $foo->bar({ foo => [ 1, 2, 3 ] });
81 } '... validation failed correctly';
82
83 dies_ok {
84     $foo->baz([ "foo" ]);
85 } '... validation failed correctly';
86
87 dies_ok {
88     $foo->baz({});
89 } '... validation failed correctly';
90
91
92
93
94
95
96