Skip to content

Commit

Permalink
Add tests for grammar formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
msujew committed Sep 26, 2023
1 parent 8a8c650 commit 049c4b7
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/langium/src/generator/template-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export function expandToString(staticParts: TemplateStringsArray, ...substitutio
}

export const SNLE = Object.freeze('__«SKIP^NEW^LINE^IF^EMPTY»__');
export const NEWLINE_REGEXP = /\r?\n/g;
export const NEWLINE_REGEXP = /\r?\n/gm;
const nonWhitespace = /\S|$/;

// add the alignment of the previous static part to all lines of the following substitution
Expand Down
3 changes: 2 additions & 1 deletion packages/langium/src/test/langium-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import * as assert from 'node:assert';
import { stream } from '../utils/stream.js';
import type { AsyncDisposable } from '../utils/disposable.js';
import { Disposable } from '../utils/disposable.js';
import { normalizeEOL } from '../generator/template-string.js';

export interface ParseHelperOptions extends BuildOptions {
/**
Expand Down Expand Up @@ -479,7 +480,7 @@ export function expectFormatting(services: LangiumServices): (expectedFormatting
formatter.formatDocument(document, { options, textDocument: identifier }));

const editedDocument = TextDocument.applyEdits(document.textDocument, edits);
expectedFunction(editedDocument, expectedFormatting.after);
expectedFunction(normalizeEOL(editedDocument), normalizeEOL(expectedFormatting.after));

const disposable = Disposable.create(() => clearDocuments(services, [document]));
if (expectedFormatting.disposeAfterCheck) {
Expand Down
63 changes: 63 additions & 0 deletions packages/langium/test/grammar/lsp/grammar-formatter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/******************************************************************************
* Copyright 2023 TypeFox GmbH
* This program and the accompanying materials are made available under the
* terms of the MIT License, which is available in the project root.
******************************************************************************/

import { describe, test } from 'vitest';
import { EmptyFileSystem, createLangiumGrammarServices, expandToString } from 'langium';
import { expectFormatting } from 'langium/test';

const services = createLangiumGrammarServices(EmptyFileSystem);
const formatting = expectFormatting(services.grammar);

describe('Grammar Formatter', () => {

test('Indents interface properties', async () => {
await formatting({
before: expandToString`
interface Test {
// This is a comment
a: string
b: number
// This is another comment
c: boolean
}
`,
after: expandToString`
interface Test {
// This is a comment
a: string
b: number
// This is another comment
c: boolean
}
`
});
});

test('Formats interface extends references', async () => {
await formatting({
before: expandToString`
interface A extends B,C, D,E{}
`,
after: expandToString`
interface A extends B, C, D, E {
}
`
});
});

test('Formats union type definitions', async () => {
await formatting({
before: expandToString`
type A= B | C | D
;
`,
after: expandToString`
type A = B | C | D;
`
});
});

});

0 comments on commit 049c4b7

Please sign in to comment.