From e107565f1918cc5a8fc3f281652c69ac5343c995 Mon Sep 17 00:00:00 2001 From: tzAcee Date: Thu, 21 Mar 2024 17:38:26 +0100 Subject: [PATCH] implement creation tests --- src/extension.ts | 4 +- src/test/ui-test/activation.test.ts | 33 +---- src/test/ui-test/file-creation.test.ts | 118 ++++++++++++++---- src/test/ui-test/utils/class-helper.ts | 15 ++- src/test/ui-test/utils/extension-helper.ts | 57 ++++++++- .../utils/extension-settings-helper.ts | 47 ++++++- src/test/ui-test/utils/vs-controller.ts | 17 ++- src/vscode_helper.ts | 2 +- 8 files changed, 230 insertions(+), 63 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index 24c8931..6089da3 100755 --- a/src/extension.ts +++ b/src/extension.ts @@ -36,11 +36,11 @@ export async function activate(context: vscode.ExtensionContext) { //Create the class there if (out) { - vscode.window.showInformationMessage('Your Class ' + res + ' has been created! \n(@'+dir_h.dir()+')'); + vscode.window.showInformationMessage('Your class "' + res + '" has been created! \n(@'+dir_h.dir()+')'); } else { - vscode.window.showErrorMessage('Your Class ' + res + ' has been not created! \n(@'+dir_h.dir()+')'); + vscode.window.showErrorMessage('Your class "' + res + '" has NOT been created! \n(@'+dir_h.dir()+')'); } }); // Display a message box to the user diff --git a/src/test/ui-test/activation.test.ts b/src/test/ui-test/activation.test.ts index f73709a..95c3a35 100644 --- a/src/test/ui-test/activation.test.ts +++ b/src/test/ui-test/activation.test.ts @@ -20,18 +20,11 @@ describe('Activation test suite', () => { it('Extension can be activated by "Alt+X"', async () => { await vsController.openNewEmptyEditor(); - let workSpaceContent = fs.readdirSync(workSpaceDir); - assert(workSpaceContent.length == 0); - const className = "testClass"; await cppCreatorExt.openExtPromptByShortcut(className); - assert(await ClassHelper.fileExistsWithContent(workSpaceDir+"/"+className+".h", ClassHelper.defaultHeaderContent(className), true)); - assert(await ClassHelper.fileExistsWithContent(workSpaceDir+"/"+className+".cpp", ClassHelper.defaultClassContent(className), true)); - - // ClassHelper deletes the checked files. - workSpaceContent = fs.readdirSync(workSpaceDir); - assert(workSpaceContent.length == 0); + assert(await ClassHelper.fileExistsWithContent(className, workSpaceDir+"/"+className+".h", ClassHelper.defaultHeaderContent(className), true)); + assert(await ClassHelper.fileExistsWithContent(className, workSpaceDir+"/"+className+".cpp", ClassHelper.defaultClassContent(className), true)); await new EditorView().closeAllEditors(); }) @@ -39,32 +32,18 @@ describe('Activation test suite', () => { it('Extension can be activated by the context menu', async () => { // test logic for context menu activ. - let workSpaceContent = fs.readdirSync(workSpaceDir); - assert(workSpaceContent.length == 0); - const className = "testClass"; await cppCreatorExt.openExtPromptByContextMenu(className, workSpaceDir); - assert(await ClassHelper.fileExistsWithContent(workSpaceDir+"/"+className+".h", ClassHelper.defaultHeaderContent(className), true)); - assert(await ClassHelper.fileExistsWithContent(workSpaceDir+"/"+className+".cpp", ClassHelper.defaultClassContent(className), true)); - - // ClassHelper deletes the checked files. - workSpaceContent = fs.readdirSync(workSpaceDir); - assert(workSpaceContent.length == 0); + assert(await ClassHelper.fileExistsWithContent(className, workSpaceDir+"/"+className+".h", ClassHelper.defaultHeaderContent(className), true)); + assert(await ClassHelper.fileExistsWithContent(className, workSpaceDir+"/"+className+".cpp", ClassHelper.defaultClassContent(className), true)); }) it('Extension can be activated by the command pallette', async () => { - let workSpaceContent = fs.readdirSync(workSpaceDir); - assert(workSpaceContent.length == 0); - const className = "testClass"; await cppCreatorExt.openExtPromptByCmdPallette(className); - assert(await ClassHelper.fileExistsWithContent(workSpaceDir+"/"+className+".h", ClassHelper.defaultHeaderContent(className), true)); - assert(await ClassHelper.fileExistsWithContent(workSpaceDir+"/"+className+".cpp", ClassHelper.defaultClassContent(className), true)); - - // ClassHelper deletes the checked files. - workSpaceContent = fs.readdirSync(workSpaceDir); - assert(workSpaceContent.length == 0); + assert(await ClassHelper.fileExistsWithContent(className, workSpaceDir+"/"+className+".h", ClassHelper.defaultHeaderContent(className), true)); + assert(await ClassHelper.fileExistsWithContent(className, workSpaceDir+"/"+className+".cpp", ClassHelper.defaultClassContent(className), true)); }); }) diff --git a/src/test/ui-test/file-creation.test.ts b/src/test/ui-test/file-creation.test.ts index 5104227..cb3cd28 100644 --- a/src/test/ui-test/file-creation.test.ts +++ b/src/test/ui-test/file-creation.test.ts @@ -16,41 +16,34 @@ describe('Creation test suite', () => { }); after(async ()=>{ + }) + afterEach(async ()=>{ + await ext_settings.setSetPath(undefined); + await ext_settings.setCreateFolder(false); + }); + it("Don't create new folder if 'Create Folder' is false", async ()=>{ assert(await ext_settings.isCreateFolderEnabled() == false) - let workSpaceContent = fs.readdirSync(workSpaceDir); - assert(workSpaceContent.length == 0); const className = "testClass"; await cppCreatorExt.openExtPromptByCmdPallette(className); - assert(await ClassHelper.fileExistsWithContent(workSpaceDir+"/"+className+".h", ClassHelper.defaultHeaderContent(className), true)); - assert(await ClassHelper.fileExistsWithContent(workSpaceDir+"/"+className+".cpp", ClassHelper.defaultClassContent(className), true)); - - // ClassHelper deletes the checked files. - workSpaceContent = fs.readdirSync(workSpaceDir); - assert(workSpaceContent.length == 0); + assert(await ClassHelper.fileExistsWithContent(className, workSpaceDir+"/"+className+".h", ClassHelper.defaultHeaderContent(className), true)); + assert(await ClassHelper.fileExistsWithContent(className, workSpaceDir+"/"+className+".cpp", ClassHelper.defaultClassContent(className), true)); }); it("Create new folder if 'Create Folder' is true", async ()=>{ assert(await ext_settings.isCreateFolderEnabled() == false) await ext_settings.setCreateFolder(true); - - let workSpaceContent = fs.readdirSync(workSpaceDir); - assert(workSpaceContent.length == 0); const className = "testClass"; await cppCreatorExt.openExtPromptByCmdPallette(className); - assert(await ClassHelper.fileExistsWithContent(workSpaceDir+"/"+className+"/"+className+".h", ClassHelper.defaultHeaderContent(className), true)); - assert(await ClassHelper.fileExistsWithContent(workSpaceDir+"/"+className+"/"+className+".cpp", ClassHelper.defaultClassContent(className), true)); - - // ClassHelper deletes the checked files, aonly folder is there. - workSpaceContent = fs.readdirSync(workSpaceDir); - assert(workSpaceContent.length == 1); + assert(await ClassHelper.fileExistsWithContent(className, workSpaceDir+"/"+className+"/"+className+".h", ClassHelper.defaultHeaderContent(className), true)); + assert(await ClassHelper.fileExistsWithContent(className, workSpaceDir+"/"+className+"/"+className+".cpp", ClassHelper.defaultClassContent(className), true)); const folderPath = workSpaceDir+"/"+className; // Check for empty folder @@ -58,31 +51,110 @@ describe('Creation test suite', () => { assert(readDirContent.length == 0); fs.rmdirSync(folderPath); - workSpaceContent = fs.readdirSync(workSpaceDir); - assert(workSpaceContent.length == 0); }); - it("Prompt for the path if 'SetPath' is true", async ()=>{ + it("Prompt for the path if 'SetPath' is true", async ()=> + { + assert(await ext_settings.isSetPath() == undefined) + + let newPath = "/tmp/newWsPath"; + await ext_settings.setSetPath(true); + + const className = "testClass"; + + await cppCreatorExt.openExtPromptWithPathPromptByCmdPallette(className, newPath); + assert(await ClassHelper.fileExistsWithContent(className, newPath+"/"+className+".h", ClassHelper.defaultHeaderContent(className), true)); + assert(await ClassHelper.fileExistsWithContent(className, newPath+"/"+className+".cpp", ClassHelper.defaultClassContent(className), true)); + + // ClassHelper deletes the checked files. + let newWorkSpaceContent = fs.readdirSync(newPath); + assert(newWorkSpaceContent.length == 0); }); - it("Don't prompt for the path if 'SetPath' is false", async ()=>{ + it("Don't prompt for the path if 'SetPath' is false", async ()=> + { + await ext_settings.setSetPath(false); + assert(await ext_settings.isCreateFolderEnabled() == false) + + const className = "testClass"; + + await cppCreatorExt.openExtPromptByCmdPallette(className); + assert(await ClassHelper.fileExistsWithContent(className, workSpaceDir+"/"+className+".h", ClassHelper.defaultHeaderContent(className), true)); + assert(await ClassHelper.fileExistsWithContent(className, workSpaceDir+"/"+className+".cpp", ClassHelper.defaultClassContent(className), true)); }); it("Don't prompt for the path if 'SetPath' is undefined", async ()=>{ + await ext_settings.setSetPath(undefined); + assert(await ext_settings.isCreateFolderEnabled() == false) + + const className = "testClass"; + + await cppCreatorExt.openExtPromptByCmdPallette(className); + assert(await ClassHelper.fileExistsWithContent(className, workSpaceDir+"/"+className+".h", ClassHelper.defaultHeaderContent(className), true)); + assert(await ClassHelper.fileExistsWithContent(className, workSpaceDir+"/"+className+".cpp", ClassHelper.defaultClassContent(className), true)); }); it("Don't prompt for the path if 'SetPath' is a string with the wished path", async ()=>{ + let newPath = "/tmp/newWsPath"; + await ext_settings.setSetPath(newPath); + + const className = "testClass"; + + await cppCreatorExt.openExtPromptByCmdPallette(className); + assert(await ClassHelper.fileExistsWithContent(className, newPath+"/"+className+".h", ClassHelper.defaultHeaderContent(className), true)); + assert(await ClassHelper.fileExistsWithContent(className, newPath+"/"+className+".cpp", ClassHelper.defaultClassContent(className), true)); + + // ClassHelper deletes the checked files. + let newWorkSpaceContent = fs.readdirSync(newPath); + assert(newWorkSpaceContent.length == 0); }); it("'SetPath' is overriden by context-menu location", async()=>{ + await ext_settings.setSetPath(true) + + const className = "testClass"; + const expDir = workSpaceDir + "/child"; + await cppCreatorExt.openExtPromptByContextMenuWithChild(className, workSpaceDir, "child"); + assert(await ClassHelper.fileExistsWithContent(className, expDir+"/"+className+".h", ClassHelper.defaultHeaderContent(className), true)); + assert(await ClassHelper.fileExistsWithContent(className, expDir+"/"+className+".cpp", ClassHelper.defaultClassContent(className), true)); + + let newWorkSpaceContent = fs.readdirSync(expDir); + assert(newWorkSpaceContent.length == 0); + }); + + it("Combine 'createFolder' with 'setPath' ('createFolder' ignored)", async()=>{ + await ext_settings.setCreateFolder(true) + + let newPath = "/tmp/newWsPath"; + + await ext_settings.setSetPath(newPath); + + const className = "testClass"; + + await cppCreatorExt.openExtPromptByCmdPallette(className); + assert(await ClassHelper.fileExistsWithContent(className, newPath+"/"+className+".h", ClassHelper.defaultHeaderContent(className), true)); + assert(await ClassHelper.fileExistsWithContent(className, newPath+"/"+className+".cpp", ClassHelper.defaultClassContent(className), true)); + + // ClassHelper deletes the checked files. + let newWorkSpaceContent = fs.readdirSync(newPath); + assert(newWorkSpaceContent.length == 0); }); - it("Combine 'createFolder' with 'setPath'", ()=>{}); + it("Combine 'createFolder' with context-menu location ('createFolder' ignored)", async ()=>{ + await ext_settings.setCreateFolder(true) - it("Combine 'createFolder' with context-menu location", ()=>{}); + const className = "testClass"; + const expDir = workSpaceDir + "/child"; + await cppCreatorExt.openExtPromptByContextMenuWithChild(className, workSpaceDir, "child"); + assert(await ClassHelper.fileExistsWithContent(className, expDir+"/"+className+".h", ClassHelper.defaultHeaderContent(className), true)); + assert(await ClassHelper.fileExistsWithContent(className, expDir+"/"+className+".cpp", ClassHelper.defaultClassContent(className), true)); + + let newWorkSpaceContent = fs.readdirSync(expDir); + assert(newWorkSpaceContent.length == 0); + }); }); \ No newline at end of file diff --git a/src/test/ui-test/utils/class-helper.ts b/src/test/ui-test/utils/class-helper.ts index 574ac9c..96fceff 100644 --- a/src/test/ui-test/utils/class-helper.ts +++ b/src/test/ui-test/utils/class-helper.ts @@ -1,15 +1,22 @@ import * as fs from "fs"; import { until } from "./util"; +import * as assert from "assert"; +import { VSController } from "./vs-controller"; + + export class ClassHelper { - static async fileExistsWithContent(path: string, content: string, deleteAfterwards: boolean) + static async fileExistsWithContent(className: string, path: string, content: string, deleteAfterwards: boolean) { + assert(content != ""); + if(await until(()=>fs.existsSync(path), 1000)) { console.error(`${path} does not exist.`) return false; } + assert(fs.existsSync(path)); let fileContent = ""; @@ -17,7 +24,7 @@ export class ClassHelper fileContent = fs.readFileSync(path).toString(); return fileContent != ""; }, 2000); - + if(fileContent != content) { console.error(`contents are not equal.`); @@ -26,6 +33,10 @@ export class ClassHelper return false; } + const pathWithoutFileName = path.substring(0, path.lastIndexOf("/")); + const expNotif = `Your class "${className}" has been created! (@${pathWithoutFileName})`; + assert(await VSController.isNotificationSent(expNotif)); + if(deleteAfterwards) { fs.unlinkSync(path); diff --git a/src/test/ui-test/utils/extension-helper.ts b/src/test/ui-test/utils/extension-helper.ts index 8e662df..9d580d9 100644 --- a/src/test/ui-test/utils/extension-helper.ts +++ b/src/test/ui-test/utils/extension-helper.ts @@ -1,7 +1,8 @@ -import { ActivityBar, InputBox, Key, VSBrowser, Workbench } from "vscode-extension-tester"; +import { ActivityBar, InputBox, Key, TreeItem, VSBrowser, Workbench } from "vscode-extension-tester"; import * as path from "path"; +import * as fs from "fs"; import * as assert from "assert"; export class CppCreatorExtHelper @@ -44,6 +45,42 @@ export class CppCreatorExtHelper await inputBox.setText(classNamePrompt); await inputBox.confirm(); } + + async openExtPromptByContextMenuWithChild(classNamePrompt: string, wsPath: string, child: string) + { + // create child-entry first + const childPath = wsPath+"/"+child; + if(!fs.existsSync(childPath)) + fs.mkdirSync(childPath); + + const explorer = await new ActivityBar().getViewControl("Explorer"); + assert(explorer != undefined); + const explorerView = await explorer.openView(); + + const explorerContent = explorerView.getContent(); + + const wsExplorer = await explorerContent.getSection("Untitled (Workspace)"); + await wsExplorer.expand(); + + const wsName = path.basename(wsPath); + let clickableItem = await wsExplorer.findItem(wsName) as TreeItem; + assert(clickableItem != undefined); + + await clickableItem.expand() + + let clickableChildItem = await clickableItem.findChildItem(path.basename(childPath)); + assert(clickableChildItem != undefined); + + const explorerMenu = await clickableChildItem.openContextMenu(); + const createElem = await explorerMenu.getItem("Create C++ Class"); + assert(createElem != undefined); + + await createElem.click(); + + let inputBox = await new InputBox().wait(); + await inputBox.setText(classNamePrompt); + await inputBox.confirm(); + } async openExtPromptByCmdPallette(classNamePrompt: string) { @@ -53,4 +90,22 @@ export class CppCreatorExtHelper await inputBox.setText(classNamePrompt); await inputBox.confirm(); } + + async openExtPromptWithPathPromptByCmdPallette(classNamePrompt: string, path: string) + { + await new Workbench().executeCommand("Create C++ Class"); + + { + let inputBox = await new InputBox().wait(); + await inputBox.setText(classNamePrompt); + await inputBox.confirm(); + } + + { + let inputBox = await new InputBox().wait(); + await inputBox.setText(path); + await inputBox.confirm(); + } + + } } \ No newline at end of file diff --git a/src/test/ui-test/utils/extension-settings-helper.ts b/src/test/ui-test/utils/extension-settings-helper.ts index 1acd8ae..a2791a2 100644 --- a/src/test/ui-test/utils/extension-settings-helper.ts +++ b/src/test/ui-test/utils/extension-settings-helper.ts @@ -1,5 +1,5 @@ import * as assert from "assert"; -import { Workbench } from "vscode-extension-tester"; +import { EditorView, LinkSetting, Workbench, TextEditor } from "vscode-extension-tester"; export class ExtensionSettings { @@ -91,16 +91,51 @@ export class ExtensionSettings async isSetPath() : Promise { const settingsEditor = await new Workbench().openSettings(); - const setting = await settingsEditor.findSettingByID("cpp.creator.setPath"); + const setting = await settingsEditor.findSettingByID("cpp.creator.setPath") as LinkSetting; assert(setting != undefined); - return await setting.getValue() as boolean | undefined | string; + await setting.openLink(); + + // get editor text + let settingsJsonEditor = await new EditorView().openEditor("settings.json") as TextEditor; + let settingsJsonText = await settingsJsonEditor.getText(); + + // parse json + let settingsJson = JSON.parse(settingsJsonText); + + // get json value + + if(settingsJson["cpp.creator.setPath"] == "") + { + settingsJson["cpp.creator.setPath"] = undefined; + await settingsJsonEditor.setText(JSON.stringify(settingsJson, null, 2)); + if(await settingsJsonEditor.isDirty()) + await settingsJsonEditor.save(); + + return undefined; + } + + return settingsJson["cpp.creator.setPath"] as boolean | string; } - async setSetPath(val: boolean | string) + async setSetPath(val: boolean | string | undefined) { const settingsEditor = await new Workbench().openSettings(); - const setting = await settingsEditor.findSettingByID("cpp.creator.setPath"); + const setting = await settingsEditor.findSettingByID("cpp.creator.setPath") as LinkSetting; assert(setting != undefined); - await setting.setValue(val); + await setting.openLink(); + + // get editor text + let settingsJsonEditor = await new EditorView().openEditor("settings.json") as TextEditor; + let settingsJsonText = await settingsJsonEditor.getText(); + + // parse json + let settingsJson = JSON.parse(settingsJsonText); + + // set json value + settingsJson["cpp.creator.setPath"] = val; + + await settingsJsonEditor.setText(JSON.stringify(settingsJson, null, 2)); + if(await settingsJsonEditor.isDirty()) + await settingsJsonEditor.save(); } } \ No newline at end of file diff --git a/src/test/ui-test/utils/vs-controller.ts b/src/test/ui-test/utils/vs-controller.ts index a5fb7b2..b515f5e 100644 --- a/src/test/ui-test/utils/vs-controller.ts +++ b/src/test/ui-test/utils/vs-controller.ts @@ -1,4 +1,4 @@ -import { Workbench, TitleBar, InputBox, VSBrowser, Key, EditorView } from 'vscode-extension-tester'; +import { Workbench, TitleBar, InputBox, VSBrowser, Key, EditorView, Notification } from 'vscode-extension-tester'; import * as fs from "fs"; import * as assert from "assert"; @@ -40,4 +40,19 @@ export class VSController assert(untitledEditor != undefined); await new EditorView().openEditor(untitledEditor as string); } + + static async isNotificationSent(text: string) + { + const notifications = await new Workbench().getNotifications(); + + for(let notif of notifications) + { + if(await notif.getMessage() == text) + { + return true; + } + } + + return false; + } } \ No newline at end of file diff --git a/src/vscode_helper.ts b/src/vscode_helper.ts index bee934d..36dec42 100755 --- a/src/vscode_helper.ts +++ b/src/vscode_helper.ts @@ -26,7 +26,7 @@ export abstract class vscode_helper { if (!res) { - vscode.window.showErrorMessage("Your Class could not be created!"); + vscode.window.showErrorMessage("Your class could NOT be created!"); return false; } else if (res.length > 60)