Commit | Line | Data |
703e92fb |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
6 | use Test::More; |
7 | |
8 | =pod |
9 | |
d03bd989 |
10 | This tests how well Moose type constraints |
11 | play with Test::Deep. |
703e92fb |
12 | |
d03bd989 |
13 | Its not as pretty as Declare::Constraints::Simple, |
703e92fb |
14 | but it is not completely horrid either. |
15 | |
16 | =cut |
17 | |
18 | BEGIN { |
19 | eval "use Test::Deep;"; |
d03bd989 |
20 | plan skip_all => "Test::Deep is required for this test" if $@; |
703e92fb |
21 | } |
22 | |
23 | use Test::Exception; |
24 | |
703e92fb |
25 | { |
26 | package Foo; |
27 | use Moose; |
28 | use Moose::Util::TypeConstraints; |
29 | |
30 | use Test::Deep qw[ |
31 | eq_deeply array_each subhashof ignore |
32 | ]; |
d03bd989 |
33 | |
703e92fb |
34 | # define your own type ... |
d03bd989 |
35 | type 'ArrayOfHashOfBarsAndRandomNumbers' |
703e92fb |
36 | => where { |
d03bd989 |
37 | eq_deeply($_, |
703e92fb |
38 | array_each( |
39 | subhashof({ |
40 | bar => Test::Deep::isa('Bar'), |
41 | random_number => ignore() |
42 | }) |
43 | ) |
44 | ) |
d03bd989 |
45 | }; |
46 | |
703e92fb |
47 | has 'bar' => ( |
48 | is => 'rw', |
49 | isa => 'ArrayOfHashOfBarsAndRandomNumbers', |
50 | ); |
51 | |
52 | package Bar; |
53 | use Moose; |
54 | } |
55 | |
56 | my $array_of_hashes = [ |
57 | { bar => Bar->new, random_number => 10 }, |
d03bd989 |
58 | { bar => Bar->new }, |
703e92fb |
59 | ]; |
60 | |
61 | my $foo; |
62 | lives_ok { |
d03bd989 |
63 | $foo = Foo->new('bar' => $array_of_hashes); |
703e92fb |
64 | } '... construction succeeded'; |
65 | isa_ok($foo, 'Foo'); |
66 | |
67 | is_deeply($foo->bar, $array_of_hashes, '... got our value correctly'); |
68 | |
69 | dies_ok { |
70 | $foo->bar({}); |
71 | } '... validation failed correctly'; |
72 | |
73 | dies_ok { |
74 | $foo->bar([{ foo => 3 }]); |
75 | } '... validation failed correctly'; |
76 | |
a28e50e4 |
77 | done_testing; |