-
Notifications
You must be signed in to change notification settings - Fork 60
/
StringTestCase.js
588 lines (514 loc) · 21.9 KB
/
StringTestCase.js
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
/* jshint -W044 : true */
/*
* Copyright 2012 Amadeus s.a.s.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var Aria = require('ariatemplates/Aria');
var TestCase = require('ariatemplates/jsunit/TestCase');
var ariaUtilsString = require('ariatemplates/utils/String');
var ariaUtilsJson = require('ariatemplates/utils/Json');
/**
* Test case for aria.utils.String
*/
module.exports = Aria.classDefinition({
$classpath : "test.aria.utils.StringTestCase",
$extends : TestCase,
$prototype : {
/**
* Test trim method
*/
testTrim : function () {
var trimResult = ariaUtilsString.trim(" \tthis is a test \n");
this.assertTrue(trimResult === "this is a test", "Expected 'this is a test', Found '" + trimResult + "'");
},
/**
* Test trim method with null argument
*/
testNullTrim : function () {
var trimResult = ariaUtilsString.trim(null);
this.assertTrue(trimResult === null);
},
/**
* Test case for the isEscaped method
*/
testIsEscaped : function () {
var pkg = ariaUtilsString;
this.assertFalse(pkg.isEscaped("{", 0));
this.assertTrue(pkg.isEscaped("\\{", 1));
this.assertFalse(pkg.isEscaped("\\\\{", 2));
this.assertTrue(pkg.isEscaped("\\\\\\{", 3));
this.assertFalse(pkg.isEscaped("a{", 1));
this.assertTrue(pkg.isEscaped("a\\{", 2));
this.assertFalse(pkg.isEscaped("a\\\\{", 3));
this.assertTrue(pkg.isEscaped("a\\\\\\{", 4));
},
/**
* Test indexOfNotEscape method
*/
testIndexOfNotEscaped : function () {
var test = "abc \\\d \d de\fg";
this.assertTrue(ariaUtilsString.indexOfNotEscaped(test, "a") === 0);
this.assertTrue(ariaUtilsString.indexOfNotEscaped(test, "\\") === 4);
this.assertTrue(ariaUtilsString.indexOfNotEscaped(test, "d") === 7);
this.assertTrue(ariaUtilsString.indexOfNotEscaped(test, "f") === -1);
this.assertTrue(ariaUtilsString.indexOfNotEscaped(test, "h") === -1);
},
/**
* Test escapeHTML method
*/
testEscapeHTML : function () {
this.assertTrue(ariaUtilsString.escapeHTML("<div> a & b </div>", "a") === "<div> a & b </div>", "Escape failed.");
},
testEscapeHTMLAttr : function () {
this.assertTrue(ariaUtilsString.escapeHTMLAttr("'") === "'", "Attribute escape failed.");
this.assertTrue(ariaUtilsString.escapeHTMLAttr('"') === """, "Attribute escape failed.");
this.assertTrue(ariaUtilsString.escapeHTMLAttr("'\"") === "'"", "Attribute escape failed.");
},
testEscapeForHTML : function () {
var originalString = "<div id='id' class=\"class\">/</div>";
var escapedForAttrString = "<div id='id' class="class">/</div>";
var escapedForHTMLString = "<div id='id' class=\"class\">/</div>";
var escapedForAllString = "<div id='id' class="class">/</div>";
this.assertTrue(ariaUtilsString.escapeForHTML(originalString) === escapedForAllString, "Full HTML escape failed.");
this.assertTrue(ariaUtilsString.escapeForHTML(originalString, null) === escapedForAllString, "Full HTML escape failed.");
this.assertTrue(ariaUtilsString.escapeForHTML(originalString, 5) === escapedForAllString, "Full HTML escape failed.");
this.assertTrue(ariaUtilsString.escapeForHTML(originalString, '5') === escapedForAllString, "Full HTML escape failed.");
this.assertTrue(ariaUtilsString.escapeForHTML(originalString, true) === escapedForAllString, "Full HTML escape failed.");
this.assertTrue(ariaUtilsString.escapeForHTML(originalString, {
text : true,
attr : true
}) === escapedForAllString, "Full HTML escape failed.");
this.assertTrue(ariaUtilsString.escapeForHTML(originalString, false) === originalString, "Full HTML escape failed.");
this.assertTrue(ariaUtilsString.escapeForHTML(originalString, {
text : false,
attr : false
}) === originalString, "Full HTML escape failed.");
this.assertTrue(ariaUtilsString.escapeForHTML(originalString, {
text : false,
attr : true
}) === escapedForAttrString, "Full HTML escape failed.");
this.assertTrue(ariaUtilsString.escapeForHTML(originalString, {
text : true,
attr : false
}) === escapedForHTMLString, "Full HTML escape failed.");
},
/**
* Test stripAccents method
*/
testStripAccents : function () {
this.assertTrue(ariaUtilsString.stripAccents("àébïuô") === "aebiuo", "Strip Accents failed failed.");
},
/**
* Test nextWhiteSpace method
*/
testNextWhiteSpace : function () {
var test = " abc def\tghi";
this.assertTrue(ariaUtilsString.nextWhiteSpace(test, 0, 5) === 0);
this.assertTrue(ariaUtilsString.nextWhiteSpace(test, 1, 5) === 4);
this.assertTrue(ariaUtilsString.nextWhiteSpace(test, 5, 9) === 8);
this.assertTrue(ariaUtilsString.nextWhiteSpace(test, 1, 3) === -1);
this.assertTrue(ariaUtilsString.nextWhiteSpace('', 2, 5) === -1);
},
/**
* Test encodeForQuotedHTMLAttribute method
*/
testEncodeForQuotedHTMLAttribute : function () {
this.assertEquals(ariaUtilsString.encodeForQuotedHTMLAttribute(undefined), "");
this.assertEquals(ariaUtilsString.encodeForQuotedHTMLAttribute(""), "");
this.assertEquals(ariaUtilsString.encodeForQuotedHTMLAttribute("abc"), "abc");
this.assertEquals(ariaUtilsString.encodeForQuotedHTMLAttribute('"'), '"');
this.assertEquals(ariaUtilsString.encodeForQuotedHTMLAttribute('"abc'), '"abc');
this.assertEquals(ariaUtilsString.encodeForQuotedHTMLAttribute('a"b'), 'a"b');
this.assertEquals(ariaUtilsString.encodeForQuotedHTMLAttribute('a"b"c"d'), 'a"b"c"d');
this.assertEquals(ariaUtilsString.encodeForQuotedHTMLAttribute('"""'), '"""');
this.assertEquals(ariaUtilsString.encodeForQuotedHTMLAttribute('"'), '"');
},
/**
* Test endsWith method
*/
testEndsWith : function () {
this.assertTrue(ariaUtilsString.endsWith("", ""));
this.assertFalse(ariaUtilsString.endsWith("", "a"));
this.assertTrue(ariaUtilsString.endsWith("a", "a"));
this.assertFalse(ariaUtilsString.endsWith("ab", "a"));
this.assertTrue(ariaUtilsString.endsWith("aba", "a"));
this.assertFalse(ariaUtilsString.endsWith("aba", "b"));
this.assertTrue(ariaUtilsString.endsWith("abc", ""));
this.assertTrue(ariaUtilsString.endsWith("abc", "bc"));
this.assertTrue(ariaUtilsString.endsWith("a b", " b"));
this.assertFalse(ariaUtilsString.endsWith("a b", "a"));
},
/**
* Test capitalize method
*/
testCapitalize : function () {
var result = ariaUtilsString.capitalize("this is a test");
this.assertTrue(result === "This is a test", "Expected 'This is a test', Found '" + result + "'");
},
/**
* Test pad method
*/
testPad : function () {
var initial, size, expected, got;
var toTest = {
"" : {
"-1" : "",
0 : "",
1 : "a",
2 : "aa",
5 : "aaaaa"
},
"b" : {
"-1" : "b",
0 : "b",
1 : "b",
2 : "ab",
5 : "aaaab"
},
"cc" : {
"-1" : "cc",
0 : "cc",
1 : "cc",
2 : "cc",
5 : "aaacc"
}
};
for (initial in toTest) {
if (toTest.hasOwnProperty(initial)) {
for (size in toTest[initial]) {
if (toTest[initial].hasOwnProperty(size)) {
expected = toTest[initial][size];
got = ariaUtilsString.pad(initial, size, "a", true);
this.assertEquals(got, expected, "Padding " + initial + " size " + size
+ " got %1 expected %2");
}
}
}
}
// reverse
toTest = {
"" : {
0 : "",
1 : "a",
2 : "aa",
5 : "aaaaa"
},
"b" : {
0 : "b",
1 : "b",
2 : "ba",
5 : "baaaa"
},
"cc" : {
0 : "cc",
1 : "cc",
2 : "cc",
5 : "ccaaa"
}
};
for (initial in toTest) {
if (toTest.hasOwnProperty(initial)) {
for (size in toTest[initial]) {
if (toTest[initial].hasOwnProperty(size)) {
expected = toTest[initial][size];
got = ariaUtilsString.pad(initial, size, "a", false);
this.assertEquals(got, expected, "Padding " + initial + " size " + size
+ " got %1 expected %2");
}
}
}
}
},
/**
* Test crop method - crop from beginning
*/
testCropBegin : function () {
// cropping a
var toTest = {
"" : {
0 : "",
1 : "",
2 : "",
5 : ""
},
"a" : {
0 : "",
1 : "a",
2 : "a",
5 : "a"
},
"ab" : {
0 : "b",
1 : "b",
2 : "ab",
5 : "ab"
},
"aba" : {
0 : "ba",
1 : "ba",
2 : "ba",
5 : "aba"
},
"aab" : {
0 : "b",
1 : "b",
2 : "ab",
5 : "aab"
},
"caabaa" : {
0 : "caabaa",
1 : "caabaa",
2 : "caabaa",
5 : "caabaa"
}
};
for (var initial in toTest) {
if (toTest.hasOwnProperty(initial)) {
for (var size in toTest[initial]) {
if (toTest[initial].hasOwnProperty(size)) {
var expected = toTest[initial][size];
var got = ariaUtilsString.crop(initial, size, "a", true);
this.assertEquals(got, expected, "Cropping " + initial + " size " + size
+ " got %1 expected %2");
}
}
}
}
},
/**
* Test crop method - crop from end
*/
testCropEnd : function () {
// cropping a
var toTest = {
"" : {
0 : "",
1 : "",
2 : "",
5 : ""
},
"a" : {
0 : "",
1 : "a",
2 : "a",
5 : "a"
},
"ab" : {
0 : "ab",
1 : "ab",
2 : "ab",
5 : "ab"
},
"aba" : {
0 : "ab",
1 : "ab",
2 : "ab",
5 : "aba"
},
"aab" : {
0 : "aab",
1 : "aab",
2 : "aab",
5 : "aab"
},
"caabaa" : {
0 : "caab",
1 : "caab",
2 : "caab",
5 : "caaba"
}
};
for (var initial in toTest) {
if (toTest.hasOwnProperty(initial)) {
for (var size in toTest[initial]) {
if (toTest[initial].hasOwnProperty(size)) {
var expected = toTest[initial][size];
var got = ariaUtilsString.crop(initial, size, "a", false);
this.assertEquals(got, expected, "Cropping " + initial + " size " + size
+ " got %1 expected %2");
}
}
}
}
},
/**
* Test chunk method - chunk from begin
*/
testChunkBegin : function () {
var fromBegin = {
"" : [[0, [""]], [1, [""]], [2, [""]], [[1, 2], [""]]],
"a" : [[0, ["a"]], [1, ["a"]], [2, ["a"]], [[1, 2], ["a"]]],
"ab" : [[0, ["ab"]], [1, ["a", "b"]], [2, ["ab"]], [[1, 2], ["a", "b"]]],
"abc" : [[0, ["abc"]], [1, ["a", "b", "c"]], [2, ["ab", "c"]], [[1, 2], ["a", "bc"]]],
"abcd" : [[0, ["abcd"]], [1, ["a", "b", "c", "d"]], [2, ["ab", "cd"]], [[1, 2], ["a", "bc", "d"]]],
"abcdefg" : [[0, ["abcdefg"]], [1, ["a", "b", "c", "d", "e", "f", "g"]], [2, ["ab", "cd", "ef", "g"]],
[[1, 2], ["a", "bc", "defg"]]],
"1234" : [[1, ["1", "2", "3", "4"]], [3, ["123", "4"]], [[2, 3], ["12", "34"]]]
};
for (var testing in fromBegin) {
if (fromBegin.hasOwnProperty(testing)) {
for (var i = 0, len = fromBegin[testing].length; i < len; i += 1) {
var size = fromBegin[testing][i][0];
var expected = aria.utils.Json.convertToJsonString(fromBegin[testing][i][1]);
var got = aria.utils.Json.convertToJsonString(ariaUtilsString.chunk(testing, size, true));
this.assertEquals(expected, got, "Testing -" + testing + "- size " + size
+ " expected %1 got %2");
}
}
}
},
/**
* Test chunk method - chunk from end
*/
testChunkEnd : function () {
var fromEnd = {
"" : [[0, [""]], [1, [""]], [2, [""]], [[1, 2], [""]]],
"a" : [[0, ["a"]], [1, ["a"]], [2, ["a"]], [[1, 2], ["a"]]],
"ab" : [[0, ["ab"]], [1, ["a", "b"]], [2, ["ab"]], [[1, 2], ["a", "b"]]],
"abc" : [[0, ["abc"]], [1, ["a", "b", "c"]], [2, ["a", "bc"]], [[1, 2], ["ab", "c"]]],
"abcd" : [[0, ["abcd"]], [1, ["a", "b", "c", "d"]], [2, ["ab", "cd"]], [[1, 2], ["a", "bc", "d"]]],
"abcdefg" : [[0, ["abcdefg"]], [1, ["a", "b", "c", "d", "e", "f", "g"]], [2, ["a", "bc", "de", "fg"]],
[[1, 2], ["abcd", "ef", "g"]]],
"1234" : [[1, ["1", "2", "3", "4"]], [3, ["1", "234"]], [[2, 3], ["12", "34"]]]
};
for (var testing in fromEnd) {
if (fromEnd.hasOwnProperty(testing)) {
for (var i = 0, len = fromEnd[testing].length; i < len; i += 1) {
var size = fromEnd[testing][i][0];
var expected = aria.utils.Json.convertToJsonString(fromEnd[testing][i][1]);
var got = aria.utils.Json.convertToJsonString(ariaUtilsString.chunk(testing, size, false));
this.assertEquals(expected, got, "Testing -" + testing + "- size " + size
+ " expected %1 got %2");
}
}
}
},
/**
* Call chunk with the wrong parameters
*/
testWrongChunk : function () {
var got = ariaUtilsString.chunk(12, 2);
this.assertEquals(got, null, "Cannot chunk numbers");
got = ariaUtilsString.chunk(null, 3);
this.assertEquals(got, null, "Cannot chunk null");
},
/**
* Substitute a string using a string
*/
testSubstituteString : function () {
var holders = {
"% 1" : "% 1",
"%1" : "thisString",
"%1aaa" : "thisStringaaa",
"a%1aa" : "athisStringaa",
"aaa%1" : "aaathisString",
"%1111" : "thisString1111"
}, got;
var epxected = [];
for (var key in holders) {
if (holders.hasOwnProperty(key)) {
got = ariaUtilsString.substitute(holders[key], "thisString");
this.assertEquals(got, holders[key], "Expecting %2, got %1");
}
}
},
/**
* Substitue a string using an array, only one position in the holder
*/
testSubstituteArrayOfOne : function () {
var holders = {
"% 1" : "% 1",
"%1" : "thisString",
"%1aaa" : "thisStringaaa",
"a%1aa" : "athisStringaa",
"aaa%1" : "aaathisString",
"%1111" : "thisString1111"
}, got;
var epxected = [];
for (var key in holders) {
if (holders.hasOwnProperty(key)) {
got = ariaUtilsString.substitute(holders[key], ["thisString", "another"]);
this.assertEquals(got, holders[key], "Expecting %2, got %1");
}
}
},
/**
* Substitue a string using an array, multiple positions in the holder
*/
testSubstituteMultiArray : function () {
var holders = {
"%1%2" : "onetwo",
"%1aaa%2" : "oneaaatwo",
"%1%2%3" : "onetwo%3",
"%2%1" : "twoone",
"%%12" : "%one2"
}, got;
var epxected = [];
for (var key in holders) {
if (holders.hasOwnProperty(key)) {
got = ariaUtilsString.substitute(holders[key], ["one", "two"]);
this.assertEquals(got, holders[key], "Expecting %2, got %1");
}
}
},
testSubstituteVarArgs : function () {
// common ----------------------------------------------------------
var substitute = ariaUtilsString.substitute;
var self = this;
var test = function(string, args, expectedResult) {
var result = substitute.apply(null, [string].concat(args));
self.assertEquals(result, expectedResult);
};
// tests -----------------------------------------------------------
test('%1%2%3%4', [[1, 2, 3, 4]], '1234');
test('%1%2%3%4', [[1, 2], [3, 4]], '1234');
test('%1%2%3%4', [1, [2], 3, [4]], '1234');
test('%1%2%3%4', [1, [2, [3], 4]], '1234');
},
testCamelToDashed : function () {
var cc2d = ariaUtilsString.camelToDashed;
// standard
this.assertEquals(cc2d("fooBarBaz"), "foo-bar-baz");
// trailing uppercase
this.assertEquals(cc2d("fooBarBazN"), "foo-bar-baz-n");
// leading uppercase
this.assertEquals(cc2d("FooBaz"), "foo-baz");
// consecutive uppercase
this.assertEquals(cc2d("fooABarACBaz"), "foo-a-bar-a-c-baz");
// all in one
this.assertEquals(cc2d("ABC"), "a-b-c");
},
testDashedToCamel : function () {
var d2cc = ariaUtilsString.dashedToCamel;
this.assertEquals(d2cc("foo-bar-baz"), "fooBarBaz");
this.assertEquals(d2cc("foo-bar-baz-n"), "fooBarBazN");
this.assertEquals(d2cc("foo-a-bar-a-c-baz"), "fooABarACBaz");
this.assertEquals(d2cc("a-b-c"), "aBC");
},
testWrap : function () {
// common ----------------------------------------------------------
var wrap = ariaUtilsString.wrap;
var self = this;
var test = function(string, wrapper, expectedResult) {
var result = wrap(string, wrapper);
self.assertEquals(result, expectedResult);
};
// tests -----------------------------------------------------------
test('', '', '');
test('base', '', 'base');
test('', '|', '||');
test('base', '|', '|base|');
test('base', 0, '0base0');
}
}
});