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