summaryrefslogtreecommitdiff
path: root/test/parallel/test-readline-keys.js
diff options
context:
space:
mode:
authorPrince J Wesley <princejohnwesley@gmail.com>2016-06-23 08:42:57 +0530
committerPrince J Wesley <princejohnwesley@gmail.com>2016-08-17 23:44:39 +0530
commit4b883a3fb452e45f088bd47d8fa5479070ebb620 (patch)
treeaa35473d8986f22960b0740e82c5b4fad854df3d /test/parallel/test-readline-keys.js
parentb417087ca7268de5ec6da503f428ee2ddd38fdfd (diff)
downloadnode-new-4b883a3fb452e45f088bd47d8fa5479070ebb620.tar.gz
readline: keypress trigger for escape character
Fixes: https://github.com/nodejs/node/issues/7379 PR-URL: https://github.com/nodejs/node/pull/7382 Reviewed-By: jasnell - James M Snell <jasnell@gmail.com> Reviewed-By: Roman Reiss <me@silverwind.io>
Diffstat (limited to 'test/parallel/test-readline-keys.js')
-rw-r--r--test/parallel/test-readline-keys.js62
1 files changed, 62 insertions, 0 deletions
diff --git a/test/parallel/test-readline-keys.js b/test/parallel/test-readline-keys.js
index ef9e2eba90..6bbbb2e918 100644
--- a/test/parallel/test-readline-keys.js
+++ b/test/parallel/test-readline-keys.js
@@ -44,6 +44,49 @@ function addTest(sequences, expectedKeys) {
assert.deepStrictEqual(keys, expectedKeys);
}
+// Simulate key interval test cases
+// Returns a function that takes `next` test case and returns a thunk
+// that can be called to run tests in sequence
+// e.g.
+// addKeyIntervalTest(..)
+// (addKeyIntervalTest(..)
+// (addKeyIntervalTest(..)(noop)))()
+// where noop is a terminal function(() => {}).
+
+const addKeyIntervalTest = (sequences, expectedKeys, interval = 550,
+ assertDelay = 550) => {
+ return (next) => () => {
+
+ if (!Array.isArray(sequences)) {
+ sequences = [ sequences ];
+ }
+
+ if (!Array.isArray(expectedKeys)) {
+ expectedKeys = [ expectedKeys ];
+ }
+
+ expectedKeys = expectedKeys.map(function(k) {
+ return k ? extend({ ctrl: false, meta: false, shift: false }, k) : k;
+ });
+
+ const keys = [];
+ fi.on('keypress', (s, k) => keys.push(k));
+
+ const emitKeys = ([head, ...tail]) => {
+ if (head) {
+ fi.write(head);
+ setTimeout(() => emitKeys(tail), interval);
+ } else {
+ setTimeout(() => {
+ next();
+ assert.deepStrictEqual(keys, expectedKeys);
+ }, assertDelay);
+ }
+ };
+ emitKeys(sequences);
+ };
+};
+
// regular alphanumerics
addTest('io.JS', [
{ name: 'i', sequence: 'i' },
@@ -149,3 +192,22 @@ addTest('\x1b[31ma\x1b[39ma', [
{ name: 'undefined', sequence: '\x1b[39m', code: '[39m' },
{ name: 'a', sequence: 'a' },
]);
+
+// Reduce array of addKeyIntervalTest(..) right to left
+// with () => {} as initial function
+const runKeyIntervalTests = [
+ // escape character
+ addKeyIntervalTest('\x1b', [
+ { name: 'escape', sequence: '\x1b', meta: true }
+ ]),
+ // chain of escape characters
+ addKeyIntervalTest('\x1b\x1b\x1b\x1b'.split(''), [
+ { name: 'escape', sequence: '\x1b', meta: true },
+ { name: 'escape', sequence: '\x1b', meta: true },
+ { name: 'escape', sequence: '\x1b', meta: true },
+ { name: 'escape', sequence: '\x1b', meta: true }
+ ])
+].reverse().reduce((acc, fn) => fn(acc), () => {});
+
+// run key interval tests one after another
+runKeyIntervalTests();