We only need local $? if we inline calls to DEMOLISH
[gitmo/Moose.git] / t / examples / 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 use Test::Requires {
18     'Declare::Constraints::Simple' => '0.01', # skip all if not installed
19 };
20
21 use Test::Fatal;
22
23 {
24     package Foo;
25     use Moose;
26     use Moose::Util::TypeConstraints;
27     use Declare::Constraints::Simple -All;
28
29     # define your own type ...
30     type( 'HashOfArrayOfObjects',
31         {
32         where => IsHashRef(
33             -keys   => HasLength,
34             -values => IsArrayRef(IsObject)
35         )
36     } );
37
38     has 'bar' => (
39         is  => 'rw',
40         isa => 'HashOfArrayOfObjects',
41     );
42
43     # inline the constraints as anon-subtypes
44     has 'baz' => (
45         is  => 'rw',
46         isa => subtype( { as => 'ArrayRef', where => IsArrayRef(IsInt) } ),
47     );
48
49     package Bar;
50     use Moose;
51 }
52
53 my $hash_of_arrays_of_objs = {
54    foo1 => [ Bar->new ],
55    foo2 => [ Bar->new, Bar->new ],
56 };
57
58 my $array_of_ints = [ 1 .. 10 ];
59
60 my $foo;
61 is( exception {
62     $foo = Foo->new(
63        'bar' => $hash_of_arrays_of_objs,
64        'baz' => $array_of_ints,
65     );
66 }, undef, '... construction succeeded' );
67 isa_ok($foo, 'Foo');
68
69 is_deeply($foo->bar, $hash_of_arrays_of_objs, '... got our value correctly');
70 is_deeply($foo->baz, $array_of_ints, '... got our value correctly');
71
72 isnt( exception {
73     $foo->bar([]);
74 }, undef, '... validation failed correctly' );
75
76 isnt( exception {
77     $foo->bar({ foo => 3 });
78 }, undef, '... validation failed correctly' );
79
80 isnt( exception {
81     $foo->bar({ foo => [ 1, 2, 3 ] });
82 }, undef, '... validation failed correctly' );
83
84 isnt( exception {
85     $foo->baz([ "foo" ]);
86 }, undef, '... validation failed correctly' );
87
88 isnt( exception {
89     $foo->baz({});
90 }, undef, '... validation failed correctly' );
91
92 done_testing;