Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 158 lines (153 sloc) 3.543 kB
ee2912c validation de formulaires
Éric Cholet authored
1 #!perl -w
2
3 use strict;
80c6d2e @book Ajout de la contrainte url. C'est basique, mais ca peut suffire.
book authored
4 use Test::More tests => 67;
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 => {
41 required => 'r1',
42 constraints => { r1 => 'email' },
43 },
44 inputs => [
45 { input => { r1 => 'foo@example.com' },
46 fields => { r1 => 'foo@example.com' },
47 valid => 1,
48 },
49 { input => { r1 => 'foo@example' },
50 fields => { r1 => 'foo@example' },
51 valid => 0,
52 invalid => { r1 => 'email' },
53 },
54 { input => { },
55 fields => { r1 => undef },
56 valid => 0,
57 invalid => { r1 => 'required' },
58 },
59 ],
60 },
61 { profile => {
62 required => 'r1',
63 constraints => { r1 => 'numeric' },
64 },
65 inputs => [
66 { input => { r1 => 42 },
67 fields => { r1 => 42 },
68 valid => 1,
69 },
70 { input => { },
71 fields => { r1 => undef },
72 valid => 0,
73 invalid => { r1 => 'required' },
74 },
75 { input => { r1 => 'abc' },
76 fields => { r1 => 'abc' },
77 valid => 0,
78 invalid => { r1 => 'numeric' },
79 },
80 ],
81 },
9aa8b2c bug: optional+constraint
Éric Cholet authored
82 { profile => {
83 optional => 'r1',
84 constraints => { r1 => 'numeric' },
85 },
86 inputs => [
87 { input => { r1 => 42 },
88 fields => { r1 => 42 },
89 valid => 1,
90 },
91 { input => { },
92 fields => { r1 => undef },
93 valid => 1,
94 },
95 { input => { r1 => 'abc' },
96 fields => { r1 => 'abc' },
97 valid => 0,
98 invalid => { r1 => 'numeric' },
99 },
100 ],
101 },
5bc0b00 formulaires: contrôles custom
Éric Cholet authored
102 { profile => {
103 optional => 'r1',
104 constraints => { r1 => sub { $_[0] =~ /^[A-Z]*$/ } },
105 },
106 inputs => [
107 { input => { r1 => 'ABC' },
108 fields => { r1 => 'ABC' },
109 valid => 1,
110 },
111 { input => { },
112 fields => { r1 => undef },
113 valid => 1,
114 },
115 { input => { r1 => 'abc' },
116 fields => { r1 => 'abc' },
117 valid => 0,
118 invalid => { r1 => 'custom' },
119 },
120 ],
121 },
80c6d2e @book Ajout de la contrainte url. C'est basique, mais ca peut suffire.
book authored
122 { profile => {
123 required => 'r1',
124 constraints => { r1 => 'url' },
125 },
126 inputs => [
127 { input => { r1 => 42 },
128 fields => { r1 => 42 },
129 valid => 0,
130 invalid => { r1 => 'url' },
131 },
132 { input => { r1 => 'http://abc' },
133 fields => { r1 => 'http://abc' },
134 valid => 1,
135 },
136 ],
137 },
ee2912c validation de formulaires
Éric Cholet authored
138 );
139 require_ok('Act::Form');
140
141 for my $t (@tests) {
142 my $f = Act::Form->new(%{$t->{profile}});
143 ok($f);
144 for my $i (@{$t->{inputs}}) {
145 my $res = $f->validate($i->{input});
146 ok($res == $i->{valid});
147 is_deeply($f->fields, $i->{fields});
148 if ($i->{invalid}) {
149 is_deeply($f->invalid, $i->{invalid});
150 }
b713dc0 test cohérence
Éric Cholet authored
151 else {
152 ok(!defined($i->{invalid}));
153 }
ee2912c validation de formulaires
Éric Cholet authored
154 }
155 }
156
157 __END__
Something went wrong with that request. Please try again.