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