Repository navigation
Expand file tree
/
Copy pathreflection.test.ts
More file actions
62 lines (47 loc) · 2.33 KB
/
Copy pathreflection.test.ts
File metadata and controls
62 lines (47 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { PHPEngine, ReflectionClass, ReflectionFunction } from "../../index.js";
import { PHPLiteral, PHPReference } from "../../src/runtime/PHPVariable.js";
describe("Reflection Runtime Tests", () => {
let engine: PHPEngine;
beforeEach(() => {
engine = new PHPEngine({ watch: false });
});
afterEach(() => {
engine.close();
});
test("ReflectionFunction and ReflectionClass metadata queries", async () => {
const ctx = engine.createContext();
await ctx.eval("function my_fn($a, $b = 'default') { return $a; }");
const fn = ctx.functions["my_fn"] || engine.functions["my_fn"];
expect(fn).toBeDefined();
const refFn = await ReflectionFunction[engine.symbols.SYMBOL_PHP_NEW](ctx, new PHPLiteral("my_fn"), new PHPLiteral(fn));
expect(refFn.getName(ctx)).toBe("my_fn");
expect(refFn.getNumberOfParameters(ctx)).toBe(2);
expect(refFuncRequired(ctx, refFn)).toBe(1);
const refCls = await ReflectionClass[engine.symbols.SYMBOL_PHP_NEW](ctx, new PHPLiteral("ReflectionClass"));
expect(refCls.getName(ctx)).toBe("ReflectionClass");
expect(refCls.isInstantiable(ctx)).toBe(true);
});
test("ReflectionFunction fallback parsing for raw JavaScript function without symbol metadata", async () => {
const ctx = engine.createContext();
function rawJsFunction(ctx: any, paramOne: any, paramTwo = "hello", paramThree = 42) {
return paramOne;
}
const refFn = await ReflectionFunction[engine.symbols.SYMBOL_PHP_NEW](ctx, new PHPLiteral("rawJsFunction"), new PHPLiteral(rawJsFunction));
expect(refFn.getName(ctx)).toBe("rawJsFunction");
expect(refFn.getNumberOfParameters(ctx)).toBe(3);
expect(refFn.getNumberOfRequiredParameters(ctx)).toBe(1);
const params = await refFn.getParameters(ctx);
expect(params.length).toBe(3);
expect(params[0].getName(ctx)).toBe("paramOne");
expect(params[0].isOptional(ctx)).toBe(false);
expect(params[1].getName(ctx)).toBe("paramTwo");
expect(params[1].isOptional(ctx)).toBe(true);
expect(params[1].getDefaultValue(ctx)).toBe("hello");
expect(params[2].getName(ctx)).toBe("paramThree");
expect(params[2].isOptional(ctx)).toBe(true);
expect(params[2].getDefaultValue(ctx)).toBe(42);
});
});
function refFuncRequired(ctx: any, refFn: ReflectionFunction): number {
return refFn.getNumberOfRequiredParameters(ctx);
}