0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/test/parallel/test-readline-csi.js
davidmarkclements 31d6cec60e errors: alter ERR_INVALID_CURSOR_POS
Changes the base instance for ERR_INVALID_CURSOR_POS from Error to
TypeError as a more accurate representation of the error.

PR-URL: https://github.com/nodejs/node/pull/19960
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Khaidi Chu <i@2333.moe>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
2018-04-17 14:30:08 +02:00

93 lines
2.3 KiB
JavaScript

// Flags: --expose-internals
'use strict';
const common = require('../common');
const assert = require('assert');
const readline = require('readline');
const { Writable } = require('stream');
const { CSI } = require('internal/readline');
{
assert(CSI);
assert.strictEqual(CSI.kClearToBeginning, '\x1b[1K');
assert.strictEqual(CSI.kClearToEnd, '\x1b[0K');
assert.strictEqual(CSI.kClearLine, '\x1b[2K');
assert.strictEqual(CSI.kClearScreenDown, '\x1b[0J');
assert.strictEqual(CSI`1${2}3`, '\x1b[123');
}
class TestWritable extends Writable {
constructor() {
super();
this.data = '';
}
_write(chunk, encoding, callback) {
this.data += chunk.toString();
callback();
}
}
const writable = new TestWritable();
readline.clearScreenDown(writable);
assert.deepStrictEqual(writable.data, CSI.kClearScreenDown);
writable.data = '';
readline.clearLine(writable, -1);
assert.deepStrictEqual(writable.data, CSI.kClearToBeginning);
writable.data = '';
readline.clearLine(writable, 1);
assert.deepStrictEqual(writable.data, CSI.kClearToEnd);
writable.data = '';
readline.clearLine(writable, 0);
assert.deepStrictEqual(writable.data, CSI.kClearLine);
// Nothing is written when moveCursor 0, 0
[
[0, 0, ''],
[1, 0, '\x1b[1C'],
[-1, 0, '\x1b[1D'],
[0, 1, '\x1b[1B'],
[0, -1, '\x1b[1A'],
[1, 1, '\x1b[1C\x1b[1B'],
[-1, 1, '\x1b[1D\x1b[1B'],
[-1, -1, '\x1b[1D\x1b[1A'],
[1, -1, '\x1b[1C\x1b[1A'],
].forEach((set) => {
writable.data = '';
readline.moveCursor(writable, set[0], set[1]);
assert.deepStrictEqual(writable.data, set[2]);
});
// Undefined or null as stream should not throw.
readline.cursorTo(null);
readline.cursorTo();
writable.data = '';
readline.cursorTo(writable, 'a');
assert.strictEqual(writable.data, '');
writable.data = '';
readline.cursorTo(writable, 'a', 'b');
assert.strictEqual(writable.data, '');
writable.data = '';
common.expectsError(
() => readline.cursorTo(writable, 'a', 1),
{
type: TypeError,
code: 'ERR_INVALID_CURSOR_POS',
message: 'Cannot set cursor row without setting its column'
});
assert.strictEqual(writable.data, '');
writable.data = '';
readline.cursorTo(writable, 1, 'a');
assert.strictEqual(writable.data, '\x1b[2G');
writable.data = '';
readline.cursorTo(writable, 1, 2);
assert.strictEqual(writable.data, '\x1b[3;2H');