@@ -873,3 +873,80 @@ function isWarned(emitter) { |
873 | 873 | assert.strictEqual(rl._prompt, '$ '); |
874 | 874 | } |
875 | 875 | }); |
| 876 | + |
| 877 | +// For the purposes of the following tests, we do not care about the exact |
| 878 | +// value of crlfDelay, only that the behaviour conforms to what's expected. |
| 879 | +// Setting it to Infinity allows the test to succeed even under extreme |
| 880 | +// CPU stress. |
| 881 | +const crlfDelay = Infinity; |
| 882 | + |
| 883 | +[ true, false ].forEach(function(terminal) { |
| 884 | + // sending multiple newlines at once that does not end with a new line |
| 885 | + // and a `end` event(last line is) |
| 886 | + |
| 887 | + // \r\n should emit one line event, not two |
| 888 | + { |
| 889 | + const fi = new FakeInput(); |
| 890 | + const rli = new readline.Interface( |
| 891 | + { |
| 892 | + input: fi, |
| 893 | + output: fi, |
| 894 | + terminal: terminal, |
| 895 | + crlfDelay |
| 896 | + } |
| 897 | + ); |
| 898 | + const expectedLines = ['foo', 'bar', 'baz', 'bat']; |
| 899 | + let callCount = 0; |
| 900 | + rli.on('line', function(line) { |
| 901 | + assert.strictEqual(line, expectedLines[callCount]); |
| 902 | + callCount++; |
| 903 | + }); |
| 904 | + fi.emit('data', expectedLines.join('\r\n')); |
| 905 | + assert.strictEqual(callCount, expectedLines.length - 1); |
| 906 | + rli.close(); |
| 907 | + } |
| 908 | + |
| 909 | + // \r\n should emit one line event when split across multiple writes. |
| 910 | + { |
| 911 | + const fi = new FakeInput(); |
| 912 | + const rli = new readline.Interface({ |
| 913 | + input: fi, |
| 914 | + output: fi, |
| 915 | + terminal: terminal, |
| 916 | + crlfDelay |
| 917 | + }); |
| 918 | + const expectedLines = ['foo', 'bar', 'baz', 'bat']; |
| 919 | + let callCount = 0; |
| 920 | + rli.on('line', function(line) { |
| 921 | + assert.strictEqual(line, expectedLines[callCount]); |
| 922 | + callCount++; |
| 923 | + }); |
| 924 | + expectedLines.forEach(function(line) { |
| 925 | + fi.emit('data', `${line}\r`); |
| 926 | + fi.emit('data', '\n'); |
| 927 | + }); |
| 928 | + assert.strictEqual(callCount, expectedLines.length); |
| 929 | + rli.close(); |
| 930 | + } |
| 931 | + |
| 932 | + // Emit one line event when the delay between \r and \n is |
| 933 | + // over the default crlfDelay but within the setting value. |
| 934 | + { |
| 935 | + const fi = new FakeInput(); |
| 936 | + const delay = 125; |
| 937 | + const rli = new readline.Interface({ |
| 938 | + input: fi, |
| 939 | + output: fi, |
| 940 | + terminal: terminal, |
| 941 | + crlfDelay |
| 942 | + }); |
| 943 | + let callCount = 0; |
| 944 | + rli.on('line', () => callCount++); |
| 945 | + fi.emit('data', '\r'); |
| 946 | + setTimeout(common.mustCall(() => { |
| 947 | + fi.emit('data', '\n'); |
| 948 | + assert.strictEqual(callCount, 1); |
| 949 | + rli.close(); |
| 950 | + }), delay); |
| 951 | + } |
| 952 | +}); |