-
Notifications
You must be signed in to change notification settings - Fork 24
/
methodsTest.html
412 lines (377 loc) · 14.6 KB
/
methodsTest.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<link rel="Stylesheet" media="screen" href="../../../qunit/testsuite.css" />
<script type="text/javascript" src="../../jquery/dist/jquery.js"></script>
<script type="text/javascript" src="../../../qunit/testrunner.js"></script>
<script type="text/javascript" src="string.js"></script>
<script type="text/javascript" src="array.js"></script>
<script type="text/javascript" src="date.js"></script>
<script type="text/javascript">
test("String.trim()", function() {
ok( "" == "".trim(), "Expected no modification" );
ok( "" == " ".trim(), "Expected removal of single whitespace" );
ok( "" == " ".trim(), "Expected removal of multiple whitespace" );
ok( "peter" == "peter".trim(), "Expected no modification" );
ok( "peter" == " peter".trim(), "Expected trim at start" );
ok( "peter" == "peter ".trim(), "Expected trim at end" );
ok( "peter" == " peter ".trim(), "Expected trim at start and end" );
ok( "peter" == " peter ".trim(), "Expected lots of trim at start and end" );
ok( "pet er" == "pet er".trim(), "Expected no modification" );
ok( "pet er" == " pet er".trim(), "Expected trim at start" );
ok( "pet er" == "pet er ".trim(), "Expected trim at end" );
ok( "pet er" == " pet er ".trim(), "Expected trim at start and end" );
ok( "pet er" == " pet er ".trim(), "Expected lots of trim at start and end" );
ok( "p et er" == "p et er".trim(), "Expected no modification" );
ok( "p et er" == " p et er".trim(), "Expected trim at start" );
ok( "p et er" == "p et er ".trim(), "Expected trim at end" );
ok( "p et er" == " p et er ".trim(), "Expected trim at start and end" );
ok( "p et er" == " p et er ".trim(), "Expected lots of trim at start and end" );
ok( "Hello Boys and Girls!" == " Hello Boys and Girls! ".trim(), "Check example" );
} );
test("String.startsWith(prefix)", function() {
ok( "peter".startsWith("p") );
ok( "peter".startsWith("pet") );
ok( "peter".startsWith("pete") );
ok( "peter".startsWith("peter") );
ok( !"peter".startsWith("xst") );
ok( !"peter".startsWith("petr") );
ok( "/%$/".startsWith("/") );
ok( "/%$/".startsWith("/%") );
ok( !"/%$/".startsWith("/$") );
ok( "/%$/".startsWith("") );
ok( "".startsWith("") );
});
test("String.startsWith(prefix, offset)", function() {
ok( "peter".startsWith("e", 1) );
ok( "peter".startsWith("et", 1) );
ok( "peter".startsWith("ete", 1) );
ok( "peter".startsWith("eter", 1) );
ok( "peter".startsWith("t", 2) );
ok( "peter".startsWith("te", 2) );
ok( "peter".startsWith("ter", 2) );
ok( !"peter".startsWith("p", 1) );
ok( !"peter".startsWith("pe", 1) );
ok( !"peter".startsWith("e", 2) );
ok( !"peter".startsWith("et", 2) );
ok( !"peter".startsWith("pe", -1) );
ok( "peter".startsWith("r", 4) );
ok( !"peter".startsWith("r", 5) );
ok( "/%$/".startsWith("%", 1) );
ok( "/%$/".startsWith("%$", 1) );
ok( "/%$/".startsWith("$/", 2) );
ok( !"/%$/".startsWith("%$\"", 1) );
ok( "/%$/".startsWith("", 0) );
ok( "".startsWith("", 0) );
});
test("String.endsWith(suffix)", function() {
ok( "peter".endsWith("r") );
ok( "peter".endsWith("er") );
ok( "peter".endsWith("ter") );
ok( "peter".endsWith("eter") );
ok( "peter".endsWith("peter") );
ok( !"peter".endsWith("x") );
ok( !"peter".endsWith("xer") );
ok( !"peter".endsWith("pter") );
ok( "/%$/".endsWith("/") );
ok( "/%$/".endsWith("$/") );
ok( !"/%$/".endsWith("%/") );
ok( "/%$/".endsWith("") );
ok( "".endsWith("") );
});
test("String.truncate()", function() {
ok( "thisistenc thisistenc thisistenc ".truncate() == "thisistenc thisistenc thisi..." );
ok( "thisistenc thisistenc ".truncate() == "thisistenc thisistenc " );
});
test("String.truncate(length)", function() {
ok( "thisistenc ".truncate(5) == "th..." );
ok( "thisi".truncate() == "thisi" );
});
test("String.truncate(length, truncation)", function() {
ok( "thisistenc thisistenc thisistenc ".truncate(30, "x") == "thisistenc thisistenc thisistx" );
ok( "thisistenc thisistenc ".truncate(30, "x") == "thisistenc thisistenc " );
ok( "thisistenc ".truncate(5, "x") == "thisx" );
ok( "thisi".truncate(5, "x") == "thisi" );
});
test("String.stripTags()", function() {
ok( "<div id='hi'>Bla</div>".stripTags() == "Bla" );
var testString = [
'<html>',
'<head>',
'<link rel="stylesheet" href="../../jquery/build/test/data/testsuite.css" />',
'<script type="text/javascript" src="../../jquery/dist/jquery.js"><\/script>',
'<script type="text/javascript" src="../../jquery/build/test/data/testrunner.js"><\/script>',
'<script type="text/javascript" src="jquery.string.js"><\/script>',
'<script type="text/javascript" src="jquery.array.js"><\/script>',
'<script type="text/javascript">'
].join('');
ok( !testString.stripTags() );
});
test("Array.forEach()", function() {
var stuff = "";
["foo", "bar"].forEach(function(element, index, array) {
stuff += element;
});
ok( stuff == "foobar" );
});
test("Array.every()", function() {
ok( [12, 54, 18, 130, 44].every(function(element, index, array) {
return element >= 10;
}) === true );
ok( [12, 5, 8, 130, 44].every(function(element, index, array) {
return element >= 10;
}) === false );
});
test("Array.some()", function() {
ok( [12, 5, 8, 1, 44].some(function(element, index, array) {
return element >= 10;
}) === true );
ok( [2, 5, 8, 1, 4].some(function(element, index, array) {
return element >= 10;
}) === false );
});
test("Array.map()", function() {
var s = ["hello", "Array", "WORLD"];
var r = s.map(function(element, index, array) {
return element.toUpperCase();
});
isSet( s, ["hello", "Array", "WORLD"] );
isSet( r, ["HELLO", "ARRAY", "WORLD"] );
s = [1, 4, 9];
r = s.map(Math.sqrt);
isSet( s, [1, 4, 9] );
isSet( r, [1, 2, 3] );
});
test("Array.filter()", function() {
var s = [12, 5, 8, 1, 44];
var r = s.filter(function(element, index, array) {
return element >= 10;
});
isSet( s, [12, 5, 8, 1, 44] );
isSet( r, [12, 44] );
});
test("Array.indexOf()", function() {
ok( [12, 5, 8, 5, 44].indexOf(5) == 1 );
ok( [12, 5, 8, 5, 44].indexOf(5, 2) == 3 );
ok( [12, 5, 8, 5, 44].indexOf(5, 4) == -1 );
});
test("Array.unique()", function() {
var s = [1, 2, 1, 4, 5, 4];
var r = s.unique();
isSet( s, [1, 2, 1, 4, 5, 4] );
isSet( r, [1, 2, 4, 5] );
});
test("Date.isLeapYear()", function() {
var dtm = new Date('01/01/2008');
ok( dtm.isLeapYear() == true, 'is a leap year' );
dtm = new Date('01/01/2007');
ok( dtm.isLeapYear() == false, 'is not a lear' );
});
test("Date.isWeekend()", function() {
var dtm = new Date('01/07/2007');
ok( dtm.isWeekend() == true, 'on a Sunday' );
dtm = new Date('01/08/2007');
ok( dtm.isWeekend() == false, 'on a Monday' );
dtm = new Date('01/09/2007');
ok( dtm.isWeekend() == false, 'on a Tuesday' );
dtm = new Date('01/10/2007');
ok( dtm.isWeekend() == false, 'on a Wednesday' );
dtm = new Date('01/11/2007');
ok( dtm.isWeekend() == false, 'on a Thursday' );
dtm = new Date('01/12/2007');
ok( dtm.isWeekend() == false, 'on a Friday' );
dtm = new Date('01/06/2007');
ok( dtm.isWeekend() == true, 'on a Saturday' );
});
test("Date.isWeekDay()", function() {
var dtm = new Date('01/07/2007');
ok( dtm.isWeekDay() == false, 'on a Sunday' );
dtm = new Date('01/08/2007');
ok( dtm.isWeekDay() == true, 'on a Monday' );
dtm = new Date('01/09/2007');
ok( dtm.isWeekDay() == true, 'on a Tuesday' );
dtm = new Date('01/10/2007');
ok( dtm.isWeekDay() == true, 'on a Wednesday' );
dtm = new Date('01/11/2007');
ok( dtm.isWeekDay() == true, 'on a Thursday' );
dtm = new Date('01/12/2007');
ok( dtm.isWeekDay() == true, 'on a Friday' );
dtm = new Date('01/06/2007');
ok( dtm.isWeekDay() == false, 'on a Saturday' );
});
test("Date.getDaysInMonth()", function() {
var dtm = new Date('01/01/2007');
ok( dtm.getDaysInMonth() == 31, 'in January');
dtm = new Date('02/01/2007');
ok( dtm.getDaysInMonth() == 28, 'in February');
dtm = new Date('02/01/2008');
ok( dtm.getDaysInMonth() == 29, 'in February on a leap year');
dtm = new Date('03/01/2007');
ok( dtm.getDaysInMonth() == 31, 'in March');
dtm = new Date('04/01/2007');
ok( dtm.getDaysInMonth() == 30, 'in April');
dtm = new Date('05/01/2007');
ok( dtm.getDaysInMonth() == 31, 'in May');
dtm = new Date('06/01/2007');
ok( dtm.getDaysInMonth() == 30, 'in June');
dtm = new Date('07/01/2007');
ok( dtm.getDaysInMonth() == 31, 'in July');
dtm = new Date('08/01/2007');
ok( dtm.getDaysInMonth() == 31, 'in August');
dtm = new Date('09/01/2007');
ok( dtm.getDaysInMonth() == 30, 'in September');
dtm = new Date('10/01/2007');
ok( dtm.getDaysInMonth() == 31, 'in October');
dtm = new Date('11/01/2007');
ok( dtm.getDaysInMonth() == 30, 'in November');
dtm = new Date('12/01/2007');
ok( dtm.getDaysInMonth() == 31, 'in December');
});
test("Date.getDayName()", function() {
var dtm = new Date('01/07/2007');
ok( dtm.getDayName() == 'Sunday', 'on a Sunday' );
dtm = new Date('01/08/2007');
ok( dtm.getDayName() == 'Monday', 'on a Monday' );
dtm = new Date('01/09/2007');
ok( dtm.getDayName() == 'Tuesday', 'on a Tuesday' );
dtm = new Date('01/10/2007');
ok( dtm.getDayName() == 'Wednesday', 'on a Wednesday' );
dtm = new Date('01/11/2007');
ok( dtm.getDayName() == 'Thursday', 'on a Thursday' );
dtm = new Date('01/12/2007');
ok( dtm.getDayName() == 'Friday', 'on a Friday' );
dtm = new Date('01/06/2007');
ok( dtm.getDayName() == 'Saturday', 'on a Saturday' );
dtm = new Date('01/07/2007');
ok( dtm.getDayName(true) == 'Sun', 'on a Sunday abbreviated' );
dtm = new Date('01/08/2007');
ok( dtm.getDayName(true) == 'Mon', 'on a Monday abbreviated' );
dtm = new Date('01/09/2007');
ok( dtm.getDayName(true) == 'Tue', 'on a Tuesday abbreviated' );
dtm = new Date('01/10/2007');
ok( dtm.getDayName(true) == 'Wed', 'on a Wednesday abbreviated' );
dtm = new Date('01/11/2007');
ok( dtm.getDayName(true) == 'Thu', 'on a Thursday abbreviated' );
dtm = new Date('01/12/2007');
ok( dtm.getDayName(true) == 'Fri', 'on a Friday abbreviated' );
dtm = new Date('01/06/2007');
ok( dtm.getDayName(true) == 'Sat', 'on a Saturday abbreviated' );
});
test("Date.getMonthName()", function() {
var dtm = new Date('01/01/2007');
ok( dtm.getMonthName() == 'January', 'in January');
dtm = new Date('02/01/2007');
ok( dtm.getMonthName() == 'February', 'in February');
dtm = new Date('03/01/2007');
ok( dtm.getMonthName() == 'March', 'in March');
dtm = new Date('04/01/2007');
ok( dtm.getMonthName() == 'April', 'in April');
dtm = new Date('05/01/2007');
ok( dtm.getMonthName() == 'May', 'in May');
dtm = new Date('06/01/2007');
ok( dtm.getMonthName() == 'June', 'in June');
dtm = new Date('07/01/2007');
ok( dtm.getMonthName() == 'July', 'in July');
dtm = new Date('08/01/2007');
ok( dtm.getMonthName() == 'August', 'in August');
dtm = new Date('09/01/2007');
ok( dtm.getMonthName() == 'September', 'in September');
dtm = new Date('10/01/2007');
ok( dtm.getMonthName() == 'October', 'in October');
dtm = new Date('11/01/2007');
ok( dtm.getMonthName() == 'November', 'in November');
dtm = new Date('12/01/2007');
ok( dtm.getMonthName() == 'December', 'in December');
dtm = new Date('01/01/2007');
ok( dtm.getMonthName(true) == 'Jan', 'in January');
dtm = new Date('02/01/2007');
ok( dtm.getMonthName(true) == 'Feb', 'in February');
dtm = new Date('03/01/2007');
ok( dtm.getMonthName(true) == 'Mar', 'in March');
dtm = new Date('04/01/2007');
ok( dtm.getMonthName(true) == 'Apr', 'in April');
dtm = new Date('05/01/2007');
ok( dtm.getMonthName(true) == 'May', 'in May');
dtm = new Date('06/01/2007');
ok( dtm.getMonthName(true) == 'Jun', 'in June');
dtm = new Date('07/01/2007');
ok( dtm.getMonthName(true) == 'Jul', 'in July');
dtm = new Date('08/01/2007');
ok( dtm.getMonthName(true) == 'Aug', 'in August');
dtm = new Date('09/01/2007');
ok( dtm.getMonthName(true) == 'Sep', 'in September');
dtm = new Date('10/01/2007');
ok( dtm.getMonthName(true) == 'Oct', 'in October');
dtm = new Date('11/01/2007');
ok( dtm.getMonthName(true) == 'Nov', 'in November');
dtm = new Date('12/01/2007');
ok( dtm.getMonthName(true) == 'Dec', 'in December');
});
test("Date.getDayOfYear()", function() {
var dtm = new Date('01/01/2007');
ok( dtm.getDayOfYear() == 0, 'First day of the year' );
dtm = new Date('12/31/2007');
ok( dtm.getDayOfYear() == 364, 'Last day of the year' );
});
test("Date.getWeekOfYear()", function() {
var dtm = new Date('01/01/2007');
ok( dtm.getWeekOfYear() == 0, 'First week of the year' );
dtm = new Date('12/31/2007');
ok( dtm.getWeekOfYear() == 52, 'Last week of the year' );
});
test("Date.setDayOfYear()", function() {
var dtm = new Date('01/01/2007');
ok( dtm.setDayOfYear(365).getDayOfYear() == 364, 'Last day of the year' );
dtm = new Date('12/31/2007');
ok( dtm.setDayOfYear(1).getDayOfYear() == 0, 'First day of the year' );
});
test("Date.addYears()", function() {
var dtm = new Date('01/01/2007');
dtm.addYears(1);
ok( dtm.getFullYear() == 2008, 'Add one year' );
dtm.addYears(-1);
ok( dtm.getFullYear() == 2007, 'Subtract one year' );
});
test("Date.addMonths()", function() {
var dtm = new Date('01/01/2007');
dtm.addMonths(1);
ok( dtm.getMonthName() == 'February', 'Add one month' );
dtm.addMonths(-1);
ok( dtm.getMonthName() == 'January', 'Subtract one month' );
});
test("Date.addDays()", function() {
var dtm = new Date('01/01/2007');
dtm.addDays(1);
ok( dtm.getDayName() == 'Tuesday', 'Add one day' );
dtm.addDays(-1);
ok( dtm.getDayName() == 'Monday', 'Subtract one day' );
});
test("Date.addHours()", function() {
var dtm = new Date('01/01/2007');
dtm.addHours(24);
ok( dtm.getDayName() == 'Tuesday', 'Add 24 hours' );
dtm.addHours(-24);
ok( dtm.getDayName() == 'Monday', 'Subtract 24 hours' );
});
test("Date.addMinutes()", function() {
var dtm = new Date('01/01/2007');
dtm.addMinutes(1440);
ok( dtm.getDayName() == 'Tuesday', 'Add 1440 minutes' );
dtm.addMinutes(-1440);
ok( dtm.getDayName() == 'Monday', 'Subtract 1440 minutes' );
});
test("Date.addSeconds()", function() {
var dtm = new Date('01/01/2007');
dtm.addSeconds(86400);
ok( dtm.getDayName() == 'Tuesday', 'Add 86400 seconds' );
dtm.addSeconds(-86400);
ok( dtm.getDayName() == 'Monday', 'Subtract 86400 seconds' );
});
</script>
</head>
<body>
<h1>jQuery - Methods Test Suite</h1>
<div id="main"></div>
<ol id="tests"></ol>
</body>
</html>