Commit | Line | Data |
7a50b450 |
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 Mouse type constraints |
11 | play with Declare::Constraints::Simple. |
12 | |
13 | Pretty well if I do say so myself :) |
14 | |
15 | =cut |
16 | |
17 | BEGIN { |
18 | eval "use Declare::Constraints::Simple;"; |
19 | plan skip_all => "Declare::Constraints::Simple is required for this test" if $@; |
20 | plan tests => 9; |
21 | } |
22 | |
23 | use Test::Exception; |
24 | |
25 | { |
26 | package Foo; |
27 | use Mouse; |
28 | use Mouse::Util::TypeConstraints; |
29 | use Declare::Constraints::Simple -All; |
30 | |
31 | # define your own type ... |
32 | type( 'HashOfArrayOfObjects', |
33 | where => IsHashRef( |
34 | -keys => HasLength, |
35 | -values => IsArrayRef(IsObject) |
36 | ) |
37 | ); |
38 | |
39 | has 'bar' => ( |
40 | is => 'rw', |
41 | isa => 'HashOfArrayOfObjects', |
42 | ); |
43 | |
44 | # inline the constraints as anon-subtypes |
45 | has 'baz' => ( |
46 | is => 'rw', |
47 | isa => subtype( as => 'ArrayRef', where => IsArrayRef(IsInt) ), |
48 | ); |
49 | |
50 | package Bar; |
51 | use Mouse; |
52 | } |
53 | |
54 | my $hash_of_arrays_of_objs = { |
55 | foo1 => [ Bar->new ], |
56 | foo2 => [ Bar->new, Bar->new ], |
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, |
66 | ); |
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 | |
86 | dies_ok { |
87 | $foo->baz([ "foo" ]); |
88 | } '... validation failed correctly'; |
89 | |
90 | dies_ok { |
91 | $foo->baz({}); |
92 | } '... validation failed correctly'; |