-
Notifications
You must be signed in to change notification settings - Fork 28.2k
/
test-readline-interface.js
828 lines (771 loc) · 23.6 KB
/
test-readline-interface.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
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
'use strict';
const common = require('../common');
const assert = require('assert');
const readline = require('readline');
const EventEmitter = require('events').EventEmitter;
const { Writable, Readable } = require('stream');
class FakeInput extends EventEmitter {
resume() {}
pause() {}
write() {}
end() {}
}
function isWarned(emitter) {
for (const name in emitter) {
const listeners = emitter[name];
if (listeners.warned) return true;
}
return false;
}
{
// Default crlfDelay is 100ms
const fi = new FakeInput();
const rli = new readline.Interface({ input: fi, output: fi });
assert.strictEqual(rli.crlfDelay, 100);
rli.close();
}
{
// Minimum crlfDelay is 100ms
const fi = new FakeInput();
const rli = new readline.Interface({ input: fi, output: fi, crlfDelay: 0});
assert.strictEqual(rli.crlfDelay, 100);
rli.close();
}
{
// set crlfDelay to float 100.5ms
const fi = new FakeInput();
const rli = new readline.Interface({
input: fi,
output: fi,
crlfDelay: 100.5
});
assert.strictEqual(rli.crlfDelay, 100.5);
rli.close();
}
{
const fi = new FakeInput();
const rli = new readline.Interface({
input: fi,
output: fi,
crlfDelay: 5000
});
assert.strictEqual(rli.crlfDelay, 5000);
rli.close();
}
[ true, false ].forEach(function(terminal) {
// disable history
{
const fi = new FakeInput();
const rli = new readline.Interface(
{ input: fi, output: fi, terminal: terminal, historySize: 0 }
);
assert.strictEqual(rli.historySize, 0);
fi.emit('data', 'asdf\n');
assert.deepStrictEqual(rli.history, terminal ? [] : undefined);
rli.close();
}
// default history size 30
{
const fi = new FakeInput();
const rli = new readline.Interface(
{ input: fi, output: fi, terminal: terminal }
);
assert.strictEqual(rli.historySize, 30);
fi.emit('data', 'asdf\n');
assert.deepStrictEqual(rli.history, terminal ? ['asdf'] : undefined);
rli.close();
}
// sending a full line
{
const fi = new FakeInput();
const rli = new readline.Interface(
{ input: fi, output: fi, terminal: terminal }
);
let called = false;
rli.on('line', function(line) {
called = true;
assert.strictEqual(line, 'asdf');
});
fi.emit('data', 'asdf\n');
assert.ok(called);
}
// sending a blank line
{
const fi = new FakeInput();
const rli = new readline.Interface(
{ input: fi, output: fi, terminal: terminal }
);
let called = false;
rli.on('line', function(line) {
called = true;
assert.strictEqual(line, '');
});
fi.emit('data', '\n');
assert.ok(called);
}
// sending a single character with no newline
{
const fi = new FakeInput();
const rli = new readline.Interface(fi, {});
let called = false;
rli.on('line', function(line) {
called = true;
});
fi.emit('data', 'a');
assert.ok(!called);
rli.close();
}
// sending a single character with no newline and then a newline
{
const fi = new FakeInput();
const rli = new readline.Interface(
{ input: fi, output: fi, terminal: terminal }
);
let called = false;
rli.on('line', function(line) {
called = true;
assert.strictEqual(line, 'a');
});
fi.emit('data', 'a');
assert.ok(!called);
fi.emit('data', '\n');
assert.ok(called);
rli.close();
}
// sending multiple newlines at once
{
const fi = new FakeInput();
const rli = new readline.Interface(
{ input: fi, output: fi, terminal: terminal }
);
const expectedLines = ['foo', 'bar', 'baz'];
let callCount = 0;
rli.on('line', function(line) {
assert.strictEqual(line, expectedLines[callCount]);
callCount++;
});
fi.emit('data', `${expectedLines.join('\n')}\n`);
assert.strictEqual(callCount, expectedLines.length);
rli.close();
}
// sending multiple newlines at once that does not end with a new line
{
const fi = new FakeInput();
const rli = new readline.Interface(
{ input: fi, output: fi, terminal: terminal }
);
const expectedLines = ['foo', 'bar', 'baz', 'bat'];
let callCount = 0;
rli.on('line', function(line) {
assert.strictEqual(line, expectedLines[callCount]);
callCount++;
});
fi.emit('data', expectedLines.join('\n'));
assert.strictEqual(callCount, expectedLines.length - 1);
rli.close();
}
// sending multiple newlines at once that does not end with a new(empty)
// line and a `end` event
{
const fi = new FakeInput();
const rli = new readline.Interface(
{ input: fi, output: fi, terminal: terminal }
);
const expectedLines = ['foo', 'bar', 'baz', ''];
let callCount = 0;
rli.on('line', function(line) {
assert.strictEqual(line, expectedLines[callCount]);
callCount++;
});
rli.on('close', function() {
callCount++;
});
fi.emit('data', expectedLines.join('\n'));
fi.emit('end');
assert.strictEqual(callCount, expectedLines.length);
rli.close();
}
// sending multiple newlines at once that does not end with a new line
// and a `end` event(last line is)
// \r\n should emit one line event, not two
{
const fi = new FakeInput();
const rli = new readline.Interface(
{ input: fi, output: fi, terminal: terminal }
);
const expectedLines = ['foo', 'bar', 'baz', 'bat'];
let callCount = 0;
rli.on('line', function(line) {
assert.strictEqual(line, expectedLines[callCount]);
callCount++;
});
fi.emit('data', expectedLines.join('\r\n'));
assert.strictEqual(callCount, expectedLines.length - 1);
rli.close();
}
// \r\n should emit one line event when split across multiple writes.
{
const fi = new FakeInput();
const rli = new readline.Interface(
{ input: fi, output: fi, terminal: terminal }
);
const expectedLines = ['foo', 'bar', 'baz', 'bat'];
let callCount = 0;
rli.on('line', function(line) {
assert.strictEqual(line, expectedLines[callCount]);
callCount++;
});
expectedLines.forEach(function(line) {
fi.emit('data', `${line}\r`);
fi.emit('data', '\n');
});
assert.strictEqual(callCount, expectedLines.length);
rli.close();
}
// \r should behave like \n when alone
{
const fi = new FakeInput();
const rli = new readline.Interface(
{ input: fi, output: fi, terminal: true }
);
const expectedLines = ['foo', 'bar', 'baz', 'bat'];
let callCount = 0;
rli.on('line', function(line) {
assert.strictEqual(line, expectedLines[callCount]);
callCount++;
});
fi.emit('data', expectedLines.join('\r'));
assert.strictEqual(callCount, expectedLines.length - 1);
rli.close();
}
// \r at start of input should output blank line
{
const fi = new FakeInput();
const rli = new readline.Interface(
{ input: fi, output: fi, terminal: true }
);
const expectedLines = ['', 'foo' ];
let callCount = 0;
rli.on('line', function(line) {
assert.strictEqual(line, expectedLines[callCount]);
callCount++;
});
fi.emit('data', '\rfoo\r');
assert.strictEqual(callCount, expectedLines.length);
rli.close();
}
// Emit two line events when the delay
// between \r and \n exceeds crlfDelay
{
const fi = new FakeInput();
const delay = 200;
const rli = new readline.Interface({
input: fi,
output: fi,
terminal: terminal,
crlfDelay: delay
});
let callCount = 0;
rli.on('line', function(line) {
callCount++;
});
fi.emit('data', '\r');
setTimeout(common.mustCall(() => {
fi.emit('data', '\n');
assert.strictEqual(callCount, 2);
rli.close();
}), delay * 2);
}
// Emit one line events when the delay between \r and \n is
// over the default crlfDelay but within the setting value
{
const fi = new FakeInput();
const delay = 125;
const crlfDelay = common.platformTimeout(1000);
const rli = new readline.Interface({
input: fi,
output: fi,
terminal: terminal,
crlfDelay
});
let callCount = 0;
rli.on('line', function(line) {
callCount++;
});
fi.emit('data', '\r');
setTimeout(common.mustCall(() => {
fi.emit('data', '\n');
assert.strictEqual(callCount, 1);
rli.close();
}), delay);
}
// set crlfDelay to `Infinity` is allowed
{
const fi = new FakeInput();
const delay = 200;
const crlfDelay = Infinity;
const rli = new readline.Interface({
input: fi,
output: fi,
terminal: terminal,
crlfDelay
});
let callCount = 0;
rli.on('line', function(line) {
callCount++;
});
fi.emit('data', '\r');
setTimeout(common.mustCall(() => {
fi.emit('data', '\n');
assert.strictEqual(callCount, 1);
rli.close();
}), delay);
}
// \t when there is no completer function should behave like an ordinary
// character
{
const fi = new FakeInput();
const rli = new readline.Interface(
{ input: fi, output: fi, terminal: true }
);
let called = false;
rli.on('line', function(line) {
assert.strictEqual(line, '\t');
assert.strictEqual(called, false);
called = true;
});
fi.emit('data', '\t');
fi.emit('data', '\n');
assert.ok(called);
rli.close();
}
// \t does not become part of the input when there is a completer function
{
const fi = new FakeInput();
const completer = (line) => [[], line];
const rli = new readline.Interface({
input: fi,
output: fi,
terminal: true,
completer: completer
});
let called = false;
rli.on('line', function(line) {
assert.strictEqual(line, 'foo');
assert.strictEqual(called, false);
called = true;
});
for (const character of '\tfo\to\t') {
fi.emit('data', character);
}
fi.emit('data', '\n');
assert.ok(called);
rli.close();
}
// constructor throws if completer is not a function or undefined
{
const fi = new FakeInput();
assert.throws(function() {
readline.createInterface({
input: fi,
completer: 'string is not valid'
});
}, function(err) {
if (err instanceof TypeError) {
if (/Argument "completer" must be a function/.test(err)) {
return true;
}
}
return false;
});
}
// duplicate lines are removed from history when
// `options.removeHistoryDuplicates` is `true`
{
const fi = new FakeInput();
const rli = new readline.Interface({
input: fi,
output: fi,
terminal: true,
removeHistoryDuplicates: true
});
const expectedLines = ['foo', 'bar', 'baz', 'bar', 'bat', 'bat'];
let callCount = 0;
rli.on('line', function(line) {
assert.strictEqual(line, expectedLines[callCount]);
callCount++;
});
fi.emit('data', `${expectedLines.join('\n')}\n`);
assert.strictEqual(callCount, expectedLines.length);
fi.emit('keypress', '.', { name: 'up' }); // 'bat'
assert.strictEqual(rli.line, expectedLines[--callCount]);
fi.emit('keypress', '.', { name: 'up' }); // 'bar'
assert.notStrictEqual(rli.line, expectedLines[--callCount]);
assert.strictEqual(rli.line, expectedLines[--callCount]);
fi.emit('keypress', '.', { name: 'up' }); // 'baz'
assert.strictEqual(rli.line, expectedLines[--callCount]);
fi.emit('keypress', '.', { name: 'up' }); // 'foo'
assert.notStrictEqual(rli.line, expectedLines[--callCount]);
assert.strictEqual(rli.line, expectedLines[--callCount]);
assert.strictEqual(callCount, 0);
rli.close();
}
// duplicate lines are not removed from history when
// `options.removeHistoryDuplicates` is `false`
{
const fi = new FakeInput();
const rli = new readline.Interface({
input: fi,
output: fi,
terminal: true,
removeHistoryDuplicates: false
});
const expectedLines = ['foo', 'bar', 'baz', 'bar', 'bat', 'bat'];
let callCount = 0;
rli.on('line', function(line) {
assert.strictEqual(line, expectedLines[callCount]);
callCount++;
});
fi.emit('data', `${expectedLines.join('\n')}\n`);
assert.strictEqual(callCount, expectedLines.length);
fi.emit('keypress', '.', { name: 'up' }); // 'bat'
assert.strictEqual(rli.line, expectedLines[--callCount]);
fi.emit('keypress', '.', { name: 'up' }); // 'bar'
assert.notStrictEqual(rli.line, expectedLines[--callCount]);
assert.strictEqual(rli.line, expectedLines[--callCount]);
fi.emit('keypress', '.', { name: 'up' }); // 'baz'
assert.strictEqual(rli.line, expectedLines[--callCount]);
fi.emit('keypress', '.', { name: 'up' }); // 'bar'
assert.strictEqual(rli.line, expectedLines[--callCount]);
fi.emit('keypress', '.', { name: 'up' }); // 'foo'
assert.strictEqual(rli.line, expectedLines[--callCount]);
assert.strictEqual(callCount, 0);
rli.close();
}
// sending a multi-byte utf8 char over multiple writes
{
const buf = Buffer.from('☮', 'utf8');
const fi = new FakeInput();
const rli = new readline.Interface(
{ input: fi, output: fi, terminal: terminal }
);
let callCount = 0;
rli.on('line', function(line) {
callCount++;
assert.strictEqual(line, buf.toString('utf8'));
});
[].forEach.call(buf, function(i) {
fi.emit('data', Buffer.from([i]));
});
assert.strictEqual(callCount, 0);
fi.emit('data', '\n');
assert.strictEqual(callCount, 1);
rli.close();
}
// Regression test for repl freeze, #1968:
// check that nothing fails if 'keypress' event throws.
{
const fi = new FakeInput();
const rli = new readline.Interface(
{ input: fi, output: fi, terminal: true }
);
const keys = [];
fi.on('keypress', function(key) {
keys.push(key);
if (key === 'X') {
throw new Error('bad thing happened');
}
});
try {
fi.emit('data', 'fooX');
} catch (e) { }
fi.emit('data', 'bar');
assert.strictEqual(keys.join(''), 'fooXbar');
rli.close();
}
// calling readline without `new`
{
const fi = new FakeInput();
const rli = readline.Interface(
{ input: fi, output: fi, terminal: terminal }
);
let called = false;
rli.on('line', function(line) {
called = true;
assert.strictEqual(line, 'asdf');
});
fi.emit('data', 'asdf\n');
assert.ok(called);
rli.close();
}
if (terminal) {
// question
{
const fi = new FakeInput();
const rli = new readline.Interface(
{ input: fi, output: fi, terminal: terminal }
);
const expectedLines = ['foo'];
rli.question(expectedLines[0], function() {
rli.close();
});
const cursorPos = rli._getCursorPos();
assert.strictEqual(cursorPos.rows, 0);
assert.strictEqual(cursorPos.cols, expectedLines[0].length);
rli.close();
}
// sending a multi-line question
{
const fi = new FakeInput();
const rli = new readline.Interface(
{ input: fi, output: fi, terminal: terminal }
);
const expectedLines = ['foo', 'bar'];
rli.question(expectedLines.join('\n'), function() {
rli.close();
});
const cursorPos = rli._getCursorPos();
assert.strictEqual(cursorPos.rows, expectedLines.length - 1);
assert.strictEqual(cursorPos.cols, expectedLines.slice(-1)[0].length);
rli.close();
}
{
// Beginning and end of line
const fi = new FakeInput();
const rli = new readline.Interface({
input: fi,
output: fi,
prompt: '',
terminal: terminal
});
fi.emit('data', 'the quick brown fox');
fi.emit('keypress', '.', { ctrl: true, name: 'a' });
let cursorPos = rli._getCursorPos();
assert.strictEqual(cursorPos.rows, 0);
assert.strictEqual(cursorPos.cols, 0);
fi.emit('keypress', '.', { ctrl: true, name: 'e' });
cursorPos = rli._getCursorPos();
assert.strictEqual(cursorPos.rows, 0);
assert.strictEqual(cursorPos.cols, 19);
rli.close();
}
{
// `wordLeft` and `wordRight`
const fi = new FakeInput();
const rli = new readline.Interface({
input: fi,
output: fi,
prompt: '',
terminal: terminal
});
fi.emit('data', 'the quick brown fox');
fi.emit('keypress', '.', { ctrl: true, name: 'left' });
let cursorPos = rli._getCursorPos();
assert.strictEqual(cursorPos.rows, 0);
assert.strictEqual(cursorPos.cols, 16);
fi.emit('keypress', '.', { meta: true, name: 'b' });
cursorPos = rli._getCursorPos();
assert.strictEqual(cursorPos.rows, 0);
assert.strictEqual(cursorPos.cols, 10);
fi.emit('keypress', '.', { ctrl: true, name: 'right' });
cursorPos = rli._getCursorPos();
assert.strictEqual(cursorPos.rows, 0);
assert.strictEqual(cursorPos.cols, 16);
fi.emit('keypress', '.', { meta: true, name: 'f' });
cursorPos = rli._getCursorPos();
assert.strictEqual(cursorPos.rows, 0);
assert.strictEqual(cursorPos.cols, 19);
rli.close();
}
{
// `deleteWordLeft`
[
{ ctrl: true, name: 'w' },
{ ctrl: true, name: 'backspace' },
{ meta: true, name: 'backspace' }
]
.forEach((deleteWordLeftKey) => {
let fi = new FakeInput();
let rli = new readline.Interface({
input: fi,
output: fi,
prompt: '',
terminal: terminal
});
fi.emit('data', 'the quick brown fox');
fi.emit('keypress', '.', { ctrl: true, name: 'left' });
rli.on('line', common.mustCall((line) => {
assert.strictEqual(line, 'the quick fox');
}));
fi.emit('keypress', '.', deleteWordLeftKey);
fi.emit('data', '\n');
rli.close();
// No effect if pressed at beginning of line
fi = new FakeInput();
rli = new readline.Interface({
input: fi,
output: fi,
prompt: '',
terminal: terminal
});
fi.emit('data', 'the quick brown fox');
fi.emit('keypress', '.', { ctrl: true, name: 'a' });
rli.on('line', common.mustCall((line) => {
assert.strictEqual(line, 'the quick brown fox');
}));
fi.emit('keypress', '.', deleteWordLeftKey);
fi.emit('data', '\n');
rli.close();
});
}
{
// `deleteWordRight`
[
{ ctrl: true, name: 'delete' },
{ meta: true, name: 'delete' },
{ meta: true, name: 'd' }
]
.forEach((deleteWordRightKey) => {
let fi = new FakeInput();
let rli = new readline.Interface({
input: fi,
output: fi,
prompt: '',
terminal: terminal
});
fi.emit('data', 'the quick brown fox');
fi.emit('keypress', '.', { ctrl: true, name: 'left' });
fi.emit('keypress', '.', { ctrl: true, name: 'left' });
rli.on('line', common.mustCall((line) => {
assert.strictEqual(line, 'the quick fox');
}));
fi.emit('keypress', '.', deleteWordRightKey);
fi.emit('data', '\n');
rli.close();
// No effect if pressed at end of line
fi = new FakeInput();
rli = new readline.Interface({
input: fi,
output: fi,
prompt: '',
terminal: terminal
});
fi.emit('data', 'the quick brown fox');
rli.on('line', common.mustCall((line) => {
assert.strictEqual(line, 'the quick brown fox');
}));
fi.emit('keypress', '.', deleteWordRightKey);
fi.emit('data', '\n');
rli.close();
});
}
}
// isFullWidthCodePoint() should return false for non-numeric values
[true, false, null, undefined, {}, [], 'あ'].forEach((v) => {
assert.strictEqual(readline.isFullWidthCodePoint('あ'), false);
});
// wide characters should be treated as two columns.
assert.strictEqual(readline.isFullWidthCodePoint('a'.charCodeAt(0)), false);
assert.strictEqual(readline.isFullWidthCodePoint('あ'.charCodeAt(0)), true);
assert.strictEqual(readline.isFullWidthCodePoint('谢'.charCodeAt(0)), true);
assert.strictEqual(readline.isFullWidthCodePoint('고'.charCodeAt(0)), true);
assert.strictEqual(readline.isFullWidthCodePoint(0x1f251), true); // surrogate
assert.strictEqual(readline.codePointAt('ABC', 0), 0x41);
assert.strictEqual(readline.codePointAt('あいう', 1), 0x3044);
assert.strictEqual(readline.codePointAt('\ud800\udc00', 0), // surrogate
0x10000);
assert.strictEqual(readline.codePointAt('\ud800\udc00A', 2), // surrogate
0x41);
assert.strictEqual(readline.getStringWidth('abcde'), 5);
assert.strictEqual(readline.getStringWidth('古池や'), 6);
assert.strictEqual(readline.getStringWidth('ノード.js'), 9);
assert.strictEqual(readline.getStringWidth('你好'), 4);
assert.strictEqual(readline.getStringWidth('안녕하세요'), 10);
assert.strictEqual(readline.getStringWidth('A\ud83c\ude00BC'),
5); // surrogate
// check if vt control chars are stripped
assert.strictEqual(
readline.stripVTControlCharacters('\u001b[31m> \u001b[39m'),
'> '
);
assert.strictEqual(
readline.stripVTControlCharacters('\u001b[31m> \u001b[39m> '),
'> > '
);
assert.strictEqual(
readline.stripVTControlCharacters('\u001b[31m\u001b[39m'),
''
);
assert.strictEqual(
readline.stripVTControlCharacters('> '),
'> '
);
assert.strictEqual(readline.getStringWidth('\u001b[31m> \u001b[39m'), 2);
assert.strictEqual(readline.getStringWidth('\u001b[31m> \u001b[39m> '), 4);
assert.strictEqual(readline.getStringWidth('\u001b[31m\u001b[39m'), 0);
assert.strictEqual(readline.getStringWidth('> '), 2);
{
const fi = new FakeInput();
assert.deepStrictEqual(fi.listeners(terminal ? 'keypress' : 'data'), []);
}
// check EventEmitter memory leak
for (let i = 0; i < 12; i++) {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.close();
assert.strictEqual(isWarned(process.stdin._events), false);
assert.strictEqual(isWarned(process.stdout._events), false);
}
// can create a new readline Interface with a null output argument
{
const fi = new FakeInput();
const rli = new readline.Interface(
{ input: fi, output: null, terminal: terminal }
);
let called = false;
rli.on('line', function(line) {
called = true;
assert.strictEqual(line, 'asdf');
});
fi.emit('data', 'asdf\n');
assert.ok(called);
assert.doesNotThrow(function() {
rli.setPrompt('ddd> ');
});
assert.doesNotThrow(function() {
rli.prompt();
});
assert.doesNotThrow(function() {
rli.write('really shouldnt be seeing this');
});
assert.doesNotThrow(function() {
rli.question('What do you think of node.js? ', function(answer) {
console.log('Thank you for your valuable feedback:', answer);
rli.close();
});
});
}
{
const expected = terminal
? ['\u001b[1G', '\u001b[0J', '$ ', '\u001b[3G']
: ['$ '];
let counter = 0;
const output = new Writable({
write: common.mustCall((chunk, enc, cb) => {
assert.strictEqual(chunk.toString(), expected[counter++]);
cb();
rl.close();
}, expected.length)
});
const rl = readline.createInterface({
input: new Readable({ read: common.mustCall() }),
output: output,
prompt: '$ ',
terminal: terminal
});
rl.prompt();
assert.strictEqual(rl._prompt, '$ ');
}
});