|
| 1 | +/****************************************************************************** |
| 2 | + * Copyright 2021 TypeFox GmbH |
| 3 | + * This program and the accompanying materials are made available under the |
| 4 | + * terms of the MIT License, which is available in the project root. |
| 5 | + ******************************************************************************/ |
| 6 | + |
| 7 | +import type { LangiumConfig } from '../../src/package.js'; |
| 8 | +import { EmptyFileSystem, URI, type Grammar } from 'langium'; |
| 9 | +import { describe, expect, test } from 'vitest'; |
| 10 | +import { RelativePath } from '../../src/package.js'; |
| 11 | +import { serializeGrammar } from '../../src/generator/grammar-serializer.js'; |
| 12 | +import { createLangiumGrammarServices } from 'langium/grammar'; |
| 13 | +import { expandToString } from 'langium/generate'; |
| 14 | + |
| 15 | +const grammarServices = createLangiumGrammarServices(EmptyFileSystem); |
| 16 | + |
| 17 | +describe('Grammar serializer', () => { |
| 18 | + test('should include comments of AST elements', async () => { |
| 19 | + // arrange |
| 20 | + const config: LangiumConfig = { |
| 21 | + projectName: 'Magic', |
| 22 | + languages: [], |
| 23 | + [RelativePath]: '/path/to/magic', |
| 24 | + }; |
| 25 | + |
| 26 | + const grammarText = expandToString` |
| 27 | + grammar Test |
| 28 | +
|
| 29 | + /** |
| 30 | + * This is the entry rule |
| 31 | + */ |
| 32 | + entry Model: /** This is the name assignment */ name='test'; |
| 33 | + `; |
| 34 | + |
| 35 | + const document = grammarServices.shared.workspace.LangiumDocumentFactory.fromString<Grammar>(grammarText, URI.file('test.langium')); |
| 36 | + grammarServices.shared.workspace.LangiumDocuments.addDocument(document); |
| 37 | + await grammarServices.shared.workspace.DocumentBuilder.build([document]); |
| 38 | + const grammar = document.parseResult.value; |
| 39 | + |
| 40 | + // act |
| 41 | + const moduleString = serializeGrammar(grammarServices.grammar, [grammar], config); |
| 42 | + |
| 43 | + // assert |
| 44 | + expect(moduleString).toMatch('"$comment": "/** This is the name assignment */"'); |
| 45 | + expect(moduleString).toMatch('"$comment": "/**\\\\n * This is the entry rule\\\\n */"'); |
| 46 | + }); |
| 47 | + |
| 48 | +}); |
0 commit comments