Skip to content

Commit 1dc6d9b

Browse files
authored
replace global mockInstrument function with a local one (#66)
1 parent 52f6d81 commit 1dc6d9b

File tree

16 files changed

+240
-215
lines changed

16 files changed

+240
-215
lines changed

assembly/implement.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,22 @@ export function testImpl(name: string, testFunction: () => void): void {
1616
}
1717

1818
export function mockImpl<T extends Function>(
19-
oldFunction: T,
20-
newFunction: T,
19+
originalFunction: T,
20+
mockFunction: T,
2121
): MockFn {
22-
if (!isFunction<T>(oldFunction) || !isFunction<T>(newFunction)) {
22+
if (!isFunction<T>(originalFunction) || !isFunction<T>(mockFunction)) {
2323
ERROR("mock paramemter receive a function");
2424
}
25-
const mockFn = new MockFn(oldFunction.index, newFunction.index);
26-
mockFunctionStatus.set(oldFunction.index, newFunction.index);
25+
const mockFn = new MockFn(originalFunction.index, mockFunction.index);
26+
mockFunctionStatus.setMockFunction(
27+
originalFunction.index,
28+
mockFunction.index,
29+
);
2730
return mockFn;
2831
}
29-
export function unmockImpl<T extends Function>(oldFunction: T): void {
30-
mockFunctionStatus.setIgnore(oldFunction.index, true);
32+
export function unmockImpl<T extends Function>(originalFunction: T): void {
33+
mockFunctionStatus.setMockedFunctionIgnore(originalFunction.index, true);
3134
}
32-
export function remockImpl<T extends Function>(oldFunction: T): void {
33-
mockFunctionStatus.setIgnore(oldFunction.index, false);
35+
export function remockImpl<T extends Function>(originalFunction: T): void {
36+
mockFunctionStatus.setMockedFunctionIgnore(originalFunction.index, false);
3437
}

assembly/index.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,27 @@ export function test(name: string, testFunction: () => void): void {
2929

3030
/**
3131
* mock some function
32-
* @param oldFunction function you want to mock
33-
* @param newFunction the new function.
32+
* @param originalFunction function you want to mock
33+
* @param mockFunction the new function.
3434
* @returns Mock Status { callTime : u32}
3535
*/
3636
export function mock<T extends Function>(
37-
oldFunction: T,
38-
newFunction: T,
37+
originalFunction: T,
38+
mockFunction: T,
3939
): MockFn {
40-
return mockImpl<T>(oldFunction, newFunction);
40+
return mockImpl<T>(originalFunction, mockFunction);
4141
}
4242
/**
4343
* unmock this function, can only be used in mocked function
4444
*/
45-
export function unmock<T extends Function>(oldFunction: T): void {
46-
unmockImpl(oldFunction);
45+
export function unmock<T extends Function>(originalFunction: T): void {
46+
unmockImpl(originalFunction);
4747
}
4848
/**
4949
* remock this function, can only be used in mocked function. Pair of {unmock}
5050
*/
51-
export function remock<T extends Function>(oldFunction: T): void {
52-
remockImpl(oldFunction);
51+
export function remock<T extends Function>(originalFunction: T): void {
52+
remockImpl(originalFunction);
5353
}
5454

5555
export function expect<T>(value: T): Value<T> {

assembly/mockInstrument.ts

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,35 @@
1-
export declare namespace mockFunctionStatus {
2-
function clear(): void;
3-
function set(k: u32, v: u32): void;
4-
function get(k: u32): u32;
5-
function lastGet(): u32;
6-
function has(k: u32): bool;
7-
function getCalls(oldIndex: u32, newIndex: u32): u32;
8-
function setIgnore(k: u32, v: bool): void;
1+
export namespace mockFunctionStatus {
2+
3+
@external("__unittest_framework_env","setMockFunction")
4+
export declare function setMockFunction(
5+
originalFunctionIndex: u32,
6+
mockFunctionIndex: u32,
7+
): void;
8+
9+
10+
@external("__unittest_framework_env","getMockedFunctionCalls")
11+
export declare function getMockedFunctionCalls(
12+
originalFunctionIndex: u32,
13+
mockFunctionIndex: u32,
14+
): u32;
15+
16+
17+
@external("__unittest_framework_env","setMockedFunctionIgnore")
18+
export declare function setMockedFunctionIgnore(
19+
originalFunctionIndex: u32,
20+
ignore: bool,
21+
): void;
922
}
1023

1124
export class MockFn {
1225
get calls(): u32 {
13-
return mockFunctionStatus.getCalls(this.oldIndex, this.newIndex);
26+
return mockFunctionStatus.getMockedFunctionCalls(
27+
this.originalFunctionIndex,
28+
this.mockFunctionIndex,
29+
);
1430
}
1531
constructor(
16-
public oldIndex: u32,
17-
public newIndex: u32,
32+
public originalFunctionIndex: u32,
33+
public mockFunctionIndex: u32,
1834
) {}
1935
}

instrumentation/CovInstrumentationWalker.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ void CovInstrumentationWalker::introduceReportFun() noexcept {
2626
std::array<BinaryenType, 3U> iii_{BinaryenTypeInt32(), BinaryenTypeInt32(),
2727
BinaryenTypeInt32()};
2828
const BinaryenType iii = BinaryenTypeCreate(iii_.data(), iii_.size());
29-
BinaryenAddFunctionImport(module, reportFunName, "covInstrument", "traceExpression", iii,
29+
BinaryenAddFunctionImport(module, reportFunName, "__unittest_framework_env", "traceExpression", iii,
3030
wasm::Type::none);
3131
}
3232
}

instrumentation/MockInstrumentationWalker.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ bool MockInstrumentationWalker::mockFunctionDuplicateImportedCheck() const noexc
8383
std::array<BinaryenType, 2U> ii_ =
8484
std::array<BinaryenType, 2U>{BinaryenTypeInt32(), BinaryenTypeInt32()};
8585
const BinaryenType ii = BinaryenTypeCreate(ii_.data(), ii_.size());
86-
BinaryenAddFunctionImport(module, this->checkMock.data(), "mockInstrument", "checkMock", ii,
86+
BinaryenAddFunctionImport(module, this->checkMock.data(), "__unittest_framework_env", "checkMock", ii,
8787
BinaryenTypeInt32());
8888
}
8989
return checkRepeat;

instrumentation/MockInstrumentationWalker.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class MockInstrumentationWalker final : public wasm::PostWalker<MockInstrumentat
2727
/// @param _checkMock
2828
explicit MockInstrumentationWalker(
2929
wasm::Module *const _module,
30-
const std::string _checkMock = "mockInstrument/checkMock") noexcept
30+
const std::string _checkMock = "__unittest_framework_env/checkMock") noexcept
3131
: module(_module), checkMock(_checkMock), moduleBuilder(wasm::Builder(*_module)) {
3232
for (const auto &elemSegment : _module->elementSegments) {
3333
if (elemSegment->type.isFunction()) {

src/core/covRecorder.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,19 @@ export class CoverageRecorder {
55

66
getCollectionFuncSet(): Record<string, unknown> {
77
return {
8-
covInstrument: {
9-
traceExpression: (functionIndex: number, basicBlockIndex: number, type: number): void => {
10-
switch (type) {
11-
case 1: // call in
12-
case 2: {
13-
// call out
14-
// do not need for now
15-
break;
16-
}
17-
case 0: {
18-
this._runtimeTrace.push([functionIndex, basicBlockIndex]);
19-
break;
20-
}
8+
traceExpression: (functionIndex: number, basicBlockIndex: number, type: number): void => {
9+
switch (type) {
10+
case 1: // call in
11+
case 2: {
12+
// call out
13+
// do not need for now
14+
break;
2115
}
22-
},
16+
case 0: {
17+
this._runtimeTrace.push([functionIndex, basicBlockIndex]);
18+
break;
19+
}
20+
}
2321
},
2422
};
2523
}

src/core/execute.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import { ensureDirSync } from "fs-extra";
44
import { instantiate, Imports as ASImports } from "@assemblyscript/loader";
55
import { ExecutionResultSummary } from "../executionResult.js";
66
import { Imports, ImportsArgument, InstrumentResult } from "../interface.js";
7-
import { mockInstrumentFunc } from "../utils/import.js";
87
import { supplyDefaultFunction } from "../utils/index.js";
98
import { parseImportFunctionInfo } from "../utils/wasmparser.js";
109
import { ExecutionRecorder, ExecutionResult } from "./executionRecorder.js";
10+
import { MockStatusRecorder } from "./mockStatusRecorder.js";
1111
import { CoverageRecorder } from "./covRecorder.js";
1212
import assert from "node:assert";
1313
import { ExecutionError, handleWebAssemblyError } from "../utils/errorTraceHandler.js";
@@ -31,14 +31,17 @@ async function nodeExecutor(
3131

3232
const executionRecorder = new ExecutionRecorder();
3333
const coverageRecorder = new CoverageRecorder();
34+
const mockStatusRecorder = new MockStatusRecorder();
3435

3536
const importsArg = new ImportsArgument(executionRecorder);
3637
const userDefinedImportsObject = imports === undefined ? {} : imports!(importsArg);
3738
const importObject: ASImports = {
3839
wasi_snapshot_preview1: wasi.wasiImport,
39-
...executionRecorder.getCollectionFuncSet(importsArg),
40-
mockInstrument: mockInstrumentFunc,
41-
...coverageRecorder.getCollectionFuncSet(),
40+
__unittest_framework_env: {
41+
...executionRecorder.getCollectionFuncSet(importsArg),
42+
...mockStatusRecorder.getMockFuncSet(),
43+
...coverageRecorder.getCollectionFuncSet(),
44+
},
4245
...userDefinedImportsObject,
4346
} as ASImports;
4447
const binaryBuffer = await readFile(instrumentResult.instrumentedWasm);
@@ -90,7 +93,7 @@ async function nodeExecutor(
9093
await exceptionHandler(error);
9194
}
9295
executionRecorder.finishTestFunction();
93-
mockInstrumentFunc["mockFunctionStatus.clear"]();
96+
mockStatusRecorder.clear();
9497
}
9598
}
9699

src/core/executionRecorder.ts

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -107,26 +107,24 @@ export class ExecutionRecorder implements UnitTestFramework {
107107
this.logRecorder.addLog(msg);
108108
}
109109

110-
getCollectionFuncSet(arg: ImportsArgument): Record<string, Record<string, unknown>> {
110+
getCollectionFuncSet(arg: ImportsArgument): Record<string, unknown> {
111111
return {
112-
__unittest_framework_env: {
113-
addDescription: (description: number): void => {
114-
this._addDescription(arg.exports!.__getString(description));
115-
},
116-
removeDescription: (): void => {
117-
this._removeDescription();
118-
},
119-
registerTestFunction: (index: number): void => {
120-
this._registerTestFunction(index);
121-
},
122-
collectCheckResult: (result: number, codeInfoIndex: number, actualValue: number, expectValue: number): void => {
123-
this.collectCheckResult(
124-
result !== 0,
125-
codeInfoIndex,
126-
arg.exports!.__getString(actualValue),
127-
arg.exports!.__getString(expectValue)
128-
);
129-
},
112+
addDescription: (description: number): void => {
113+
this._addDescription(arg.exports!.__getString(description));
114+
},
115+
removeDescription: (): void => {
116+
this._removeDescription();
117+
},
118+
registerTestFunction: (index: number): void => {
119+
this._registerTestFunction(index);
120+
},
121+
collectCheckResult: (result: number, codeInfoIndex: number, actualValue: number, expectValue: number): void => {
122+
this.collectCheckResult(
123+
result !== 0,
124+
codeInfoIndex,
125+
arg.exports!.__getString(actualValue),
126+
arg.exports!.__getString(expectValue)
127+
);
130128
},
131129
};
132130
}

src/core/instrument.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export async function instrument(
1616
const baseName = sourceFile.slice(0, -5);
1717
const result = new InstrumentResult(baseName);
1818

19-
const reportFunction = "covInstrument/traceExpression";
19+
const reportFunction = "__unittest_framework_env/traceExpression";
2020

2121
const source = instrumenter.allocateUTF8(sourceFile);
2222
const output = instrumenter.allocateUTF8(result.instrumentedWasm);

0 commit comments

Comments
 (0)