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