Updated code format tests to be OS-agnostic

- Test results for formatCode.ts are now designed to match irrespective of the system's line separators: The previously used multi-line template strings created UNIX-style line separators, which do not match on Windows systems. Therefore, line endings are now explicitly written into the expected output string, unfortunately affecting readability.
- Inside `formatCode`, input strings are now split into lines by any valid line separator. Previously, only the system's line separators have been considered, which may produce some unexpected results when receiving line endings from other systems.
This commit is contained in:
Cajetan Grill 2023-07-28 11:08:46 +02:00
parent af01569d6b
commit e0596a22a0
2 changed files with 4 additions and 9 deletions

View File

@ -1,3 +1,4 @@
import {EOL} from 'os';
import { formatCode } from './formatCode';
const input1 = `{ foo: true }`;
@ -13,20 +14,14 @@ foo: true,
bar: 123
}`;
const output3 = `{
\tfoo: true,
\tbar: 123
}`;
const output3 = `{${EOL}\tfoo: true,${EOL}\tbar: 123${EOL}}`;
const input4 = `{
\t\t\t\tfoo: true,
\t\t\t\tbar: 123
}`;
const output4 = `{
\tfoo: true,
\tbar: 123
}`;
const output4 = `{${EOL}\tfoo: true,${EOL}\tbar: 123${EOL}}`;
describe('format', () => {
it('should produce correct result', () => {

View File

@ -2,7 +2,7 @@ import { EOL } from 'os';
export const formatCode = (s: string): string => {
let indent: number = 0;
let lines = s.split(EOL);
let lines = s.split(/[\r\n]+/);
lines = lines.map(line => {
line = line.trim().replace(/^\*/g, ' *');
let i = indent;