Skip to content

Commit

Permalink
finish replacement tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tzAcee committed Mar 23, 2024
1 parent c7d0fd0 commit e04246b
Show file tree
Hide file tree
Showing 2 changed files with 251 additions and 0 deletions.
233 changes: 233 additions & 0 deletions src/test/ui-test/file-content-replacement.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,86 +63,319 @@ describe('File-content replacement test suite', () => {

it('[header file content] - HEADERFILENAME once', async () => {

await ext_settings.setHeaderFileContentPreset("{{*HEADERFILENAME*}}");
const className = "testClass";

await cppCreatorExt.openExtPromptByCmdPallette(className);

const fileName = className + ".h";
const path = workSpaceDir + "/" + fileName;
const expectedContent = fileName;

assert(await ClassHelper.fileExistsWithContent(className, path, expectedContent, false));
})

it('[header file content] - SOURCEFILENAME once', async () => {
await ext_settings.setHeaderFileContentPreset("{{*SOURCEFILENAME*}}");
const className = "testClass";

await cppCreatorExt.openExtPromptByCmdPallette(className);

const fileName = className + ".h";
const path = workSpaceDir + "/" + fileName;
const expectedContent = fileName.replace(".h", ".cpp");

assert(await ClassHelper.fileExistsWithContent(className, path, expectedContent, false));
})

it('[header file content] - CLASSNAMEUPPER once', async () => {
await ext_settings.setHeaderFileContentPreset("{{*CLASSNAMEUPPER*}}");
const className = "testClass";

await cppCreatorExt.openExtPromptByCmdPallette(className);

const fileName = className + ".h";
const path = workSpaceDir + "/" + fileName;
const expectedContent = className.toUpperCase();

assert(await ClassHelper.fileExistsWithContent(className, path, expectedContent, false));
})

it('[header file content] - CLASSNAMELOWER once', async () => {
await ext_settings.setHeaderFileContentPreset("{{*CLASSNAMELOWER*}}");
const className = "testClass";

await cppCreatorExt.openExtPromptByCmdPallette(className);

const fileName = className + ".h";
const path = workSpaceDir + "/" + fileName;
const expectedContent = className.toLowerCase();

assert(await ClassHelper.fileExistsWithContent(className, path, expectedContent, false));
})

it('[header file content] - CLASSNAMECAPI once', async () => {
await ext_settings.setHeaderFileContentPreset("{{*CLASSNAMECAPI*}}");
const className = "testClass";

await cppCreatorExt.openExtPromptByCmdPallette(className);

const fileName = className + ".h";
const path = workSpaceDir + "/" + fileName;
const expectedContent = className[0].toUpperCase() + className.substring(1);

assert(await ClassHelper.fileExistsWithContent(className, path, expectedContent, false));
})

it('[header file content] - CLASSNAME once', async () => {
await ext_settings.setHeaderFileContentPreset("{{*CLASSNAME*}}");
const className = "testClass";

await cppCreatorExt.openExtPromptByCmdPallette(className);

const fileName = className + ".h";
const path = workSpaceDir + "/" + fileName;
const expectedContent = className

assert(await ClassHelper.fileExistsWithContent(className, path, expectedContent, false));
})

it('[header file content] - CURRENTDATETIME once', async () => {
await ext_settings.setHeaderFileContentPreset("{{*CURRENTDATETIME*}}");
const className = "testClass";

await cppCreatorExt.openExtPromptByCmdPallette(className);

const currentDateTimeString = new Date().toLocaleString();

const fileName = className + ".h";
const path = workSpaceDir + "/" + fileName;

const content = await ClassHelper.fileExistsGetContent(path, className);

try
{
assert(content == currentDateTimeString);
} catch(e)
{
const diff = Math.abs(new Date(content).getTime() - new Date(currentDateTimeString).getTime());
console.log("time diff", diff);
assert(diff < 1000);
}

})

it('[header file content] - CURRENTDATE once', async () => {
await ext_settings.setHeaderFileContentPreset("{{*CURRENTDATE*}}");
const className = "testClass";

await cppCreatorExt.openExtPromptByCmdPallette(className);

const currentDateString = new Date().toLocaleDateString();

const fileName = className + ".h";
const path = workSpaceDir + "/" + fileName;

const content = await ClassHelper.fileExistsGetContent(path, className);

assert(content == currentDateString);
})

it('[header file content] - CURRENTTIME once', async () => {
await ext_settings.setHeaderFileContentPreset("{{*CURRENTTIME*}}");
const className = "testClass";

await cppCreatorExt.openExtPromptByCmdPallette(className);

const currentTimeString = new Date().toLocaleTimeString();

const fileName = className + ".h";
const path = workSpaceDir + "/" + fileName;

const content = await ClassHelper.fileExistsGetContent(path, className);

const trimmedContent = content.trim().replace(/\u{2007}/gu, ' ').replace(/\u{202F}/gu, ' ');
const trimmedCurrentTimeString = currentTimeString.trim().replace(/\u{2007}/gu, ' ').replace(/\u{202F}/gu, ' ');

assert(trimmedContent == trimmedCurrentTimeString);
})

it('[header file content] - combination', async () => {

await ext_settings.setHeaderFileContentPreset("{{*CLASSNAMECAPI*}}-{{*CLASSNAME*}}-{{*HEADERFILENAME*}}");
const className = "testClass";

await cppCreatorExt.openExtPromptByCmdPallette(className);

const fileName = className + ".h";
const path = workSpaceDir + "/" + fileName;
const capitalized = className[0].toUpperCase() + className.substring(1);
const expectedContent = capitalized + "-"+className+"-"+fileName;

assert(await ClassHelper.fileExistsWithContent(className, path, expectedContent, false));
})

// source file content tests:
it('[source file content] - HEADERFILENAME multiple', async () => {
await ext_settings.setSourceFileContentPreset("{{*HEADERFILENAME*}}-{{*HEADERFILENAME*}}");
const className = "testClass";

await cppCreatorExt.openExtPromptByCmdPallette(className);

const fileName = className + ".cpp";
const path = workSpaceDir + "/" + fileName;
const expectedContent = fileName.replace(".cpp", ".h")+"-"+fileName.replace(".cpp", ".h");

assert(await ClassHelper.fileExistsWithContent(className, path, expectedContent, false));
})

it('[source file content] - HEADERFILENAME once', async () => {
await ext_settings.setSourceFileContentPreset("{{*HEADERFILENAME*}}");
const className = "testClass";

await cppCreatorExt.openExtPromptByCmdPallette(className);

const fileName = className + ".cpp";
const path = workSpaceDir + "/" + fileName;
const expectedContent = fileName.replace(".cpp", ".h");

assert(await ClassHelper.fileExistsWithContent(className, path, expectedContent, false));
})

it('[source file content] - SOURCEFILENAME once', async () => {
await ext_settings.setSourceFileContentPreset("{{*SOURCEFILENAME*}}");
const className = "testClass";

await cppCreatorExt.openExtPromptByCmdPallette(className);

const fileName = className + ".cpp";
const path = workSpaceDir + "/" + fileName;
const expectedContent = fileName;

assert(await ClassHelper.fileExistsWithContent(className, path, expectedContent, false));
})

it('[source file content] - CLASSNAMEUPPER once', async () => {
await ext_settings.setSourceFileContentPreset("{{*CLASSNAMEUPPER*}}");
const className = "testClass";

await cppCreatorExt.openExtPromptByCmdPallette(className);

const fileName = className + ".cpp";
const path = workSpaceDir + "/" + fileName;
const expectedContent = className.toUpperCase();

assert(await ClassHelper.fileExistsWithContent(className, path, expectedContent, false));
})

it('[source file content] - CLASSNAMELOWER once', async () => {
await ext_settings.setSourceFileContentPreset("{{*CLASSNAMELOWER*}}");
const className = "testClass";

await cppCreatorExt.openExtPromptByCmdPallette(className);

const fileName = className + ".cpp";
const path = workSpaceDir + "/" + fileName;
const expectedContent = className.toLowerCase();

assert(await ClassHelper.fileExistsWithContent(className, path, expectedContent, false));
})

it('[source file content] - CLASSNAMECAPI once', async () => {
await ext_settings.setSourceFileContentPreset("{{*CLASSNAMECAPI*}}");
const className = "testClass";

await cppCreatorExt.openExtPromptByCmdPallette(className);

const fileName = className + ".cpp";
const path = workSpaceDir + "/" + fileName;
const expectedContent = className[0].toUpperCase() + className.substring(1);

assert(await ClassHelper.fileExistsWithContent(className, path, expectedContent, false));
})

it('[source file content] - CLASSNAME once', async () => {
await ext_settings.setSourceFileContentPreset("{{*CLASSNAME*}}");
const className = "testClass";

await cppCreatorExt.openExtPromptByCmdPallette(className);

const fileName = className + ".cpp";
const path = workSpaceDir + "/" + fileName;
const expectedContent = className;

assert(await ClassHelper.fileExistsWithContent(className, path, expectedContent, false));
})

it('[source file content] - CURRENTDATETIME once', async () => {
await ext_settings.setSourceFileContentPreset("{{*CURRENTDATETIME*}}");
const className = "testClass";

await cppCreatorExt.openExtPromptByCmdPallette(className);

const currentDateTimeString = new Date().toLocaleString();

const fileName = className + ".cpp";
const path = workSpaceDir + "/" + fileName;

const content = await ClassHelper.fileExistsGetContent(path, className);

try
{
assert(content == currentDateTimeString);
} catch(e)
{
assert(Math.abs(new Date(content).getTime() - new Date(currentDateTimeString).getTime()) < 10);
}

})

it('[source file content] - CURRENTDATE once', async () => {
await ext_settings.setSourceFileContentPreset("{{*CURRENTDATE*}}");
const className = "testClass";

await cppCreatorExt.openExtPromptByCmdPallette(className);

const currentDateString = new Date().toLocaleDateString();

const fileName = className + ".cpp";
const path = workSpaceDir + "/" + fileName;

const content = await ClassHelper.fileExistsGetContent(path, className);

assert(content == currentDateString);
})

it('[source file content] - CURRENTTIME once', async () => {
await ext_settings.setSourceFileContentPreset("{{*CURRENTTIME*}}");
const className = "testClass";

await cppCreatorExt.openExtPromptByCmdPallette(className);

const currentTimeString = new Date().toLocaleTimeString();

const fileName = className + ".cpp";
const path = workSpaceDir + "/" + fileName;

const content = await ClassHelper.fileExistsGetContent(path, className);
const trimmedContent = content.trim().replace(/\u{2007}/gu, ' ').replace(/\u{202F}/gu, ' ');
const trimmedCurrentTimeString = currentTimeString.trim().replace(/\u{2007}/gu, ' ').replace(/\u{202F}/gu, ' ');

assert(trimmedContent == trimmedCurrentTimeString);
})

it('[source file content] - combination', async () => {

await ext_settings.setSourceFileContentPreset("{{*CLASSNAMECAPI*}}-{{*CLASSNAME*}}-{{*SOURCEFILENAME*}}");
const className = "testClass";

await cppCreatorExt.openExtPromptByCmdPallette(className);

const fileName = className + ".cpp";
const path = workSpaceDir + "/" + fileName;
const capitalized = className[0].toUpperCase() + className.substring(1);
const expectedContent = capitalized + "-"+className+"-"+fileName;

assert(await ClassHelper.fileExistsWithContent(className, path, expectedContent, false))
})
});
18 changes: 18 additions & 0 deletions src/test/ui-test/utils/class-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,24 @@ export class ClassHelper
return true;
}

static async fileExistsGetContent(path: string, className: string)
{
assert(await this.fileExists(path));

let fileContent = "";

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

const pathWithoutFileName = path.substring(0, path.lastIndexOf("/"));
const expNotif = `Your class "${className}" has been created! (@${pathWithoutFileName})`;
assert(await VSController.isNotificationSent(expNotif));

return fileContent;
}

static async fileExistsWithContent(className: string, path: string, content: string, deleteAfterwards: boolean)
{
assert(content != "");
Expand Down

0 comments on commit e04246b

Please sign in to comment.