5 use Moose::Util::TypeConstraints;
6 use MooseX::Types::Structured qw(Dict Tuple Optional);
7 use MooseX::Types::Moose qw(Int Str ArrayRef HashRef);
9 # Create some TCs from which errors will be generated
11 my $simple_tuple = subtype 'simple_tuple', as Tuple[Int,Str];
12 my $simple_dict = subtype 'simple_dict', as Dict[name=>Str,age=>Int];
14 # Make sure the constraints we made validate as expected
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}";
21 ## Let's check all the expected validation errors for tuple
23 like $simple_tuple->validate({a=>1,b=>2}),
24 qr/Validation failed for 'simple_tuple' with value { a: 1, b: 2 }/,
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';
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';
35 like $simple_tuple->validate([1]),
36 qr/Validation failed for 'Str' with value NULL/,
39 like $simple_tuple->validate([1,'hello','too many']),
40 qr/More values than Type Constraints!/,
43 ## And the same thing for dicts [name=>Str,age=>Int]
45 like $simple_dict->validate([1,2]),
46 qr/ with value \[ 1, 2 \]/,
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';
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';
57 like $simple_dict->validate({name=>'John'}),
58 qr/failed for 'Int' with value NULL/,
61 like $simple_dict->validate({name=>'Vincent', age=>15,extra=>'morethanIneed'}),
62 qr/More values than Type Constraints!/,
65 ## TODO some with Optional (or Maybe) and slurpy
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]];
70 like $optional_tuple->validate({a=>1,b=>2}),
71 qr/Validation failed for 'optional_tuple' with value { a: 1, b: 2 }/,
74 like $optional_tuple->validate(['a','b']),
75 qr/failed for 'Int' with value a/,
76 'Correctly failed due to "a" not an Int';
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';
82 like $optional_tuple->validate([1,'hello','too many']),
83 qr/More values than Type Constraints!/,
86 like $optional_dict->validate([1,2]),
87 qr/ with value \[ 1, 2 \]/,
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';
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';
98 like $optional_dict->validate({name=>'Vincent', age=>15,extra=>'morethanIneed'}),
99 qr/More values than Type Constraints!/,
102 ## Deeper constraints
104 my $deep_tuple = subtype 'deep_tuple',
114 ok $deep_tuple->check([1,{a=>2},{name=>'Vincent',age=>15}]),
118 my $message = $deep_tuple->validate([1,{a=>2},{name=>'Vincent',age=>'Hello'}]);
120 qr/Validation failed for 'Dict\[name,Str,age,Int\]'/,
121 'Example deeper error';
124 like $simple_tuple->validate(["aaa","bbb"]),
125 qr/'Int' with value aaa/,
126 'correct deeper error';
128 like $deep_tuple->validate([1,{a=>2},{name=>'Vincent1',age=>'Hello1'}]),
129 qr/'Int' with value Hello1/,
130 'correct deeper error';
134 ok !$deep_tuple->validate([1,{a=>2},{name=>'John',age=>40}]), 'Validates ok';