From 0fba741e5394d840fa2ac249793c51f77a680c32 Mon Sep 17 00:00:00 2001 From: Masashi Hirano Date: Mon, 14 May 2018 00:22:27 +0900 Subject: [PATCH 1/2] test: improve coverage for readline.Interface Add tests in parallel/test-readline-interface to increase coverage --- test/parallel/test-readline-interface.js | 171 +++++++++++++++++++++++ 1 file changed, 171 insertions(+) diff --git a/test/parallel/test-readline-interface.js b/test/parallel/test-readline-interface.js index 30afba843a2d98..90a9544d544a2d 100644 --- a/test/parallel/test-readline-interface.js +++ b/test/parallel/test-readline-interface.js @@ -613,6 +613,43 @@ function isWarned(emitter) { rli.close(); } + { + // Back and Forward one character + const fi = new FakeInput(); + const rli = new readline.Interface({ + input: fi, + output: fi, + prompt: '', + terminal: terminal + }); + fi.emit('data', 'the quick brown fox'); + let cursorPos = rli._getCursorPos(); + assert.strictEqual(cursorPos.rows, 0); + assert.strictEqual(cursorPos.cols, 19); + + // back one character + fi.emit('keypress', '.', { ctrl: true, name: 'b' }); + cursorPos = rli._getCursorPos(); + assert.strictEqual(cursorPos.rows, 0); + assert.strictEqual(cursorPos.cols, 18); + // back one character + fi.emit('keypress', '.', { ctrl: true, name: 'b' }); + cursorPos = rli._getCursorPos(); + assert.strictEqual(cursorPos.rows, 0); + assert.strictEqual(cursorPos.cols, 17); + // forward one character + fi.emit('keypress', '.', { ctrl: true, name: 'f' }); + cursorPos = rli._getCursorPos(); + assert.strictEqual(cursorPos.rows, 0); + assert.strictEqual(cursorPos.cols, 18); + // forward one character + fi.emit('keypress', '.', { ctrl: true, name: 'f' }); + cursorPos = rli._getCursorPos(); + assert.strictEqual(cursorPos.rows, 0); + assert.strictEqual(cursorPos.cols, 19); + rli.close(); + } + { // `wordLeft` and `wordRight` const fi = new FakeInput(); @@ -728,6 +765,117 @@ function isWarned(emitter) { }); } + // deleteLeft + { + const fi = new FakeInput(); + const rli = new readline.Interface({ + input: fi, + output: fi, + prompt: '', + terminal: terminal + }); + fi.emit('data', 'the quick brown fox'); + let cursorPos = rli._getCursorPos(); + assert.strictEqual(cursorPos.rows, 0); + assert.strictEqual(cursorPos.cols, 19); + + // deleteLeft + fi.emit('keypress', '.', { ctrl: true, name: 'h' }); + cursorPos = rli._getCursorPos(); + assert.strictEqual(cursorPos.rows, 0); + assert.strictEqual(cursorPos.cols, 18); + rli.on('line', common.mustCall((line) => { + assert.strictEqual(line, 'the quick brown fo'); + })); + fi.emit('data', '\n'); + rli.close(); + } + + // deleteRight + { + const fi = new FakeInput(); + const rli = new readline.Interface({ + input: fi, + output: fi, + prompt: '', + terminal: terminal + }); + fi.emit('data', 'the quick brown fox'); + + // go to the start of the line + fi.emit('keypress', '.', { ctrl: true, name: 'a' }); + let cursorPos = rli._getCursorPos(); + assert.strictEqual(cursorPos.rows, 0); + assert.strictEqual(cursorPos.cols, 0); + + // `deleteLeft` + fi.emit('keypress', '.', { ctrl: true, name: 'd' }); + cursorPos = rli._getCursorPos(); + assert.strictEqual(cursorPos.rows, 0); + assert.strictEqual(cursorPos.cols, 0); + rli.on('line', common.mustCall((line) => { + assert.strictEqual(line, 'he quick brown fox'); + })); + fi.emit('data', '\n'); + rli.close(); + } + + + // deleteLineLeft + { + const fi = new FakeInput(); + const rli = new readline.Interface({ + input: fi, + output: fi, + prompt: '', + terminal: terminal + }); + fi.emit('data', 'the quick brown fox'); + let cursorPos = rli._getCursorPos(); + assert.strictEqual(cursorPos.rows, 0); + assert.strictEqual(cursorPos.cols, 19); + + // delete from current to start of line + fi.emit('keypress', '.', { ctrl: true, shift: true, name: 'backspace' }); + cursorPos = rli._getCursorPos(); + assert.strictEqual(cursorPos.rows, 0); + assert.strictEqual(cursorPos.cols, 0); + rli.on('line', common.mustCall((line) => { + assert.strictEqual(line, ''); + })); + fi.emit('data', '\n'); + rli.close(); + } + + // deleteLineRight + { + const fi = new FakeInput(); + const rli = new readline.Interface({ + input: fi, + output: fi, + prompt: '', + terminal: terminal + }); + fi.emit('data', 'the quick brown fox'); + + // go to the start of the line + fi.emit('keypress', '.', { ctrl: true, name: 'a' }); + let cursorPos = rli._getCursorPos(); + assert.strictEqual(cursorPos.rows, 0); + assert.strictEqual(cursorPos.cols, 0); + + // delete from current to end of line + fi.emit('keypress', '.', { ctrl: true, shift: true, name: 'delete' }); + cursorPos = rli._getCursorPos(); + assert.strictEqual(cursorPos.rows, 0); + assert.strictEqual(cursorPos.cols, 0); + rli.on('line', common.mustCall((line) => { + assert.strictEqual(line, ''); + })); + fi.emit('data', '\n'); + rli.close(); + } + // multi-line cursor position { const fi = new FakeInput(); @@ -742,6 +890,29 @@ function isWarned(emitter) { const cursorPos = rli._getCursorPos(); assert.strictEqual(cursorPos.rows, 1); assert.strictEqual(cursorPos.cols, 5); + rli.close(); + } + + // clear the whole screen + { + const fi = new FakeInput(); + const rli = new readline.Interface({ + input: fi, + output: fi, + prompt: '', + terminal: terminal + }); + const lines = ['line 1', 'line 2', 'line 3']; + fi.emit('data', lines.join('\n')); + fi.emit('keypress', '.', { ctrl: true, name: 'l' }); + const cursorPos = rli._getCursorPos(); + assert.strictEqual(cursorPos.rows, 0); + assert.strictEqual(cursorPos.cols, 6); + rli.on('line', common.mustCall((line) => { + assert.strictEqual(line, 'line 3'); + })); + fi.emit('data', '\n'); + rli.close(); } } From 5e8d19b090c722d807625dd9352cca38fa598e1c Mon Sep 17 00:00:00 2001 From: Masashi Hirano Date: Wed, 16 May 2018 21:09:34 +0900 Subject: [PATCH 2/2] test: use caps for the first letter in each comment --- test/parallel/test-readline-interface.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/test/parallel/test-readline-interface.js b/test/parallel/test-readline-interface.js index 90a9544d544a2d..0550019e64f46f 100644 --- a/test/parallel/test-readline-interface.js +++ b/test/parallel/test-readline-interface.js @@ -627,22 +627,22 @@ function isWarned(emitter) { assert.strictEqual(cursorPos.rows, 0); assert.strictEqual(cursorPos.cols, 19); - // back one character + // Back one character fi.emit('keypress', '.', { ctrl: true, name: 'b' }); cursorPos = rli._getCursorPos(); assert.strictEqual(cursorPos.rows, 0); assert.strictEqual(cursorPos.cols, 18); - // back one character + // Back one character fi.emit('keypress', '.', { ctrl: true, name: 'b' }); cursorPos = rli._getCursorPos(); assert.strictEqual(cursorPos.rows, 0); assert.strictEqual(cursorPos.cols, 17); - // forward one character + // Forward one character fi.emit('keypress', '.', { ctrl: true, name: 'f' }); cursorPos = rli._getCursorPos(); assert.strictEqual(cursorPos.rows, 0); assert.strictEqual(cursorPos.cols, 18); - // forward one character + // Forward one character fi.emit('keypress', '.', { ctrl: true, name: 'f' }); cursorPos = rli._getCursorPos(); assert.strictEqual(cursorPos.rows, 0); @@ -779,7 +779,7 @@ function isWarned(emitter) { assert.strictEqual(cursorPos.rows, 0); assert.strictEqual(cursorPos.cols, 19); - // deleteLeft + // Delete left character fi.emit('keypress', '.', { ctrl: true, name: 'h' }); cursorPos = rli._getCursorPos(); assert.strictEqual(cursorPos.rows, 0); @@ -802,13 +802,13 @@ function isWarned(emitter) { }); fi.emit('data', 'the quick brown fox'); - // go to the start of the line + // Go to the start of the line fi.emit('keypress', '.', { ctrl: true, name: 'a' }); let cursorPos = rli._getCursorPos(); assert.strictEqual(cursorPos.rows, 0); assert.strictEqual(cursorPos.cols, 0); - // `deleteLeft` + // Delete right character fi.emit('keypress', '.', { ctrl: true, name: 'd' }); cursorPos = rli._getCursorPos(); assert.strictEqual(cursorPos.rows, 0); @@ -835,7 +835,7 @@ function isWarned(emitter) { assert.strictEqual(cursorPos.rows, 0); assert.strictEqual(cursorPos.cols, 19); - // delete from current to start of line + // Delete from current to start of line fi.emit('keypress', '.', { ctrl: true, shift: true, name: 'backspace' }); cursorPos = rli._getCursorPos(); assert.strictEqual(cursorPos.rows, 0); @@ -858,13 +858,13 @@ function isWarned(emitter) { }); fi.emit('data', 'the quick brown fox'); - // go to the start of the line + // Go to the start of the line fi.emit('keypress', '.', { ctrl: true, name: 'a' }); let cursorPos = rli._getCursorPos(); assert.strictEqual(cursorPos.rows, 0); assert.strictEqual(cursorPos.cols, 0); - // delete from current to end of line + // Delete from current to end of line fi.emit('keypress', '.', { ctrl: true, shift: true, name: 'delete' }); cursorPos = rli._getCursorPos(); assert.strictEqual(cursorPos.rows, 0); @@ -893,7 +893,7 @@ function isWarned(emitter) { rli.close(); } - // clear the whole screen + // Clear the whole screen { const fi = new FakeInput(); const rli = new readline.Interface({