Validation Test Helper #413
-
Is there an example that exists to demonstrate how to test the custom validation rules written for the language? Could only find #340, but that was only testing the parsing. Currently working on my master thesis, so there are braking changes all the time, so could be nice to have some automated test so I don't have to manually check them each time I change something. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
we expose some testing functionality in the framework, but none for validation so far. For a quick custom setup you could simply check whether the diagnostics returned from the First iteration: Setting up the test const services = createLanguageServices();
const helper = parseHelper<RootAST>(services);
const documentContent = "...";
(async function () {
const doc = await helper(documentContent);
const diagnostics = await services.validation.DocumentValidator.validateDocument(doc);
console.log(JSON.stringify(diagnostics, undefined, 2)); // Store this logged string somewhere
})(); Second iteration: Using the printed diagnostics to identify regressions const services = createLanguageServices();
const helper = parseHelper<RootAST>(services);
const documentContent = "...";
test('validation test', async () => {
const expectation = [ /** array of printed diagnostics */ ];
const doc = await helper(documentContent);
const diagnostics = await services.validation.DocumentValidator.validateDocument(doc);
expect(diagnostics ).toEqual(expectation);
}); It's a bit crude and only works for a whole document, but it's quick to set up and get going. A question out of pure personal curiosity: What are you using Langium for in your thesis? I'm also in the process of writing my master thesis (on parsing and unbounded lookahead, see Chevrotain/chevrotain#1714) |
Beta Was this translation helpful? Give feedback.
Hi @MathiasSJacobsen,
we expose some testing functionality in the framework, but none for validation so far. For a quick custom setup you could simply check whether the diagnostics returned from the
DocumentValidator
match whatever you expect. If you want a simple regression test, you could do something like this:First iteration: Setting up the test