Skip to content

Commit 497bceb

Browse files
stropitektargos
authored andcommitted
test: increase test coverage of readline-interface
Adds coverage for: - question callback - history navigation - bad historySize option - multi-line output - history is bound and most recent elements are preserved PR-URL: #16062 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Michaël Zasso <targos@protonmail.com>
1 parent 1be3f50 commit 497bceb

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

test/parallel/test-readline-interface.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,37 @@ function isWarned(emitter) {
375375
});
376376
}
377377

378+
// constructor throws if historySize is not a positive number
379+
{
380+
const fi = new FakeInput();
381+
assert.throws(function() {
382+
readline.createInterface({
383+
input: fi, historySize: 'not a number'
384+
});
385+
}, common.expectsError({
386+
type: TypeError,
387+
message: 'Argument "historySize" must be a positive number'
388+
}));
389+
390+
assert.throws(function() {
391+
readline.createInterface({
392+
input: fi, historySize: -1
393+
});
394+
}, common.expectsError({
395+
type: TypeError,
396+
message: 'Argument "historySize" must be a positive number'
397+
}));
398+
399+
assert.throws(function() {
400+
readline.createInterface({
401+
input: fi, historySize: NaN
402+
});
403+
}, common.expectsError({
404+
type: TypeError,
405+
message: 'Argument "historySize" must be a positive number'
406+
}));
407+
}
408+
378409
// duplicate lines are removed from history when
379410
// `options.removeHistoryDuplicates` is `true`
380411
{
@@ -404,6 +435,14 @@ function isWarned(emitter) {
404435
assert.notStrictEqual(rli.line, expectedLines[--callCount]);
405436
assert.strictEqual(rli.line, expectedLines[--callCount]);
406437
assert.strictEqual(callCount, 0);
438+
fi.emit('keypress', '.', { name: 'down' }); // 'baz'
439+
assert.strictEqual(rli.line, 'baz');
440+
fi.emit('keypress', '.', { name: 'n', ctrl: true }); // 'bar'
441+
assert.strictEqual(rli.line, 'bar');
442+
fi.emit('keypress', '.', { name: 'down' }); // 'bat'
443+
assert.strictEqual(rli.line, 'bat');
444+
fi.emit('keypress', '.', { name: 'down' }); // ''
445+
assert.strictEqual(rli.line, '');
407446
rli.close();
408447
}
409448

@@ -499,7 +538,35 @@ function isWarned(emitter) {
499538
rli.close();
500539
}
501540

541+
// calling the question callback
542+
{
543+
let called = false;
544+
const fi = new FakeInput();
545+
const rli = new readline.Interface(
546+
{ input: fi, output: fi, terminal: terminal }
547+
);
548+
rli.question('foo?', function(answer) {
549+
called = true;
550+
assert.strictEqual(answer, 'bar');
551+
});
552+
rli.write('bar\n');
553+
assert.ok(called);
554+
rli.close();
555+
}
556+
502557
if (terminal) {
558+
// history is bound
559+
{
560+
const fi = new FakeInput();
561+
const rli = new readline.Interface(
562+
{ input: fi, output: fi, terminal, historySize: 2 }
563+
);
564+
const lines = ['line 1', 'line 2', 'line 3'];
565+
fi.emit('data', lines.join('\n') + '\n');
566+
assert.strictEqual(rli.history.length, 2);
567+
assert.strictEqual(rli.history[0], 'line 3');
568+
assert.strictEqual(rli.history[1], 'line 2');
569+
}
503570
// question
504571
{
505572
const fi = new FakeInput();
@@ -667,6 +734,22 @@ function isWarned(emitter) {
667734
rli.close();
668735
});
669736
}
737+
738+
// multi-line cursor position
739+
{
740+
const fi = new FakeInput();
741+
const rli = new readline.Interface({
742+
input: fi,
743+
output: fi,
744+
prompt: '',
745+
terminal: terminal
746+
});
747+
fi.columns = 10;
748+
fi.emit('data', 'multi-line text');
749+
const cursorPos = rli._getCursorPos();
750+
assert.strictEqual(cursorPos.rows, 1);
751+
assert.strictEqual(cursorPos.cols, 5);
752+
}
670753
}
671754

672755
// isFullWidthCodePoint() should return false for non-numeric values

0 commit comments

Comments
 (0)