u
This commit is contained in:
49
test/lines.test.js
Normal file
49
test/lines.test.js
Normal file
@@ -0,0 +1,49 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Tests for splitLines in src/lines.js.
|
||||
*
|
||||
* Run with: node --test
|
||||
* (or) node --test test/lines.test.js
|
||||
*
|
||||
* The helper must count lines the way editors and `wc -l` do: a trailing
|
||||
* newline terminates the final line rather than starting an extra empty one.
|
||||
*/
|
||||
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
|
||||
import { splitLines } from '../src/lines.js';
|
||||
|
||||
test('splitLines: empty file has no lines', () => {
|
||||
assert.deepEqual(splitLines(''), []);
|
||||
});
|
||||
|
||||
test('splitLines: no trailing newline is unchanged', () => {
|
||||
assert.deepEqual(splitLines('a\nb\nc'), ['a', 'b', 'c']);
|
||||
});
|
||||
|
||||
test('splitLines: trailing newline does not add a phantom line', () => {
|
||||
// The reported bug: this used to yield 4 elements (['a','b','c','']).
|
||||
assert.deepEqual(splitLines('a\nb\nc\n'), ['a', 'b', 'c']);
|
||||
assert.equal(splitLines('a\nb\nc\n').length, 3);
|
||||
});
|
||||
|
||||
test('splitLines: terminated and non-terminated agree on line count', () => {
|
||||
assert.equal(splitLines('a\nb\nc\n').length, splitLines('a\nb\nc').length);
|
||||
});
|
||||
|
||||
test('splitLines: a real trailing blank line is preserved', () => {
|
||||
assert.deepEqual(splitLines('a\n\n'), ['a', '']);
|
||||
assert.equal(splitLines('a\n\n').length, 2);
|
||||
});
|
||||
|
||||
test('splitLines: a lone newline is one empty line', () => {
|
||||
assert.deepEqual(splitLines('\n'), ['']);
|
||||
assert.equal(splitLines('\n').length, 1);
|
||||
});
|
||||
|
||||
test('splitLines: multiple trailing newlines keep the real blank lines', () => {
|
||||
assert.deepEqual(splitLines('a\n\n\n'), ['a', '', '']);
|
||||
assert.equal(splitLines('a\n\n\n').length, 3);
|
||||
});
|
||||
Reference in New Issue
Block a user