|
| 1 | +// Flags: --expose-internals |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +const common = require('../common'); |
| 5 | +const assert = require('assert'); |
| 6 | +const readline = require('readline/promises'); |
| 7 | +const { Writable } = require('stream'); |
| 8 | +const { CSI } = require('internal/readline/utils'); |
| 9 | + |
| 10 | +class TestWritable extends Writable { |
| 11 | + constructor() { |
| 12 | + super(); |
| 13 | + this.data = ''; |
| 14 | + } |
| 15 | + _write(chunk, encoding, callback) { |
| 16 | + this.data += chunk.toString(); |
| 17 | + callback(); |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +const writable = new TestWritable(); |
| 22 | + |
| 23 | +readline.clearScreenDown(writable).then(common.mustCall()); |
| 24 | +assert.deepStrictEqual(writable.data, CSI.kClearScreenDown); |
| 25 | +readline.clearScreenDown(writable).then(common.mustCall()); |
| 26 | + |
| 27 | +readline.clearScreenDown(writable, null); |
| 28 | + |
| 29 | +// Verify that clearScreenDown() does not throw on null or undefined stream. |
| 30 | +readline.clearScreenDown(null).then(common.mustCall()); |
| 31 | +readline.clearScreenDown(undefined).then(common.mustCall()); |
| 32 | + |
| 33 | +writable.data = ''; |
| 34 | +readline.clearLine(writable, -1).then(common.mustCall()); |
| 35 | +assert.deepStrictEqual(writable.data, CSI.kClearToLineBeginning); |
| 36 | + |
| 37 | +writable.data = ''; |
| 38 | +readline.clearLine(writable, 1).then(common.mustCall()); |
| 39 | +assert.deepStrictEqual(writable.data, CSI.kClearToLineEnd); |
| 40 | + |
| 41 | +writable.data = ''; |
| 42 | +readline.clearLine(writable, 0).then(common.mustCall()); |
| 43 | +assert.deepStrictEqual(writable.data, CSI.kClearLine); |
| 44 | + |
| 45 | +writable.data = ''; |
| 46 | +readline.clearLine(writable, -1).then(common.mustCall()); |
| 47 | +assert.deepStrictEqual(writable.data, CSI.kClearToLineBeginning); |
| 48 | + |
| 49 | +readline.clearLine(writable, 0, null).then(common.mustCall()); |
| 50 | + |
| 51 | +// Verify that clearLine() does not throw on null or undefined stream. |
| 52 | +readline.clearLine(null, 0).then(common.mustCall()); |
| 53 | +readline.clearLine(undefined, 0).then(common.mustCall()); |
| 54 | +readline.clearLine(null, 0).then(common.mustCall()); |
| 55 | +readline.clearLine(undefined, 0).then(common.mustCall()); |
| 56 | + |
| 57 | +// Nothing is written when moveCursor 0, 0 |
| 58 | +[ |
| 59 | + [0, 0, ''], |
| 60 | + [1, 0, '\x1b[1C'], |
| 61 | + [-1, 0, '\x1b[1D'], |
| 62 | + [0, 1, '\x1b[1B'], |
| 63 | + [0, -1, '\x1b[1A'], |
| 64 | + [1, 1, '\x1b[1C\x1b[1B'], |
| 65 | + [-1, 1, '\x1b[1D\x1b[1B'], |
| 66 | + [-1, -1, '\x1b[1D\x1b[1A'], |
| 67 | + [1, -1, '\x1b[1C\x1b[1A'], |
| 68 | +].forEach((set) => { |
| 69 | + writable.data = ''; |
| 70 | + readline.moveCursor(writable, set[0], set[1]).then(common.mustCall()); |
| 71 | + assert.deepStrictEqual(writable.data, set[2]); |
| 72 | + writable.data = ''; |
| 73 | + readline.moveCursor(writable, set[0], set[1]).then(common.mustCall()); |
| 74 | + assert.deepStrictEqual(writable.data, set[2]); |
| 75 | +}); |
| 76 | + |
| 77 | +readline.moveCursor(writable, 1, 1, null).then(common.mustCall()); |
| 78 | + |
| 79 | +// Verify that moveCursor() does not reject on null or undefined stream. |
| 80 | +readline.moveCursor(null, 1, 1).then(common.mustCall()); |
| 81 | +readline.moveCursor(undefined, 1, 1).then(common.mustCall()); |
| 82 | +readline.moveCursor(null, 1, 1).then(common.mustCall()); |
| 83 | +readline.moveCursor(undefined, 1, 1).then(common.mustCall()); |
| 84 | + |
| 85 | +// Undefined or null as stream should not throw. |
| 86 | +readline.cursorTo(null).then(common.mustCall()); |
| 87 | +readline.cursorTo().then(common.mustCall()); |
| 88 | +readline.cursorTo(null, 1, 1).then(common.mustCall()); |
| 89 | +readline.cursorTo(undefined, 1, 1).then(common.mustCall()); |
| 90 | + |
| 91 | +writable.data = ''; |
| 92 | +readline.cursorTo(writable, 'a').then(common.mustCall()); |
| 93 | +assert.strictEqual(writable.data, ''); |
| 94 | + |
| 95 | +writable.data = ''; |
| 96 | +readline.cursorTo(writable, 'a', 'b').then(common.mustCall()); |
| 97 | +assert.strictEqual(writable.data, ''); |
| 98 | + |
| 99 | +writable.data = ''; |
| 100 | +assert.rejects( |
| 101 | + () => readline.cursorTo(writable, 'a', 1), |
| 102 | + { |
| 103 | + name: 'TypeError', |
| 104 | + code: 'ERR_INVALID_CURSOR_POS', |
| 105 | + message: 'Cannot set cursor row without setting its column' |
| 106 | + }).then(common.mustCall()); |
| 107 | +assert.strictEqual(writable.data, ''); |
| 108 | + |
| 109 | +writable.data = ''; |
| 110 | +readline.cursorTo(writable, 1, 'a').then(common.mustCall()); |
| 111 | +assert.strictEqual(writable.data, '\x1b[2G'); |
| 112 | + |
| 113 | +writable.data = ''; |
| 114 | +readline.cursorTo(writable, 1).then(common.mustCall()); |
| 115 | +assert.strictEqual(writable.data, '\x1b[2G'); |
| 116 | + |
| 117 | +writable.data = ''; |
| 118 | +readline.cursorTo(writable, 1, 2).then(common.mustCall()); |
| 119 | +assert.strictEqual(writable.data, '\x1b[3;2H'); |
| 120 | + |
| 121 | +writable.data = ''; |
| 122 | +readline.cursorTo(writable, 1, 2).then(common.mustCall()); |
| 123 | +assert.strictEqual(writable.data, '\x1b[3;2H'); |
| 124 | + |
| 125 | +writable.data = ''; |
| 126 | +readline.cursorTo(writable, 1).then(common.mustCall()); |
| 127 | +assert.strictEqual(writable.data, '\x1b[2G'); |
| 128 | + |
| 129 | +// Verify that cursorTo() rejects if x or y is NaN. |
| 130 | +assert.rejects(() => readline.cursorTo(writable, NaN), |
| 131 | + { code: 'ERR_INVALID_ARG_VALUE' }).then(common.mustCall()); |
| 132 | + |
| 133 | +assert.rejects(() => readline.cursorTo(writable, 1, NaN), |
| 134 | + { code: 'ERR_INVALID_ARG_VALUE' }).then(common.mustCall()); |
| 135 | + |
| 136 | +assert.rejects(() => readline.cursorTo(writable, NaN, NaN), |
| 137 | + { code: 'ERR_INVALID_ARG_VALUE' }).then(common.mustCall()); |
0 commit comments