got the basic function for Optional, but the regex is still troubled, now is having...
[gitmo/MooseX-Types-Structured.git] / t / 01-basic.t
1 BEGIN {
2         use strict;
3         use warnings;
4         use Test::More tests=>34;
5         use Test::Exception;
6         
7         use_ok 'Moose::Util::TypeConstraints';
8         use_ok 'MooseX::Meta::TypeConstraint::Structured::Generator';
9         use_ok 'MooseX::Meta::TypeConstraint::Structured::Positional';
10         use_ok 'MooseX::Meta::TypeConstraint::Structured::Optional';    
11         use_ok 'MooseX::Meta::TypeConstraint::Structured::Named';
12 }
13
14 my $optional = MooseX::Meta::TypeConstraint::Structured::Generator->new(
15                 name => 'Optional',
16                 structured_type => 'MooseX::Meta::TypeConstraint::Structured::Optional',
17                 package_defined_in => __PACKAGE__,
18                 parent => find_type_constraint('ArrayRef'),
19         );
20
21 my $tuple = MooseX::Meta::TypeConstraint::Structured::Generator->new(
22                 name => 'Tuple',
23                 structured_type => 'MooseX::Meta::TypeConstraint::Structured::Positional',
24                 package_defined_in => __PACKAGE__,
25                 parent => find_type_constraint('ArrayRef'),
26         );
27
28 Moose::Util::TypeConstraints::register_type_constraint($optional);
29 Moose::Util::TypeConstraints::register_type_constraint($tuple);
30
31 ## Make sure the new type constraints have been registered
32
33 ok Moose::Util::TypeConstraints::find_type_constraint('Tuple')
34  => 'Found the Tuple Type';
35
36 {
37         package Test::MooseX::Types::Structured::BasicAttributes;
38         
39         use Moose;
40         use Moose::Util::TypeConstraints;
41         
42         has 'tuple' => (is=>'rw', isa=>'Tuple[Int,Str,Int]');
43         has 'tuple_with_parameterized' => (is=>'rw', isa=>'Tuple[Int,Str,Int,ArrayRef[Int]]');
44         has 'tuple_with_optional' => (is=>'rw', isa=>'Tuple[Int,Str,Int,Optional[Int,Int]]');
45         has 'tuple_with_union' => (is=>'rw', isa=>'Tuple[Int,Str,Int|Object,Optional[Int|Object,Int]]');
46 }
47
48 #use Data::Dump qw/dump/;
49 #warn dump Moose::Util::TypeConstraints::list_all_type_constraints;
50
51 ok my $positioned_obj = Test::MooseX::Types::Structured::BasicAttributes->new,
52  => 'Got a good object';
53
54 ok Moose::Util::TypeConstraints::find_type_constraint('Tuple[Int,Str,Int]')
55  => 'Found expected type constraint';
56
57 ok Moose::Util::TypeConstraints::find_type_constraint('Tuple[Int,Str,Int,Optional[Int,Int]]')
58  => 'Found expected type constraint';
59  
60 ## Test tuple (Tuple[Int,Str,Int])
61
62 ok $positioned_obj->tuple([1,'hello',3])
63  => "[1,'hello',3] properly suceeds";
64
65 throws_ok sub {
66         $positioned_obj->tuple([1,2,'world']);
67 }, qr/Validation failed for 'Int' failed with value world/ => "[1,2,'world'] properly fails";
68
69 throws_ok sub {
70         $positioned_obj->tuple(['hello1',2,3]);
71 }, qr/Validation failed for 'Int' failed with value hello1/ => "['hello',2,3] properly fails";
72
73 throws_ok sub {
74         $positioned_obj->tuple(['hello2',2,'world']);
75 }, qr/Validation failed for 'Int' failed with value hello2/ => "['hello',2,'world'] properly fails";
76
77
78 ## Test tuple_with_parameterized (Tuple[Int,Str,Int,ArrayRef[Int]])
79
80 ok $positioned_obj->tuple_with_parameterized([1,'hello',3,[1,2,3]])
81  => "[1,'hello',3,[1,2,3]] properly suceeds";
82
83 throws_ok sub {
84         $positioned_obj->tuple_with_parameterized([1,2,'world']);
85 }, qr/Validation failed for 'Int' failed with value world/ => "[1,2,'world'] properly fails";
86
87 throws_ok sub {
88         $positioned_obj->tuple_with_parameterized(['hello1',2,3]);
89 }, qr/Validation failed for 'Int' failed with value hello1/ => "['hello',2,3] properly fails";
90
91 throws_ok sub {
92         $positioned_obj->tuple_with_parameterized(['hello2',2,'world']);
93 }, qr/Validation failed for 'Int' failed with value hello2/ => "['hello',2,'world'] properly fails";
94
95 throws_ok sub {
96         $positioned_obj->tuple_with_parameterized([1,'hello',3,[1,2,'world']]);
97 }, qr/Validation failed for 'ArrayRef\[Int\]'/ => "[1,'hello',3,[1,2,'world']] properly fails";
98
99
100 ## Test tuple_with_optional (Tuple[Int,Str,Int,Optional[Int,Int]])
101
102 ok $positioned_obj->tuple_with_optional([1,'hello',3])
103  => "[1,'hello',3] properly suceeds";
104
105 ok $positioned_obj->tuple_with_optional([1,'hello',3,1])
106  => "[1,'hello',3,1] properly suceeds";
107
108 ok $positioned_obj->tuple_with_optional([1,'hello',3,4])
109  => "[1,'hello',3,4] properly suceeds";
110
111 ok $positioned_obj->tuple_with_optional([1,'hello',3,4,5])
112  => "[1,'hello',3,4,5] properly suceeds";
113
114 throws_ok sub {
115         $positioned_obj->tuple_with_optional([1,'hello',3,4,5,6]);
116 }, qr/Too Many arguments for the available type constraints/ => "[1,'hello',3,4,5,6] properly fails";
117
118 throws_ok sub {
119         $positioned_obj->tuple_with_optional([1,2,'world']);
120 }, qr/Validation failed for 'Int' failed with value world/ => "[1,2,'world'] properly fails";
121
122 throws_ok sub {
123         $positioned_obj->tuple_with_optional(['hello1',2,3]);
124 }, qr/Validation failed for 'Int' failed with value hello1/ => "['hello',2,3] properly fails";
125
126 throws_ok sub {
127         $positioned_obj->tuple_with_optional(['hello2',2,'world']);
128 }, qr/Validation failed for 'Int' failed with value hello2/ => "['hello',2,'world'] properly fails";
129
130 ## tuple_with_union Tuple[Int,Str,Int|Object,Optional[Int|Object,Int]]
131
132 ok $positioned_obj->tuple_with_union([1,'hello',3])
133  => "[1,'hello',3] properly suceeds";
134
135 ok $positioned_obj->tuple_with_union([1,'hello',3,1])
136  => "[1,'hello',3,1] properly suceeds";
137
138 ok $positioned_obj->tuple_with_union([1,'hello',3,4])
139  => "[1,'hello',3,4] properly suceeds";
140
141 ok $positioned_obj->tuple_with_union([1,'hello',3,4,5])
142  => "[1,'hello',3,4,5] properly suceeds";
143
144 throws_ok sub {
145         $positioned_obj->tuple_with_union([1,'hello',3,4,5,6]);
146 }, qr/Too Many arguments for the available type constraints/ => "[1,'hello',3,4,5,6] properly fails";
147
148 throws_ok sub {
149         $positioned_obj->tuple_with_union([1,2,'world']);
150 }, qr/Validation failed for 'Int' failed with value world/ => "[1,2,'world'] properly fails";
151
152 throws_ok sub {
153         $positioned_obj->tuple_with_union(['hello1',2,3]);
154 }, qr/Validation failed for 'Int' failed with value hello1/ => "['hello',2,3] properly fails";
155
156 throws_ok sub {
157         $positioned_obj->tuple_with_union(['hello2',2,'world']);
158 }, qr/Validation failed for 'Int' failed with value hello2/ => "['hello',2,'world'] properly fails";
159
160
161 #ok Moose::Util::TypeConstraints::_detect_parameterized_type_constraint('HashRef[key1 => Int, key2=>Int, key3=>ArrayRef[Int]]')
162 # => 'detected correctly';
163  
164 #is_deeply 
165 #       [Moose::Util::TypeConstraints::_parse_parameterized_type_constraint('HashRef[key1 => Int, key2=>Int, key3=>ArrayRef[Int]]')],
166 #       ["HashRef", "key1", "Int", "key2", "Int", "key3", "ArrayRef[Int]"]
167 # => 'Correctly parsed HashRef[key1 => Int, key2=>Int, key3=>ArrayRef[Int]]';