-
Notifications
You must be signed in to change notification settings - Fork 2
/
unittests.js
610 lines (549 loc) · 19.8 KB
/
unittests.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
/*
* Copyright (c) 2013 Peter Flynn.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
/*jslint vars: true, plusplus: true, devel: true, browser: true, nomen: true, indent: 4 */
/*global define, describe, it, expect, beforeEach, afterEach, waitsFor, runs, brackets, waitsForDone */
define(function (require, exports, module) {
"use strict";
// Extension module to test
var Counter = require("Counter");
function expectCount(code, total, sloc) {
var result = Counter.countSloc(code);
expect(result.total).toBe(total);
expect(result.sloc).toBe(sloc);
}
function expectUnsupported(code) {
try {
var result = Counter.countSloc(code);
expect(false).toBe(true);
} catch (ex) {
expect(ex instanceof Counter.Unsupported).toBe(true);
}
}
describe("[pf] SLOC counting", function () {
it("handle empty files", function () {
var code = "";
expectCount(code, 1, 0);
});
it("count simple SLOC", function () {
var code =
"foo();\n" +
"bar();\n" +
"baz();";
expectCount(code, 3, 3);
});
it("don't count newline at EOF", function () {
var code =
"foo();\n" +
"bar();\n";
expectCount(code, 3, 2);
});
it("ignore blank lines", function () {
var code =
"foo();\n" +
"\n" +
"bar();\n" +
" \n" +
"baz();";
expectCount(code, 5, 3);
});
it("ignore entirely blank files", function () {
var code =
"\n" +
"\n" +
" \n" +
" \n";
expectCount(code, 5, 0);
});
it("count line ending in line comment", function () {
var code =
"foo();\n" +
"bar(); // comment\n" +
"baz();";
expectCount(code, 3, 3);
});
it("ignore lone line comment", function () {
var code =
"foo();\n" +
" // comment\n" +
"baz();";
expectCount(code, 3, 2);
});
it("count line ending in single-line block comment", function () {
var code =
"foo();\n" +
"bar(); /* comment */\n" +
"baz();";
expectCount(code, 3, 3);
});
it("count line starting in single-line block comment", function () {
var code =
"foo();\n" +
"/* comment */ bar();\n" +
"baz();";
expectCount(code, 3, 3);
});
it("count code sandwiched between single-line block comment", function () {
var code =
"foo();\n" +
"/* comment */ bar(); /* comment B */\n" +
"baz();";
expectCount(code, 3, 3);
});
it("ignore single-line block comment", function () {
var code =
"foo();\n" +
" /* comment */\n" +
"baz();";
expectCount(code, 3, 2);
});
it("ignore pair of block comments on 1 line", function () {
var code =
"foo();\n" +
"/* comment 1 */ /* comment 2 */\n" +
"baz();";
expectCount(code, 3, 2);
});
it("ignore two-line block comment", function () {
var code =
"foo();\n" +
" /* comment 1\n" +
" comment 2 */\n" +
"baz();";
expectCount(code, 4, 2);
});
it("ignore N-line block comment", function () {
var code =
"foo();\n" +
" /* comment 1\n" +
" comment 2\n" +
" comment 3 */\n" +
"baz();";
expectCount(code, 5, 2);
});
it("count line ending in start of block comment", function () {
var code =
"foo();\n" +
"bar(); /* comment 1\n" +
" comment 2\n" +
" comment 3 */\n" +
"baz();";
expectCount(code, 5, 3);
});
it("count line starting with end of block comment", function () {
var code =
"foo();\n" +
"/* comment 1\n" +
" comment 2\n" +
" comment 3 */ bar();\n" +
"baz();";
expectCount(code, 5, 3);
});
it("count line sandwiched between block comments", function () {
var code =
"foo();\n" +
"/* comment 1\n" +
" comment 2\n" +
" comment 3 */ bar(); /* comment 1B\n" +
" comment 2B\n" +
" comment 3B */\n" +
"baz();";
expectCount(code, 7, 3);
});
it("ignore entire block comment inside string", function () {
var code =
"foo();\n" +
"bar('/* foo */');\n" +
"baz();";
expectCount(code, 3, 3);
});
it("ignore block comment start inside string", function () {
var code =
"foo();\n" +
"bar('/* foo');\n" +
"realCode();\n" +
"/* comment 1\n" +
" comment 2 */\n" +
"baz();";
expectCount(code, 6, 4);
code =
"foo();\n" +
"bar('/* foo'); /* real comment */\n" +
"realCode();\n" +
"/* comment 1\n" +
" comment 2 */\n" +
"baz();";
expectCount(code, 6, 4);
});
it("find block comment end inside 'string'", function () {
var code =
"foo();\n" +
"/* comment 1\n" +
"bar('...*/\n" +
"baz1();\n" +
"baz2('*/')\n" +
"baz3();";
expectCount(code, 6, 4);
});
it("understand escaped quotes", function () {
var code =
"foo();\n" +
"bar('\\'/*');\n" +
"realCode();\n" +
" /* comment 2\n" +
" comment 3 */\n" +
"baz();";
expectCount(code, 6, 4);
code = code.replace(/'/g, '"');
expectCount(code, 6, 4);
});
it("don't misunderstand nested differing quotes", function () {
var code =
"foo();\n" +
"bar('\"/*');\n" +
"realCode();\n" +
" /* comment 2\n" +
" comment 3 */\n" +
"baz();";
expectCount(code, 6, 4);
code =
"foo();\n" +
"bar(\"'/*\");\n" +
"realCode();\n" +
" /* comment 2\n" +
" comment 3 */\n" +
"baz();";
expectCount(code, 6, 4);
});
it("ignore block comment start inside block comment", function () {
var code =
"foo();\n" +
"/* comment 1\n" +
"/* comment 2\n" +
" comment 3\n" +
" comment 4 */\n" +
"baz();";
expectCount(code, 6, 2);
});
it("ignore block comment start inside block comment, single-line", function () {
var code =
"foo();\n" +
"/* comment 1 /* still comment 1 */\n" +
"baz();";
expectCount(code, 3, 2);
});
it("ignore block comment start inside line comment", function () {
var code =
"foo();\n" +
"bar(); // comment 1 /* still comment 1\n" +
"realCode();\n" +
"baz();";
expectCount(code, 4, 4);
});
it("*don't* ignore block comment end inside line comment", function () {
var code =
"foo();\n" +
"/* comment 1\n" +
" // comment 2 */\n" +
"baz();";
expectCount(code, 4, 2);
});
it("ignore quote inside regexp", function () {
// Actually just flagged for now (due to /*), since regexps aren't really supported
var code;
code =
"foo();\n" +
"bar(/'/); /* comment 1\n" +
" comment 2\n" +
" comment 3 */\n" +
"baz();";
// expectCount(code, 5, 3);
expectUnsupported(code);
code =
"foo();\n" +
"bar(/'/); /* ' comment 1\n" +
" comment 2\n" +
" comment 3 */\n" +
"baz();";
// expectCount(code, 5, 3);
expectUnsupported(code);
});
it("ignore block comment start inside regexp", function () {
// Actually just flagged for now (due to /*), since regexps aren't really supported
var code;
code =
"foo();\n" +
"bar(/\\/* foo/); /* real comment */\n" +
"realCode();\n" +
"/* comment 1\n" +
" comment 2 */\n" +
"baz();";
// expectCount(code, 6, 4);
expectUnsupported(code);
code =
"foo();\n" +
"bar(/\\/* foo/);\n" +
"realCode();\n" +
"/* comment 1\n" +
" comment 2 */\n" +
"baz();";
// expectCount(code, 6, 4);
expectUnsupported(code);
});
it("ignore escaped regexp end", function () {
// Actually just flagged for now (due to /*), since regexps aren't really supported
var code;
code =
"foo();\n" +
"bar(/xyz\\/xyz\\/* foo/); /* real comment */\n" +
"realCode();\n" +
"/* comment 1\n" +
" comment 2 */\n" +
"baz();";
// expectCount(code, 6, 4);
expectUnsupported(code);
code =
"foo();\n" +
"bar(/xyz\\/xyz\\/* foo/);\n" +
"realCode();\n" +
"/* comment 1\n" +
" comment 2 */\n" +
"baz();";
// expectCount(code, 6, 4);
expectUnsupported(code);
});
// Regular expressions ----------------------------------------------------------------------------------------
// We can't parse regexps correctly, so we need to reliably warn whenever the code after a (non-comment) "/"
// is important for understanding subsequent lines.
it("flag block comment start after possible regexp", function () {
var code;
code =
"foo();\n" +
"bar(/xyz/); /* real comment */\n" +
"baz();";
expectUnsupported(code);
code = // expected false positive
"foo();\n" +
"bar(1/3); /* real comment */\n" +
"baz();";
expectUnsupported(code);
code =
"foo();\n" +
"bar(/xyz/); /* real comment\n" +
" comment 2 */\n" +
"baz();";
expectUnsupported(code);
});
it("flag string start after possible regexp only if line ends in '\\'", function () {
var code;
code =
"foo();\n" +
"bar(/xyz/); baz(' string\\\n" +
" still string');\n" +
"last();";
expectUnsupported(code);
code = code.replace(/'/g, "\"");
expectUnsupported(code);
code =
"foo();\n" +
"bar(/xyz/); baz(' string\\ \n" + // trailing whitespace shouldn't have any effect
" still string');\n" +
"last();";
expectUnsupported(code);
code = code.replace(/'/g, "\"");
expectUnsupported(code);
code =
"foo();\n" +
"bar(/xyz/); baz('string');\n" +
"last();";
expectCount(code, 3, 3);
code = code.replace(/'/g, "\"");
expectCount(code, 3, 3);
code = // expected regexp false positive
"foo();\n" +
"bar(1/3); baz(' string\\\n" +
" still string');\n" +
"last();";
expectUnsupported(code);
code = code.replace(/'/g, "\"");
expectUnsupported(code);
code = // expected regexp false positive
"foo();\n" +
"bar(1/3); baz('string');\n" +
"last();";
expectCount(code, 3, 3);
code = code.replace(/'/g, "\"");
expectCount(code, 3, 3);
code = // inside regexp
"foo();\n" +
"bar(/'/); \\\n" +
" still string');\n" +
"last();";
expectUnsupported(code);
code = code.replace(/'/g, "\"");
expectUnsupported(code);
code = // inside regexp
"foo();\n" +
"bar(/'/);\n" +
"last();";
expectCount(code, 3, 3);
code = code.replace(/'/g, "\"");
expectCount(code, 3, 3);
});
it("don't flag string BEFORE regexp", function () {
var code;
code =
"foo();\n" +
"bar('abc'); baz(/xyz/);\n" +
"last();";
expectCount(code, 3, 3);
code = code.replace(/'/g, "\"");
expectCount(code, 3, 3);
});
it("don't flag block comment BEFORE regexp", function () {
var code;
code =
"foo();\n" +
"/* comment */ baz(/xyz/);\n" +
"last();";
expectCount(code, 3, 3);
});
it("don't flag block unexpected block comment end inside regexp", function () {
var code;
code =
"foo();\n" +
"baz(/.*/);\n" +
"last();";
expectCount(code, 3, 3);
});
it("don't flag line comment start after possible regexp", function () {
var code =
"foo();\n" +
"bar(/xyz/); // comment\n" +
"baz();";
expectCount(code, 3, 3);
});
it("don't flag possible regexps inside comments", function () {
var code;
code =
"foo();\n" +
"bar(); /* real /comment/ /* */\n" +
"baz();";
expectCount(code, 3, 3);
code =
"foo();\n" +
"bar(); /* comment 1\n" +
" comment /2/ /*\n" +
" comment 3 */\n" +
"baz();";
expectCount(code, 5, 3);
code =
"foo();\n" +
"bar(); // real /line/ comment /*\n" +
"baz();";
expectCount(code, 3, 3);
});
it("don't flag possible regexps inside strings", function () {
var code;
code =
"foo();\n" +
"bar('string /content/ here'); /* foo */\n" +
"baz();";
expectCount(code, 3, 3);
code = code.replace(/'/g, "\"");
expectCount(code, 3, 3);
code =
"foo();\n" +
"bar('string \\\n" +
" /content/ here'); /* foo */\n" +
"baz();";
expectCount(code, 4, 4);
code = code.replace(/'/g, "\"");
expectCount(code, 4, 4);
});
// Sanity checks ----------------------------------------------------------------------------------------------
it("flag code with unclosed block comment", function () {
var code;
code =
"foo();\n" +
"/* comment 1\n" +
"realCode();\n" +
"baz();";
expectUnsupported(code);
code =
"foo();\n" +
"bar(); /* comment 1\n" +
"realCode('*/');\n" +
"baz();";
expectUnsupported(code);
});
it("flag code with unexpected block comment end", function () {
var code =
"foo();\n" +
"bar();*/\n" +
"baz();";
expectUnsupported(code);
});
it("flag code with unclosed string", function () {
var code;
code =
"foo('string);\n" +
"realCode();\n" +
"baz();";
expectUnsupported(code);
code =
"foo(\"string);\n" +
"realCode();\n" +
"baz();";
expectUnsupported(code);
code =
"foo('string);\n" +
"realCode(');\n" +
"baz();";
expectUnsupported(code);
code =
"foo(\"string);\n" +
"realCode(\");\n" +
"baz();";
expectUnsupported(code);
});
it("don't mis-flag code with ES5 string wrap", function () {
var code;
code =
"foo('string\\\n" +
"still string');\n" +
"baz();";
expectCount(code, 3, 3);
code =
"foo(\"string\\\n" +
"still string\");\n" +
"baz();";
expectCount(code, 3, 3);
});
// it("flag code with unclosed regexp", function () { -- regexps not really supported, see flagging cases above
// var code =
// "foo(/string);\n" +
// "realCode();\n" +
// "baz();";
// expectUnsupported(code);
// });
}); // top-level describe()
});