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