Skip to content

Commit ce5e7f5

Browse files
committed
fix
1 parent b365767 commit ce5e7f5

File tree

6 files changed

+39
-30
lines changed

6 files changed

+39
-30
lines changed

assembly/env.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ export namespace assertResult {
1212
export declare function registerTestFunction(index: u32): void;
1313

1414

15-
@external("__unittest_framework_env","finishTestFunction")
16-
export declare function finishTestFunction(): void;
17-
18-
1915
@external("__unittest_framework_env","collectCheckResult")
2016
export declare function collectCheckResult(
2117
result: bool,

assembly/implement.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ export function describeImpl(
1212
export function testImpl(description: string, testFunction: () => void): void {
1313
assertResult.addDescription(description);
1414
assertResult.registerTestFunction(testFunction.index);
15-
assertResult.finishTestFunction();
1615
assertResult.removeDescription();
1716
}
1817

src/core/execute.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ async function nodeExecutor(
3131
const executionRecorder = new ExecutionRecorder();
3232
const coverageRecorder = new CoverageRecorder();
3333

34-
const importsArg = new ImportsArgument();
34+
const importsArg = new ImportsArgument(executionRecorder);
3535
const userDefinedImportsObject = imports === undefined ? {} : imports!(importsArg);
3636
const importObject: ASImports = {
3737
wasi_snapshot_preview1: wasi.wasiImport,
@@ -53,18 +53,21 @@ async function nodeExecutor(
5353
const execTestFunction = ins.exports["executeTestFunction"];
5454
assert(typeof execTestFunction === "function");
5555
if (matchedTestNames === undefined) {
56-
// means execute all testFunctions
56+
// By default, all testcases are executed
5757
for (const functionInfo of executionRecorder.registerFunctions) {
58-
const functionIndex = functionInfo[1];
58+
const [testCaseName, functionIndex] = functionInfo;
59+
executionRecorder.startTestFunction(testCaseName);
5960
(execTestFunction as (a: number) => void)(functionIndex);
61+
executionRecorder.finishTestFunction();
6062
mockInstrumentFunc["mockFunctionStatus.clear"]();
6163
}
6264
} else {
6365
for (const functionInfo of executionRecorder.registerFunctions) {
64-
const [testName, functionIndex] = functionInfo;
65-
if (matchedTestNames.includes(testName)) {
66-
console.log(testName);
66+
const [testCaseName, functionIndex] = functionInfo;
67+
if (matchedTestNames.includes(testCaseName)) {
68+
executionRecorder.startTestFunction(testCaseName);
6769
(execTestFunction as (a: number) => void)(functionIndex);
70+
executionRecorder.finishTestFunction();
6871
mockInstrumentFunc["mockFunctionStatus.clear"]();
6972
}
7073
}

src/core/executionRecorder.ts

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,11 @@ export class ExecutionRecorder implements UnitTestFramework {
4646

4747
registerFunctions: [string, number][] = [];
4848
#currentTestDescriptions: string[] = [];
49-
#logRecorder = new LogRecorder();
49+
#testCaseFullName: string = "";
50+
logRecorder = new LogRecorder();
5051

51-
get #currentTestDescription(): string {
52-
return this.#currentTestDescriptions.join(" ");
52+
set testCaseFullName(testCaseFullName: string) {
53+
this.#testCaseFullName = testCaseFullName;
5354
}
5455

5556
_addDescription(description: string): void {
@@ -58,34 +59,37 @@ export class ExecutionRecorder implements UnitTestFramework {
5859
_removeDescription(): void {
5960
this.#currentTestDescriptions.pop();
6061
}
61-
registerTestFunction(fncIndex: number): void {
62-
this.registerFunctions.push([this.#currentTestDescription, fncIndex]);
63-
this.#logRecorder.onStartTest();
62+
_registerTestFunction(fncIndex: number): void {
63+
const testCaseFullName = this.#currentTestDescriptions.join(" ");
64+
this.registerFunctions.push([testCaseFullName, fncIndex]);
6465
}
65-
_finishTestFunction(): void {
66-
const logMessages: string[] | null = this.#logRecorder.onFinishTest();
66+
startTestFunction(testCaseFullName: string): void {
67+
this.#testCaseFullName = testCaseFullName;
68+
this.logRecorder.onStartTest();
69+
}
70+
finishTestFunction(): void {
71+
const logMessages: string[] | null = this.logRecorder.onFinishTest();
6772
if (logMessages !== null) {
68-
const testCaseFullName = this.#currentTestDescription;
69-
this.result.failedLogMessages[testCaseFullName] = (this.result.failedLogMessages[testCaseFullName] || []).concat(
70-
logMessages
71-
);
73+
this.result.failedLogMessages[this.#testCaseFullName] = (
74+
this.result.failedLogMessages[this.#testCaseFullName] || []
75+
).concat(logMessages);
7276
}
7377
}
7478

7579
collectCheckResult(result: boolean, codeInfoIndex: number, actualValue: string, expectValue: string): void {
7680
this.result.total++;
7781
if (!result) {
78-
this.#logRecorder.markTestFailed();
82+
this.logRecorder.markTestFailed();
7983
this.result.fail++;
80-
const testCaseFullName = this.#currentTestDescription;
84+
const testCaseFullName = this.#testCaseFullName;
8185
const assertMessage: AssertMessage = [codeInfoIndex.toString(), actualValue, expectValue];
8286
this.result.failedInfo[testCaseFullName] = this.result.failedInfo[testCaseFullName] || [];
8387
this.result.failedInfo[testCaseFullName].push(assertMessage);
8488
}
8589
}
8690

8791
log(msg: string): void {
88-
this.#logRecorder.addLog(msg);
92+
this.logRecorder.addLog(msg);
8993
}
9094

9195
getCollectionFuncSet(arg: ImportsArgument): Record<string, Record<string, unknown>> {
@@ -98,10 +102,7 @@ export class ExecutionRecorder implements UnitTestFramework {
98102
this._removeDescription();
99103
},
100104
registerTestFunction: (index: number): void => {
101-
this.registerTestFunction(index);
102-
},
103-
finishTestFunction: () => {
104-
this._finishTestFunction();
105+
this._registerTestFunction(index);
105106
},
106107
collectCheckResult: (result: number, codeInfoIndex: number, actualValue: number, expectValue: number): void => {
107108
this.collectCheckResult(

src/interface.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ export class ImportsArgument {
186186
module: WebAssembly.Module | null = null;
187187
instance: WebAssembly.Instance | null = null;
188188
exports: (ASUtil & Record<string, unknown>) | null = null;
189+
constructor(public framework: UnitTestFramework) {}
189190
}
190191

191192
export type Imports = ((arg: ImportsArgument) => Record<string, unknown>) | null;

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,21 @@ describe("execution recorder", () => {
55
test("add single description", () => {
66
const recorder = new ExecutionRecorder();
77
recorder._addDescription("description");
8+
recorder._registerTestFunction(1);
9+
expect(recorder.registerFunctions).toEqual([["description", 1]]);
810

11+
recorder.startTestFunction("description");
912
recorder.collectCheckResult(false, 0, "", "");
1013
expect(recorder.result.failedInfo).toHaveProperty("description");
1114
});
1215
test("add multiple descriptions", () => {
1316
const recorder = new ExecutionRecorder();
1417
recorder._addDescription("description1");
1518
recorder._addDescription("description2");
19+
recorder._registerTestFunction(1);
20+
expect(recorder.registerFunctions).toEqual([["description1 description2", 1]]);
1621

22+
recorder.startTestFunction("description1 description2");
1723
recorder.collectCheckResult(false, 0, "", "");
1824
expect(recorder.result.failedInfo).toHaveProperty("description1 description2");
1925
});
@@ -23,7 +29,10 @@ describe("execution recorder", () => {
2329
recorder._addDescription("description1");
2430
recorder._addDescription("description2");
2531
recorder._removeDescription();
32+
recorder._registerTestFunction(1);
33+
expect(recorder.registerFunctions).toEqual([["description1", 1]]);
2634

35+
recorder.startTestFunction("description1");
2736
recorder.collectCheckResult(false, 0, "", "");
2837
expect(recorder.result.failedInfo).toHaveProperty("description1");
2938
});

0 commit comments

Comments
 (0)