support for recursive types, documentation and tests for that, random documentation...
[gitmo/MooseX-Types.git] / t / 15_recursion.t
1 ## Test case inspired by Stevan Little
2
3 BEGIN {
4     package MooseX::Types::Test::Recursion;
5     
6     use Moose;
7
8     use Moose::Util::TypeConstraints;
9     use MooseX::Types::Moose qw(Str HashRef);
10     use MooseX::Types -declare => [qw(
11         RecursiveHashRef
12     )];
13
14     ## Define a recursive subtype and Cthulhu save us.
15     subtype RecursiveHashRef()
16      => as HashRef[Str() | RecursiveHashRef()];
17 }
18
19 {
20     package MooseX::Types::Test::Recursion::TestRunner;
21     
22     BEGIN {
23         use Test::More tests=>5;
24         use Test::Exception;
25         
26         ## Grab the newly created test type constraint
27         MooseX::Types::Test::Recursion->import(':all');
28     };
29
30     
31     ok RecursiveHashRef->check({key=>"value"})
32      => 'properly validated {key=>"value"}';
33      
34     ok RecursiveHashRef->check({key=>{subkey=>"value"}})
35      => 'properly validated {key=>{subkey=>"value"}}';
36      
37     ok RecursiveHashRef->check({
38         key=>{
39             subkey=>"value",
40             subkey2=>{
41                 ssubkey1=>"value3",
42                 ssubkey2=>"value4"
43             }
44         }
45     }) => 'properly validated deeper recursive values';
46     
47     ok ! RecursiveHashRef->check({key=>[1,2,3]})
48      => 'Properly invalidates bad value';
49      
50     ok ! RecursiveHashRef->check({key=>{subkey=>"value",subkey2=>{ssubkey=>[1,2,3]}}})
51      => 'Properly invalidates bad value deeply';
52 }
53
54
55
56
57
58