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