4 use Test::More tests=>25;
7 use Moose::Util::TypeConstraints;
8 use MooseX::Types::Structured qw(Dict Tuple Optional);
9 use MooseX::Types::Moose qw(Int Str ArrayRef HashRef);
11 # Create some TCs from which errors will be generated
13 my $simple_tuple = subtype 'simple_tuple', as Tuple[Int,Str];
14 my $simple_dict = subtype 'simple_dict', as Dict[name=>Str,age=>Int];
16 # Make sure the constraints we made validate as expected
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}";
23 ## Let's check all the expected validation errors for tuple
25 like $simple_tuple->validate({a=>1,b=>2}),
26 qr/Validation failed for 'simple_tuple' failed with value { a: 1, b: 2 }/,
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';
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';
37 like $simple_tuple->validate([1]),
38 qr/Validation failed for 'Str' failed with value NULL/,
41 like $simple_tuple->validate([1,'hello','too many']),
42 qr/More values than Type Constraints!/,
45 ## And the same thing for dicts [name=>Str,age=>Int]
47 like $simple_dict->validate([1,2]),
48 qr/ failed with value \[ 1, 2 \]/,
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';
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';
59 like $simple_dict->validate({name=>'John'}),
60 qr/failed for 'Int' failed with value NULL/,
63 like $simple_dict->validate({name=>'Vincent', age=>15,extra=>'morethanIneed'}),
64 qr/More values than Type Constraints!/,
67 ## TODO some with Optional (or Maybe) and slurpy
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]];
72 like $optional_tuple->validate({a=>1,b=>2}),
73 qr/Validation failed for 'optional_tuple' failed with value { a: 1, b: 2 }/,
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';
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';
84 like $optional_tuple->validate([1,'hello','too many']),
85 qr/More values than Type Constraints!/,
88 like $optional_dict->validate([1,2]),
89 qr/ failed with value \[ 1, 2 \]/,
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';
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';
100 like $optional_dict->validate({name=>'Vincent', age=>15,extra=>'morethanIneed'}),
101 qr/More values than Type Constraints!/,
104 ## Deeper constraints
106 my $deep_tuple = subtype 'deep_tuple',
116 ok $deep_tuple->check([1,{a=>2},{name=>'Vincent',age=>15}]),
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';
125 ok !$deep_tuple->validate([1,{a=>2},{name=>'John',age=>40}]), 'Validates ok';