-
Notifications
You must be signed in to change notification settings - Fork 76
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
4a963b5
commit 7b0a010
Showing
12 changed files
with
407 additions
and
67 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"devDependencies": { | ||
"vitest": "0.34" | ||
}, | ||
"scripts": { | ||
"build": "tsc -b tsconfig.src.json", | ||
"watch": "tsc -b tsconfig.src.json --watch", | ||
"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,10 @@ | ||
{ | ||
"compilerOptions": { | ||
"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 { 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 && await services.shared.workspace.DocumentBuilder.update([], [ document?.uri ]); | ||
}); | ||
|
||
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; | ||
} |
60 changes: 60 additions & 0 deletions
60
packages/generator-langium/templates/test/test/parsing/parsing.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,60 @@ | ||
import { beforeAll, describe, expect, test } from "vitest"; | ||
import { EmptyFileSystem, type LangiumDocument } from "langium"; | ||
import { expandToString as s} from "langium/generate"; | ||
import { 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([]); | ||
}); | ||
|
||
describe('Parsing tests', () => { | ||
|
||
test('parse simple model', async () => { | ||
document = await parse(` | ||
person Langium | ||
Hello Langium! | ||
`); | ||
|
||
// check for absensce of parser errors the classic way: | ||
// deacivated, find a much more human readable way below! | ||
// expect(document.parseResult.parserErrors).toHaveLength(0); | ||
|
||
expect( | ||
// here we use a (tagged) template expression to create a human readable representation | ||
// of the AST part we are interested in and that is to be compared to our expectation; | ||
// prior to the tagged template expression we check for validity of the parsed document object | ||
// by means of the reusable function 'checkDocumentValid()' to sort out (critical) typos first; | ||
checkDocumentValid(document) || s` | ||
Persons: | ||
${document.parseResult.value?.persons?.map(p => p.name)?.join('\n ')} | ||
Greetings to: | ||
${document.parseResult.value?.greetings?.map(g => g.person.$refText)?.join('\n ')} | ||
` | ||
).toBe(s` | ||
Persons: | ||
Langium | ||
Greetings to: | ||
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.