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 $@; |
21 | plan tests => 5; |
703e92fb |
22 | } |
23 | |
24 | use Test::Exception; |
25 | |
703e92fb |
26 | { |
27 | package Foo; |
28 | use Moose; |
29 | use Moose::Util::TypeConstraints; |
30 | |
31 | use Test::Deep qw[ |
32 | eq_deeply array_each subhashof ignore |
33 | ]; |
d03bd989 |
34 | |
703e92fb |
35 | # define your own type ... |
d03bd989 |
36 | type 'ArrayOfHashOfBarsAndRandomNumbers' |
703e92fb |
37 | => where { |
d03bd989 |
38 | eq_deeply($_, |
703e92fb |
39 | array_each( |
40 | subhashof({ |
41 | bar => Test::Deep::isa('Bar'), |
42 | random_number => ignore() |
43 | }) |
44 | ) |
45 | ) |
d03bd989 |
46 | }; |
47 | |
703e92fb |
48 | has 'bar' => ( |
49 | is => 'rw', |
50 | isa => 'ArrayOfHashOfBarsAndRandomNumbers', |
51 | ); |
52 | |
53 | package Bar; |
54 | use Moose; |
55 | } |
56 | |
57 | my $array_of_hashes = [ |
58 | { bar => Bar->new, random_number => 10 }, |
d03bd989 |
59 | { bar => Bar->new }, |
703e92fb |
60 | ]; |
61 | |
62 | my $foo; |
63 | lives_ok { |
d03bd989 |
64 | $foo = Foo->new('bar' => $array_of_hashes); |
1808c2da |
65 | } 'construction succeeded'; |
703e92fb |
66 | isa_ok($foo, 'Foo'); |
67 | |
1808c2da |
68 | is_deeply($foo->bar, $array_of_hashes, 'got our value correctly'); |
703e92fb |
69 | |
70 | dies_ok { |
71 | $foo->bar({}); |
1808c2da |
72 | } 'validation failed correctly'; |
703e92fb |
73 | |
74 | dies_ok { |
75 | $foo->bar([{ foo => 3 }]); |
1808c2da |
76 | } 'validation failed correctly'; |
703e92fb |
77 | |
78 | |