-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
generator-langium: extended yeoman generator extension to offer parsi…
…ng, linking, and validation test stubs (#1282) * providing an additional automated test running 'npm test' and checking its proper termination
- Loading branch information
1 parent
ed5c055
commit 2ca2b3a
Showing
16 changed files
with
444 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"devDependencies": { | ||
"vitest": "~1.0.0" | ||
}, | ||
"scripts": { | ||
"test": "vitest run" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"compilerOptions": { | ||
"rootDir": ".", | ||
"noEmit": true | ||
}, | ||
"include": [ | ||
"src/**/*.ts", | ||
"test/**/*.ts" | ||
] | ||
} | ||
|
11 changes: 11 additions & 0 deletions
11
packages/generator-langium/templates/test/.vscode-extensions.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
// See https://go.microsoft.com/fwlink/?LinkId=827846 to learn about workspace recommendations. | ||
// Extension identifier format: ${publisher}.${name}. Example: vscode.csharp | ||
|
||
// List of extensions which should be recommended for users of this workspace. | ||
"recommendations": [ | ||
"langium.langium-vscode", | ||
"ZixuanChen.vitest-explorer", | ||
"kingwl.vscode-vitest-runner" | ||
] | ||
} |
53 changes: 53 additions & 0 deletions
53
packages/generator-langium/templates/test/test/linking/linking.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { afterEach, beforeAll, describe, expect, test } from "vitest"; | ||
import { EmptyFileSystem, type LangiumDocument } from "langium"; | ||
import { expandToString as s } from "langium/generate"; | ||
import { clearDocuments, parseHelper } from "langium/test"; | ||
import { create<%= LanguageName %>Services } from "../../src/language/<%= language-id %>-module.js"; | ||
import { Model, isModel } from "../../src/language/generated/ast.js"; | ||
|
||
let services: ReturnType<typeof create<%= LanguageName %>Services>; | ||
let parse: ReturnType<typeof parseHelper<Model>>; | ||
let document: LangiumDocument<Model> | undefined; | ||
|
||
beforeAll(async () => { | ||
services = create<%= LanguageName %>Services(EmptyFileSystem); | ||
parse = parseHelper<Model>(services.<%= LanguageName %>); | ||
|
||
// activate the following if your linking test requires elements from a built-in library, for example | ||
// await services.shared.workspace.WorkspaceManager.initializeWorkspace([]); | ||
}); | ||
|
||
afterEach(async () => { | ||
document && clearDocuments(services.shared, [ document ]); | ||
}); | ||
|
||
describe('Linking tests', () => { | ||
|
||
test('linking of greetings', async () => { | ||
document = await parse(` | ||
person Langium | ||
Hello Langium! | ||
`); | ||
|
||
expect( | ||
// here we first check for validity of the parsed document object by means of the reusable function | ||
// 'checkDocumentValid()' to sort out (critical) typos first, | ||
// and then evaluate the cross references we're interested in by checking | ||
// the referenced AST element as well as for a potential error message; | ||
checkDocumentValid(document) | ||
|| document.parseResult.value.greetings.map(g => g.person.ref?.name || g.person.error?.message).join('\n') | ||
).toBe(s` | ||
Langium | ||
`); | ||
}); | ||
}); | ||
|
||
function checkDocumentValid(document: LangiumDocument): string | undefined { | ||
return document.parseResult.parserErrors.length && s` | ||
Parser errors: | ||
${document.parseResult.parserErrors.map(e => e.message).join('\n ')} | ||
` | ||
|| document.parseResult.value === undefined && `ParseResult is 'undefined'.` | ||
|| !isModel(document.parseResult.value) && `Root AST object is a ${document.parseResult.value.$type}, expected a '${Model}'.` | ||
|| undefined; | ||
} |
Oops, something went wrong.