Skip to content

Commit

Permalink
test(client): setup e2e tests for diagnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
EmileRolley committed Jun 14, 2024
1 parent c05939e commit 0e714ce
Show file tree
Hide file tree
Showing 13 changed files with 395 additions and 105 deletions.
3 changes: 2 additions & 1 deletion client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
},
"devDependencies": {
"@types/vscode": "^1.75.1",
"@vscode/test-electron": "^2.2.3"
"@vscode/test-electron": "^2.2.3",
"glob": "^10.4.1"
}
}
68 changes: 34 additions & 34 deletions client/src/test/completion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,37 @@ import * as vscode from "vscode";
import * as assert from "assert";
import { getDocUri, activate } from "./helper";

suite("Should do completion", () => {
const docUri = getDocUri("completion.txt");

test("Completes JS/TS in txt file", async () => {
await testCompletion(docUri, new vscode.Position(0, 0), {
items: [
{ label: "JavaScript", kind: vscode.CompletionItemKind.Text },
{ label: "TypeScript", kind: vscode.CompletionItemKind.Text },
],
});
});
});

async function testCompletion(
docUri: vscode.Uri,
position: vscode.Position,
expectedCompletionList: vscode.CompletionList,
) {
await activate(docUri);

// Executing the command `vscode.executeCompletionItemProvider` to simulate triggering completion
const actualCompletionList = (await vscode.commands.executeCommand(
"vscode.executeCompletionItemProvider",
docUri,
position,
)) as vscode.CompletionList;

assert.ok(actualCompletionList.items.length >= 2);
expectedCompletionList.items.forEach((expectedItem, i) => {
const actualItem = actualCompletionList.items[i];
assert.equal(actualItem.label, expectedItem.label);
assert.equal(actualItem.kind, expectedItem.kind);
});
}
// suite("Should do completion", () => {
// const docUri = getDocUri("completion.txt");
//
// test("Completes JS/TS in txt file", async () => {
// await testCompletion(docUri, new vscode.Position(0, 0), {
// items: [
// { label: "JavaScript", kind: vscode.CompletionItemKind.Text },
// { label: "TypeScript", kind: vscode.CompletionItemKind.Text },
// ],
// });
// });
// });
//
// async function testCompletion(
// docUri: vscode.Uri,
// position: vscode.Position,
// expectedCompletionList: vscode.CompletionList,
// ) {
// await activate(docUri);
//
// // Executing the command `vscode.executeCompletionItemProvider` to simulate triggering completion
// const actualCompletionList = (await vscode.commands.executeCommand(
// "vscode.executeCompletionItemProvider",
// docUri,
// position,
// )) as vscode.CompletionList;
//
// assert.ok(actualCompletionList.items.length >= 2);
// expectedCompletionList.items.forEach((expectedItem, i) => {
// const actualItem = actualCompletionList.items[i];
// assert.equal(actualItem.label, expectedItem.label);
// assert.equal(actualItem.kind, expectedItem.kind);
// });
// }
74 changes: 49 additions & 25 deletions client/src/test/diagnostics.test.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,58 @@
/* --------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
* ------------------------------------------------------------------------------------------ */

import * as vscode from "vscode";
import * as assert from "assert";
import { getDocUri, activate } from "./helper";
import { getDocUri, activate, mainUri, mainPath } from "./helper";

suite("Should get diagnostics", () => {
const docUri = getDocUri("diagnostics.txt");
/**
* End-to-end test suite for diagnostics.
*
* How it works:
* 1. Start a vscode instance.
* 2. Activate the 'publicodes-language-server' extension in the instance.
* 3. Open the main document (main.publicodes).
* 4. For each test:
* a. copy the content of the test document to the main document,
* b. save the main document and wait for the diagnostics to be computed.
*/

test("Diagnoses uppercase texts", async () => {
await testDiagnostics(docUri, [
{
message: "ANY is all uppercase.",
range: toRange(0, 0, 0, 3),
severity: vscode.DiagnosticSeverity.Warning,
source: "ex",
},
suite("Should get diagnostics", () => {
test("Missing name in import macros", async () => {
await testDiagnostics(getDocUri("diagnostics-import.publicodes"), [
{
message: "ANY is all uppercase.",
range: toRange(0, 14, 0, 17),
severity: vscode.DiagnosticSeverity.Warning,
source: "ex",
message: `[ Erreur dans la macro 'importer!' ]
Le nom du package est manquant dans la macro 'importer!' dans le fichier: ${mainPath}.
[ Solution ]
Ajoutez le nom du package dans la macro 'importer!'.
[ Exemple ]
importer!:
depuis:
nom: package-name
les règles:
- ruleA
- ruleB
...
`,
range: toRange(0, 0, 0, 9),
severity: vscode.DiagnosticSeverity.Error,
source: "publicodes",
},
]);
});

test("Malformed expression", async () => {
await testDiagnostics(getDocUri("diagnostics-expressions.publicodes"), [
{
message: "OS is all uppercase.",
range: toRange(0, 18, 0, 20),
severity: vscode.DiagnosticSeverity.Warning,
source: "ex",
message: `[ InternalError ]
➡️ Dans la règle "logement . électricité . photovoltaique"
✖️
Un problème est survenu lors du parsing de l'expression \`autoconsommation + production +\` :
le parseur Nearley n'a pas réussi à parser l'expression.
`,
range: toRange(0, 0, 0, 39),
severity: vscode.DiagnosticSeverity.Error,
source: "publicodes",
},
]);
});
Expand All @@ -46,7 +70,7 @@ async function testDiagnostics(
) {
await activate(docUri);

const actualDiagnostics = vscode.languages.getDiagnostics(docUri);
const actualDiagnostics = vscode.languages.getDiagnostics(mainUri);

assert.equal(actualDiagnostics.length, expectedDiagnostics.length);

Expand Down
39 changes: 23 additions & 16 deletions client/src/test/helper.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/* --------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
* ------------------------------------------------------------------------------------------ */

import * as vscode from "vscode";
import * as path from "path";

Expand All @@ -11,20 +6,28 @@ export let editor: vscode.TextEditor;
export let documentEol: string;
export let platformEol: string;

/**
* Activates the vscode.lsp-sample extension
*/
export async function initialize() {
doc = await vscode.workspace.openTextDocument(mainUri);
editor = await vscode.window.showTextDocument(doc);
}

export async function activate(docUri: vscode.Uri) {
// The extensionId is `publisher.name` from package.json
const ext = vscode.extensions.getExtension("vscode-samples.lsp-sample")!;
await ext.activate();
try {
doc = await vscode.workspace.openTextDocument(docUri);
editor = await vscode.window.showTextDocument(doc);
await sleep(2000); // Wait for server activation
} catch (e) {
console.error(e);
const ext = vscode.extensions.getExtension(
"EmileRolley.publicodes-language-server",
)!;
if (!ext.isActive) {
await ext.activate();
try {
await initialize();
} catch (e) {
console.error(e);
}
}
const content = await vscode.workspace.fs.readFile(docUri);
await setTestContent(content.toString());
await vscode.workspace.saveAll();
await sleep(500); // Wait for server activation
}

async function sleep(ms: number) {
Expand All @@ -34,10 +37,14 @@ async function sleep(ms: number) {
export const getDocPath = (p: string) => {
return path.resolve(__dirname, "../../testFixture", p);
};

export const getDocUri = (p: string) => {
return vscode.Uri.file(getDocPath(p));
};

export const mainPath = "main.publicodes";
export const mainUri = getDocUri(mainPath);

export async function setTestContent(content: string): Promise<boolean> {
const all = new vscode.Range(
doc.positionAt(0),
Expand Down
42 changes: 20 additions & 22 deletions client/src/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* ------------------------------------------------------------------------------------------ */
import * as path from "path";
import Mocha from "mocha";
import * as glob from "glob";
import { glob } from "glob";

export function run(): Promise<void> {
// Create the mocha test
Expand All @@ -17,27 +17,25 @@ export function run(): Promise<void> {
const testsRoot = __dirname;

return new Promise((resolve, reject) => {
glob("**.test.js", { cwd: testsRoot }, (err, files) => {
if (err) {
return reject(err);
}
glob("**.test.js", { cwd: testsRoot })
.then((files) => {
// Add files to the test suite
files.forEach((f) => mocha.addFile(path.resolve(testsRoot, f)));

// Add files to the test suite
files.forEach((f) => mocha.addFile(path.resolve(testsRoot, f)));

try {
// Run the mocha test
mocha.run((failures) => {
if (failures > 0) {
reject(new Error(`${failures} tests failed.`));
} else {
resolve();
}
});
} catch (err) {
console.error(err);
reject(err);
}
});
try {
// Run the mocha test
mocha.run((failures) => {
if (failures > 0) {
reject(new Error(`${failures} tests failed.`));
} else {
resolve();
}
});
} catch (err) {
console.error(err);
reject(err);
}
})
.catch(reject);
});
}
5 changes: 5 additions & 0 deletions client/testFixture/diagnostics-expressions.publicodes
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
logement . électricité . photovoltaique:
titre: Panneaux photovoltaïques
formule:
somme:
- autoconsommation + production +
11 changes: 11 additions & 0 deletions client/testFixture/diagnostics-import.publicodes
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
importer!:
depuis:
# nom: '@incubateur-ademe/publicodes-commun'
url: https://github.com/incubateur-ademe/publicodes-commun
dans: commun
les règles:
- intensité électricité
- mois par an
- semaines par an
- jours par semaine
- jours par an
1 change: 0 additions & 1 deletion client/testFixture/diagnostics.txt

This file was deleted.

5 changes: 5 additions & 0 deletions client/testFixture/main.publicodes
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
logement . électricité . photovoltaique:
titre: Panneaux photovoltaïques
formule:
somme:
- autoconsommation + production +
13 changes: 10 additions & 3 deletions client/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@
"module": "nodenext",
"target": "es2021",
"esModuleInterop": true,
"lib": ["es2021"],
"lib": [
"es2021"
],
"outDir": "out",
"rootDir": "src",
"sourceMap": true,
"composite": true,
"incremental": true
},
"include": ["src"],
"exclude": ["node_modules", ".vscode-test"]
"include": [
"src"
],
"exclude": [
"node_modules",
".vscode-test"
]
}
Loading

0 comments on commit 0e714ce

Please sign in to comment.