Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: improve readline/emitKeypressEvents.js coverage #42908

Merged
merged 2 commits into from
May 22, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 59 additions & 15 deletions test/parallel/test-readline-emit-keypress-events.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,66 @@ require('../common');
const assert = require('assert');
const readline = require('readline');
const PassThrough = require('stream').PassThrough;
const stream = new PassThrough();
const sequence = [];
const keys = [];

readline.emitKeypressEvents(stream);

stream.on('keypress', (s, k) => {
sequence.push(s);
keys.push(k);
});

stream.write('foo');

assert.deepStrictEqual(sequence, ['f', 'o', 'o']);
assert.deepStrictEqual(keys, [
const expectedSequence = ['f', 'o', 'o'];
const expectedKeys = [
{ sequence: 'f', name: 'f', ctrl: false, meta: false, shift: false },
{ sequence: 'o', name: 'o', ctrl: false, meta: false, shift: false },
{ sequence: 'o', name: 'o', ctrl: false, meta: false, shift: false },
]);
];

{
const stream = new PassThrough();
const sequence = [];
const keys = [];

readline.emitKeypressEvents(stream);
stream.on('keypress', (s, k) => {
sequence.push(s);
keys.push(k);
});
stream.write('foo');

assert.deepStrictEqual(sequence, expectedSequence);
assert.deepStrictEqual(keys, expectedKeys);
}

{
const stream = new PassThrough();
const sequence = [];
const keys = [];

stream.on('keypress', (s, k) => {
sequence.push(s);
keys.push(k);
});
readline.emitKeypressEvents(stream);
stream.write('foo');

assert.deepStrictEqual(sequence, expectedSequence);
assert.deepStrictEqual(keys, expectedKeys);
}

{
const stream = new PassThrough();
const sequence = [];
const keys = [];
const keypressListener = (s, k) => {
sequence.push(s);
keys.push(k);
};

stream.on('keypress', keypressListener);
readline.emitKeypressEvents(stream);
stream.removeListener('keypress', keypressListener);
stream.write('foo');

assert.deepStrictEqual(sequence, []);
assert.deepStrictEqual(keys, []);

stream.on('keypress', keypressListener);
stream.write('foo');

assert.deepStrictEqual(sequence, expectedSequence);
assert.deepStrictEqual(keys, expectedKeys);
}