Skip to content

Commit

Permalink
added another test, simplified test infrastructure, fixed linting issue
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannesMeierSE committed Feb 29, 2024
1 parent 25f21bb commit 136a1f9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 22 deletions.
2 changes: 1 addition & 1 deletion packages/langium/src/test/langium-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,7 @@ export interface QuickFixResult<T extends AstNode = AstNode> extends AsyncDispos
* After applying this quick-fix, 'input' is transformed to 'outputAfterFix'.
*/
export function testQuickFix<T extends AstNode = AstNode>(services: LangiumServices):
(input: string, diagnosticCode: string, outputAfterFix: string | undefined, options?: ParseHelperOptions) => Promise<QuickFixResult<T>> {
(input: string, diagnosticCode: string, outputAfterFix: string | undefined, options?: ParseHelperOptions) => Promise<QuickFixResult<T>> {
const validateHelper = validationHelper<T>(services);
return async (input, diagnosticCode, outputAfterFix, options) => {
// parse + validate
Expand Down
31 changes: 19 additions & 12 deletions packages/langium/test/grammar/grammar-validator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type { ValidationResult } from 'langium/test';
import { clearDocuments, expectError, expectIssue, expectNoIssues, expectWarning, parseHelper, validationHelper } from 'langium/test';
import { afterEach, beforeAll, describe, expect, test } from 'vitest';
import { DiagnosticSeverity } from 'vscode-languageserver';
import { beforeTwoAlternatives, beforeWithInfers } from './lsp/grammar-code-actions.test.js';
import { beforeAnotherRule, beforeSinglelternative, beforeTwoAlternatives, beforeWithInfers } from './lsp/grammar-code-actions.test.js';

const services = createLangiumGrammarServices(EmptyFileSystem);
const parse = parseHelper(services.grammar);
Expand Down Expand Up @@ -436,24 +436,31 @@ describe('Parser rules used only as type in cross-references are not marked as u
// these test cases target https://github.com/eclipse-langium/langium/issues/1309

test('union of two types', async () => {
const validation = await validate(beforeTwoAlternatives);
expect(validation.diagnostics).toHaveLength(1);
const ruleWithHint = validation.document.parseResult.value.rules.find(e => e.name === 'Person')!;
expectIssue(validation, {
node: ruleWithHint,
severity: DiagnosticSeverity.Hint
});
await validateRule(beforeTwoAlternatives);
});

test('union of two types, with infers', async () => {
const validation = await validate(beforeWithInfers);
expect(validation.diagnostics).toHaveLength(1);
test('only a single type', async () => {
await validateRule(beforeSinglelternative);
});

test('rule using a nested rule', async () => {
await validateRule(beforeAnotherRule, 2); // 2 hints, since there is another "unused" rule (which is out-of-scope here)
});

test('union of two types, with "infers" keyword', async () => {
await validateRule(beforeWithInfers);
});

async function validateRule(grammar: string, foundDiagnostics: number = 1) {
const validation = await validate(grammar);
expect(validation.diagnostics).toHaveLength(foundDiagnostics);
const ruleWithHint = validation.document.parseResult.value.rules.find(e => e.name === 'Person')!;
expectIssue(validation, {
node: ruleWithHint,
severity: DiagnosticSeverity.Hint
});
});
return ruleWithHint;
}
});

describe('Reserved names', () => {
Expand Down
32 changes: 23 additions & 9 deletions packages/langium/test/grammar/lsp/grammar-code-actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ export const expectedSingleAlternative = grammarRuleVsType(`
Greeting: 'Hello' person=[Person:ID] '!';
`);

export const beforeAnotherRule = grammarRuleVsType(`
Person: Neighbor | AnotherPerson;
AnotherPerson: Friend;
Greeting: 'Hello' person=[Person:ID] '!';
`);

export const expectedAnotherRule = grammarRuleVsType(`
type Person = Neighbor | AnotherPerson;
AnotherPerson: Friend;
Greeting: 'Hello' person=[Person:ID] '!';
`);

export const beforeWithInfers = grammarRuleVsType(`
Person infers PersonType: Neighbor | Friend;
Greeting: 'Hello' person=[PersonType:ID] '!';
Expand All @@ -60,24 +72,26 @@ function grammarRuleVsType(body: string): string {
describe('Langium grammar quick-fixes for validations: Parser rules used only as type in cross-references are not marked as unused, but with a hint suggesting a type declaration', () => {
// these test cases target https://github.com/eclipse-langium/langium/issues/1309

test('The parser rule has an implicitly inferred type', async () => {
await testLogic(beforeTwoAlternatives, expectedTwoAlternatives);
test('The parser rule has an implicitly inferred type: two alternatives', async () => {
await testReplaceAction(beforeTwoAlternatives, expectedTwoAlternatives);
});

test('The parser rule has an implicitly inferred type: single alternative', async () => {
await testReplaceAction(beforeSinglelternative, expectedSingleAlternative);
});

test('The parser rule has an implicitly inferred type', async () => {
await testLogic(beforeSinglelternative, expectedSingleAlternative);
test('The parser rule used a nested rule', async () => {
await testReplaceAction(beforeAnotherRule, expectedAnotherRule);
});

test('The parser rule has an explicitly inferred type', async () => {
await testLogic(beforeWithInfers, expectedWithInfers);
test('The parser rule has an explicitly inferred type (with two alternatives)', async () => {
await testReplaceAction(beforeWithInfers, expectedWithInfers);
});

async function testLogic(textBefore: string, textAfter: string) {
async function testReplaceAction(textBefore: string, textAfter: string) {
const result = await testQuickFixes(textBefore, IssueCodes.ParserRuleToTypeDecl, textAfter);
const action = result.action;
expect(action).toBeTruthy();
expect(action!.title).toBe('Replace parser rule by type declaration');
}

// TODO test cases, where no quick-fix action is provided
});

0 comments on commit 136a1f9

Please sign in to comment.