Files
UnrealLuaDemo/Script/lua/Tests/Syntax/PureClassesAndGenerics.lua
T

200 lines
6.7 KiB
Lua

local ____lualib = require("lualib_bundle")
local __TS__ClassExtends = ____lualib.__TS__ClassExtends
local __TS__New = ____lualib.__TS__New
local __TS__InstanceOf = ____lualib.__TS__InstanceOf
local ____exports = __module.env
local M__Tests_Harness_TestRunner = __MODULE__.Import("Tests.Harness.TestRunner", __module)
local Suite = M__Tests_Harness_TestRunner.Suite
local Test = M__Tests_Harness_TestRunner.Test
local AssertEqual = M__Tests_Harness_TestRunner.AssertEqual
local AssertTrue = M__Tests_Harness_TestRunner.AssertTrue
local AssertFalse = M__Tests_Harness_TestRunner.AssertFalse
local AssertNil = M__Tests_Harness_TestRunner.AssertNil
local BaseEntity = __TS__DefineClass("BaseEntity", __module)
BaseEntity.name = "BaseEntity"
function BaseEntity.prototype.____constructor(self, id, name)
self.id = id
self.name = name
end
function BaseEntity.prototype.GetId(self)
return self.id
end
function BaseEntity.prototype.GetName(self)
return self.name
end
local PureMonster = __TS__DefineClass("PureMonster", __module)
PureMonster.name = "PureMonster"
__TS__ClassExtends(PureMonster, BaseEntity)
function PureMonster.prototype.____constructor(self, id, name, level)
if level == nil then
level = PureMonster.DefaultLevel
end
BaseEntity.prototype.____constructor(self, id, name)
self.level = level
end
function PureMonster.prototype.GetLevel(self)
return self.level
end
function PureMonster.prototype.Describe(self)
return ((((self.name .. "#") .. tostring(self.id)) .. "(Lv.") .. tostring(self.level)) .. ")"
end
function PureMonster.CompareLevel(a, b)
return a:GetLevel() - b:GetLevel()
end
PureMonster.DefaultLevel = 1
local RingBuffer = __TS__DefineClass("RingBuffer", __module)
RingBuffer.name = "RingBuffer"
function RingBuffer.prototype.____constructor(self, capacity)
self.buffer = {}
self.head = 0
self.count = 0
self.capacity = capacity
end
function RingBuffer.prototype.Push(self, item)
if self.count < self.capacity then
local idx = (self.head + self.count) % self.capacity
self.buffer[idx + 1] = item
self.count = self.count + 1
else
self.buffer[self.head + 1] = item
self.head = (self.head + 1) % self.capacity
end
end
function RingBuffer.prototype.Pop(self)
if self.count == 0 then
return nil
end
local item = self.buffer[self.head + 1]
self.buffer[self.head + 1] = nil
self.head = (self.head + 1) % self.capacity
self.count = self.count - 1
return item
end
function RingBuffer.prototype.GetCount(self)
return self.count
end
local function FindById(list, targetId)
do
local i = 0
while i < #list do
if list[i + 1]:GetId() == targetId then
return list[i + 1]
end
i = i + 1
end
end
return nil
end
local function SafeFrameworkBoundary(shouldThrow)
do
local function ____catch(e)
return true, {
false,
tostring(e)
}
end
local ____try, ____hasReturned, ____returnValue = pcall(function()
if shouldThrow then
error("simulated_boundary_error", 0)
end
return true, {true, "ok"}
end)
if not ____try then
____hasReturned, ____returnValue = ____catch(____hasReturned)
end
if ____hasReturned then
return table.unpack(____returnValue)
end
end
end
function ____exports.RunPureClassesAndGenericsTests()
Suite(
"Syntax.PureClassesAndGenerics",
function()
Test(
"pure TS class inheritance, abstract methods and static helpers",
function()
local m1 = __TS__New(PureMonster, 1, "Slime")
local m2 = __TS__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(__TS__InstanceOf(m1, PureMonster))
AssertTrue(__TS__InstanceOf(m1, BaseEntity))
end
)
Test(
"generic class RingBuffer<T> and constrained generic function",
function()
local rb = __TS__New(RingBuffer, 3)
rb:Push(10)
rb:Push(20)
rb:Push(30)
rb:Push(40)
AssertEqual(
rb:GetCount(),
3
)
AssertEqual(
rb:Pop(),
20
)
AssertEqual(
rb:Pop(),
30
)
AssertEqual(
rb:Pop(),
40
)
AssertNil(rb:Pop())
local monsters = {
__TS__New(PureMonster, 10, "Orc", 5),
__TS__New(PureMonster, 20, "Goblin", 2)
}
local found = FindById(monsters, 20)
AssertTrue(found ~= nil)
AssertEqual(
found:GetName(),
"Goblin"
)
AssertNil(FindById(monsters, 999))
end
)
Test(
"@unlua-allow-try framework boundary try/catch/throw",
function()
local ok1, msg1 = SafeFrameworkBoundary(false)
AssertTrue(ok1)
AssertEqual(msg1, "ok")
local ok2, msg2 = SafeFrameworkBoundary(true)
AssertFalse(ok2)
AssertTrue((string.find(msg2, "simulated_boundary_error", 1, true)) ~= nil)
end
)
end
)
end
function __module.UpdateImports()
Suite = M__Tests_Harness_TestRunner.Suite
Test = M__Tests_Harness_TestRunner.Test
AssertEqual = M__Tests_Harness_TestRunner.AssertEqual
AssertTrue = M__Tests_Harness_TestRunner.AssertTrue
AssertFalse = M__Tests_Harness_TestRunner.AssertFalse
AssertNil = M__Tests_Harness_TestRunner.AssertNil
end