summaryrefslogtreecommitdiff
path: root/test/parallel/test-repl-tab-complete.js
diff options
context:
space:
mode:
authorDiosney Sarmiento <diosney.s@gmail.com>2016-07-03 18:29:34 -0400
committerLance Ball <lball@redhat.com>2016-07-08 16:47:05 -0400
commit9fbe456db15a63409916c207cc1157321a307409 (patch)
treee0b107b52b1bf71617a86378b32a508cd497c611 /test/parallel/test-repl-tab-complete.js
parent18ae74cf9802ff2a65f87262c71d00ba8d68d133 (diff)
downloadandroid-node-v8-9fbe456db15a63409916c207cc1157321a307409.tar.gz
android-node-v8-9fbe456db15a63409916c207cc1157321a307409.tar.bz2
android-node-v8-9fbe456db15a63409916c207cc1157321a307409.zip
repl: add support for custom completions
Allow user code to override the default `complete()` function from `readline.Interface`. See: https://nodejs.org/api/readline.html#readline_use_of_the_completer_function Ref: https://github.com/nodejs/node-v0.x-archive/pull/8484 PR-URL: https://github.com/nodejs/node/pull/7527 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Lance Ball <lball@redhat.com>
Diffstat (limited to 'test/parallel/test-repl-tab-complete.js')
-rw-r--r--test/parallel/test-repl-tab-complete.js67
1 files changed, 66 insertions, 1 deletions
diff --git a/test/parallel/test-repl-tab-complete.js b/test/parallel/test-repl-tab-complete.js
index 3d32d8e0ce..73a3fd148b 100644
--- a/test/parallel/test-repl-tab-complete.js
+++ b/test/parallel/test-repl-tab-complete.js
@@ -32,7 +32,7 @@ testMe.complete('console.lo', common.mustCall(function(error, data) {
assert.deepStrictEqual(data, [['console.log'], 'console.lo']);
}));
-// Tab Complete will return globaly scoped variables
+// Tab Complete will return globally scoped variables
putIn.run(['};']);
testMe.complete('inner.o', common.mustCall(function(error, data) {
assert.deepStrictEqual(data, works);
@@ -283,3 +283,68 @@ if (typeof Intl === 'object') {
testNonGlobal.complete('I', common.mustCall((error, data) => {
assert.deepStrictEqual(data, builtins);
}));
+
+// 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 completerSyncMode(line) {
+ const hits = customCompletions.filter((c) => {
+ return c.indexOf(line) === 0;
+ });
+ // 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 testCustomCompleterAsyncMode = repl.start({
+ prompt: '',
+ input: putIn,
+ output: putIn,
+ completer: function completerAsyncMode(line, callback) {
+ const hits = customCompletions.filter((c) => {
+ return c.indexOf(line) === 0;
+ });
+ // 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'
+ ]);
+}));