8e09c77d9b467a8001692e4d3e5cf8dc6c19d0ef
[gitmo/MooseX-Types-Structured.git] / t / suger.t
1 BEGIN {
2         use strict;
3         use warnings;
4         use Test::More tests=>3;
5 }
6
7 ## This is a first pass at what the regex enhancements to
8 ## Moose::Util::TypeConstraints is going to look like.  Basically I copyied
9 ## bits and added a little more parsing ability.
10  
11 {
12     ## Copied from Moose::Util::TypeConstraints
13     use re "eval";
14
15     my $any;
16     my $valid_chars = qr{[\w:]};
17     my $type_atom   = qr{ $valid_chars+ };
18     
19     my $type                = qr{  $valid_chars+  (?: \[  (??{$any})  \] )? }x;
20     my $type_capture_parts  = qr{ ($valid_chars+) (?: \[ ((??{$any})) \] )? }x;
21     my $type_with_parameter = qr{  $valid_chars+      \[  (??{$any})  \]    }x;
22     
23     my $op_union = qr{ \s* \| \s* }x;
24     my $union    = qr{ $type (?: $op_union $type )+ }x;
25     
26     ## New Stuff for structured types.
27     my $comma = qr{,};
28     my $indirection = qr{=>};
29     my $divider_ops = qr{ $comma | $indirection }x;
30     my $structure_divider = qr{\s* $divider_ops \s*}x;    
31     my $structure_elements = qr{ ($type $structure_divider*)+ }x;
32
33         ## Addd the | $structure_elements to this.
34     $any = qr{ $type | $union | $structure_elements }x;
35     
36     ## New Proposed methods to parse and create
37     sub _parse_structured_type_constraint {
38         { no warnings 'void'; $any; } # force capture of interpolated lexical
39         
40         my($base, $elements) = ($_[0] =~ m{ $type_capture_parts }x);
41         return ($base, [split($structure_divider, $elements)]);
42     }
43     
44     is_deeply
45         [_parse_structured_type_constraint('ArrayRef[Int,Str]')],
46         ["ArrayRef", ["Int", "Str"]]
47      => 'Correctly parsed ArrayRef[Int,Str]';
48      
49     is_deeply
50         [_parse_structured_type_constraint('ArrayRef[ArrayRef[Int],Str]')],
51         ["ArrayRef", ["ArrayRef[Int]", "Str"]]
52      => 'Correctly parsed ArrayRef[ArrayRef[Int],Str]';
53          
54     is_deeply 
55         [_parse_structured_type_constraint('HashRef[key1 => Int, key2=>Int, key3=>ArrayRef[Int]]')],
56         ["HashRef", ["key1", "Int", "key2", "Int", "key3", "ArrayRef[Int]"]]
57      => 'Correctly parsed HashRef[key1 => Int, key2=>Int, key3=>ArrayRef[Int]]';
58
59 }
60