Set up unit testing for engines
This commit is contained in:
@ -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
48
engines/test-utils.ts
Normal 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);
|
||||
}
|
||||
});
|
||||
};
|
Reference in New Issue
Block a user