Skip to content

Commit 21715b0

Browse files
authored
refactor: extract SingleExecutionResult from ExecutionRecorder (#46)
1 parent ec96308 commit 21715b0

File tree

9 files changed

+56
-51
lines changed

9 files changed

+56
-51
lines changed

src/core/execute.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ import { WASI } from "node:wasi";
22
import { promises } from "node:fs";
33
import { ensureDirSync } from "fs-extra";
44
import { instantiate, Imports as ASImports } from "@assemblyscript/loader";
5-
import { AssertResult } from "../assertResult.js";
5+
import { ExecutionResult } from "../executionResult.js";
66
import { Imports, ImportsArgument } from "../index.js";
77
import { InstrumentResult } from "../interface.js";
88
import { mockInstrumentFunc } from "../utils/import.js";
99
import { supplyDefaultFunction } from "../utils/index.js";
1010
import { parseImportFunctionInfo } from "../utils/wasmparser.js";
11-
import { ExecutionRecorder } from "./executionRecorder.js";
11+
import { ExecutionRecorder, SingleExecutionResult } from "./executionRecorder.js";
1212
import { CoverageRecorder } from "./covRecorder.js";
1313

1414
const readFile = promises.readFile;
@@ -17,7 +17,7 @@ async function nodeExecutor(
1717
instrumentResult: InstrumentResult,
1818
outFolder: string,
1919
imports: Imports
20-
): Promise<ExecutionRecorder> {
20+
): Promise<SingleExecutionResult> {
2121
const wasi = new WASI({
2222
args: ["node", instrumentResult.baseName],
2323
env: process.env,
@@ -56,20 +56,20 @@ async function nodeExecutor(
5656
throw new Error("node executor abort.");
5757
}
5858
coverageRecorder.outputTrace(instrumentResult.traceFile);
59-
return executionRecorder;
59+
return executionRecorder.result;
6060
}
6161

6262
export async function execWasmBinaries(
6363
outFolder: string,
6464
instrumentResults: InstrumentResult[],
6565
imports: Imports
66-
): Promise<AssertResult> {
67-
const assertRes = new AssertResult();
66+
): Promise<ExecutionResult> {
67+
const assertRes = new ExecutionResult();
6868
ensureDirSync(outFolder);
6969
await Promise.all<void>(
7070
instrumentResults.map(async (instrumentResult): Promise<void> => {
71-
const recorder: ExecutionRecorder = await nodeExecutor(instrumentResult, outFolder, imports);
72-
await assertRes.merge(recorder, instrumentResult.expectInfo);
71+
const result: SingleExecutionResult = await nodeExecutor(instrumentResult, outFolder, imports);
72+
await assertRes.merge(result, instrumentResult.expectInfo);
7373
})
7474
);
7575
return assertRes;

src/core/executionRecorder.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,16 @@ class LogRecorder {
2727
}
2828
}
2929

30-
// to do: split execution environment and recorder
31-
export class ExecutionRecorder implements IAssertResult, UnitTestFramework {
30+
export class SingleExecutionResult implements IAssertResult {
3231
total: number = 0;
3332
fail: number = 0;
34-
failed_info: AssertFailMessage = {};
33+
failedInfo: AssertFailMessage = {};
3534
failedLogMessages: FailedLogMessages = {};
35+
}
36+
37+
// to do: split execution environment and recorder
38+
export class ExecutionRecorder implements UnitTestFramework {
39+
result = new SingleExecutionResult();
3640

3741
registerFunctions: [string, number][] = [];
3842
#currentTestDescriptions: string[] = [];
@@ -56,19 +60,21 @@ export class ExecutionRecorder implements IAssertResult, UnitTestFramework {
5660
const logMessages: string[] | null = this.#logRecorder.onFinishTest();
5761
if (logMessages !== null) {
5862
const testCaseFullName = this.#currentTestDescription;
59-
this.failedLogMessages[testCaseFullName] = (this.failedLogMessages[testCaseFullName] || []).concat(logMessages);
63+
this.result.failedLogMessages[testCaseFullName] = (this.result.failedLogMessages[testCaseFullName] || []).concat(
64+
logMessages
65+
);
6066
}
6167
}
6268

6369
collectCheckResult(result: boolean, codeInfoIndex: number, actualValue: string, expectValue: string): void {
64-
this.total++;
70+
this.result.total++;
6571
if (!result) {
6672
this.#logRecorder.markTestFailed();
67-
this.fail++;
73+
this.result.fail++;
6874
const testCaseFullName = this.#currentTestDescription;
6975
const assertMessage: AssertMessage = [codeInfoIndex.toString(), actualValue, expectValue];
70-
this.failed_info[testCaseFullName] = this.failed_info[testCaseFullName] || [];
71-
this.failed_info[testCaseFullName].push(assertMessage);
76+
this.result.failedInfo[testCaseFullName] = this.result.failedInfo[testCaseFullName] || [];
77+
this.result.failedInfo[testCaseFullName].push(assertMessage);
7278
}
7379
}
7480

src/assertResult.ts renamed to src/executionResult.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import chalk from "chalk";
55

66
const readFile = promises.readFile;
77

8-
export class AssertResult {
8+
export class ExecutionResult {
99
fail = 0;
1010
total = 0;
1111
failedInfos: FailedInfoMap = new Map();
@@ -18,7 +18,7 @@ export class AssertResult {
1818
try {
1919
const expectContent = await readFile(expectInfoFilePath, { encoding: "utf8" });
2020
expectInfo = json2map(JSON.parse(expectContent) as ExpectInfo);
21-
for (const [testcaseName, value] of json2map<AssertMessage[]>(result.failed_info)) {
21+
for (const [testcaseName, value] of json2map<AssertMessage[]>(result.failedInfo)) {
2222
const errorMsgs: string[] = [];
2323
for (const msg of value) {
2424
const [index, actualValue, expectValue] = msg;

src/interface.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export type ExpectInfo = Record<ExpectInfoIndex, ExpectInfoDebugLocation>;
7979
export interface IAssertResult {
8080
fail: number;
8181
total: number;
82-
failed_info: AssertFailMessage;
82+
failedInfo: AssertFailMessage;
8383
failedLogMessages: FailedLogMessages;
8484
}
8585

src/utils/projectRoot.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,3 @@ import { join } from "node:path";
22
import { fileURLToPath, URL } from "node:url";
33

44
export const projectRoot = join(fileURLToPath(new URL(".", import.meta.url)), "..", "..");
5-
console.log(projectRoot);

tests/cpp/lit/expectInstrument/expect.test.wasm.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/ts/test/core/executionRecorder.test.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ describe("execution recorder", () => {
77
recorder._addDescription("description");
88

99
recorder.collectCheckResult(false, 0, "", "");
10-
expect(recorder.failed_info).toHaveProperty("description");
10+
expect(recorder.result.failedInfo).toHaveProperty("description");
1111
});
1212
test("add multiple descriptions", () => {
1313
const recorder = new ExecutionRecorder();
1414
recorder._addDescription("description1");
1515
recorder._addDescription("description2");
1616

1717
recorder.collectCheckResult(false, 0, "", "");
18-
expect(recorder.failed_info).toHaveProperty("description1 - description2");
18+
expect(recorder.result.failedInfo).toHaveProperty("description1 - description2");
1919
});
2020

2121
test("remove descriptions", () => {
@@ -25,7 +25,7 @@ describe("execution recorder", () => {
2525
recorder._removeDescription();
2626

2727
recorder.collectCheckResult(false, 0, "", "");
28-
expect(recorder.failed_info).toHaveProperty("description1");
28+
expect(recorder.result.failedInfo).toHaveProperty("description1");
2929
});
3030
});
3131

@@ -34,23 +34,23 @@ describe("execution recorder", () => {
3434
const recorder = new ExecutionRecorder();
3535
recorder.collectCheckResult(false, 0, "actual", "expect");
3636

37-
expect(recorder.total).toBe(1);
38-
expect(recorder.fail).toBe(1);
37+
expect(recorder.result.total).toBe(1);
38+
expect(recorder.result.fail).toBe(1);
3939
});
4040
test("collect true results", () => {
4141
const recorder = new ExecutionRecorder();
4242
recorder.collectCheckResult(true, 0, "actual1", "expect1");
4343

44-
expect(recorder.total).toBe(1);
45-
expect(recorder.fail).toBe(0);
44+
expect(recorder.result.total).toBe(1);
45+
expect(recorder.result.fail).toBe(0);
4646
});
4747
test("collect multiple results", () => {
4848
const recorder = new ExecutionRecorder();
4949
recorder.collectCheckResult(true, 0, "actual1", "expect1");
5050
recorder.collectCheckResult(false, 1, "actual2", "expect2");
5151

52-
expect(recorder.total).toBe(2);
53-
expect(recorder.fail).toBe(1);
52+
expect(recorder.result.total).toBe(2);
53+
expect(recorder.result.fail).toBe(1);
5454
});
5555
});
5656
});
Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
11
import { join } from "node:path";
22
import { fileURLToPath, URL } from "node:url";
33
import { FailedInfoMap, IAssertResult } from "../../../src/interface.js";
4-
import { AssertResult } from "../../../src/assertResult.js";
4+
import { ExecutionResult } from "../../../src/executionResult.js";
55
import chalk from "chalk";
66

77
const __dirname = fileURLToPath(new URL(".", import.meta.url));
88

9-
test("no failed_info merge", async () => {
10-
const assertResult = new AssertResult();
9+
test("no failedInfo merge", async () => {
10+
const executionResult = new ExecutionResult();
1111
const testcaseA: IAssertResult = {
1212
fail: 0,
1313
total: 28,
14-
failed_info: {},
14+
failedInfo: {},
1515
failedLogMessages: {},
1616
};
1717
const expectInfoFIlePath = join(__dirname, "..", "fixture", "assertResultTest.expectInfo.json");
18-
await assertResult.merge(testcaseA, expectInfoFIlePath);
19-
expect(assertResult.fail).toEqual(0);
20-
expect(assertResult.total).toEqual(28);
21-
expect(assertResult.failedInfos).toEqual(new Map<string, string[]>());
18+
await executionResult.merge(testcaseA, expectInfoFIlePath);
19+
expect(executionResult.fail).toEqual(0);
20+
expect(executionResult.total).toEqual(28);
21+
expect(executionResult.failedInfos).toEqual(new Map<string, string[]>());
2222
});
2323

2424
test("equal failed", async () => {
25-
const assertResult = new AssertResult();
26-
const actualString = "A long sentence for testing errorMsg.length > 160 in assertResult.ts merge function";
27-
const expectString = "= A long sentence for testing errorMsg.length > 160 in assertResult.ts merge function ";
25+
const executionResult = new ExecutionResult();
26+
const actualString = "A long sentence for testing errorMsg.length > 160 in executionResult.ts merge function";
27+
const expectString = "= A long sentence for testing errorMsg.length > 160 in executionResult.ts merge function ";
2828
const testcaseA: IAssertResult = {
2929
fail: 1,
3030
total: 28,
31-
failed_info: {
31+
failedInfo: {
3232
A: [
3333
["1", "100", "= 200"],
3434
["3", "[10]", "= [1]"],
@@ -41,7 +41,7 @@ test("equal failed", async () => {
4141
},
4242
};
4343
const expectInfoFIlePath = join(__dirname, "..", "fixture", "assertResultTest.expectInfo.json");
44-
await assertResult.merge(testcaseA, expectInfoFIlePath);
44+
await executionResult.merge(testcaseA, expectInfoFIlePath);
4545
const expectFailedInfo: FailedInfoMap = new Map();
4646
expectFailedInfo.set("A", {
4747
assertMessages: [
@@ -52,36 +52,36 @@ test("equal failed", async () => {
5252
],
5353
logMessages: ["log message 1", "log message 2", "log message 3"],
5454
});
55-
expect(assertResult.fail).toEqual(1);
56-
expect(assertResult.total).toEqual(28);
57-
expect(assertResult.failedInfos).toEqual(expectFailedInfo);
55+
expect(executionResult.fail).toEqual(1);
56+
expect(executionResult.total).toEqual(28);
57+
expect(executionResult.failedInfos).toEqual(expectFailedInfo);
5858
});
5959

6060
test("print", async () => {
61-
const assertResult = new AssertResult();
61+
const executionResult = new ExecutionResult();
6262
const testcaseA: IAssertResult = {
6363
fail: 1,
6464
total: 28,
65-
failed_info: {
65+
failedInfo: {
6666
A: [["1", "100", "= 200"]],
6767
},
6868
failedLogMessages: {
6969
A: ["log message 1", "log message 2", "log message 3"],
7070
},
7171
};
7272
const expectInfoFIlePath = join(__dirname, "..", "fixture", "assertResultTest.expectInfo.json");
73-
await assertResult.merge(testcaseA, expectInfoFIlePath);
73+
await executionResult.merge(testcaseA, expectInfoFIlePath);
7474

7575
{
7676
const outputs: string[] = [];
7777
chalk.level = 0; // disable color
78-
assertResult.print((msg) => outputs.push(msg));
78+
executionResult.print((msg) => outputs.push(msg));
7979
expect(outputs.join("\n")).toMatchSnapshot();
8080
}
8181
{
8282
const outputs: string[] = [];
8383
chalk.level = 1; // force enable color
84-
assertResult.print((msg) => outputs.push(msg));
84+
executionResult.print((msg) => outputs.push(msg));
8585
expect(outputs.join("\n")).toMatchSnapshot();
8686
}
8787
});

0 commit comments

Comments
 (0)