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