source: Dev/branches/rest-dojo-ui/client/dojox/validate/tests/validate.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

File size: 13.0 KB
Line 
1define(["doh", "../_base", "../check", "../us", "../ca", "../isbn", "../web"], function(doh, validate, check, us, ca, isbn){
2
3doh.register("dojox.validate.tests.validate",
4        [{
5                name: "isText",
6
7                setUp: function(){
8                        var localeList = ["en-us"];
9                        for(var i = 0 ; i < localeList.length; i ++){
10                                dojo.requireLocalization("dojo.cldr", "currency", localeList[i]);
11                                dojo.requireLocalization("dojo.cldr", "number", localeList[i]);
12                        }
13                },
14
15                runTest: function(tests){
16                        tests.t(isbn('0596007590')); //test string input
17                        tests.t(isbn('0-596-00759-0')); //test string input with dashes
18                        tests.f(isbn(596007590)); //test numerical input as well
19                        tests.t(isbn("960-425-059-0"));
20                        tests.t(isbn(9604250590)); //test numerical input as well
21                        tests.t(isbn('0-9752298-0-X')); // test string with X
22                        tests.t(isbn('0-9752298-0-x'));
23                        tests.t(isbn('097522980x'));
24                        tests.t(isbn('097522980X'));
25                        tests.f(isbn(596007598)); //testing failures
26                        tests.f(isbn('059-600759-X')); //testing failures
27                        tests.f(isbn('059600')); // too short
28
29                        tests.t(isbn('9780596007591'));
30                        tests.t(isbn('978-0-596 00759-1'));
31                        tests.t(isbn(9780596007591));
32                        tests.f(isbn('978059600759X'));
33                        tests.f(isbn('978-3250-596 00759-1 '));
34                        tests.f(isbn('3250-596 00759 '));
35
36                        tests.t(validate.isText('            x'));
37                        tests.t(validate.isText('x             '));
38                        tests.t(validate.isText('        x     '));
39                        tests.f(validate.isText('   '));
40                        tests.f(validate.isText(''));
41               
42                        // test lengths
43                        tests.t(validate.isText('123456', {length: 6} ));
44                        tests.f(validate.isText('1234567', {length: 6} ));
45                        tests.t(validate.isText('1234567', {minlength: 6} ));
46                        tests.t(validate.isText('123456', {minlength: 6} ));
47                        tests.f(validate.isText('12345', {minlength: 6} ));
48                        tests.f(validate.isText('1234567', {maxlength: 6} ));
49                        tests.t(validate.isText('123456', {maxlength: 6} ));
50                }
51        },
52        {
53                name: "isIpAddress",
54                runTest: function(tests){
55                        tests.t(validate.isIpAddress('24.17.155.40'));
56                        tests.f(validate.isIpAddress('024.17.155.040'));
57                        tests.t(validate.isIpAddress('255.255.255.255'));
58                        tests.f(validate.isIpAddress('256.255.255.255'));
59                        tests.f(validate.isIpAddress('255.256.255.255'));
60                        tests.f(validate.isIpAddress('255.255.256.255'));
61                        tests.f(validate.isIpAddress('255.255.255.256'));
62
63                        // test dotted hex
64                        tests.t(validate.isIpAddress('0x18.0x11.0x9b.0x28'));
65                        tests.f(validate.isIpAddress('0x18.0x11.0x9b.0x28', {allowDottedHex: false}) );
66                        tests.t(validate.isIpAddress('0x18.0x000000011.0x9b.0x28'));
67                        tests.t(validate.isIpAddress('0xff.0xff.0xff.0xff'));
68                        tests.f(validate.isIpAddress('0x100.0xff.0xff.0xff'));
69
70                        // test dotted octal
71                        tests.t(validate.isIpAddress('0030.0021.0233.0050'));
72                        tests.f(validate.isIpAddress('0030.0021.0233.0050', {allowDottedOctal: false}) );
73                        tests.t(validate.isIpAddress('0030.0000021.0233.00000050'));
74                        tests.t(validate.isIpAddress('0377.0377.0377.0377'));
75                        tests.f(validate.isIpAddress('0400.0377.0377.0377'));
76                        tests.f(validate.isIpAddress('0377.0378.0377.0377'));
77                        tests.f(validate.isIpAddress('0377.0377.0380.0377'));
78                        tests.f(validate.isIpAddress('0377.0377.0377.377'));
79               
80                        // test decimal
81                        tests.t(validate.isIpAddress('3482223595'));
82                        tests.t(validate.isIpAddress('0'));
83                        tests.t(validate.isIpAddress('4294967295'));
84                        tests.f(validate.isIpAddress('4294967296'));
85                        tests.f(validate.isIpAddress('3482223595', {allowDecimal: false}));
86               
87                        // test hex
88                        tests.t(validate.isIpAddress('0xCF8E83EB'));
89                        tests.t(validate.isIpAddress('0x0'));
90                        tests.t(validate.isIpAddress('0x00ffffffff'));
91                        tests.f(validate.isIpAddress('0x100000000'));
92                        tests.f(validate.isIpAddress('0xCF8E83EB', {allowHex: false}));
93                       
94                        // IPv6
95                        tests.t(validate.isIpAddress('fedc:BA98:7654:3210:FEDC:BA98:7654:3210'));
96                        tests.t(validate.isIpAddress('1080:0:0:0:8:800:200C:417A'));
97                        tests.f(validate.isIpAddress('1080:0:0:0:8:800:200C:417A', {allowIPv6: false}));
98               
99                        // Hybrid of IPv6 and IPv4
100                        tests.t(validate.isIpAddress('0:0:0:0:0:0:13.1.68.3'));
101                        tests.t(validate.isIpAddress('0:0:0:0:0:FFFF:129.144.52.38'));
102                        tests.f(validate.isIpAddress('0:0:0:0:0:FFFF:129.144.52.38', {allowHybrid: false}));
103                       
104                }
105        },
106        {
107                name: "isUrlTest",
108                runTest: function(tests){
109                       
110                        tests.t(validate.isUrl('www.yahoo.com'));
111                        tests.t(validate.isUrl('http://www.yahoo.com'));
112                        tests.t(validate.isUrl('https://www.yahoo.com'));
113                        tests.f(validate.isUrl('http://.yahoo.com'));
114                        tests.f(validate.isUrl('http://www.-yahoo.com'));
115                        tests.f(validate.isUrl('http://www.yahoo-.com'));
116                        tests.t(validate.isUrl('http://y-a---h-o-o.com'));
117                        tests.t(validate.isUrl('http://www.y.com'));
118                        tests.t(validate.isUrl('http://www.yahoo.museum'));
119                        tests.t(validate.isUrl('http://www.yahoo.co.uk'));
120                        tests.f(validate.isUrl('http://www.micro$oft.com'));
121               
122                        tests.t(validate.isUrl('http://www.y.museum:8080'));
123                        tests.t(validate.isUrl('http://12.24.36.128:8080'));
124                        tests.f(validate.isUrl('http://12.24.36.128:8080', {allowIP: false} ));
125                        tests.t(validate.isUrl('www.y.museum:8080'));
126                        tests.f(validate.isUrl('www.y.museum:8080', {scheme: true} ));
127                        tests.t(validate.isUrl('localhost:8080', {allowLocal: true} ));
128                        tests.f(validate.isUrl('localhost:8080', {} ));
129                        tests.t(validate.isUrl('http://www.yahoo.com/index.html?a=12&b=hello%20world#anchor'));
130                        tests.t(validate.isUrl('http://www.yahoo.xyz'));
131                        tests.t(validate.isUrl('http://www.yahoo.com/index.html#anchor'));
132                        tests.t(validate.isUrl('http://cocoon.apache.org/2.1/'));
133                }
134        },
135        {
136                name: "isEmailAddress",
137                runTest: function(tests) {
138                        tests.t(validate.isEmailAddress('x@yahoo.com'));
139                        tests.f(validate.isEmailAddress('x@yahoo'));
140                        tests.t(validate.isEmailAddress('x.y.z.w@yahoo.com'));
141                        tests.f(validate.isEmailAddress('x..y.z.w@yahoo.com'));
142                        tests.f(validate.isEmailAddress('x.@yahoo.com'));
143                        tests.f(validate.isEmailAddress('.x@yahoo.com'));
144                        tests.t(validate.isEmailAddress('azAZ09!#$%.&\'*+-/=?_`{|}y@yahoo.com'));
145                        tests.t(validate.isEmailAddress('x=y@yahoo.com'));
146                        tests.f(validate.isEmailAddress('x(y@yahoo.com'));
147                        tests.f(validate.isEmailAddress('x)y@yahoo.com'));
148                        tests.f(validate.isEmailAddress('x<y@yahoo.com'));
149                        tests.f(validate.isEmailAddress('x>y@yahoo.com'));
150                        tests.f(validate.isEmailAddress('x[y@yahoo.com'));
151                        tests.f(validate.isEmailAddress('x]y@yahoo.com'));
152                        tests.f(validate.isEmailAddress('x:y@yahoo.com'));
153                        tests.f(validate.isEmailAddress('x;y@yahoo.com'));
154                        tests.f(validate.isEmailAddress('x@y@yahoo.com'));
155                        tests.f(validate.isEmailAddress('x\\y@yahoo.com'));
156                        tests.f(validate.isEmailAddress('x,y@yahoo.com'));
157                        tests.f(validate.isEmailAddress('x\"y@yahoo.com'));
158                        tests.t(validate.isEmailAddress('x@z.com'));
159                        tests.t(validate.isEmailAddress('x@yahoo.x'));
160                        tests.t(validate.isEmailAddress('x@yahoo.museum'));
161                        tests.t(validate.isEmailAddress("o'mally@yahoo.com"));
162                        tests.t(validate.isEmailAddress("o''mally@yahoo.com"));
163                        tests.t(validate.isEmailAddress("'mally@yahoo.com"));
164                        tests.t(validate.isEmailAddress("fred&barney@stonehenge.com"));
165                        tests.t(validate.isEmailAddress("fred&&barney@stonehenge.com"));
166               
167                        // local addresses
168                        tests.t(validate.isEmailAddress("fred&barney@localhost", {allowLocal: true} ));
169                        tests.f(validate.isEmailAddress("fred&barney@localhost"));
170               
171                        // addresses with cruft
172                        tests.t(validate.isEmailAddress("mailto:fred&barney@stonehenge.com", {allowCruft: true} ));
173                        tests.t(validate.isEmailAddress("<fred&barney@stonehenge.com>", {allowCruft: true} ));
174                        tests.f(validate.isEmailAddress("mailto:fred&barney@stonehenge.com"));
175                        tests.f(validate.isEmailAddress("<fred&barney@stonehenge.com>"));
176       
177                        // local addresses with cruft
178                        tests.t(validate.isEmailAddress("<mailto:fred&barney@localhost>", {allowLocal: true, allowCruft: true} ));
179                        tests.f(validate.isEmailAddress("<mailto:fred&barney@localhost>", {allowCruft: true} ));
180                        tests.f(validate.isEmailAddress("<mailto:fred&barney@localhost>", {allowLocal: true} ));
181                }
182        },
183        {
184                name: "isEmailsAddressList",
185                runTest: function(tests) {
186                        tests.t(validate.isEmailAddressList(
187                                "x@yahoo.com \n x.y.z.w@yahoo.com ; o'mally@yahoo.com , fred&barney@stonehenge.com \n" )
188                        );
189                        tests.t(validate.isEmailAddressList(
190                                "x@yahoo.com \n x.y.z.w@localhost \n o'mally@yahoo.com \n fred&barney@localhost",
191                                {allowLocal: true} )
192                        );
193                        tests.f(validate.isEmailAddressList(
194                                "x@yahoo.com; x.y.z.w@localhost; o'mally@yahoo.com; fred&barney@localhost", {listSeparator: ";"} )
195                        );
196                        tests.t(validate.isEmailAddressList(
197                                        "mailto:x@yahoo.com; <x.y.z.w@yahoo.com>; <mailto:o'mally@yahoo.com>; fred&barney@stonehenge.com",
198                                        {allowCruft: true, listSeparator: ";"} )
199                        );
200                        tests.f(validate.isEmailAddressList(
201                                        "mailto:x@yahoo.com; <x.y.z.w@yahoo.com>; <mailto:o'mally@yahoo.com>; fred&barney@stonehenge.com",
202                                        {listSeparator: ";"} )
203                        );
204                        tests.t(validate.isEmailAddressList(
205                                        "mailto:x@yahoo.com; <x.y.z.w@localhost>; <mailto:o'mally@localhost>; fred&barney@localhost",
206                                        {allowLocal: true, allowCruft: true, listSeparator: ";"} )
207                        );
208                }
209        },
210        {
211                name: "getEmailAddressList",
212                runTest: function(tests) {
213                        var list = "x@yahoo.com \n x.y.z.w@yahoo.com ; o'mally@yahoo.com , fred&barney@stonehenge.com";
214                        tests.is(4, validate.getEmailAddressList(list).length);
215
216                        var localhostList = "x@yahoo.com; x.y.z.w@localhost; o'mally@yahoo.com; fred&barney@localhost";
217                        tests.is(0, validate.getEmailAddressList(localhostList).length);
218                        tests.is(4, validate.getEmailAddressList(localhostList, {allowLocal: true} ).length);
219                }
220        },
221        {
222                name: "isInRangeInt",
223                runTest: function(tests) {
224                        // test integers
225                        tests.f(validate.isInRange( '0', {min: 1, max: 100} ));
226                        tests.t(validate.isInRange( '1', {min: 1, max: 100} ));
227                        tests.f(validate.isInRange( '-50', {min: 1, max: 100} ));
228//                      tests.t(validate.isInRange( '+50', {min: 1, max: 100} )); //TODO: dojo.number.parse does not support plus sign
229                        tests.t(validate.isInRange( '100', {min: 1, max: 100} ));
230                        tests.f(validate.isInRange( '101', {min: 1, max: 100} ));
231                }
232        },
233        {
234                name:"isInRangeReal",
235                runTest: function(tests){
236       
237                        tests.f(validate.isInRange( '0.9', {min: 1.0, max: 10.0, locale: 'en-us'} ));
238                        tests.t(validate.isInRange( '1.0', {min: 1.0, max: 10.0, locale: 'en-us'} ));
239                        tests.f(validate.isInRange( '-5.0', {min: 1.0, max: 10.0, locale: 'en-us'} ));
240//                      tests.t(validate.isInRange( '+5.50', {min: 1.0, max: 10.0, locale: 'en-us'} )); //TODO: dojo.number.parse does not support plus sign
241                        tests.t(validate.isInRange( '10.0', {min: 1.0, max: 10.0, locale: 'en-us'} ));
242                        tests.f(validate.isInRange( '10.1', {min: 1.0, max: 10.0, locale: 'en-us'} ));
243// TODO: dojo.number.parse does not support scientific notation at this time
244//                      tests.f(validate.isInRange( '5.566e28', {min: 5.567e28, max: 6.000e28, locale: 'en-us'} ));
245//                      tests.t(validate.isInRange( '5.7e28', {min: 5.567e28, max: 6.000e28, locale: 'en-us'} ));
246//                      tests.f(validate.isInRange( '6.00000001e28', {min: 5.567e28, max: 6.000e28, locale: 'en-us'} ));
247//                      tests.f(validate.isInRange( '10.000.000,12345e-5', {decimal: ",", max: 10000000.1e-5, locale: 'de-de'} ));
248//                      tests.f(validate.isInRange( '10.000.000,12345e-5', {decimal: ",", min: 10000000.2e-5, locale: 'de-de'} ));
249                        tests.t(validate.isInRange('1,500,000', { min: 0, locale: 'en-us'}));
250                        tests.f(validate.isInRange('1,500,000', { min: 1000, max: 20000, locale: 'en-us'}));
251                }
252        },
253        {
254                name: "isUsPhoneNumber",
255                runTest: function(tests) {
256                        tests.t(us.isPhoneNumber('(111) 111-1111'));
257                        tests.t(us.isPhoneNumber('(111) 111 1111'));
258                        tests.t(us.isPhoneNumber('111 111 1111'));
259                        tests.t(us.isPhoneNumber('111.111.1111'));
260                        tests.t(us.isPhoneNumber('111-111-1111'));
261                        tests.t(us.isPhoneNumber('111/111-1111'));
262                        tests.f(us.isPhoneNumber('111 111-1111'));
263                        tests.f(us.isPhoneNumber('111-1111'));
264                        tests.f(us.isPhoneNumber('(111)-111-1111'));
265               
266                        // test extensions
267                        tests.t(us.isPhoneNumber('111-111-1111 x1'));
268                        tests.t(us.isPhoneNumber('111-111-1111 x12'));
269                        tests.t(us.isPhoneNumber('111-111-1111 x1234'));
270                }
271        },
272        {
273                name: "isUsSocialSecurityNumber",
274                runTest: function(tests) {
275                        tests.t(us.isSocialSecurityNumber('123-45-6789'));
276                        tests.t(us.isSocialSecurityNumber('123 45 6789'));
277                        tests.t(us.isSocialSecurityNumber('123456789'));
278                        tests.f(us.isSocialSecurityNumber('123-45 6789'));
279                        tests.f(us.isSocialSecurityNumber('12345 6789'));
280                        tests.f(us.isSocialSecurityNumber('123-456789'));
281                }
282        },
283        {
284                name:"isUsZipCode",
285                runTest: function(tests) {
286                        tests.t(us.isZipCode('12345-6789'));
287                        tests.t(us.isZipCode('12345 6789'));
288                        tests.t(us.isZipCode('123456789'));
289                        tests.t(us.isZipCode('12345'));
290                }
291        },
292        {
293                name:"isCaZipCode",
294                runTest: function(tests) {
295                        tests.t(ca.isPostalCode('A1Z 3F3'));
296                        tests.f(ca.isPostalCode('1AZ 3F3'));
297                        tests.t(ca.isPostalCode('a1z 3f3'));
298                        tests.f(ca.isPostalCode('xxxxxx'));
299                        tests.f(ca.isPostalCode('A1Z3F3'));
300                       
301                }
302        },
303        {
304                name:"isUsState",
305                runTest: function(tests) {
306                        tests.t(us.isState('CA'));
307                        tests.t(us.isState('ne'));
308                        tests.t(us.isState('PR'));
309                        tests.f(us.isState('PR', {allowTerritories: false} ));
310                        tests.t(us.isState('AA'));
311                        tests.f(us.isState('AA', {allowMilitary: false} ));
312                }
313        }
314]);
315
316});
Note: See TracBrowser for help on using the repository browser.