mirror of
https://github.com/nodejs/node.git
synced 2025-05-01 17:03:34 +00:00

The m flag has no effect on regular expressions that don't match the start or the end of a line. PR-URL: https://github.com/nodejs/node/pull/38124 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
78 lines
2.0 KiB
JavaScript
78 lines
2.0 KiB
JavaScript
'use strict';
|
|
|
|
const common = require('../common');
|
|
const fs = require('fs');
|
|
const { join } = require('path');
|
|
const readline = require('readline');
|
|
const assert = require('assert');
|
|
|
|
const tmpdir = require('../common/tmpdir');
|
|
tmpdir.refresh();
|
|
|
|
const filename = join(tmpdir.path, 'test.txt');
|
|
|
|
const testContents = [
|
|
'',
|
|
'\n',
|
|
'line 1',
|
|
'line 1\nline 2 南越国是前203年至前111年存在于岭南地区的一个国家\nline 3\ntrailing',
|
|
'line 1\nline 2\nline 3 ends with newline\n',
|
|
];
|
|
|
|
async function testSimple() {
|
|
for (const fileContent of testContents) {
|
|
fs.writeFileSync(filename, fileContent);
|
|
|
|
const readable = fs.createReadStream(filename);
|
|
const rli = readline.createInterface({
|
|
input: readable,
|
|
crlfDelay: Infinity
|
|
});
|
|
|
|
const iteratedLines = [];
|
|
for await (const k of rli) {
|
|
iteratedLines.push(k);
|
|
}
|
|
|
|
const expectedLines = fileContent.split('\n');
|
|
if (expectedLines[expectedLines.length - 1] === '') {
|
|
expectedLines.pop();
|
|
}
|
|
assert.deepStrictEqual(iteratedLines, expectedLines);
|
|
assert.strictEqual(iteratedLines.join(''), fileContent.replace(/\n/g, ''));
|
|
}
|
|
}
|
|
|
|
async function testMutual() {
|
|
for (const fileContent of testContents) {
|
|
fs.writeFileSync(filename, fileContent);
|
|
|
|
const readable = fs.createReadStream(filename);
|
|
const rli = readline.createInterface({
|
|
input: readable,
|
|
crlfDelay: Infinity
|
|
});
|
|
|
|
const expectedLines = fileContent.split('\n');
|
|
if (expectedLines[expectedLines.length - 1] === '') {
|
|
expectedLines.pop();
|
|
}
|
|
const iteratedLines = [];
|
|
let iterated = false;
|
|
for await (const k of rli) {
|
|
// This outer loop should only iterate once.
|
|
assert.strictEqual(iterated, false);
|
|
iterated = true;
|
|
|
|
iteratedLines.push(k);
|
|
for await (const l of rli) {
|
|
iteratedLines.push(l);
|
|
}
|
|
assert.deepStrictEqual(iteratedLines, expectedLines);
|
|
}
|
|
assert.deepStrictEqual(iteratedLines, expectedLines);
|
|
}
|
|
}
|
|
|
|
testSimple().then(testMutual).then(common.mustCall());
|