import { Suite, Test, AssertEqual, AssertTrue, AssertFalse, AssertNil, } from "../Harness/TestRunner"; interface Identifiable { GetId(): number; } abstract class BaseEntity implements Identifiable { protected id: number; protected name: string; constructor(id: number, name: string) { this.id = id; this.name = name; } GetId(): number { return this.id; } GetName(): string { return this.name; } abstract Describe(): string; } class PureMonster extends BaseEntity { static DefaultLevel = 1; private level: number; constructor(id: number, name: string, level = PureMonster.DefaultLevel) { super(id, name); this.level = level; } GetLevel(): number { return this.level; } override Describe(): string { return `${this.name}#${this.id}(Lv.${this.level})`; } static CompareLevel(this: void, a: PureMonster, b: PureMonster): number { return a.GetLevel() - b.GetLevel(); } } class RingBuffer { private buffer: (T | undefined)[] = []; private head = 0; private count = 0; private capacity: number; constructor(capacity: number) { this.capacity = capacity; } Push(item: T): void { if (this.count < this.capacity) { const idx = (this.head + this.count) % this.capacity; this.buffer[idx] = item; this.count++; } else { this.buffer[this.head] = item; this.head = (this.head + 1) % this.capacity; } } Pop(): T | undefined { if (this.count === 0) return undefined; const item = this.buffer[this.head]; this.buffer[this.head] = undefined; this.head = (this.head + 1) % this.capacity; this.count--; return item; } GetCount(): number { return this.count; } } function FindById(this: void, list: T[], targetId: number): T | undefined { for (let i = 0; i < list.length; i++) { if (list[i].GetId() === targetId) { return list[i]; } } return undefined; } function SafeFrameworkBoundary(this: void, shouldThrow: boolean): LuaMultiReturn<[boolean, string]> { /** @unlua-allow-try */ try { if (shouldThrow) { /** @unlua-allow-try */ throw "simulated_boundary_error"; } return $multi(true, "ok"); } catch (e) { return $multi(false, tostring(e)); } } export function RunPureClassesAndGenericsTests(this: void): void { Suite("Syntax.PureClassesAndGenerics", () => { Test("pure TS class inheritance, abstract methods and static helpers", () => { const m1 = new PureMonster(1, "Slime"); const m2 = new PureMonster(2, "Dragon", 50); AssertEqual(m1.GetId(), 1); AssertEqual(m1.GetLevel(), 1); AssertEqual(m1.Describe(), "Slime#1(Lv.1)"); AssertEqual(m2.Describe(), "Dragon#2(Lv.50)"); AssertTrue(PureMonster.CompareLevel(m2, m1) > 0); AssertTrue(m1 instanceof PureMonster); AssertTrue(m1 instanceof BaseEntity); }); Test("generic class RingBuffer and constrained generic function", () => { const rb = new RingBuffer(3); rb.Push(10); rb.Push(20); rb.Push(30); rb.Push(40); // overwrites 10 AssertEqual(rb.GetCount(), 3); AssertEqual(rb.Pop(), 20); AssertEqual(rb.Pop(), 30); AssertEqual(rb.Pop(), 40); AssertNil(rb.Pop()); const monsters = [new PureMonster(10, "Orc", 5), new PureMonster(20, "Goblin", 2)]; const found = FindById(monsters, 20); AssertTrue(found !== undefined); AssertEqual(found!.GetName(), "Goblin"); AssertNil(FindById(monsters, 999)); }); Test("@unlua-allow-try framework boundary try/catch/throw", () => { const [ok1, msg1] = SafeFrameworkBoundary(false); AssertTrue(ok1); AssertEqual(msg1, "ok"); const [ok2, msg2] = SafeFrameworkBoundary(true); AssertFalse(ok2); AssertTrue(string.find(msg2, "simulated_boundary_error", 1, true)[0] !== undefined); }); }); }