Skip to content

Commit

Permalink
implement creation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tzAcee committed Mar 21, 2024
1 parent d5b606d commit e107565
Show file tree
Hide file tree
Showing 8 changed files with 230 additions and 63 deletions.
4 changes: 2 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 6 additions & 27 deletions src/test/ui-test/activation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,51 +20,30 @@ 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();
})

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));
});
})
118 changes: 95 additions & 23 deletions src/test/ui-test/file-creation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,73 +16,145 @@ 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
const readDirContent = fs.readdirSync(folderPath);
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);
});
});
15 changes: 13 additions & 2 deletions src/test/ui-test/utils/class-helper.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
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 = "";

await until(()=>{
fileContent = fs.readFileSync(path).toString();
return fileContent != "";
}, 2000);

if(fileContent != content)
{
console.error(`contents are not equal.`);
Expand All @@ -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);
Expand Down
57 changes: 56 additions & 1 deletion src/test/ui-test/utils/extension-helper.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
{
Expand All @@ -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();
}

}
}
Loading

0 comments on commit e107565

Please sign in to comment.