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