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