fixed incorrect alway finding an error with TC->validate
[gitmo/MooseX-Types-Structured.git] / t / 12-error.t
CommitLineData
797510e3 1BEGIN {
2 use strict;
3 use warnings;
d716430a 4 use Test::More tests=>25;
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}),
26 qr/Validation failed for 'simple_tuple' failed with value { a => 1, b => 2 }/,
27 'Wrong basic type';
28
29like $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
33like $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
37like $simple_tuple->validate([1]),
38 qr/Validation failed for 'Str' failed with value NULL/,
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]),
48 qr/ failed with value \[ 1, 2 \]/,
49 'Wrong basic type';
50
51like $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
55like $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
59like $simple_dict->validate({name=>'John'}),
60 qr/failed for 'Int' failed with value NULL/,
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';
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
76like $optional_tuple->validate(['a','b']),
77 qr/failed for 'Int' failed with value a/,
78 'Correctly failed due to "a" not an Int';
79
80like $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
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]),
89 qr/ failed with value \[ 1, 2 \]/,
90 'Wrong basic type';
91
92like $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
96like $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
100like $optional_dict->validate({name=>'Vincent', age=>15,extra=>'morethanIneed'}),
101 qr/More values than Type Constraints!/,
102 'Too Many values';
103
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 ];
115
116ok $deep_tuple->check([1,{a=>2},{name=>'Vincent',age=>15}]),
117 'Good Constraint';
118
119like $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';
d716430a 122
123## Success Tests...
124
125ok !$deep_tuple->validate([1,{a=>2},{name=>'John',age=>40}]), 'Validates ok';