feat(runtime-adapters): Enable TypeScript decorator support for Deno and Bun runtimes and add decorator tests
This commit is contained in:
91
test/decorator.all.ts
Normal file
91
test/decorator.all.ts
Normal file
@@ -0,0 +1,91 @@
|
||||
import { tap, expect } from '../ts_tapbundle/index.js';
|
||||
|
||||
/**
|
||||
* Simple class decorator for testing decorator support across runtimes
|
||||
*/
|
||||
function testDecorator(target: any) {
|
||||
target.decoratorApplied = true;
|
||||
target.decoratorData = 'Decorator was applied successfully';
|
||||
return target;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method decorator for testing
|
||||
*/
|
||||
function logMethod(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
|
||||
const originalMethod = descriptor.value;
|
||||
descriptor.value = function (...args: any[]) {
|
||||
const result = originalMethod.apply(this, args);
|
||||
return `[logged] ${result}`;
|
||||
};
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parameter decorator for testing
|
||||
*/
|
||||
function validateParam(target: any, propertyKey: string, parameterIndex: number) {
|
||||
// Mark that parameter validation decorator was applied
|
||||
if (!target.decoratedParams) {
|
||||
target.decoratedParams = {};
|
||||
}
|
||||
if (!target.decoratedParams[propertyKey]) {
|
||||
target.decoratedParams[propertyKey] = [];
|
||||
}
|
||||
target.decoratedParams[propertyKey].push(parameterIndex);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test class with decorators
|
||||
*/
|
||||
@testDecorator
|
||||
class TestClass {
|
||||
public name: string = 'test';
|
||||
|
||||
@logMethod
|
||||
public greet(message: string): string {
|
||||
return `Hello, ${message}!`;
|
||||
}
|
||||
|
||||
public getValue(): number {
|
||||
return 42;
|
||||
}
|
||||
|
||||
public testParams(@validateParam value: string): string {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
// Tests
|
||||
tap.test('Class decorator should be applied', async () => {
|
||||
expect((TestClass as any).decoratorApplied).toEqual(true);
|
||||
expect((TestClass as any).decoratorData).toEqual('Decorator was applied successfully');
|
||||
});
|
||||
|
||||
tap.test('Method decorator should modify method behavior', async () => {
|
||||
const instance = new TestClass();
|
||||
const result = instance.greet('World');
|
||||
expect(result).toEqual('[logged] Hello, World!');
|
||||
});
|
||||
|
||||
tap.test('Regular methods should work normally', async () => {
|
||||
const instance = new TestClass();
|
||||
expect(instance.getValue()).toEqual(42);
|
||||
expect(instance.name).toEqual('test');
|
||||
});
|
||||
|
||||
tap.test('Parameter decorator should be applied', async () => {
|
||||
const decoratedParams = (TestClass.prototype as any).decoratedParams;
|
||||
expect(decoratedParams).toBeDefined();
|
||||
expect(decoratedParams.testParams).toBeDefined();
|
||||
expect(decoratedParams.testParams).toContain(0);
|
||||
});
|
||||
|
||||
tap.test('Decorator metadata preservation', async () => {
|
||||
const instance = new TestClass();
|
||||
expect(instance instanceof TestClass).toEqual(true);
|
||||
expect(instance.constructor.name).toEqual('TestClass');
|
||||
expect(instance.testParams('hello')).toEqual('hello');
|
||||
});
|
||||
|
||||
export default tap.start();
|
||||
Reference in New Issue
Block a user