node/test/parallel/test-repl-tab-complete-custom-completer.js
Dario Piotrowicz aee9bc0fc6
test: refactor repl tab complete tests
refactor the test/parallel/test-repl-tab-complete.js file by:
 - making the tests in the file self-contained
   (instead of all of them sharing the same REPL instance and
   constantly calling `.clear` on it)
 - using the test runner with appropriate descriptions to make
   clearer what is being tested
 - extracting some tests in their own js test files
   (to increase isolation of the tests and help with issues such
   as flakiness)

PR-URL: https://github.com/nodejs/node/pull/58636
Reviewed-By: Giovanni Bucci <github@puskin.it>
Reviewed-By: James M Snell <jasnell@gmail.com>
2025-06-15 09:48:49 +00:00

75 lines
2.1 KiB
JavaScript

'use strict';
const common = require('../common');
const ArrayStream = require('../common/arraystream');
const assert = require('assert');
const repl = require('repl');
const putIn = new ArrayStream();
// To test custom completer function.
// Sync mode.
{
const customCompletions = 'aaa aa1 aa2 bbb bb1 bb2 bb3 ccc ddd eee'.split(' ');
const testCustomCompleterSyncMode = repl.start({
prompt: '',
input: putIn,
output: putIn,
completer: function completer(line) {
const hits = customCompletions.filter((c) => c.startsWith(line));
// Show all completions if none found.
return [hits.length ? hits : customCompletions, line];
}
});
// On empty line should output all the custom completions
// without complete anything.
testCustomCompleterSyncMode.complete('', common.mustCall((error, data) => {
assert.deepStrictEqual(data, [
customCompletions,
'',
]);
}));
// On `a` should output `aaa aa1 aa2` and complete until `aa`.
testCustomCompleterSyncMode.complete('a', common.mustCall((error, data) => {
assert.deepStrictEqual(data, [
'aaa aa1 aa2'.split(' '),
'a',
]);
}));
}
// To test custom completer function.
// Async mode.
{
const customCompletions = 'aaa aa1 aa2 bbb bb1 bb2 bb3 ccc ddd eee'.split(' ');
const testCustomCompleterAsyncMode = repl.start({
prompt: '',
input: putIn,
output: putIn,
completer: function completer(line, callback) {
const hits = customCompletions.filter((c) => c.startsWith(line));
// Show all completions if none found.
callback(null, [hits.length ? hits : customCompletions, line]);
}
});
// On empty line should output all the custom completions
// without complete anything.
testCustomCompleterAsyncMode.complete('', common.mustCall((error, data) => {
assert.deepStrictEqual(data, [
customCompletions,
'',
]);
}));
// On `a` should output `aaa aa1 aa2` and complete until `aa`.
testCustomCompleterAsyncMode.complete('a', common.mustCall((error, data) => {
assert.deepStrictEqual(data, [
'aaa aa1 aa2'.split(' '),
'a',
]);
}));
}