lib/Moose/Meta/Attribute/Native/Trait.pm: factor out some of the namespace resolution...
[gitmo/Moose.git] / t / examples / 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 Moose 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 use Test::Requires {
19     'Test::Deep' => '0.01', # skip all if not installed
20 };
21
22 use Test::Fatal;
23
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     ];
32
33     # define your own type ...
34     type 'ArrayOfHashOfBarsAndRandomNumbers'
35         => where {
36             eq_deeply($_,
37                 array_each(
38                     subhashof({
39                         bar           => Test::Deep::isa('Bar'),
40                         random_number => ignore()
41                     })
42                 )
43             )
44         };
45
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 },
57     { bar => Bar->new },
58 ];
59
60 my $foo;
61 is( exception {
62     $foo = Foo->new('bar' => $array_of_hashes);
63 }, undef, '... construction succeeded' );
64 isa_ok($foo, 'Foo');
65
66 is_deeply($foo->bar, $array_of_hashes, '... got our value correctly');
67
68 isnt( exception {
69     $foo->bar({});
70 }, undef, '... validation failed correctly' );
71
72 isnt( exception {
73     $foo->bar([{ foo => 3 }]);
74 }, undef, '... validation failed correctly' );
75
76 done_testing;