Add various things
[gitmo/Mouse.git] / t / 200_examples / 005_example_w_TestDeep.t
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 Test::Deep.
12
13 Its not as pretty as Declare::Constraints::Simple,
14 but it is not completely horrid either.
15
16 =cut
17
18 BEGIN {
19     eval "use Test::Deep;";
20     plan skip_all => "Test::Deep is required for this test" if $@;
21     plan tests => 5;
22 }
23
24 use Test::Exception;
25
26 {
27     package Foo;
28     use Mouse;
29     use Mouse::Util::TypeConstraints;
30
31     use Test::Deep qw[
32         eq_deeply array_each subhashof ignore
33     ];
34
35     # define your own type ...
36     type 'ArrayOfHashOfBarsAndRandomNumbers'
37         => where {
38             eq_deeply($_,
39                 array_each(
40                     subhashof({
41                         bar           => Test::Deep::isa('Bar'),
42                         random_number => ignore()
43                     })
44                 )
45             )
46         };
47
48     has 'bar' => (
49         is  => 'rw',
50         isa => 'ArrayOfHashOfBarsAndRandomNumbers',
51     );
52
53     package Bar;
54     use Mouse;
55 }
56
57 my $array_of_hashes = [
58     { bar => Bar->new, random_number => 10 },
59     { bar => Bar->new },
60 ];
61
62 my $foo;
63 lives_ok {
64     $foo = Foo->new('bar' => $array_of_hashes);
65 } '... construction succeeded';
66 isa_ok($foo, 'Foo');
67
68 is_deeply($foo->bar, $array_of_hashes, '... got our value correctly');
69
70 dies_ok {
71     $foo->bar({});
72 } '... validation failed correctly';
73
74 dies_ok {
75     $foo->bar([{ foo => 3 }]);
76 } '... validation failed correctly';
77
78