-
Notifications
You must be signed in to change notification settings - Fork 9
/
test.js
747 lines (721 loc) · 21.3 KB
/
test.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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
/* istanbul instrument in package utility2 */
// assets.utility2.header.js - start
/* jslint utility2:true */
/* istanbul ignore next */
// run shared js-env code - init-local
(function () {
"use strict";
let isEnvNode;
let local;
// init debugInline
if (!globalThis.debugInline) {
let consoleError;
consoleError = console.error;
globalThis.debugInline = function (...argList) {
/*
* this function will both print <argList> to stderr and
* return <argList>[0]
*/
consoleError("\n\ndebugInline");
consoleError(...argList);
consoleError("\n");
return argList[0];
};
}
// init isEnvNode
isEnvNode = (
typeof process === "object" && process &&
process.versions && typeof process.versions.node === "string"
);
// init function
function objectDeepCopyWithKeysSorted(obj) {
/*
* this function will recursively deep-copy <obj> with keys sorted
*/
let sorted;
if (typeof obj !== "object" || !obj) {
return obj;
}
// recursively deep-copy list with child-keys sorted
if (Array.isArray(obj)) {
return obj.map(objectDeepCopyWithKeysSorted);
}
// recursively deep-copy obj with keys sorted
sorted = {};
Object.keys(obj).sort().forEach(function (key) {
sorted[key] = objectDeepCopyWithKeysSorted(obj[key]);
});
return sorted;
}
function assertJsonEqual(aa, bb) {
/*
* this function will assert JSON.stringify(<aa>) === JSON.stringify(<bb>)
*/
aa = JSON.stringify(objectDeepCopyWithKeysSorted(aa));
bb = JSON.stringify(objectDeepCopyWithKeysSorted(bb));
if (aa !== bb) {
throw new Error(JSON.stringify(aa) + " !== " + JSON.stringify(bb));
}
}
function assertOrThrow(passed, msg) {
/*
* this function will throw <msg> if <passed> is falsy
*/
if (passed) {
return;
}
throw (
(
msg &&
typeof msg.message === "string" &&
typeof msg.stack === "string"
)
// if msg is err, then leave as is
? msg
: new Error(
typeof msg === "string"
// if msg is string, then leave as is
? msg
// else JSON.stringify(msg)
: JSON.stringify(msg, undefined, 4)
)
);
}
function documentQuerySelectorAll(selector) {
/*
* this function will return document.querySelectorAll(<selector>)
* or empty list if function is not available
*/
return Array.from(
(
typeof document === "object" && document &&
typeof document.querySelectorAll === "function"
)
? document.querySelectorAll(selector)
: []
);
}
function identity(val) {
/*
* this function will return <val>
*/
return val;
}
function noop() {
/*
* this function will do nothing
*/
return;
}
function objectAssignDefault(tgt = {}, src = {}, depth = 0) {
/*
* this function will if items from <tgt> are null, undefined,
* or "", then overwrite them with items from <src>
*/
function recurse(tgt, src, depth) {
Object.entries(src).forEach(function ([
key, bb
]) {
let aa;
aa = tgt[key];
if (aa === undefined || aa === null || aa === "") {
tgt[key] = bb;
return;
}
if (
depth !== 0 &&
typeof aa === "object" && aa && !Array.isArray(aa) &&
typeof bb === "object" && bb && !Array.isArray(bb)
) {
recurse(aa, bb, depth - 1);
}
});
}
recurse(tgt, src, depth | 0);
return tgt;
}
function onErrorThrow(err) {
/*
* this function will throw <err> if exists
*/
if (err) {
throw err;
}
}
// init local
local = {
assertJsonEqual,
assertOrThrow,
documentQuerySelectorAll,
identity,
isEnvNode,
local,
noop,
objectAssignDefault,
objectDeepCopyWithKeysSorted,
onErrorThrow
};
globalThis.globalLocal = local;
}());
// assets.utility2.header.js - end
/* jslint utility2:true */
(function (local) {
"use strict";
/* istanbul ignore next */
// run shared js-env code - init-before
(function () {
// init local
local = globalThis.utility2 || require("./lib.utility2.js");
local = local.requireReadme();
globalThis.local = local;
// init test
local.testRunDefault(local);
}());
// run shared js-env code - function
(function () {
let {
assertJsonEqual,
assertOrThrow,
isEnvNode,
noop,
onErrorThrow,
tryCatchOnError
} = local;
local.testCase_assertXxx_default = function (opt, onError) {
/*
* this function will test assertXxx's default handling-behavior
*/
// test assertion passed
assertOrThrow(true, true);
// test assertion failed with undefined message
tryCatchOnError(function () {
assertOrThrow(undefined);
}, function (err) {
// validate err
assertJsonEqual(err.message, "");
});
// test assertion failed with string message
tryCatchOnError(function () {
assertOrThrow(undefined, "aa");
}, function (err) {
// validate err
assertJsonEqual(err.message, "aa");
});
// test assertion failed with errObj
tryCatchOnError(function () {
assertOrThrow(undefined, new Error("aa"));
}, function (err) {
// validate err
assertJsonEqual(err.message, "aa");
});
// test assertion failed with json object
tryCatchOnError(function () {
assertOrThrow(undefined, {
aa: 1
});
}, function (err) {
// validate err
assertJsonEqual(err.message, "{\n \"aa\": 1\n}");
});
onError(undefined, opt);
};
local.testCase_buildApidoc_default = function (opt, onError) {
/*
* this function will test buildApidoc's default handling-behavior
*/
local._testCase_buildApidoc_default({
blacklistDict: {}
}, onError, opt);
};
local.testCase_buildApp_default = function (opt, onError) {
/*
* this function will test buildApp's default handling-behavior
*/
local._testCase_buildApp_default({
customizeAssetsList: [
{
file: "/assets.hello.txt",
url: "/assets.hello.txt"
}, {
file: "/assets.script_only.html",
url: "/assets.script_only.html"
}, {
file: "/assets.utility2.lib.istanbul.js",
url: "/assets.utility2.lib.istanbul.js"
}, {
file: "/assets.utility2.lib.jslint.js",
url: "/assets.utility2.lib.jslint.js"
}, {
file: "/assets.utility2.lib.marked.js",
url: "/assets.utility2.lib.marked.js"
}, {
file: "/assets.utility2.rollup.js",
url: "/assets.utility2.rollup.js"
}
],
customizeReadmeList: [
// customize quickstart-example-js-instruction
{
merge: (
/\n#\u0020quickstart\u0020example.js\n[\S\s]*?\n\n\n/
)
// customize quickstart-example-js-comment
}, {
aa: "\n<!-- utility2-comment\n",
bb: "\n"
// customize quickstart-example-js-comment
}, {
aa: "\nutility2-comment -->\n",
bb: "\n"
// customize quickstart-example-js-comment
}, {
aa: "\n<!-- utility2-comment\n",
bb: "\n"
// customize quickstart-example-js-script-1
}, {
aa: "<script src=\"assets.utility2.js\"></script>\n",
bb: "\n"
// customize quickstart-example-js-script-2
}, {
aa: "<script src=\"assets.utility2.rollup.js\"></script>\n",
bb: (
"<script src=\"assets.utility2.lib.istanbul.js\">" +
"</script>\n" +
"<script src=\"assets.utility2.lib.jslint.js\">" +
"</script>\n" +
"<script src=\"assets.utility2.lib.marked.js\">" +
"</script>\n" +
"<script src=\"assets.utility2.js\"></script>\n"
)
// customize quickstart-example-js-comment
}, {
aa: "\nutility2-comment -->\n",
bb: "\n"
// customize quickstart-example-js-screenshot
}, {
merge: (
/\n```[^`]*?\n#\u0020extra\u0020screenshots\n/
)
// customize build-script
}, {
merge: (
/\n#\u0020internal\u0020build\u0020script\n[\S\s]*?\nshCiMain\n/
)
}
]
}, onError, opt);
};
local.testCase_chromeDevtoolsClient_coverage = async function (opt, onError) {
/*
* this function will test chromeDevtoolsClient's coverage handling-behavior
*/
if (!isEnvNode) {
onError(undefined, opt);
return;
}
await local.chromeDevtoolsClientCreate({
modeCoverageHack: 1
});
onError(undefined, opt);
};
local.testCase_cliRun_default = function (opt, onError) {
/*
* this function will test cliRun's default handling-behavior
*/
if (!isEnvNode) {
onError(undefined, opt);
return;
}
local.testMock([
[
local, {
replStart: noop
}
], [
local.cliDict, {}
], [
process, {
argv: []
}
], [
require("repl"), {
start: noop
}
], [
require("vm"), {
runInThisContext: noop
}
]
], function (onError) {
// test default handling-behavior
local.cliDict = {
_default: noop
};
local.cliRun({
rgxComment: (
/^/
)
});
// test builtin handling-behavior
[
"--eval",
"--help",
"--interactive",
"--version",
"undefined"
].forEach(function (key) {
process.argv[2] = key;
local.cliDict = {};
local.cliRun({
rgxComment: (
/^/
)
});
});
// test err handling-behavior
local.cliDict = {};
tryCatchOnError(local.cliRun.bind(undefined, {}), assertOrThrow);
onError(undefined, opt);
}, onError);
};
local.testCase_eventListenerXxx_default = function (opt, onError) {
/*
* this function will test eventListenerXxx's default handling-behavior
*/
let listener;
listener = function ({
msg,
type
}) {
assertJsonEqual(msg, "bb");
assertJsonEqual(type, "aa");
};
local.eventListenerAdd("aa", {}, listener);
local.eventListenerAdd("aa", {
once: true
}, listener);
local.eventListenerEmit("aa", "bb");
local.eventListenerEmit("aa", "bb");
local.eventListenerRemove(listener);
onError(undefined, opt);
};
local.testCase_libUtility2Js_standalone = function (opt, onError) {
/*
* this function will test lib.utility2.js's standalone handling-behavior
*/
if (!isEnvNode) {
onError(undefined, opt);
return;
}
require("fs").readFile("lib.utility2.js", "utf8", function (err, data) {
onErrorThrow(err);
require("fs").writeFile(".tmp/lib.utility2.js", data.replace(
"/* istanbul instrument in package utility2 */",
""
), function (err) {
onErrorThrow(err);
require("./.tmp/lib.utility2.js");
});
onError(undefined, opt);
});
};
local.testCase_listShuffle_default = function (opt, onError) {
/*
* this function will test listShuffle's default handling-behavior
*/
opt = {};
// init list
opt.list = "[0,1]";
// shuffle list 100 times
opt.ii = 0;
while (opt.ii < 100) {
opt.listShuffled = JSON.stringify(
local.listShuffle(JSON.parse(opt.list))
);
// validate shuffled list
assertJsonEqual(opt.listShuffled.length, opt.list.length);
opt.changed = opt.changed || opt.listShuffled !== opt.list;
opt.ii += 1;
}
// validate list changed at least once during shuffle
assertOrThrow(opt.changed, opt);
onError(undefined, opt);
};
local.testCase_replStart_default = function (opt, onError) {
/*
* this function will test replStart's default handling-behavior
*/
if (!isEnvNode) {
onError(undefined, opt);
return;
}
local.replStart();
// hack-coverage - test replStart's muliple-call handling-behavior
local.replStart();
local.testMock([
[
require("child_process"), {
spawn: function () {
return {
on: function (evt, callback) {
callback(undefined, evt);
}
};
}
}
],
// suppress process.stdout
[
process.stdout, {
write: noop
}
]
], function (onError) {
[
// test null-case handling-behavior
"",
// test shell handling-behavior
"$ :\n",
// test git diff handling-behavior
"$ git diff\n",
// test git log handling-behavior
"$ git log\n",
// test ll handling-behavior
"$ ll\n",
// test charCode handling-behavior
"charCode abcd\n",
// test charSort handling-behavior
"charSort abcd\n",
// test keys handling-behavior
"keys {}\n",
// test print handling-behavior
"print abcd\n",
// test err handling-behavior
"undefined()\n"
].forEach(function (script) {
globalThis.utility2_repl1.eval(script, null, "repl", noop);
});
onError(undefined, opt);
}, onError);
};
local.testCase_setTimeoutOnError_default = function (opt, onError) {
/*
* this function will test setTimeoutOnError's default handling-behavior
*/
// test null-case handling-behavior
assertJsonEqual(local.setTimeoutOnError(), undefined);
// test onError handling-behavior
assertJsonEqual(
local.setTimeoutOnError(onError, 0, null, {}, opt),
{}
);
};
local.testCase_stringHtmlSafe_default = function (opt, onError) {
/*
* this function will test stringHtmlSafe's default handling-behavior
*/
assertJsonEqual(
local.stringHtmlSafe(
local.stringHtmlSafe(local.stringCharsetAscii).slice(32, -1)
),
(
" !"#$%&'()*+,-./0123456789:;<=>?@" +
"ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
)
);
onError(undefined, opt);
};
local.testCase_stringXxx_default = function (opt, onError) {
/*
* this function will test stringXxx's default handling-behavior
*/
assertJsonEqual(
local.regexpCharsetEncodeUri.source,
local.stringRegexpEscape(
local.stringCharsetEncodeUri
).replace("\\-", "-")
);
assertJsonEqual(
local.regexpCharsetEncodeUriComponent.source,
local.stringRegexpEscape(
local.stringCharsetEncodeUriComponent
).replace("\\-", "-")
);
assertJsonEqual(
local.stringCharsetEncodeUri,
Array.from(
new Set(encodeURI(local.stringCharsetAscii).split(""))
).sort().join("")
);
assertJsonEqual(
local.stringCharsetEncodeUriComponent,
Array.from(
new Set(encodeURIComponent(local.stringCharsetAscii).split(""))
).sort().join("")
);
assertJsonEqual(
local.stringRegexpEscape(local.stringCharsetAscii),
(
"\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007" +
"\b\t\n\u000b\f\r\u000e\u000f" +
"\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017" +
"\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f" +
" !\"#\\$%&'\\(\\)\\*\\+,\\-\\.\\/0123456789:;<=>\\?@" +
"ABCDEFGHIJKLMNOPQRSTUVWXYZ\\[\\\\\\]\\^_`" +
"abcdefghijklmnopqrstuvwxyz\\{\\|\\}~" +
"\u007f"
)
);
onError(undefined, opt);
};
local.testCase_testMock_err = function (opt, onError) {
/*
* this function will test testMock's err handling-behavior
*/
try {
local.testMock([], function () {
throw new Error();
});
} catch (errCaught) {
// handle err
assertOrThrow(errCaught, errCaught);
onError(undefined, opt);
}
};
local.testCase_uiAnimateXxx_default = function (opt, onError) {
/*
* this function will test uiAnimateXxx's default handling-behavior
*/
if (isEnvNode) {
onError(undefined, opt);
return;
}
opt = document.createElement("div");
// test uiAnimateSlideXxx handling-behavior
local.uiAnimateSlideDown();
local.uiAnimateSlideUp();
opt.classList.add("uiAnimateSlide");
local.uiAnimateSlideDown(opt);
assertOrThrow(
opt.style.maxHeight.indexOf("px") >= 0,
opt.style.maxHeight
);
local.uiAnimateSlideUp(opt);
assertJsonEqual(opt.style.maxHeight, "0px");
// test uiAnimateSlideAccordian handling-behavior
local.uiAnimateSlideAccordian(
opt,
[
opt, document.createElement("div")
]
);
onError(undefined, opt);
};
local.testCase_urlJoin_default = function (opt, onError) {
/*
* this function will test urlJoin's default handling-behavior
*/
assertJsonEqual(local.urlJoin("", ""), "/");
assertJsonEqual(local.urlJoin("http://aa/bb", "zz"), "http://aa/zz");
assertJsonEqual(
local.urlJoin("http://aa/bb/", "zz"),
"http://aa/bb/zz"
);
assertJsonEqual(
local.urlJoin("http://aa/bb/", "/zz"),
"http://aa/zz"
);
assertJsonEqual(local.urlJoin("http://aa/bb/", "//zz"), "http://zz");
assertJsonEqual(
local.urlJoin("http://aa/bb/", "http://zz"),
"http://zz"
);
onError(undefined, opt);
};
local.testCase_uuid4Create_default = function (opt, onError) {
/*
* this function will test uuid4Create's default handling-behavior
*/
assertOrThrow(
local.regexpValidateUuid.test(local.uuid4Create()),
local.uuid4Create()
);
onError(undefined, opt);
};
local.testCase_webpage_default = async function (opt, onError) {
/*
* this function will test webpage's default handling-behavior
*/
local._testCase_webpage_default(opt, onError);
};
local.testCase_webpage_err = async function (opt, onError) {
/*
* this function will test webpage's err handling-behavior
*/
if (isEnvNode) {
await local.browserTest({
modeSilent: true,
url: (
"http://127.0.0.1:" + process.env.PORT +
"/?npm_config_mode_test=1" +
"&npm_config_mode_test_case=testCase_webpage_err"
)
});
onError(undefined, opt);
return;
}
if (local.npm_config_mode_test_case !== "testCase_webpage_err") {
onError(undefined, opt);
return;
}
// ignore err in coverage-case
globalThis.utility2_testReport.testPlatformList[0].testCaseList = [];
globalThis.utility2_testReport.testsPending = 0;
setTimeout(function () {
// test err from callback handling-behavior
onError(new Error(), opt);
// test err from multiple-callback handling-behavior
onError(undefined, opt);
}, 2000);
// test uncaught-err handling-behavior
setTimeout(assertOrThrow.bind(undefined, undefined));
};
// run node js-env code - init-after
/* istanbul ignore next */
(function () {
if (!isEnvNode) {
return;
}
// init cli
if (module !== require.main || globalThis.utility2_rollup) {
return;
}
local.assetsDict["/assets.script_only.html"] = (
"<h1>script_only_test</h1>\n" +
"<script src=\"assets.utility2.js\"></script>\n" +
"<script>\n" +
"window.utility2.onReadyIncrement();\n" +
"window.addEventListener(\"load\", window.utility2.onReadyDecrement);\n" +
"</script>\n" +
"<script src=\"assets.example.js\"></script>\n" +
"<script src=\"assets.test.js\"></script>\n"
);
if (process.argv[2]) {
// start with coverage
if (process.env.npm_config_mode_coverage) {
process.argv.splice(1, 1, __dirname + "/lib.istanbul.js", "cover");
local.istanbul.cliDict[process.argv[2]]();
return;
}
// start
process.argv.splice(1, 1);
process.argv[1] = require("path").resolve(process.argv[1]);
require("module").runMain();
}
// runme
if (process.env.npm_config_runme) {
require(require("path").resolve(process.env.npm_config_runme));
}
}());
}());
}());