From 2f3b93da7c3f60b2ed6df7fee7dbe161e8051c84 Mon Sep 17 00:00:00 2001 From: XMadrid Date: Mon, 22 Sep 2025 16:22:46 +0800 Subject: [PATCH 1/2] replace global mockInstrument function with a local one --- assembly/implement.ts | 18 ++--- assembly/index.ts | 18 ++--- assembly/mockInstrument.ts | 24 +++--- instrumentation/CovInstrumentationWalker.cpp | 2 +- instrumentation/MockInstrumentationWalker.cpp | 2 +- instrumentation/MockInstrumentationWalker.hpp | 2 +- src/core/covRecorder.ts | 26 +++---- src/core/execute.ts | 13 ++-- src/core/executionRecorder.ts | 36 ++++----- src/core/instrument.ts | 2 +- src/core/mockStatusRecorder.ts | 74 ++++++++++++++++++ src/utils/import.ts | 62 --------------- tests/cpp/lit/run.cjs | 77 +++++++++---------- tests/e2e/assertFailed/stdout.txt | 4 +- tests/ts/test/core/mockStatusRecorder.test.ts | 38 +++++++++ tests/ts/test/utils/import.test.ts | 40 ---------- 16 files changed, 223 insertions(+), 215 deletions(-) create mode 100644 src/core/mockStatusRecorder.ts delete mode 100644 src/utils/import.ts create mode 100644 tests/ts/test/core/mockStatusRecorder.test.ts delete mode 100644 tests/ts/test/utils/import.test.ts diff --git a/assembly/implement.ts b/assembly/implement.ts index adab134..6eb3a84 100644 --- a/assembly/implement.ts +++ b/assembly/implement.ts @@ -16,19 +16,19 @@ export function testImpl(name: string, testFunction: () => void): void { } export function mockImpl( - oldFunction: T, - newFunction: T, + originalFunction: T, + mockFunction: T, ): MockFn { - if (!isFunction(oldFunction) || !isFunction(newFunction)) { + if (!isFunction(originalFunction) || !isFunction(mockFunction)) { ERROR("mock paramemter receive a function"); } - const mockFn = new MockFn(oldFunction.index, newFunction.index); - mockFunctionStatus.set(oldFunction.index, newFunction.index); + const mockFn = new MockFn(originalFunction.index, mockFunction.index); + mockFunctionStatus.setMockFunction(originalFunction.index, mockFunction.index); return mockFn; } -export function unmockImpl(oldFunction: T): void { - mockFunctionStatus.setIgnore(oldFunction.index, true); +export function unmockImpl(originalFunction: T): void { + mockFunctionStatus.setMockedFunctionIgnore(originalFunction.index, true); } -export function remockImpl(oldFunction: T): void { - mockFunctionStatus.setIgnore(oldFunction.index, false); +export function remockImpl(originalFunction: T): void { + mockFunctionStatus.setMockedFunctionIgnore(originalFunction.index, false); } diff --git a/assembly/index.ts b/assembly/index.ts index c1696ae..9522494 100644 --- a/assembly/index.ts +++ b/assembly/index.ts @@ -29,27 +29,27 @@ export function test(name: string, testFunction: () => void): void { /** * mock some function - * @param oldFunction function you want to mock - * @param newFunction the new function. + * @param originalFunction function you want to mock + * @param mockFunction the new function. * @returns Mock Status { callTime : u32} */ export function mock( - oldFunction: T, - newFunction: T, + originalFunction: T, + mockFunction: T, ): MockFn { - return mockImpl(oldFunction, newFunction); + return mockImpl(originalFunction, mockFunction); } /** * unmock this function, can only be used in mocked function */ -export function unmock(oldFunction: T): void { - unmockImpl(oldFunction); +export function unmock(originalFunction: T): void { + unmockImpl(originalFunction); } /** * remock this function, can only be used in mocked function. Pair of {unmock} */ -export function remock(oldFunction: T): void { - remockImpl(oldFunction); +export function remock(originalFunction: T): void { + remockImpl(originalFunction); } export function expect(value: T): Value { diff --git a/assembly/mockInstrument.ts b/assembly/mockInstrument.ts index 4db244e..0d31e71 100644 --- a/assembly/mockInstrument.ts +++ b/assembly/mockInstrument.ts @@ -1,19 +1,21 @@ -export declare namespace mockFunctionStatus { - function clear(): void; - function set(k: u32, v: u32): void; - function get(k: u32): u32; - function lastGet(): u32; - function has(k: u32): bool; - function getCalls(oldIndex: u32, newIndex: u32): u32; - function setIgnore(k: u32, v: bool): void; +export namespace mockFunctionStatus { + + @external("__unittest_framework_env","setMockFunction") + export declare function setMockFunction(originalFunctionIndex: u32, mockFunctionIndex: u32): void; + + @external("__unittest_framework_env","getMockedFunctionCalls") + export declare function getMockedFunctionCalls(originalFunctionIndex: u32, mockFunctionIndex: u32): u32; + + @external("__unittest_framework_env","setMockedFunctionIgnore") + export declare function setMockedFunctionIgnore(originalFunctionIndex: u32, ignore: bool): void; } export class MockFn { get calls(): u32 { - return mockFunctionStatus.getCalls(this.oldIndex, this.newIndex); + return mockFunctionStatus.getMockedFunctionCalls(this.originalFunctionIndex, this.mockFunctionIndex); } constructor( - public oldIndex: u32, - public newIndex: u32, + public originalFunctionIndex: u32, + public mockFunctionIndex: u32, ) {} } diff --git a/instrumentation/CovInstrumentationWalker.cpp b/instrumentation/CovInstrumentationWalker.cpp index 24fdf7c..f343a40 100644 --- a/instrumentation/CovInstrumentationWalker.cpp +++ b/instrumentation/CovInstrumentationWalker.cpp @@ -26,7 +26,7 @@ void CovInstrumentationWalker::introduceReportFun() noexcept { std::array iii_{BinaryenTypeInt32(), BinaryenTypeInt32(), BinaryenTypeInt32()}; const BinaryenType iii = BinaryenTypeCreate(iii_.data(), iii_.size()); - BinaryenAddFunctionImport(module, reportFunName, "covInstrument", "traceExpression", iii, + BinaryenAddFunctionImport(module, reportFunName, "__unittest_framework_env", "traceExpression", iii, wasm::Type::none); } } diff --git a/instrumentation/MockInstrumentationWalker.cpp b/instrumentation/MockInstrumentationWalker.cpp index 19967be..5968b94 100644 --- a/instrumentation/MockInstrumentationWalker.cpp +++ b/instrumentation/MockInstrumentationWalker.cpp @@ -83,7 +83,7 @@ bool MockInstrumentationWalker::mockFunctionDuplicateImportedCheck() const noexc std::array ii_ = std::array{BinaryenTypeInt32(), BinaryenTypeInt32()}; const BinaryenType ii = BinaryenTypeCreate(ii_.data(), ii_.size()); - BinaryenAddFunctionImport(module, this->checkMock.data(), "mockInstrument", "checkMock", ii, + BinaryenAddFunctionImport(module, this->checkMock.data(), "__unittest_framework_env", "checkMock", ii, BinaryenTypeInt32()); } return checkRepeat; diff --git a/instrumentation/MockInstrumentationWalker.hpp b/instrumentation/MockInstrumentationWalker.hpp index 39db9b8..f71708a 100644 --- a/instrumentation/MockInstrumentationWalker.hpp +++ b/instrumentation/MockInstrumentationWalker.hpp @@ -27,7 +27,7 @@ class MockInstrumentationWalker final : public wasm::PostWalkerelementSegments) { if (elemSegment->type.isFunction()) { diff --git a/src/core/covRecorder.ts b/src/core/covRecorder.ts index b69b5ee..3e84f37 100644 --- a/src/core/covRecorder.ts +++ b/src/core/covRecorder.ts @@ -5,21 +5,19 @@ export class CoverageRecorder { getCollectionFuncSet(): Record { return { - covInstrument: { - traceExpression: (functionIndex: number, basicBlockIndex: number, type: number): void => { - switch (type) { - case 1: // call in - case 2: { - // call out - // do not need for now - break; - } - case 0: { - this._runtimeTrace.push([functionIndex, basicBlockIndex]); - break; - } + traceExpression: (functionIndex: number, basicBlockIndex: number, type: number): void => { + switch (type) { + case 1: // call in + case 2: { + // call out + // do not need for now + break; } - }, + case 0: { + this._runtimeTrace.push([functionIndex, basicBlockIndex]); + break; + } + } }, }; } diff --git a/src/core/execute.ts b/src/core/execute.ts index 2ceb7db..a87e95c 100644 --- a/src/core/execute.ts +++ b/src/core/execute.ts @@ -4,10 +4,10 @@ import { ensureDirSync } from "fs-extra"; import { instantiate, Imports as ASImports } from "@assemblyscript/loader"; import { ExecutionResultSummary } from "../executionResult.js"; import { Imports, ImportsArgument, InstrumentResult } from "../interface.js"; -import { mockInstrumentFunc } from "../utils/import.js"; import { supplyDefaultFunction } from "../utils/index.js"; import { parseImportFunctionInfo } from "../utils/wasmparser.js"; import { ExecutionRecorder, ExecutionResult } from "./executionRecorder.js"; +import { MockStatusRecorder } from "./mockStatusRecorder.js"; import { CoverageRecorder } from "./covRecorder.js"; import assert from "node:assert"; import { ExecutionError, handleWebAssemblyError } from "../utils/errorTraceHandler.js"; @@ -31,14 +31,17 @@ async function nodeExecutor( const executionRecorder = new ExecutionRecorder(); const coverageRecorder = new CoverageRecorder(); + const mockStatusRecorder = new MockStatusRecorder(); const importsArg = new ImportsArgument(executionRecorder); const userDefinedImportsObject = imports === undefined ? {} : imports!(importsArg); const importObject: ASImports = { wasi_snapshot_preview1: wasi.wasiImport, - ...executionRecorder.getCollectionFuncSet(importsArg), - mockInstrument: mockInstrumentFunc, - ...coverageRecorder.getCollectionFuncSet(), + __unittest_framework_env: { + ...executionRecorder.getCollectionFuncSet(importsArg), + ...mockStatusRecorder.getMockFuncSet(), + ...coverageRecorder.getCollectionFuncSet(), + }, ...userDefinedImportsObject, } as ASImports; const binaryBuffer = await readFile(instrumentResult.instrumentedWasm); @@ -90,7 +93,7 @@ async function nodeExecutor( await exceptionHandler(error); } executionRecorder.finishTestFunction(); - mockInstrumentFunc["mockFunctionStatus.clear"](); + mockStatusRecorder.clear(); } } diff --git a/src/core/executionRecorder.ts b/src/core/executionRecorder.ts index b49241a..4ee5391 100644 --- a/src/core/executionRecorder.ts +++ b/src/core/executionRecorder.ts @@ -107,26 +107,24 @@ export class ExecutionRecorder implements UnitTestFramework { this.logRecorder.addLog(msg); } - getCollectionFuncSet(arg: ImportsArgument): Record> { + getCollectionFuncSet(arg: ImportsArgument): Record { return { - __unittest_framework_env: { - addDescription: (description: number): void => { - this._addDescription(arg.exports!.__getString(description)); - }, - removeDescription: (): void => { - this._removeDescription(); - }, - registerTestFunction: (index: number): void => { - this._registerTestFunction(index); - }, - collectCheckResult: (result: number, codeInfoIndex: number, actualValue: number, expectValue: number): void => { - this.collectCheckResult( - result !== 0, - codeInfoIndex, - arg.exports!.__getString(actualValue), - arg.exports!.__getString(expectValue) - ); - }, + addDescription: (description: number): void => { + this._addDescription(arg.exports!.__getString(description)); + }, + removeDescription: (): void => { + this._removeDescription(); + }, + registerTestFunction: (index: number): void => { + this._registerTestFunction(index); + }, + collectCheckResult: (result: number, codeInfoIndex: number, actualValue: number, expectValue: number): void => { + this.collectCheckResult( + result !== 0, + codeInfoIndex, + arg.exports!.__getString(actualValue), + arg.exports!.__getString(expectValue) + ); }, }; } diff --git a/src/core/instrument.ts b/src/core/instrument.ts index 9bada45..026682a 100644 --- a/src/core/instrument.ts +++ b/src/core/instrument.ts @@ -16,7 +16,7 @@ export async function instrument( const baseName = sourceFile.slice(0, -5); const result = new InstrumentResult(baseName); - const reportFunction = "covInstrument/traceExpression"; + const reportFunction = "__unittest_framework_env/traceExpression"; const source = instrumenter.allocateUTF8(sourceFile); const output = instrumenter.allocateUTF8(result.instrumentedWasm); diff --git a/src/core/mockStatusRecorder.ts b/src/core/mockStatusRecorder.ts new file mode 100644 index 0000000..728f69a --- /dev/null +++ b/src/core/mockStatusRecorder.ts @@ -0,0 +1,74 @@ +interface MockObject { + calls: number; + ignore: boolean; + mockFunctionIndex: number; +} + +export class MockStatusRecorder { + private _mockStatus = new Map(); + + private hasMocked(functionIndex: number): boolean { + const mockObject = this._mockStatus.get(functionIndex); + if (mockObject === undefined) { + return false; + } + return !mockObject.ignore; + } + + // isCall = true, return -1 if not mocked; + // isCall = false, return oldIndex if not mocked. + _checkMock(functionIndex: number, isCall: boolean): number { + if (this.hasMocked(functionIndex)) { + const mockObject = this._mockStatus.get(functionIndex); + mockObject!.calls++; + return mockObject!.mockFunctionIndex; + } + return isCall ? -1 : functionIndex; + } + + _setMockFunction(originalFunctionIndex: number, mockFunctionIndex: number): void { + const mockObject: MockObject = { + calls: 0, + ignore: false, + mockFunctionIndex, + } + this._mockStatus.set(originalFunctionIndex, mockObject); + } + + _getMockedFunctionCalls(originalFunctionIndex: number, mockFunctionIndex: number): number { + const mockObject = this._mockStatus.get(originalFunctionIndex); + if (mockObject === undefined || mockObject.mockFunctionIndex !== mockFunctionIndex) { + return 0; + } + return mockObject.calls; + } + + _setMockedFunctionIgnore(originalFunctionIndex: number, ignore: boolean): void { + const mockObject = this._mockStatus.get(originalFunctionIndex); + if (mockObject === undefined) { + return; + } + mockObject.ignore = ignore; + } + + clear(): void { + this._mockStatus.clear(); + } + + getMockFuncSet(): Record { + return { + checkMock: (functionIndex: number, isCall: boolean): number => { + return this._checkMock(functionIndex, isCall); + }, + setMockFunction: (originalFunctionIndex: number, mockFunctionIndex: number): void => { + this._setMockFunction(originalFunctionIndex, mockFunctionIndex); + }, + getMockedFunctionCalls: (originalFunctionIndex: number, mockFunctionIndex: number): number => { + return this._getMockedFunctionCalls(originalFunctionIndex, mockFunctionIndex); + }, + setMockedFunctionIgnore: (originalFunctionIndex: number, ignore: boolean): void => { + this._setMockedFunctionIgnore(originalFunctionIndex, ignore); + }, + }; + } +} diff --git a/src/utils/import.ts b/src/utils/import.ts deleted file mode 100644 index d5e310a..0000000 --- a/src/utils/import.ts +++ /dev/null @@ -1,62 +0,0 @@ -import assert from "node:assert"; - -interface MockValue { - calls: number; - ignore: boolean; - newIndex: number; -} - -export const mockInstrumentFunc = { - // isCall = true, return -1 if not mocked; - // isCall = false, return oldIndex if not mocked. - checkMock(index: number, isCall: boolean): number { - if (mockInstrumentFunc["mockFunctionStatus.has"](index)) { - return mockInstrumentFunc["mockFunctionStatus.get"](index); - } - return isCall ? -1 : index; - }, - "mockFunctionStatus.last": 0, - "mockFunctionStatus.state": new Map(), - "mockFunctionStatus.clear": function () { - mockInstrumentFunc["mockFunctionStatus.state"].clear(); - }, - "mockFunctionStatus.set": function (k: number, v: number) { - const value: MockValue = { - calls: 0, - ignore: false, - newIndex: v, - }; - mockInstrumentFunc["mockFunctionStatus.state"].set(k, value); - }, - "mockFunctionStatus.get": function (k: number): number { - const fn = mockInstrumentFunc["mockFunctionStatus.state"].get(k); - assert(fn); - fn.calls++; - mockInstrumentFunc["mockFunctionStatus.last"] = k; - return fn.newIndex; - }, - "mockFunctionStatus.lastGet": function (): number { - return mockInstrumentFunc["mockFunctionStatus.last"]; - }, - "mockFunctionStatus.has": function (k: number): boolean { - const fn = mockInstrumentFunc["mockFunctionStatus.state"].get(k); - if (fn === undefined) { - return false; - } - return !fn.ignore; - }, - "mockFunctionStatus.getCalls": function (oldIndex: number, newIndex: number): number { - const fn = mockInstrumentFunc["mockFunctionStatus.state"].get(oldIndex); - if (fn === undefined || fn.newIndex !== newIndex) { - return 0; - } - return fn.calls; - }, - "mockFunctionStatus.setIgnore": function (k: number, v: boolean) { - const fn = mockInstrumentFunc["mockFunctionStatus.state"].get(k); - if (fn === undefined) { - return; - } - fn.ignore = v; - }, -}; diff --git a/tests/cpp/lit/run.cjs b/tests/cpp/lit/run.cjs index 067729f..ab2e634 100644 --- a/tests/cpp/lit/run.cjs +++ b/tests/cpp/lit/run.cjs @@ -1,62 +1,59 @@ const fs = require("fs"); const wasmBuffer = fs.readFileSync(process.argv[2]); const mockInstruFunc = { + _mockStatus: new Map(), + + hasMocked(functionIndex) { + const mockObject = this._mockStatus.get(functionIndex); + if (mockObject === undefined) { + return false; + } + return !mockObject.ignore; + }, + // isCall = true, return -1 if not mocked; // isCall = false, return oldIndex if not mocked. - checkMock(index, isCall) { - if (mockInstruFunc["mockFunctionStatus.has"](index)) { - return mockInstruFunc["mockFunctionStatus.get"](index); + checkMock(functionIndex, isCall) { + if (this.hasMocked(functionIndex)) { + const mockObject = this._mockStatus.get(functionIndex); + mockObject.calls++; + return mockObject.mockFunctionIndex; } - return isCall ? -1 : index; - }, - "mockFunctionStatus.last": 0, - "mockFunctionStatus.state": new Map(), - "mockFunctionStatus.clear": function () { - mockInstruFunc["mockFunctionStatus.state"].clear(); + return isCall ? -1 : functionIndex; }, - "mockFunctionStatus.set": function (k, v) { - const value = { + + setMockFunction(originalFunctionIndex, mockFunctionIndex) { + const mockObject = { calls: 0, ignore: false, - newIndex: v, - }; - mockInstruFunc["mockFunctionStatus.state"].set(k, value); - }, - "mockFunctionStatus.get": function (k) { - const fn = mockInstruFunc["mockFunctionStatus.state"].get(k); - assert(fn); - fn.calls++; - mockInstruFunc["mockFunctionStatus.last"] = k; - return fn.newIndex; - }, - "mockFunctionStatus.lastGet": function () { - return mockInstruFunc["mockFunctionStatus.last"]; - }, - "mockFunctionStatus.has": function (k) { - const fn = mockInstruFunc["mockFunctionStatus.state"].get(k); - if (fn === undefined) { - return false; + mockFunctionIndex, } - return !fn.ignore; + this._mockStatus.set(originalFunctionIndex, mockObject); }, - "mockFunctionStatus.getCalls": function (oldIndex, newIndex) { - const fn = mockInstruFunc["mockFunctionStatus.state"].get(oldIndex); - if (fn === undefined || fn.newIndex !== newIndex) { + + getMockedFunctionCalls(originalFunctionIndex, mockFunctionIndex) { + const mockObject = this._mockStatus.get(originalFunctionIndex); + if (mockObject === undefined || mockObject.mockFunctionIndex !== mockFunctionIndex) { return 0; } - return fn.calls; + return mockObject.calls; }, - "mockFunctionStatus.setIgnore": function (k, v) { - const fn = mockInstruFunc["mockFunctionStatus.state"].get(k); - if (fn === undefined) { + + setMockedFunctionIgnore(originalFunctionIndex, ignore) { + const mockObject = this._mockStatus.get(originalFunctionIndex); + if (mockObject === undefined) { return; } - fn.ignore = v; + mockObject.ignore = ignore; }, + + clear() { + this._mockStatus.clear(); + } }; const imports = { - mockInstrument: mockInstruFunc, - covInstrument: { + __unittest_framework_env: { + ...mockInstruFunc, traceExpression(functionIndex, index, type) { // console.log(consumer); switch (type) { diff --git a/tests/e2e/assertFailed/stdout.txt b/tests/e2e/assertFailed/stdout.txt index 753547b..24c37fa 100644 --- a/tests/e2e/assertFailed/stdout.txt +++ b/tests/e2e/assertFailed/stdout.txt @@ -11,11 +11,11 @@ Error Message: This test will fail due to an assertion error Reason: unreachable at start:tests/e2e/assertFailed/assertOnTest.test~anonymous|0 (tests/e2e/assertFailed/assertOnTest.test.ts:6:2) - at executeTestFunction (tests/e2e/assertFailed/tmp/assertOnTest.test.instrumented.wasm:1:675) + at executeTestFunction (tests/e2e/assertFailed/tmp/assertOnTest.test.instrumented.wasm:1:696) tests/e2e/assertFailed/tmp/assertOnInit.test - init: Test Crashed! Reason: unreachable at start:tests/e2e/assertFailed/assertOnInit.test (tests/e2e/assertFailed/assertOnInit.test.ts:1:0) - at ~start (tests/e2e/assertFailed/tmp/assertOnInit.test.instrumented.wasm:1:244) + at ~start (tests/e2e/assertFailed/tmp/assertOnInit.test.instrumented.wasm:1:265) diff --git a/tests/ts/test/core/mockStatusRecorder.test.ts b/tests/ts/test/core/mockStatusRecorder.test.ts new file mode 100644 index 0000000..8409d2f --- /dev/null +++ b/tests/ts/test/core/mockStatusRecorder.test.ts @@ -0,0 +1,38 @@ +// eslint-disable-next-line n/no-extraneous-import +import { jest } from "@jest/globals"; + +const mockWriteFile = jest.fn(); +jest.unstable_mockModule("node:fs", () => ({ + writeFileSync: mockWriteFile, +})); + +const { MockStatusRecorder } = await import("../../../../src/core/mockStatusRecorder.js"); + +describe("imports", () => { + test("mockInstrument", () => { + const mockStatusRecorder = new MockStatusRecorder(); + // mock(oldFunctionIndex, newFunctionIndex); + mockStatusRecorder._setMockFunction(1, 4); + expect(mockStatusRecorder._checkMock(1, true)).toEqual(4); + expect(mockStatusRecorder._checkMock(2, false)).toEqual(2); + expect(mockStatusRecorder._checkMock(2, true)).toEqual(-1); + expect(mockStatusRecorder._getMockedFunctionCalls(1, 4)).toEqual(1); + expect(mockStatusRecorder._getMockedFunctionCalls(2, 4)).toEqual(0); + expect(mockStatusRecorder._getMockedFunctionCalls(1, 3)).toEqual(0); + + // unmock(oldFunction) + mockStatusRecorder._setMockedFunctionIgnore(1, true); + expect(mockStatusRecorder._checkMock(1, false)).toEqual(1); + expect(mockStatusRecorder._checkMock(1, true)).toEqual(-1); + mockStatusRecorder._setMockedFunctionIgnore(2, true); + expect(mockStatusRecorder._checkMock(2, false)).toEqual(2); + expect(mockStatusRecorder._checkMock(2, true)).toEqual(-1); + + // remock(oldFunction) + mockStatusRecorder._setMockedFunctionIgnore(1, false); + expect(mockStatusRecorder._checkMock(1, false)).toEqual(4); + mockStatusRecorder._setMockedFunctionIgnore(2, false); + expect(mockStatusRecorder._checkMock(2, false)).toEqual(2); + expect(mockStatusRecorder._checkMock(2, true)).toEqual(-1); + }); +}); diff --git a/tests/ts/test/utils/import.test.ts b/tests/ts/test/utils/import.test.ts deleted file mode 100644 index 446b702..0000000 --- a/tests/ts/test/utils/import.test.ts +++ /dev/null @@ -1,40 +0,0 @@ -// eslint-disable-next-line n/no-extraneous-import -import { jest } from "@jest/globals"; - -const mockWriteFile = jest.fn(); -jest.unstable_mockModule("node:fs", () => ({ - writeFileSync: mockWriteFile, -})); - -const { mockInstrumentFunc } = await import("../../../../src/utils/import.js"); - -describe("imports", () => { - test("mockInstrument", () => { - // mock(oldFunctionIndex, newFunctionIndex); - mockInstrumentFunc["mockFunctionStatus.set"](1, 4); - expect(mockInstrumentFunc.checkMock(1, true)).toEqual(4); - expect(mockInstrumentFunc.checkMock(2, false)).toEqual(2); - expect(mockInstrumentFunc.checkMock(2, true)).toEqual(-1); - expect(mockInstrumentFunc["mockFunctionStatus.lastGet"]()).toEqual(1); - expect(mockInstrumentFunc["mockFunctionStatus.getCalls"](1, 4)).toEqual(1); - expect(mockInstrumentFunc["mockFunctionStatus.getCalls"](2, 4)).toEqual(0); - expect(mockInstrumentFunc["mockFunctionStatus.getCalls"](1, 3)).toEqual(0); - expect(() => mockInstrumentFunc["mockFunctionStatus.get"](2)).toThrow(); - // unmock(oldFunction) - mockInstrumentFunc["mockFunctionStatus.setIgnore"](1, true); - expect(mockInstrumentFunc.checkMock(1, false)).toEqual(1); - expect(mockInstrumentFunc.checkMock(1, true)).toEqual(-1); - mockInstrumentFunc["mockFunctionStatus.setIgnore"](2, true); - expect(mockInstrumentFunc.checkMock(2, false)).toEqual(2); - expect(mockInstrumentFunc.checkMock(2, true)).toEqual(-1); - // remock(oldFunction) - mockInstrumentFunc["mockFunctionStatus.setIgnore"](1, false); - expect(mockInstrumentFunc.checkMock(1, false)).toEqual(4); - mockInstrumentFunc["mockFunctionStatus.setIgnore"](2, false); - expect(mockInstrumentFunc.checkMock(2, false)).toEqual(2); - expect(mockInstrumentFunc.checkMock(2, true)).toEqual(-1); - // clear - mockInstrumentFunc["mockFunctionStatus.clear"](); - expect(mockInstrumentFunc["mockFunctionStatus.state"].size).toEqual(0); - }); -}); From a65b867296fd371944806f4279ad36e12ddf1870 Mon Sep 17 00:00:00 2001 From: XMadrid Date: Mon, 22 Sep 2025 16:44:45 +0800 Subject: [PATCH 2/2] fix lint --- assembly/implement.ts | 5 ++++- assembly/mockInstrument.ts | 22 ++++++++++++++++++---- src/core/mockStatusRecorder.ts | 26 +++++++++++++------------- tests/cpp/lit/run.cjs | 4 ++-- 4 files changed, 37 insertions(+), 20 deletions(-) diff --git a/assembly/implement.ts b/assembly/implement.ts index 6eb3a84..71513b2 100644 --- a/assembly/implement.ts +++ b/assembly/implement.ts @@ -23,7 +23,10 @@ export function mockImpl( ERROR("mock paramemter receive a function"); } const mockFn = new MockFn(originalFunction.index, mockFunction.index); - mockFunctionStatus.setMockFunction(originalFunction.index, mockFunction.index); + mockFunctionStatus.setMockFunction( + originalFunction.index, + mockFunction.index, + ); return mockFn; } export function unmockImpl(originalFunction: T): void { diff --git a/assembly/mockInstrument.ts b/assembly/mockInstrument.ts index 0d31e71..23c4691 100644 --- a/assembly/mockInstrument.ts +++ b/assembly/mockInstrument.ts @@ -1,18 +1,32 @@ export namespace mockFunctionStatus { @external("__unittest_framework_env","setMockFunction") - export declare function setMockFunction(originalFunctionIndex: u32, mockFunctionIndex: u32): void; + export declare function setMockFunction( + originalFunctionIndex: u32, + mockFunctionIndex: u32, + ): void; + @external("__unittest_framework_env","getMockedFunctionCalls") - export declare function getMockedFunctionCalls(originalFunctionIndex: u32, mockFunctionIndex: u32): u32; + export declare function getMockedFunctionCalls( + originalFunctionIndex: u32, + mockFunctionIndex: u32, + ): u32; + @external("__unittest_framework_env","setMockedFunctionIgnore") - export declare function setMockedFunctionIgnore(originalFunctionIndex: u32, ignore: bool): void; + export declare function setMockedFunctionIgnore( + originalFunctionIndex: u32, + ignore: bool, + ): void; } export class MockFn { get calls(): u32 { - return mockFunctionStatus.getMockedFunctionCalls(this.originalFunctionIndex, this.mockFunctionIndex); + return mockFunctionStatus.getMockedFunctionCalls( + this.originalFunctionIndex, + this.mockFunctionIndex, + ); } constructor( public originalFunctionIndex: u32, diff --git a/src/core/mockStatusRecorder.ts b/src/core/mockStatusRecorder.ts index 728f69a..3c6780f 100644 --- a/src/core/mockStatusRecorder.ts +++ b/src/core/mockStatusRecorder.ts @@ -31,7 +31,7 @@ export class MockStatusRecorder { calls: 0, ignore: false, mockFunctionIndex, - } + }; this._mockStatus.set(originalFunctionIndex, mockObject); } @@ -57,18 +57,18 @@ export class MockStatusRecorder { getMockFuncSet(): Record { return { - checkMock: (functionIndex: number, isCall: boolean): number => { - return this._checkMock(functionIndex, isCall); - }, - setMockFunction: (originalFunctionIndex: number, mockFunctionIndex: number): void => { - this._setMockFunction(originalFunctionIndex, mockFunctionIndex); - }, - getMockedFunctionCalls: (originalFunctionIndex: number, mockFunctionIndex: number): number => { - return this._getMockedFunctionCalls(originalFunctionIndex, mockFunctionIndex); - }, - setMockedFunctionIgnore: (originalFunctionIndex: number, ignore: boolean): void => { - this._setMockedFunctionIgnore(originalFunctionIndex, ignore); - }, + checkMock: (functionIndex: number, isCall: boolean): number => { + return this._checkMock(functionIndex, isCall); + }, + setMockFunction: (originalFunctionIndex: number, mockFunctionIndex: number): void => { + this._setMockFunction(originalFunctionIndex, mockFunctionIndex); + }, + getMockedFunctionCalls: (originalFunctionIndex: number, mockFunctionIndex: number): number => { + return this._getMockedFunctionCalls(originalFunctionIndex, mockFunctionIndex); + }, + setMockedFunctionIgnore: (originalFunctionIndex: number, ignore: boolean): void => { + this._setMockedFunctionIgnore(originalFunctionIndex, ignore); + }, }; } } diff --git a/tests/cpp/lit/run.cjs b/tests/cpp/lit/run.cjs index ab2e634..df1f6dc 100644 --- a/tests/cpp/lit/run.cjs +++ b/tests/cpp/lit/run.cjs @@ -27,7 +27,7 @@ const mockInstruFunc = { calls: 0, ignore: false, mockFunctionIndex, - } + }; this._mockStatus.set(originalFunctionIndex, mockObject); }, @@ -49,7 +49,7 @@ const mockInstruFunc = { clear() { this._mockStatus.clear(); - } + }, }; const imports = { __unittest_framework_env: {