DEV Community

Discussion on: Testing your DSLs in Langium

Collapse
 
lotes profile image
Markus Rudolph • Edited

Nice article! But Langium has a testing API under langium/test. Look at the tests for the Langium grammar language itself to get some inspiration.
If you miss some guides or tutorials how to do things in Langium, I invite you to start an issue or discussion on GitHub or the connected website :-).
I will create one for a testing tutorial. Thanks for making us aware of this.

import { parseHelper } from "langium/test";
import { createXXXServices } from "../xxx-module.js";
import { NodeFileSystem } from "langium/node";
import { expect } from "vitest";

const services = createXXXServices(NodeFileSystem);

//optional:
await services.shared.workspace.WorkspaceManager.initializeWorkspace([
  /* required source folders */
]);

const parse = parseHelper(services.xxx);

async function assertModelNoErrors(input: string) {
    const model = await parse(input, {validation: true});
    expect(model.parseResult.lexerErrors).toHaveLength(0);
    expect(model.parseResult.parserErrors).toHaveLength(0);
    expect(model.diagnostics ?? []).toHaveLength(0);
    return model;
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
diverse_research profile image
DiverSE

Thanks a lot!
I was already using langium/test but only parseDocument.
parseHelper is a more direct way, and we can get access to fine-grained error at the lexer or parser level thanks to your code snippet!

A more comprehensive tutorial would be nice indeed!
Even better: a set-up working out of the box when testing facilities are generated and integrated into your DSL project... I will launch a discussion about the possibility to have this feature into the langium generator.