]>
Commit | Line | Data |
---|---|---|
0c3d2fc5 TC |
1 | function validation_check(test, validator, checker) { |
2 | if (test.exception) { | |
3 | var msg = ok_exception(function(validator, value) { | |
4 | validator.test(value); | |
5 | }.bind(this, validator, test.value), "parse "+test.name, test.message); | |
6 | if (msg) { | |
7 | like(msg, test.message, "check message " + test.name); | |
8 | } | |
9 | else { | |
10 | ok(false, test.name + " no message"); | |
11 | } | |
12 | } | |
13 | else { | |
14 | var a_parsed = ok_noexception(function(validator, value){ | |
15 | return validator.test(value, {}); | |
16 | }.bind(this, validator, test.value), "parse "+test.name); | |
17 | checker(a_parsed, test); | |
18 | } | |
19 | } | |
20 | ||
21 | document.observe("dom:loaded", function() { | |
22 | plan(55); | |
23 | diag("Date validation"); | |
24 | var date_val = new BSEValidator.Rules["date"](); | |
25 | ok(date_val, "make date validator"); | |
26 | var date_tests = | |
27 | [ | |
28 | { | |
29 | name: "birthday", | |
30 | value: "30/12/1967", | |
31 | exception: false, | |
32 | year: 1967, | |
33 | month: 12, | |
34 | day: 30 | |
35 | }, | |
36 | { | |
37 | name: "birthday with spaces", | |
38 | value: " 30/12/1967 ", | |
39 | exception: false, | |
40 | year: 1967, | |
41 | month: 12, | |
42 | day: 30 | |
43 | }, | |
44 | { | |
45 | name: "low month", | |
46 | value: "30/0/1967", | |
47 | exception: true, | |
48 | message: /Month out of range/ | |
49 | }, | |
50 | { | |
51 | name: "low month in range", | |
52 | value: "30/1/1967", | |
53 | exception: false, | |
54 | year: 1967, | |
55 | month: 1, | |
56 | day: 30 | |
57 | }, | |
58 | { | |
59 | name: "high month", | |
60 | value: "30/13/1967", | |
61 | exception: true, | |
62 | message: /Month out of range/ | |
63 | }, | |
64 | { | |
65 | name: "high month in range", | |
66 | value: "30/12/1967", | |
67 | exception: false, | |
68 | year: 1967, | |
69 | month: 12, | |
70 | day: 30 | |
71 | }, | |
72 | { | |
73 | name: "high day normal", | |
74 | value: "32/12/1967", | |
75 | exception: true, | |
76 | message: /Day out of range/ | |
77 | }, | |
78 | { | |
79 | name: "high day normal in range", | |
80 | value: "31/12/1967", | |
81 | exception: false, | |
82 | year: 1967, | |
83 | month: 12, | |
84 | day: 31 | |
85 | }, | |
86 | { | |
87 | name: "high day non-leap", | |
88 | value: "29/2/1970", | |
89 | exception: true, | |
90 | message: /Day out of range/ | |
91 | }, | |
92 | { | |
93 | name: "high day non-leap in range", | |
94 | value: "28/2/1970", | |
95 | exception: false, | |
96 | year: 1970, | |
97 | month: 2, | |
98 | day: 28 | |
99 | }, | |
100 | { | |
101 | name: "high day leap", | |
102 | value: "30/2/1980", | |
103 | exception: true, | |
104 | message: /Day out of range/ | |
105 | }, | |
106 | { | |
107 | name: "high day leap in range", | |
108 | value: "29/2/1980", | |
109 | exception: false, | |
110 | year: 1980, | |
111 | month: 2, | |
112 | day: 29 | |
113 | }, | |
114 | { | |
115 | name: "low day", | |
116 | value: "0/1/1967", | |
117 | exception: true, | |
118 | message: /Day out of range/ | |
119 | }, | |
120 | { | |
121 | name: "low day in range", | |
122 | value: "1/1/1967", | |
123 | exception: false, | |
124 | year: 1967, | |
125 | month: 1, | |
126 | day: 1 | |
127 | }, | |
128 | { | |
129 | name: "bad format", | |
130 | value: "1/1/", | |
131 | exception: true, | |
132 | message: /must be dd\/mm\/yyyy$/ | |
133 | } | |
134 | ]; | |
135 | for (var i = 0; i < date_tests.length; ++i) { | |
136 | var test = date_tests[i]; | |
137 | validation_check(test, date_val, function(a_date, test) { | |
138 | is(a_date.getFullYear(), test.year, test.name + " year"); | |
139 | is(a_date.getMonth(), test.month-1, test.name + " month"); | |
140 | is(a_date.getDate(), test.day, test.name + " day"); | |
141 | }); | |
142 | } | |
143 | ||
144 | var TestField = Class.create({ | |
145 | initialize: function(options) { | |
146 | this.options = Object.extend({ required: false, rules: [] }, options); | |
147 | }, | |
148 | value: function() { | |
149 | return this.options.value; | |
150 | }, | |
151 | description: function() { | |
152 | return this.options.description | |
153 | }, | |
154 | name: function() { | |
155 | return this.options.name; | |
156 | }, | |
157 | rules: function() { | |
158 | return this.options.rules; | |
159 | }, | |
160 | has_value: function() { | |
161 | return /\S/.test(this.value()); | |
162 | }, | |
163 | required: function() { | |
164 | return this.options.required; | |
165 | } | |
166 | }); | |
167 | ||
168 | { // required | |
169 | var fields = new Hash({ | |
170 | nr1: new TestField({ | |
171 | name: "nr1", | |
172 | value: "", | |
173 | description: "NotRequired1" | |
174 | }), | |
175 | nr2: new TestField({ | |
176 | name: "nr2", | |
177 | value: " ", | |
178 | description: "NotRequired2" | |
179 | }), | |
180 | r1: new TestField({ | |
181 | name: "r1", | |
182 | value: "", | |
183 | required: true, | |
184 | description: "Required1" | |
185 | }), | |
186 | r2: new TestField({ | |
187 | name: "r2", | |
188 | value: " ", | |
189 | required: true, | |
190 | description: "Required2" | |
191 | }), | |
192 | r3: new TestField({ | |
193 | name: "r3", | |
194 | value: "x", | |
195 | required: true, | |
196 | description: "Required3" | |
197 | }) | |
198 | }); | |
199 | var val = new BSEValidator(); | |
200 | var errors = new Hash(); | |
201 | ok(!val.validate(fields, errors), "should fail validation"); | |
202 | is(errors.get("nr1"), null, "no error for nr1"); | |
203 | is(errors.get("nr2"), null, "no error for nr2"); | |
204 | is(errors.get("r1"), "Required1 is required", "check error for r1"); | |
205 | is(errors.get("r2"), "Required2 is required", "check error for r2"); | |
206 | is(errors.get("r3"), null, "no error for r3"); | |
207 | } | |
208 | ||
209 | { | |
210 | // confirm | |
211 | var fields = new Hash({ | |
212 | password: new TestField({ | |
213 | name: "password", | |
214 | value: "abc", | |
215 | description: "Password", | |
216 | rules: "", | |
217 | required: true | |
218 | }), | |
219 | confirm: new TestField({ | |
220 | name: "confirm", | |
221 | value: "abc", | |
222 | description: "Confirm", | |
223 | rules: "confirm:password", | |
224 | }), | |
225 | confirm2: new TestField({ | |
226 | name: "confirm2", | |
227 | value: "abcd", | |
228 | description: "Confirm2", | |
229 | rules: "confirm:password", | |
230 | }) | |
231 | }); | |
232 | var val = new BSEValidator(); | |
233 | var errors = new Hash(); | |
234 | val.validate(fields, errors); | |
235 | is(errors.get("confirm"), null, "should be no error for confirm"); | |
236 | is(errors.get("confirm2"), "Confirm2 must be the same as Password", | |
237 | "confirm2 should have an error"); | |
238 | } | |
239 | ||
240 | tests_done(); | |
241 | }); |