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