Skip to content

Commit eccf503

Browse files
claudiorodriguezjasnell
authored andcommitted
test: improve readline test coverage for tty
Adds the following tests for tty readline: - go to beginning and end of line - wordLeft - wordRight - deleteWordLeft - deleteWordRight PR-URL: #12064 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent ee157e5 commit eccf503

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed

test/parallel/test-readline-interface.js

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,142 @@ function isWarned(emitter) {
531531
assert.strictEqual(cursorPos.cols, expectedLines.slice(-1)[0].length);
532532
rli.close();
533533
}
534+
535+
{
536+
// Beginning and end of line
537+
const fi = new FakeInput();
538+
const rli = new readline.Interface({
539+
input: fi,
540+
output: fi,
541+
prompt: '',
542+
terminal: terminal
543+
});
544+
fi.emit('data', 'the quick brown fox');
545+
fi.emit('keypress', '.', { ctrl: true, name: 'a' });
546+
let cursorPos = rli._getCursorPos();
547+
assert.strictEqual(cursorPos.rows, 0);
548+
assert.strictEqual(cursorPos.cols, 0);
549+
fi.emit('keypress', '.', { ctrl: true, name: 'e' });
550+
cursorPos = rli._getCursorPos();
551+
assert.strictEqual(cursorPos.rows, 0);
552+
assert.strictEqual(cursorPos.cols, 19);
553+
rli.close();
554+
}
555+
556+
{
557+
// `wordLeft` and `wordRight`
558+
const fi = new FakeInput();
559+
const rli = new readline.Interface({
560+
input: fi,
561+
output: fi,
562+
prompt: '',
563+
terminal: terminal
564+
});
565+
fi.emit('data', 'the quick brown fox');
566+
fi.emit('keypress', '.', { ctrl: true, name: 'left' });
567+
let cursorPos = rli._getCursorPos();
568+
assert.strictEqual(cursorPos.rows, 0);
569+
assert.strictEqual(cursorPos.cols, 16);
570+
fi.emit('keypress', '.', { meta: true, name: 'b' });
571+
cursorPos = rli._getCursorPos();
572+
assert.strictEqual(cursorPos.rows, 0);
573+
assert.strictEqual(cursorPos.cols, 10);
574+
fi.emit('keypress', '.', { ctrl: true, name: 'right' });
575+
cursorPos = rli._getCursorPos();
576+
assert.strictEqual(cursorPos.rows, 0);
577+
assert.strictEqual(cursorPos.cols, 16);
578+
fi.emit('keypress', '.', { meta: true, name: 'f' });
579+
cursorPos = rli._getCursorPos();
580+
assert.strictEqual(cursorPos.rows, 0);
581+
assert.strictEqual(cursorPos.cols, 19);
582+
rli.close();
583+
}
584+
585+
{
586+
// `deleteWordLeft`
587+
[
588+
{ ctrl: true, name: 'w' },
589+
{ ctrl: true, name: 'backspace' },
590+
{ meta: true, name: 'backspace' }
591+
]
592+
.forEach((deleteWordLeftKey) => {
593+
let fi = new FakeInput();
594+
let rli = new readline.Interface({
595+
input: fi,
596+
output: fi,
597+
prompt: '',
598+
terminal: terminal
599+
});
600+
fi.emit('data', 'the quick brown fox');
601+
fi.emit('keypress', '.', { ctrl: true, name: 'left' });
602+
rli.on('line', common.mustCall((line) => {
603+
assert.strictEqual(line, 'the quick fox');
604+
}));
605+
fi.emit('keypress', '.', deleteWordLeftKey);
606+
fi.emit('data', '\n');
607+
rli.close();
608+
609+
// No effect if pressed at beginning of line
610+
fi = new FakeInput();
611+
rli = new readline.Interface({
612+
input: fi,
613+
output: fi,
614+
prompt: '',
615+
terminal: terminal
616+
});
617+
fi.emit('data', 'the quick brown fox');
618+
fi.emit('keypress', '.', { ctrl: true, name: 'a' });
619+
rli.on('line', common.mustCall((line) => {
620+
assert.strictEqual(line, 'the quick brown fox');
621+
}));
622+
fi.emit('keypress', '.', deleteWordLeftKey);
623+
fi.emit('data', '\n');
624+
rli.close();
625+
});
626+
}
627+
628+
{
629+
// `deleteWordRight`
630+
[
631+
{ ctrl: true, name: 'delete' },
632+
{ meta: true, name: 'delete' },
633+
{ meta: true, name: 'd' }
634+
]
635+
.forEach((deleteWordRightKey) => {
636+
let fi = new FakeInput();
637+
let rli = new readline.Interface({
638+
input: fi,
639+
output: fi,
640+
prompt: '',
641+
terminal: terminal
642+
});
643+
fi.emit('data', 'the quick brown fox');
644+
fi.emit('keypress', '.', { ctrl: true, name: 'left' });
645+
fi.emit('keypress', '.', { ctrl: true, name: 'left' });
646+
rli.on('line', common.mustCall((line) => {
647+
assert.strictEqual(line, 'the quick fox');
648+
}));
649+
fi.emit('keypress', '.', deleteWordRightKey);
650+
fi.emit('data', '\n');
651+
rli.close();
652+
653+
// No effect if pressed at end of line
654+
fi = new FakeInput();
655+
rli = new readline.Interface({
656+
input: fi,
657+
output: fi,
658+
prompt: '',
659+
terminal: terminal
660+
});
661+
fi.emit('data', 'the quick brown fox');
662+
rli.on('line', common.mustCall((line) => {
663+
assert.strictEqual(line, 'the quick brown fox');
664+
}));
665+
fi.emit('keypress', '.', deleteWordRightKey);
666+
fi.emit('data', '\n');
667+
rli.close();
668+
});
669+
}
534670
}
535671

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

0 commit comments

Comments
 (0)