Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 217 lines (212 sloc) 5.202 kb
ee2912c validation de formulaires
Éric Cholet authored
1 #!perl -w
2
3 use strict;
190718c pas de constraints si champ optionnel et vide
Éric Cholet authored
4 use Test::More tests => 99;
ee2912c validation de formulaires
Éric Cholet authored
5
6 my @tests = (
7 { profile => {
8 required => [ qw(r1 r2) ],
9 optional => [ qw(r3) ],
10 },
11 inputs => [
12 { input => { r1 => 1, r2 => 2 },
13 fields => { r1 => 1, r2 => 2, r3 => undef },
14 valid => 1,
15 },
16 { input => { r1 => 1, r2 => 2, r3 => 3 },
17 fields => { r1 => 1, r2 => 2, r3 => 3 },
18 valid => 1,
19 },
20 { input => { r1 => 1, r2 => 2, r3 => 3, r4 => 4 },
21 fields => { r1 => 1, r2 => 2, r3 => 3 },
22 valid => 1,
23 },
3f63a86 la doc de fields() dit "...have their value trimmed"
Éric Cholet authored
24 { input => { r1 => 'foo ', r2 => ' bar', r3 => ' baz '},
25 fields => { r1 => 'foo', r2 => 'bar', r3 => 'baz' },
26 valid => 1,
27 },
ee2912c validation de formulaires
Éric Cholet authored
28 { input => { r1 => 1, r3 => 3 },
29 fields => { r1 => 1, r3 => 3, r2 => undef },
30 valid => 0,
31 invalid => { r2 => 'required' },
32 },
33 { input => { r1 => 1, r2 => '', r3 => 3 },
34 fields => { r1 => 1, r3 => 3, r2 => '' },
35 valid => 0,
36 invalid => { r2 => 'required' },
37 },
38 ],
39 },
40 { profile => {
1e9bb82 dependencies
Éric Cholet authored
41 dependencies => { r1 => [ qw(r2 r3) ] },
42 },
43 inputs => [
44 { input => { },
45 fields => { r1 => undef, r2 => undef, r3 => undef },
46 valid => 1,
47 },
48 { input => { r1 => 1, r2 => 2, r3 => 3 },
49 fields => { r1 => 1, r2 => 2, r3 => 3 },
50 valid => 1,
51 },
52 { input => { r1 => 1 },
53 fields => { r1 => 1, r2 => undef, r3 => undef },
54 valid => 0,
55 invalid => { r2 => 'required', r3 => 'required' },
56 },
57 { input => { r1 => 1, r2 => 2 },
58 fields => { r1 => 1, r2 => 2, r3 => undef },
59 invalid => { r3 => 'required' },
60 valid => 0,
61 },
62 ],
63 },
64 { profile => {
65 dependencies => { r1 => [ qw(r2) ],
66 r3 => [ qw(r4) ],
67 },
68 },
69 inputs => [
70 { input => { r1 => 1 },
71 fields => { r1 => 1, r2 => undef, r3 => undef, r4 => undef },
72 valid => 0,
73 invalid => { r2 => 'required' },
74 },
75 { input => { r1 => 1, r2 => 2 },
76 fields => { r1 => 1, r2 => 2, r3 => undef, r4 => undef },
77 valid => 1,
78 },
79 { input => { r1 => 1, r2 => 2, r3 => 3 },
80 fields => { r1 => 1, r2 => 2, r3 => 3, r4 => undef },
81 valid => 0,
82 invalid => { r4 => 'required' },
83 },
84 { input => { r1 => 1, r3 => 3 },
85 fields => { r1 => 1, r2 => undef, r3 => 3, r4 => undef },
86 valid => 0,
87 invalid => { r2 => 'required', r4 => 'required' },
88 },
89 { input => { r1 => 1, r2 => 2, r3 => 3, r4 => 4 },
90 fields => { r1 => 1, r2 => 2, r3 => 3, r4 => 4 },
91 valid => 1,
92 },
93 ],
94 },
95 { profile => {
ee2912c validation de formulaires
Éric Cholet authored
96 required => 'r1',
97 constraints => { r1 => 'email' },
98 },
99 inputs => [
100 { input => { r1 => 'foo@example.com' },
101 fields => { r1 => 'foo@example.com' },
102 valid => 1,
103 },
104 { input => { r1 => 'foo@example' },
105 fields => { r1 => 'foo@example' },
106 valid => 0,
107 invalid => { r1 => 'email' },
108 },
109 { input => { },
110 fields => { r1 => undef },
111 valid => 0,
112 invalid => { r1 => 'required' },
113 },
114 ],
115 },
116 { profile => {
117 required => 'r1',
118 constraints => { r1 => 'numeric' },
119 },
120 inputs => [
121 { input => { r1 => 42 },
122 fields => { r1 => 42 },
123 valid => 1,
124 },
125 { input => { },
126 fields => { r1 => undef },
127 valid => 0,
128 invalid => { r1 => 'required' },
129 },
130 { input => { r1 => 'abc' },
131 fields => { r1 => 'abc' },
132 valid => 0,
133 invalid => { r1 => 'numeric' },
134 },
135 ],
136 },
9aa8b2c bug: optional+constraint
Éric Cholet authored
137 { profile => {
138 optional => 'r1',
139 constraints => { r1 => 'numeric' },
140 },
141 inputs => [
142 { input => { r1 => 42 },
143 fields => { r1 => 42 },
144 valid => 1,
145 },
146 { input => { },
147 fields => { r1 => undef },
148 valid => 1,
149 },
190718c pas de constraints si champ optionnel et vide
Éric Cholet authored
150 { input => { r1 => '' },
151 fields => { r1 => '' },
152 valid => 1,
153 },
9aa8b2c bug: optional+constraint
Éric Cholet authored
154 { input => { r1 => 'abc' },
155 fields => { r1 => 'abc' },
156 valid => 0,
157 invalid => { r1 => 'numeric' },
158 },
159 ],
160 },
5bc0b00 formulaires: contrôles custom
Éric Cholet authored
161 { profile => {
162 optional => 'r1',
163 constraints => { r1 => sub { $_[0] =~ /^[A-Z]*$/ } },
164 },
165 inputs => [
166 { input => { r1 => 'ABC' },
167 fields => { r1 => 'ABC' },
168 valid => 1,
169 },
170 { input => { },
171 fields => { r1 => undef },
172 valid => 1,
173 },
174 { input => { r1 => 'abc' },
175 fields => { r1 => 'abc' },
176 valid => 0,
177 invalid => { r1 => 'custom' },
178 },
179 ],
180 },
80c6d2e @book Ajout de la contrainte url. C'est basique, mais ca peut suffire.
book authored
181 { profile => {
182 required => 'r1',
183 constraints => { r1 => 'url' },
184 },
185 inputs => [
186 { input => { r1 => 42 },
187 fields => { r1 => 42 },
188 valid => 0,
189 invalid => { r1 => 'url' },
190 },
191 { input => { r1 => 'http://abc' },
192 fields => { r1 => 'http://abc' },
193 valid => 1,
194 },
195 ],
196 },
ee2912c validation de formulaires
Éric Cholet authored
197 );
198 require_ok('Act::Form');
199
200 for my $t (@tests) {
201 my $f = Act::Form->new(%{$t->{profile}});
202 ok($f);
203 for my $i (@{$t->{inputs}}) {
204 my $res = $f->validate($i->{input});
205 ok($res == $i->{valid});
206 is_deeply($f->fields, $i->{fields});
207 if ($i->{invalid}) {
208 is_deeply($f->invalid, $i->{invalid});
209 }
b713dc0 test cohérence
Éric Cholet authored
210 else {
211 ok(!defined($i->{invalid}));
212 }
ee2912c validation de formulaires
Éric Cholet authored
213 }
214 }
215
216 __END__
Something went wrong with that request. Please try again.