started on a real message stacker
[gitmo/MooseX-Types-Structured.git] / t / 12-error.t
CommitLineData
797510e3 1BEGIN {
8dbdca20 2 use strict;
3 use warnings;
21d0e759 4 use Test::More tests=>27;
797510e3 5}
6
7use Moose::Util::TypeConstraints;
7559b71f 8use MooseX::Types::Structured qw(Dict Tuple Optional);
797510e3 9use MooseX::Types::Moose qw(Int Str ArrayRef HashRef);
10
11# Create some TCs from which errors will be generated
7559b71f 12
797510e3 13my $simple_tuple = subtype 'simple_tuple', as Tuple[Int,Str];
14my $simple_dict = subtype 'simple_dict', as Dict[name=>Str,age=>Int];
15
7559b71f 16# Make sure the constraints we made validate as expected
17
797510e3 18ok $simple_tuple->check([1,'hello']), "simple_tuple validates: 1,'hello'";
19ok !$simple_tuple->check(['hello',1]), "simple_tuple fails: 'hello',1";
7559b71f 20ok $simple_dict->check({name=>'Vanessa',age=>34}), "simple_dict validates: {name=>'Vanessa',age=>34}";
21ok !$simple_dict->check({name=>$simple_dict,age=>'hello'}), "simple_dict fails: {name=>Object, age=>String}";
22
23## Let's check all the expected validation errors for tuple
24
25like $simple_tuple->validate({a=>1,b=>2}),
ae998529 26 qr/Validation failed for 'simple_tuple' with value { a: 1, b: 2 }/,
7559b71f 27 'Wrong basic type';
28
29like $simple_tuple->validate(['a','b']),
ae998529 30 qr/failed for 'simple_tuple' with value \[ "a", "b" \]/,
7559b71f 31 'Correctly failed due to "a" not an Int';
9eb6e8c6 32
7559b71f 33like $simple_tuple->validate([1,$simple_tuple]),
ae998529 34 qr/Validation failed for 'simple_tuple' with value \[ 1, MooseX::Meta::TypeConstraint::Structured/,
7559b71f 35 'Correctly failed due to object not a Str';
36
37like $simple_tuple->validate([1]),
ae998529 38 qr/Validation failed for 'Str' with value NULL/,
7559b71f 39 'Not enought values';
40
41like $simple_tuple->validate([1,'hello','too many']),
42 qr/More values than Type Constraints!/,
43 'Too Many values';
44
45## And the same thing for dicts [name=>Str,age=>Int]
46
47like $simple_dict->validate([1,2]),
ae998529 48 qr/ with value \[ 1, 2 \]/,
7559b71f 49 'Wrong basic type';
9eb6e8c6 50
7559b71f 51like $simple_dict->validate({name=>'John',age=>'a'}),
ae998529 52 qr/failed for 'Int' with value a/,
7559b71f 53 'Correctly failed due to age not an Int';
9eb6e8c6 54
7559b71f 55like $simple_dict->validate({name=>$simple_dict,age=>1}),
ae998529 56 qr/with value { age: 1, name: MooseX:/,
7559b71f 57 'Correctly failed due to object not a Str';
58
59like $simple_dict->validate({name=>'John'}),
ae998529 60 qr/failed for 'Int' with value NULL/,
7559b71f 61 'Not enought values';
62
63like $simple_dict->validate({name=>'Vincent', age=>15,extra=>'morethanIneed'}),
64 qr/More values than Type Constraints!/,
65 'Too Many values';
9eb6e8c6 66
7559b71f 67 ## TODO some with Optional (or Maybe) and slurpy
9eb6e8c6 68
7559b71f 69 my $optional_tuple = subtype 'optional_tuple', as Tuple[Int,Optional[Str]];
70 my $optional_dict = subtype 'optional_dict', as Dict[name=>Str,age=>Optional[Int]];
9eb6e8c6 71
7559b71f 72 like $optional_tuple->validate({a=>1,b=>2}),
ae998529 73 qr/Validation failed for 'optional_tuple' with value { a: 1, b: 2 }/,
7559b71f 74 'Wrong basic type';
75
76like $optional_tuple->validate(['a','b']),
ae998529 77 qr/failed for 'Int' with value a/,
7559b71f 78 'Correctly failed due to "a" not an Int';
9eb6e8c6 79
7559b71f 80like $optional_tuple->validate([1,$simple_tuple]),
ae998529 81 qr/failed for 'MooseX::Types::Structured::Optional\[Str\]' with value MooseX/,
7559b71f 82 'Correctly failed due to object not a Str';
83
84like $optional_tuple->validate([1,'hello','too many']),
85 qr/More values than Type Constraints!/,
86 'Too Many values';
87
88like $optional_dict->validate([1,2]),
ae998529 89 qr/ with value \[ 1, 2 \]/,
7559b71f 90 'Wrong basic type';
9eb6e8c6 91
7559b71f 92like $optional_dict->validate({name=>'John',age=>'a'}),
ae998529 93 qr/Validation failed for 'MooseX::Types::Structured::Optional\[Int\]' with value a/,
7559b71f 94 'Correctly failed due to age not an Int';
9eb6e8c6 95
7559b71f 96like $optional_dict->validate({name=>$simple_dict,age=>1}),
ae998529 97 qr/with value { age: 1, name: MooseX:/,
7559b71f 98 'Correctly failed due to object not a Str';
99
100like $optional_dict->validate({name=>'Vincent', age=>15,extra=>'morethanIneed'}),
101 qr/More values than Type Constraints!/,
102 'Too Many values';
9eb6e8c6 103
7559b71f 104## Deeper constraints
797510e3 105
7559b71f 106my $deep_tuple = subtype 'deep_tuple',
107 as Tuple[
108 Int,
109 HashRef,
110 Dict[
111 name=>Str,
112 age=>Int,
113 ],
114 ];
9eb6e8c6 115
7559b71f 116ok $deep_tuple->check([1,{a=>2},{name=>'Vincent',age=>15}]),
117 'Good Constraint';
9eb6e8c6 118
21d0e759 119{
120 my $message = $deep_tuple->validate([1,{a=>2},{name=>'Vincent',age=>'Hello'}]);
121 like $message,
122 qr/Validation failed for 'MooseX::Types::Structured::Dict\[name,Str,age,Int\]'/,
123 'Example deeper error';
124}
125
126like $simple_tuple->validate(["aaa","bbb"]),
127 qr/'Int' with value aaa/,
128 'correct deeper error';
129
130like $deep_tuple->validate([1,{a=>2},{name=>'Vincent1',age=>'Hello1'}]),
131 qr/'Int' with value Hello1/,
132 'correct deeper error';
133
5ac93dbb 134## Success Tests...
135
136ok !$deep_tuple->validate([1,{a=>2},{name=>'John',age=>40}]), 'Validates ok';
137
138## Deeper Tests...
139
21d0e759 140my $deeper_tc = subtype
141 as Dict[
142 a => Tuple[
143 Dict[
144 a1a => Tuple[Int],
145 a1b => Tuple[Int],
146 ],
147 Dict[
148 a2a => Tuple[Int],
149 a2b => Tuple[Int],
150 ],
151 ],
152 b => Tuple[
153 Dict[
154 b1a => Tuple[Int],
155 b1b => Tuple[Int],
156 ],
157 Dict[
158 b2a => Tuple[Int],
159 b2b => Tuple[Int],
160 ],
161 ],
162 ];
163
164{
165 my $message = $deeper_tc->validate({a=>[{a1a=>[1],a1b=>[2]},{a2a=>[3],a2b=>[4]}],b=>[{b1a=>[5],b1b=>['AA']},{b2a=>[7],b2b=>[8]}]});
166 warn $message;
167}
9eb6e8c6 168
21d0e759 169