fixed incorrect alway finding an error with TC->validate
[gitmo/MooseX-Types-Structured.git] / t / 12-error.t
1 BEGIN {
2         use strict;
3         use warnings;
4         use Test::More tests=>25;
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' failed with value { a => 1, b => 2 }/,
27  'Wrong basic type';
28
29 like $simple_tuple->validate(['a','b']),
30  qr/failed for 'simple_tuple' failed 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' failed 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' failed 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/ failed with value \[ 1, 2 \]/,
49  'Wrong basic type';
50  
51 like $simple_dict->validate({name=>'John',age=>'a'}),
52  qr/failed for 'Int' failed 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/failed 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' failed 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' failed with value { a => 1, b => 2 }/,
74  'Wrong basic type';
75
76 like $optional_tuple->validate(['a','b']),
77  qr/failed for 'Int' failed 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\]' failed 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/ failed 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\]' failed 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/failed 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 like $deep_tuple->validate([1,{a=>2},{name=>'Vincent',age=>'Hello'}]),
120   qr/Error is: Validation failed for 'MooseX::Types::Structured::Dict\[name,Str,age,Int\]'/,
121   'Example deeper error';
122   
123 ## Success Tests...
124
125 ok !$deep_tuple->validate([1,{a=>2},{name=>'John',age=>40}]), 'Validates ok';