Skip to content

Commit

Permalink
test: fix testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
bytemain committed Sep 7, 2023
1 parent 6d77ba2 commit 24390df
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 83 deletions.
2 changes: 1 addition & 1 deletion src/helper/compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function dispatch<C>(
ctx: PureContext<C>,
): Promise<void> | void {
if (idx <= stack.depth) {
throw new Error('joinPoint.proceed() called multiple times');
throw new Error('ctx.proceed() called multiple times');
}

stack.depth = idx;
Expand Down
2 changes: 0 additions & 2 deletions src/helper/hook-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,6 @@ export function createHookedFunction<ThisType, Args extends any[], Result>(
let p: Promise<Result> | undefined;
if (promise) {
p = promise;
} else if (ret && isPromiseLike(ret)) {
p = ret;
}

if (p) {
Expand Down
41 changes: 41 additions & 0 deletions test/helper/compose.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,45 @@ describe('di compose', () => {
},
});
});
it('will throw error when call proceed twice', async () => {
interface ExampleContext {
getName(): string;
getResult(): any;
}
const middleware1: Middleware<ExampleContext> = (ctx) => {
const name = ctx.getName();
console.log(`middleware1: ${name}`);
ctx.proceed();
const result = ctx.getResult();
console.log(`middleware1 result: ${result}`);
console.log(`middleware1 after: ${name}`);
};

const middleware2: Middleware<ExampleContext> = (ctx) => {
const name = ctx.getName();
console.log(`middleware2: ${name}`);
ctx.proceed();
ctx.proceed();
const result = ctx.getResult();
console.log(`middleware2 result: ${result}`);
console.log(`middleware2 after: ${name}`);
};

const all = compose<ExampleContext>([middleware1, middleware2]);
let ret = undefined as any;
expect(() => {
all({
getName() {
return 'example';
},
proceed() {
console.log('invoked');
ret = 'final result';
},
getResult(): any {
return ret;
},
});
}).toThrowError('ctx.proceed() called multiple times');
});
});
160 changes: 80 additions & 80 deletions test/injector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -463,24 +463,24 @@ describe('test injector work', () => {
}
}

// injector.createHook<TestClass, [number, number], number>({
// hook: (joinPoint: IBeforeJoinPoint<TestClass, [number, number], number>) => {
// const [a, b] = joinPoint.getArgs();
// joinPoint.setArgs([a + 1, b + 1]);
// },
// method: 'add',
// target: TestClass,
// type: HookType.Before,
// });
// injector.createHook({
// hook: () => undefined,
// method: 'add',
// target: TestClass,
// type: 'other' as any, // 不会造成任何影响(为了提高覆盖率)
// });
// const testClass = injector.get(TestClass);
// expect(testClass.add(1, 2)).toBe(5);
// expect(testClass.add(3, 4)).toBe(9);
injector.createHook<TestClass, [number, number], number>({
hook: (joinPoint: IBeforeJoinPoint<TestClass, [number, number], number>) => {
const [a, b] = joinPoint.getArgs();
joinPoint.setArgs([a + 1, b + 1]);
},
method: 'add',
target: TestClass,
type: HookType.Before,
});
injector.createHook({
hook: () => undefined,
method: 'add',
target: TestClass,
type: 'other' as any, // 不会造成任何影响(为了提高覆盖率)
});
const testClass = injector.get(TestClass);
expect(testClass.add(1, 2)).toBe(5);
expect(testClass.add(3, 4)).toBe(9);

// 同步变成异步
// Async hook on sync target
Expand Down Expand Up @@ -511,68 +511,68 @@ describe('test injector work', () => {
expect(ret).toBeInstanceOf(Promise);
expect(await ret).toBe(9);

// injector.createHook<TestClass2, [number, number], number>({
// hook: async (joinPoint) => {
// const result = joinPoint.getResult();
// joinPoint.setResult(result + 1);
// },
// method: 'add',
// target: TestClass2,
// type: HookType.After,
// });
// const testClass2 = injector.get(TestClass2);
// expect(testClass2.add(1, 2)).toBe(4);

// injector.createHooks([
// {
// hook: (joinPoint) => {
// joinPoint.proceed();
// const result = joinPoint.getResult();
// if (result === 3) {
// return joinPoint.setResult(10);
// }
// },
// method: 'add',
// target: TestClass3,
// type: HookType.Around,
// },
// ]);
// const testClass3 = injector.get(TestClass3);
// expect(testClass3.add(1, 2)).toBe(10);
// expect(testClass3.add(1, 3)).toBe(4);

// // Async hook on async target
// injector.createHooks([
// {
// awaitPromise: true,
// hook: async (joinPoint) => {
// joinPoint.proceed();
// const result = await joinPoint.getResult();
// if (result === 3) {
// return joinPoint.setResult(10);
// }
// },
// method: 'add',
// target: TestClass4,
// type: HookType.Around,
// },
// ]);
// const testClass4 = injector.get(TestClass4);
// expect(await testClass4.add(1, 2)).toBe(10);

// // Sync hook on async target
// injector.createHook<TestClass5, [number, number], number>({
// hook: async (joinPoint) => {
// const [a, b] = joinPoint.getArgs();
// joinPoint.setArgs([a + 1, b + 1]);
// joinPoint.proceed();
// },
// method: 'add',
// target: TestClass5,
// type: HookType.Around,
// });
// const testClass5 = injector.get(TestClass5);
// expect(await testClass5.add(1, 2)).toBe(5);
injector.createHook<TestClass2, [number, number], number>({
hook: async (joinPoint) => {
const result = joinPoint.getResult();
joinPoint.setResult(result + 1);
},
method: 'add',
target: TestClass2,
type: HookType.After,
});
const testClass2 = injector.get(TestClass2);
expect(testClass2.add(1, 2)).toBe(4);

injector.createHooks([
{
hook: (joinPoint) => {
joinPoint.proceed();
const result = joinPoint.getResult();
if (result === 3) {
return joinPoint.setResult(10);
}
},
method: 'add',
target: TestClass3,
type: HookType.Around,
},
]);
const testClass3 = injector.get(TestClass3);
expect(testClass3.add(1, 2)).toBe(10);
expect(testClass3.add(1, 3)).toBe(4);

// Async hook on async target
injector.createHooks([
{
awaitPromise: true,
hook: async (joinPoint) => {
joinPoint.proceed();
const result = await joinPoint.getResult();
if (result === 3) {
return joinPoint.setResult(10);
}
},
method: 'add',
target: TestClass4,
type: HookType.Around,
},
]);
const testClass4 = injector.get(TestClass4);
expect(await testClass4.add(1, 2)).toBe(10);

// Sync hook on async target
injector.createHook<TestClass5, [number, number], number>({
hook: async (joinPoint) => {
const [a, b] = joinPoint.getArgs();
joinPoint.setArgs([a + 1, b + 1]);
joinPoint.proceed();
},
method: 'add',
target: TestClass5,
type: HookType.Around,
});
const testClass5 = injector.get(TestClass5);
expect(await testClass5.add(1, 2)).toBe(5);
});

it('使用注解来创建hook', async () => {
Expand Down

0 comments on commit 24390df

Please sign in to comment.