Set up unit testing for engines

This commit is contained in:
Nilay Majorwar 2022-01-18 15:17:08 +05:30
parent a4c0ad61e4
commit d50e737682
5 changed files with 1907 additions and 30 deletions

View File

@ -106,7 +106,7 @@ class ExecutionController<RS> {
* @param param0.interval Interval between two execution steps
* @param param0.onResult Callback called with result on each execution step
*/
async executeAll({ interval, onResult }: ExecuteAllArgs<RS>) {
executeAll({ interval, onResult }: ExecuteAllArgs<RS>) {
// Clear paused state
this._isPaused = false;

48
engines/test-utils.ts Normal file
View File

@ -0,0 +1,48 @@
import { readFileSync } from "fs";
import { resolve } from "path";
import ExecutionController from "./execution-controller";
import { LanguageEngine } from "./types";
/**
* Read the entire contents of a test program.
* @param dirname Absolute path to directory containing program
* @param name Name of TXT file, without extension
* @returns Contents of file, as a \n-delimited string
*/
export const readTestProgram = (dirname: string, name: string): string => {
const path = resolve(dirname, name + ".txt");
return readFileSync(path, { encoding: "utf8" }).toString();
};
/**
* Run code on language engine and return final result.
* If error thrown, promise rejects with the error.
* @param engine Engine to use for execution
* @param code Source code to execute
* @param input STDIN input for execution
* @returns Final execution result object
*/
export const executeProgram = async <T>(
engine: LanguageEngine<T>,
code: string,
input: string = ""
): Promise<{ output: string; rendererState: T }> => {
const controller = new ExecutionController(engine);
controller.prepare(code, input);
return new Promise((resolve, reject) => {
try {
let output = "";
controller.executeAll({
interval: 0,
onResult: (res) => {
if (res.output) output += res.output;
if (res.nextStepLocation == null) {
resolve({ output, rendererState: res.rendererState });
}
},
});
} catch (error) {
reject(error);
}
});
};

17
jest.config.js Normal file
View File

@ -0,0 +1,17 @@
const nextJest = require("next/jest");
const createJestConfig = nextJest({
// Provide the path to your Next.js app to load next.config.js and .env files in your test environment
dir: "./",
});
// Add any custom config to be passed to Jest
const customJestConfig = {
// setupFilesAfterEnv: ["<rootDir>/jest.setup.js"],
// if using TypeScript with a baseUrl set to the root directory then you need the below for alias' to work
// moduleDirectories: ["node_modules", "<rootDir>/"],
testEnvironment: "jest-environment-node",
};
// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async
module.exports = createJestConfig(customJestConfig);

View File

@ -6,6 +6,7 @@
"build": "next build",
"start": "next start",
"lint": "next lint",
"test": "jest",
"dev:worker": "webpack -c worker-pack/webpack.dev.js --watch",
"build:worker": "webpack -c worker-pack/webpack.prod.js"
},
@ -19,10 +20,12 @@
"react-mosaic-component": "^5.0.0"
},
"devDependencies": {
"@types/jest": "^27.4.0",
"@types/node": "16.11.11",
"@types/react": "17.0.37",
"eslint": "8.4.0",
"eslint-config-next": "12.0.7",
"jest": "^27.4.7",
"ts-loader": "^9.2.6",
"typescript": "4.5.2",
"webpack": "^5.65.0",

1867
yarn.lock

File diff suppressed because it is too large Load Diff