Commit | Line | Data |
9448ea2c |
1 | use strict; |
2 | use warnings; |
3 | use Test::More; |
797510e3 |
4 | |
5 | use Moose::Util::TypeConstraints; |
7559b71f |
6 | use MooseX::Types::Structured qw(Dict Tuple Optional); |
797510e3 |
7 | use MooseX::Types::Moose qw(Int Str ArrayRef HashRef); |
8 | |
9 | # Create some TCs from which errors will be generated |
7559b71f |
10 | |
797510e3 |
11 | my $simple_tuple = subtype 'simple_tuple', as Tuple[Int,Str]; |
12 | my $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 |
16 | ok $simple_tuple->check([1,'hello']), "simple_tuple validates: 1,'hello'"; |
17 | ok !$simple_tuple->check(['hello',1]), "simple_tuple fails: 'hello',1"; |
7559b71f |
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}"; |
20 | |
21 | ## Let's check all the expected validation errors for tuple |
22 | |
23 | like $simple_tuple->validate({a=>1,b=>2}), |
ae998529 |
24 | qr/Validation failed for 'simple_tuple' with value { a: 1, b: 2 }/, |
7559b71f |
25 | 'Wrong basic type'; |
26 | |
27 | like $simple_tuple->validate(['a','b']), |
ae998529 |
28 | qr/failed for 'simple_tuple' with value \[ "a", "b" \]/, |
7559b71f |
29 | 'Correctly failed due to "a" not an Int'; |
9eb6e8c6 |
30 | |
7559b71f |
31 | like $simple_tuple->validate([1,$simple_tuple]), |
ae998529 |
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 | |
35 | like $simple_tuple->validate([1]), |
ae998529 |
36 | qr/Validation failed for 'Str' with value NULL/, |
7559b71f |
37 | 'Not enought values'; |
38 | |
39 | like $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 | |
45 | like $simple_dict->validate([1,2]), |
ae998529 |
46 | qr/ with value \[ 1, 2 \]/, |
7559b71f |
47 | 'Wrong basic type'; |
9eb6e8c6 |
48 | |
7559b71f |
49 | like $simple_dict->validate({name=>'John',age=>'a'}), |
ae998529 |
50 | qr/failed for 'Int' with value a/, |
7559b71f |
51 | 'Correctly failed due to age not an Int'; |
9eb6e8c6 |
52 | |
7559b71f |
53 | like $simple_dict->validate({name=>$simple_dict,age=>1}), |
ae998529 |
54 | qr/with value { age: 1, name: MooseX:/, |
7559b71f |
55 | 'Correctly failed due to object not a Str'; |
56 | |
57 | like $simple_dict->validate({name=>'John'}), |
ae998529 |
58 | qr/failed for 'Int' with value NULL/, |
7559b71f |
59 | 'Not enought values'; |
60 | |
61 | like $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}), |
ae998529 |
71 | qr/Validation failed for 'optional_tuple' with value { a: 1, b: 2 }/, |
7559b71f |
72 | 'Wrong basic type'; |
73 | |
74 | like $optional_tuple->validate(['a','b']), |
ae998529 |
75 | qr/failed for 'Int' with value a/, |
7559b71f |
76 | 'Correctly failed due to "a" not an Int'; |
9eb6e8c6 |
77 | |
7559b71f |
78 | like $optional_tuple->validate([1,$simple_tuple]), |
39fbe06b |
79 | qr/failed for 'Optional\[Str\]' with value MooseX/, |
7559b71f |
80 | 'Correctly failed due to object not a Str'; |
81 | |
82 | like $optional_tuple->validate([1,'hello','too many']), |
83 | qr/More values than Type Constraints!/, |
84 | 'Too Many values'; |
85 | |
86 | like $optional_dict->validate([1,2]), |
ae998529 |
87 | qr/ with value \[ 1, 2 \]/, |
7559b71f |
88 | 'Wrong basic type'; |
9eb6e8c6 |
89 | |
7559b71f |
90 | like $optional_dict->validate({name=>'John',age=>'a'}), |
39fbe06b |
91 | qr/Validation failed for 'Optional\[Int\]' with value a/, |
7559b71f |
92 | 'Correctly failed due to age not an Int'; |
9eb6e8c6 |
93 | |
7559b71f |
94 | like $optional_dict->validate({name=>$simple_dict,age=>1}), |
ae998529 |
95 | qr/with value { age: 1, name: MooseX:/, |
7559b71f |
96 | 'Correctly failed due to object not a Str'; |
97 | |
98 | like $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 |
104 | my $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 |
114 | ok $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 | |
124 | like $simple_tuple->validate(["aaa","bbb"]), |
125 | qr/'Int' with value aaa/, |
126 | 'correct deeper error'; |
127 | |
128 | like $deep_tuple->validate([1,{a=>2},{name=>'Vincent1',age=>'Hello1'}]), |
129 | qr/'Int' with value Hello1/, |
130 | 'correct deeper error'; |
131 | |
5ac93dbb |
132 | ## Success Tests... |
133 | |
134 | ok !$deep_tuple->validate([1,{a=>2},{name=>'John',age=>40}]), 'Validates ok'; |
135 | |
9448ea2c |
136 | done_testing(); |