|
| 1 | +// Copyright Joyent, Inc. and other Node contributors. |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a |
| 4 | +// copy of this software and associated documentation files (the |
| 5 | +// "Software"), to deal in the Software without restriction, including |
| 6 | +// without limitation the rights to use, copy, modify, merge, publish, |
| 7 | +// distribute, sublicense, and/or sell copies of the Software, and to permit |
| 8 | +// persons to whom the Software is furnished to do so, subject to the |
| 9 | +// following conditions: |
| 10 | +// |
| 11 | +// The above copyright notice and this permission notice shall be included |
| 12 | +// in all copies or substantial portions of the Software. |
| 13 | +// |
| 14 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 15 | +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 16 | +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN |
| 17 | +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 18 | +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 19 | +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
| 20 | +// USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 21 | + |
| 22 | +// Flags: --expose_internals |
| 23 | +'use strict'; |
| 24 | +require('../common'); |
| 25 | + |
| 26 | +// These test cases are in `sequential` rather than the analogous test file in |
| 27 | +// `parallel` because they become unrelaible under load. The unreliability under |
| 28 | +// load was determined empirically when the test cases were in `parallel` by |
| 29 | +// running: |
| 30 | +// tools/test.py -j 96 --repeat 192 test/parallel/test-readline-interface.js |
| 31 | + |
| 32 | +const assert = require('assert'); |
| 33 | +const readline = require('readline'); |
| 34 | +const EventEmitter = require('events').EventEmitter; |
| 35 | +const inherits = require('util').inherits; |
| 36 | + |
| 37 | +function FakeInput() { |
| 38 | + EventEmitter.call(this); |
| 39 | +} |
| 40 | +inherits(FakeInput, EventEmitter); |
| 41 | +FakeInput.prototype.resume = () => {}; |
| 42 | +FakeInput.prototype.pause = () => {}; |
| 43 | +FakeInput.prototype.write = () => {}; |
| 44 | +FakeInput.prototype.end = () => {}; |
| 45 | + |
| 46 | +[ true, false ].forEach(function(terminal) { |
| 47 | + // sending multiple newlines at once that does not end with a new line |
| 48 | + // and a `end` event(last line is) |
| 49 | + |
| 50 | + // \r\n should emit one line event, not two |
| 51 | + { |
| 52 | + const fi = new FakeInput(); |
| 53 | + const rli = new readline.Interface( |
| 54 | + { input: fi, output: fi, terminal: terminal } |
| 55 | + ); |
| 56 | + const expectedLines = ['foo', 'bar', 'baz', 'bat']; |
| 57 | + let callCount = 0; |
| 58 | + rli.on('line', function(line) { |
| 59 | + assert.strictEqual(line, expectedLines[callCount]); |
| 60 | + callCount++; |
| 61 | + }); |
| 62 | + fi.emit('data', expectedLines.join('\r\n')); |
| 63 | + assert.strictEqual(callCount, expectedLines.length - 1); |
| 64 | + rli.close(); |
| 65 | + } |
| 66 | + |
| 67 | + // \r\n should emit one line event when split across multiple writes. |
| 68 | + { |
| 69 | + const fi = new FakeInput(); |
| 70 | + const rli = new readline.Interface( |
| 71 | + { input: fi, output: fi, terminal: terminal } |
| 72 | + ); |
| 73 | + const expectedLines = ['foo', 'bar', 'baz', 'bat']; |
| 74 | + let callCount = 0; |
| 75 | + rli.on('line', function(line) { |
| 76 | + assert.strictEqual(line, expectedLines[callCount]); |
| 77 | + callCount++; |
| 78 | + }); |
| 79 | + expectedLines.forEach(function(line) { |
| 80 | + fi.emit('data', `${line}\r`); |
| 81 | + fi.emit('data', '\n'); |
| 82 | + }); |
| 83 | + assert.strictEqual(callCount, expectedLines.length); |
| 84 | + rli.close(); |
| 85 | + } |
| 86 | +}); |
0 commit comments