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