got the basic function for Optional, but the regex is still troubled, now is having...
[gitmo/MooseX-Types-Structured.git] / t / 01-basic.t
CommitLineData
bfef1b30 1BEGIN {
2 use strict;
3 use warnings;
99b27cbd 4 use Test::More tests=>34;
bfef1b30 5 use Test::Exception;
6
7 use_ok 'Moose::Util::TypeConstraints';
dbd75632 8 use_ok 'MooseX::Meta::TypeConstraint::Structured::Generator';
7e2f0558 9 use_ok 'MooseX::Meta::TypeConstraint::Structured::Positional';
99b27cbd 10 use_ok 'MooseX::Meta::TypeConstraint::Structured::Optional';
7e2f0558 11 use_ok 'MooseX::Meta::TypeConstraint::Structured::Named';
bfef1b30 12}
13
99b27cbd 14my $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
dbd75632 21my $tuple = MooseX::Meta::TypeConstraint::Structured::Generator->new(
bfef1b30 22 name => 'Tuple',
7e2f0558 23 structured_type => 'MooseX::Meta::TypeConstraint::Structured::Positional',
bfef1b30 24 package_defined_in => __PACKAGE__,
7e2f0558 25 parent => find_type_constraint('ArrayRef'),
bfef1b30 26 );
27
99b27cbd 28Moose::Util::TypeConstraints::register_type_constraint($optional);
011bacc6 29Moose::Util::TypeConstraints::register_type_constraint($tuple);
bfef1b30 30
011bacc6 31## Make sure the new type constraints have been registered
bfef1b30 32
011bacc6 33ok Moose::Util::TypeConstraints::find_type_constraint('Tuple')
34 => 'Found the Tuple Type';
bfef1b30 35
bfef1b30 36{
dbd75632 37 package Test::MooseX::Types::Structured::BasicAttributes;
bfef1b30 38
011bacc6 39 use Moose;
40 use Moose::Util::TypeConstraints;
bfef1b30 41
011bacc6 42 has 'tuple' => (is=>'rw', isa=>'Tuple[Int,Str,Int]');
99b27cbd 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]]');
bfef1b30 46}
47
99b27cbd 48#use Data::Dump qw/dump/;
49#warn dump Moose::Util::TypeConstraints::list_all_type_constraints;
011bacc6 50
dbd75632 51ok my $positioned_obj = Test::MooseX::Types::Structured::BasicAttributes->new,
bfef1b30 52 => 'Got a good object';
53
99b27cbd 54ok Moose::Util::TypeConstraints::find_type_constraint('Tuple[Int,Str,Int]')
55 => 'Found expected type constraint';
56
57ok 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
011bacc6 62ok $positioned_obj->tuple([1,'hello',3])
63 => "[1,'hello',3] properly suceeds";
64
65throws_ok sub {
66 $positioned_obj->tuple([1,2,'world']);
67}, qr/Validation failed for 'Int' failed with value world/ => "[1,2,'world'] properly fails";
bfef1b30 68
011bacc6 69throws_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
73throws_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
99b27cbd 78## Test tuple_with_parameterized (Tuple[Int,Str,Int,ArrayRef[Int]])
79
80ok $positioned_obj->tuple_with_parameterized([1,'hello',3,[1,2,3]])
81 => "[1,'hello',3,[1,2,3]] properly suceeds";
82
83throws_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
87throws_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
91throws_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
95throws_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
102ok $positioned_obj->tuple_with_optional([1,'hello',3])
103 => "[1,'hello',3] properly suceeds";
104
105ok $positioned_obj->tuple_with_optional([1,'hello',3,1])
106 => "[1,'hello',3,1] properly suceeds";
107
108ok $positioned_obj->tuple_with_optional([1,'hello',3,4])
109 => "[1,'hello',3,4] properly suceeds";
110
111ok $positioned_obj->tuple_with_optional([1,'hello',3,4,5])
112 => "[1,'hello',3,4,5] properly suceeds";
113
114throws_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
118throws_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
122throws_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
126throws_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
132ok $positioned_obj->tuple_with_union([1,'hello',3])
133 => "[1,'hello',3] properly suceeds";
134
135ok $positioned_obj->tuple_with_union([1,'hello',3,1])
136 => "[1,'hello',3,1] properly suceeds";
137
138ok $positioned_obj->tuple_with_union([1,'hello',3,4])
139 => "[1,'hello',3,4] properly suceeds";
140
141ok $positioned_obj->tuple_with_union([1,'hello',3,4,5])
142 => "[1,'hello',3,4,5] properly suceeds";
143
144throws_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
148throws_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
152throws_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
156throws_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";
011bacc6 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]]';