test: add comprehensive ts2lua-unreal TS/Lua 5.4 test suites and upgrade to v0.2.2
This commit is contained in:
1 parent
ee2e62bb34
commit
3dd61c32a9
75 files changed
+9929
-10
No files matched your search
@@ -0,0 +1,249 @@
|
||||
local ____lualib = require("lualib_bundle")
|
||||
local __TS__SparseArrayNew = ____lualib.__TS__SparseArrayNew
|
||||
local __TS__SparseArrayPush = ____lualib.__TS__SparseArrayPush
|
||||
local __TS__SparseArraySpread = ____lualib.__TS__SparseArraySpread
|
||||
local ____exports = {}
|
||||
local ____TestRunner = require("Tests.Harness.TestRunner")
|
||||
local Suite = ____TestRunner.Suite
|
||||
local Test = ____TestRunner.Test
|
||||
local AssertEqual = ____TestRunner.AssertEqual
|
||||
local AssertNear = ____TestRunner.AssertNear
|
||||
local AssertTrue = ____TestRunner.AssertTrue
|
||||
local AssertFalse = ____TestRunner.AssertFalse
|
||||
local AssertNil = ____TestRunner.AssertNil
|
||||
local AssertArrayEqual = ____TestRunner.AssertArrayEqual
|
||||
local function EvaluateCommand(action)
|
||||
repeat
|
||||
local ____switch3 = action.kind
|
||||
local ____cond3 = ____switch3 == "move"
|
||||
if ____cond3 then
|
||||
return action.x + action.y
|
||||
end
|
||||
____cond3 = ____cond3 or ____switch3 == "damage"
|
||||
if ____cond3 then
|
||||
return action.crit and action.amount * 2 or action.amount
|
||||
end
|
||||
____cond3 = ____cond3 or ____switch3 == "noop"
|
||||
if ____cond3 then
|
||||
return 0
|
||||
end
|
||||
until true
|
||||
end
|
||||
function ____exports.RunBasicTypesAndControlFlowTests()
|
||||
Suite(
|
||||
"Syntax.BasicTypesAndControlFlow",
|
||||
function()
|
||||
Test(
|
||||
"primitives, template literals, nullish coalescing and optional chaining",
|
||||
function()
|
||||
local count = 42
|
||||
local ratio = 0.5
|
||||
local label = (("Count=" .. tostring(count)) .. ",Half=") .. tostring(math.floor(count * ratio))
|
||||
AssertEqual(label, "Count=42,Half=21")
|
||||
AssertEqual(
|
||||
tostring(count * ratio),
|
||||
"21.0"
|
||||
)
|
||||
local maybeNum = nil
|
||||
local zeroNum = 0
|
||||
local falseFlag = false
|
||||
AssertEqual(maybeNum or 99, 99)
|
||||
AssertEqual(zeroNum or 99, 0)
|
||||
local ____AssertEqual_1 = AssertEqual
|
||||
local ____falseFlag_0 = falseFlag
|
||||
if ____falseFlag_0 == nil then
|
||||
____falseFlag_0 = true
|
||||
end
|
||||
____AssertEqual_1(____falseFlag_0, false)
|
||||
local a = {}
|
||||
local b = {inner = {value = 7}}
|
||||
local ____AssertNil_4 = AssertNil
|
||||
local ____opt_2 = a.inner
|
||||
____AssertNil_4(____opt_2 and ____opt_2.value)
|
||||
local ____AssertEqual_7 = AssertEqual
|
||||
local ____opt_5 = b.inner
|
||||
____AssertEqual_7(____opt_5 and ____opt_5.value or -1, 7)
|
||||
end
|
||||
)
|
||||
Test(
|
||||
"Lua 5.4 native bitwise operators and const enum inlining",
|
||||
function()
|
||||
local flags = 0
|
||||
flags = flags | 1 | 4
|
||||
AssertTrue(flags & 1 ~= 0)
|
||||
AssertFalse(flags & 2 ~= 0)
|
||||
AssertTrue(flags & 4 ~= 0)
|
||||
flags = flags & ~4
|
||||
flags = flags ~ 2
|
||||
AssertFalse(flags & 4 ~= 0)
|
||||
AssertTrue(flags & 2 ~= 0)
|
||||
AssertEqual(1 << 4, 16)
|
||||
AssertEqual((64 & 4294967295) >> 2, 16)
|
||||
local role = "Healer"
|
||||
AssertEqual(role, "Healer")
|
||||
end
|
||||
)
|
||||
Test(
|
||||
"control flow: switch/case fallthrough, loops, continue, break, labeled loop",
|
||||
function()
|
||||
local function Classify(n)
|
||||
repeat
|
||||
local ____switch10 = n
|
||||
local ____cond10 = ____switch10 == 1 or ____switch10 == 2
|
||||
if ____cond10 then
|
||||
return "small"
|
||||
end
|
||||
____cond10 = ____cond10 or ____switch10 == 3
|
||||
if ____cond10 then
|
||||
return "medium"
|
||||
end
|
||||
do
|
||||
return "large"
|
||||
end
|
||||
until true
|
||||
end
|
||||
AssertEqual(
|
||||
Classify(1),
|
||||
"small"
|
||||
)
|
||||
AssertEqual(
|
||||
Classify(2),
|
||||
"small"
|
||||
)
|
||||
AssertEqual(
|
||||
Classify(3),
|
||||
"medium"
|
||||
)
|
||||
AssertEqual(
|
||||
Classify(9),
|
||||
"large"
|
||||
)
|
||||
local sum = 0
|
||||
local i = 0
|
||||
while i < 10 do
|
||||
do
|
||||
i = i + 1
|
||||
if i % 2 == 0 then
|
||||
goto __continue11
|
||||
end
|
||||
if i > 7 then
|
||||
break
|
||||
end
|
||||
sum = sum + i
|
||||
end
|
||||
::__continue11::
|
||||
end
|
||||
AssertEqual(sum, 16)
|
||||
local doCount = 0
|
||||
repeat
|
||||
do
|
||||
doCount = doCount + 1
|
||||
end
|
||||
until not false
|
||||
AssertEqual(doCount, 1)
|
||||
local foundPair = ""
|
||||
local found = false
|
||||
do
|
||||
local r = 0
|
||||
while r < 5 and not found do
|
||||
do
|
||||
local c = 0
|
||||
while c < 5 do
|
||||
if r * 10 + c == 23 then
|
||||
foundPair = (tostring(r) .. ",") .. tostring(c)
|
||||
found = true
|
||||
break
|
||||
end
|
||||
c = c + 1
|
||||
end
|
||||
end
|
||||
r = r + 1
|
||||
end
|
||||
end
|
||||
AssertEqual(foundPair, "2,3")
|
||||
end
|
||||
)
|
||||
Test(
|
||||
"discriminated union narrowing and destructuring with defaults",
|
||||
function()
|
||||
AssertEqual(
|
||||
EvaluateCommand({kind = "move", x = 3, y = 4}),
|
||||
7
|
||||
)
|
||||
AssertEqual(
|
||||
EvaluateCommand({kind = "damage", amount = 25, crit = true}),
|
||||
50
|
||||
)
|
||||
AssertEqual(
|
||||
EvaluateCommand({kind = "damage", amount = 25, crit = false}),
|
||||
25
|
||||
)
|
||||
AssertEqual(
|
||||
EvaluateCommand({kind = "noop"}),
|
||||
0
|
||||
)
|
||||
local cfg = {host = "127.0.0.1"}
|
||||
local host = cfg.host
|
||||
local port = cfg.port
|
||||
if port == nil then
|
||||
port = 7777
|
||||
end
|
||||
AssertEqual(host, "127.0.0.1")
|
||||
AssertEqual(port, 7777)
|
||||
local coords = {10, 20, 30}
|
||||
local x, ____, z = table.unpack(coords, 1, 3)
|
||||
AssertEqual(x, 10)
|
||||
AssertEqual(z, 30)
|
||||
AssertNear(0.1 + 0.2, 0.3, 0.000001)
|
||||
end
|
||||
)
|
||||
Test(
|
||||
"rest parameters and spread into function calls",
|
||||
function()
|
||||
local function SumAll(...)
|
||||
local nums = {...}
|
||||
local total = 0
|
||||
do
|
||||
local idx = 0
|
||||
while idx < #nums do
|
||||
total = total + nums[idx + 1]
|
||||
idx = idx + 1
|
||||
end
|
||||
end
|
||||
return total
|
||||
end
|
||||
local data = {2, 4, 6, 8}
|
||||
AssertEqual(
|
||||
SumAll(table.unpack(data)),
|
||||
20
|
||||
)
|
||||
local ____AssertEqual_9 = AssertEqual
|
||||
local ____array_8 = __TS__SparseArrayNew(
|
||||
1,
|
||||
table.unpack(data)
|
||||
)
|
||||
__TS__SparseArrayPush(____array_8, 9)
|
||||
____AssertEqual_9(
|
||||
SumAll(__TS__SparseArraySpread(____array_8)),
|
||||
30
|
||||
)
|
||||
local ____array_10 = __TS__SparseArrayNew(
|
||||
0,
|
||||
table.unpack(data)
|
||||
)
|
||||
__TS__SparseArrayPush(____array_10, 10)
|
||||
local combined = {__TS__SparseArraySpread(____array_10)}
|
||||
AssertArrayEqual(combined, {
|
||||
0,
|
||||
2,
|
||||
4,
|
||||
6,
|
||||
8,
|
||||
10
|
||||
})
|
||||
end
|
||||
)
|
||||
end
|
||||
)
|
||||
end
|
||||
return ____exports
|
||||
@@ -0,0 +1,194 @@
|
||||
local ____lualib = require("lualib_bundle")
|
||||
local __TS__ArraySplice = ____lualib.__TS__ArraySplice
|
||||
local __TS__ArraySort = ____lualib.__TS__ArraySort
|
||||
local __TS__ArrayIndexOf = ____lualib.__TS__ArrayIndexOf
|
||||
local ____exports = {}
|
||||
local ____TestRunner = require("Tests.Harness.TestRunner")
|
||||
local Suite = ____TestRunner.Suite
|
||||
local Test = ____TestRunner.Test
|
||||
local AssertEqual = ____TestRunner.AssertEqual
|
||||
local AssertTrue = ____TestRunner.AssertTrue
|
||||
local AssertFalse = ____TestRunner.AssertFalse
|
||||
local AssertNil = ____TestRunner.AssertNil
|
||||
local AssertArrayEqual = ____TestRunner.AssertArrayEqual
|
||||
local function DivideWithRemainder(a, b)
|
||||
if b == 0 then
|
||||
return false, 0, 0
|
||||
end
|
||||
local q = math.floor(a / b)
|
||||
local r = a % b
|
||||
return true, q, r
|
||||
end
|
||||
local function FormatId(a, b)
|
||||
if type(a) == "number" then
|
||||
return string.format("ID_%04d", a)
|
||||
end
|
||||
return string.format("%s_%04d", a, b or 0)
|
||||
end
|
||||
function ____exports.RunFunctionsClosuresAndTuplesTests()
|
||||
Suite(
|
||||
"Syntax.FunctionsClosuresAndTuples",
|
||||
function()
|
||||
Test(
|
||||
"LuaMultiReturn and $multi destructuring",
|
||||
function()
|
||||
local ok1, q1, r1 = DivideWithRemainder(17, 5)
|
||||
AssertTrue(ok1)
|
||||
AssertEqual(q1, 3)
|
||||
AssertEqual(r1, 2)
|
||||
local ok2, q2, r2 = DivideWithRemainder(10, 0)
|
||||
AssertFalse(ok2)
|
||||
AssertEqual(q2, 0)
|
||||
AssertEqual(r2, 0)
|
||||
end
|
||||
)
|
||||
Test(
|
||||
"function overloads, closures and upvalues",
|
||||
function()
|
||||
AssertEqual(
|
||||
FormatId(7),
|
||||
"ID_0007"
|
||||
)
|
||||
AssertEqual(
|
||||
FormatId("NPC", 42),
|
||||
"NPC_0042"
|
||||
)
|
||||
local function MakeAccumulator(start)
|
||||
local acc = start
|
||||
return function(delta)
|
||||
acc = acc + delta
|
||||
return acc
|
||||
end
|
||||
end
|
||||
local acc = MakeAccumulator(10)
|
||||
AssertEqual(
|
||||
acc(5),
|
||||
15
|
||||
)
|
||||
AssertEqual(
|
||||
acc(-3),
|
||||
12
|
||||
)
|
||||
AssertEqual(
|
||||
acc(8),
|
||||
20
|
||||
)
|
||||
end
|
||||
)
|
||||
Test(
|
||||
"explicit self colon-call vs @noSelf dot-call",
|
||||
function()
|
||||
local counter = {
|
||||
value = 10,
|
||||
Add = function(self, delta)
|
||||
self.value = self.value + delta
|
||||
return self.value
|
||||
end
|
||||
}
|
||||
AssertEqual(
|
||||
counter:Add(5),
|
||||
15
|
||||
)
|
||||
AssertEqual(
|
||||
counter:Add(7),
|
||||
22
|
||||
)
|
||||
end
|
||||
)
|
||||
Test(
|
||||
"TS arrays (0-based -> Lua 1-based) and table operations",
|
||||
function()
|
||||
local arr = {10, 20, 30, 40}
|
||||
AssertEqual(#arr, 4)
|
||||
AssertEqual(arr[1], 10)
|
||||
AssertEqual(arr[4], 40)
|
||||
arr[#arr + 1] = 50
|
||||
AssertEqual(#arr, 5)
|
||||
AssertEqual(
|
||||
table.remove(arr),
|
||||
50
|
||||
)
|
||||
local removed = __TS__ArraySplice(arr, 1, 2)
|
||||
AssertArrayEqual(removed, {20, 30})
|
||||
AssertArrayEqual(arr, {10, 40})
|
||||
table.insert(arr, 2, 25)
|
||||
AssertArrayEqual(arr, {10, 25, 40})
|
||||
table.remove(arr, 1)
|
||||
AssertArrayEqual(arr, {25, 40})
|
||||
local unsorted = {
|
||||
5,
|
||||
1,
|
||||
4,
|
||||
2,
|
||||
3
|
||||
}
|
||||
__TS__ArraySort(
|
||||
unsorted,
|
||||
function(____, a, b) return a - b end
|
||||
)
|
||||
AssertArrayEqual(unsorted, {
|
||||
1,
|
||||
2,
|
||||
3,
|
||||
4,
|
||||
5
|
||||
})
|
||||
AssertEqual(
|
||||
table.concat(unsorted, "-"),
|
||||
"1-2-3-4-5"
|
||||
)
|
||||
AssertEqual(
|
||||
__TS__ArrayIndexOf(unsorted, 4),
|
||||
3
|
||||
)
|
||||
AssertEqual(
|
||||
__TS__ArrayIndexOf(unsorted, 99),
|
||||
-1
|
||||
)
|
||||
end
|
||||
)
|
||||
Test(
|
||||
"Record<K, V>, LuaTable and pairs() iteration",
|
||||
function()
|
||||
local scores = {Alice = 100, Bob = 85, Carol = 92}
|
||||
scores.Bob = nil
|
||||
local count = 0
|
||||
local total = 0
|
||||
for k, v in pairs(scores) do
|
||||
if v ~= nil then
|
||||
count = count + 1
|
||||
total = total + v
|
||||
end
|
||||
AssertTrue(k ~= "Bob")
|
||||
end
|
||||
AssertEqual(count, 2)
|
||||
AssertEqual(total, 192)
|
||||
local luaTbl = {}
|
||||
luaTbl[101] = "Sword"
|
||||
luaTbl[202] = "Shield"
|
||||
AssertEqual(luaTbl[101], "Sword")
|
||||
AssertEqual(luaTbl[202], "Shield")
|
||||
luaTbl[101] = nil
|
||||
AssertNil(luaTbl[101])
|
||||
end
|
||||
)
|
||||
Test(
|
||||
"Lua patterns (replacing forbidden JS RegExp)",
|
||||
function()
|
||||
local tag, numStr = string.match("Item_Sword_042", "^(%a+)_%a+_(%d+)$")
|
||||
AssertEqual(tag, "Item")
|
||||
AssertEqual(numStr, "042")
|
||||
local replaced, subCount = string.gsub("HP: 100, MP: 50", "%d+", "999")
|
||||
AssertEqual(replaced, "HP: 999, MP: 999")
|
||||
AssertEqual(subCount, 2)
|
||||
local tokens = {}
|
||||
for tok in string.gmatch("alpha,beta,gamma", "[^,]+") do
|
||||
tokens[#tokens + 1] = tok
|
||||
end
|
||||
AssertArrayEqual(tokens, {"alpha", "beta", "gamma"})
|
||||
end
|
||||
)
|
||||
end
|
||||
)
|
||||
end
|
||||
return ____exports
|
||||
@@ -0,0 +1,193 @@
|
||||
local ____lualib = require("lualib_bundle")
|
||||
local __TS__Class = ____lualib.__TS__Class
|
||||
local __TS__ClassExtends = ____lualib.__TS__ClassExtends
|
||||
local __TS__New = ____lualib.__TS__New
|
||||
local __TS__InstanceOf = ____lualib.__TS__InstanceOf
|
||||
local ____exports = {}
|
||||
local ____TestRunner = require("Tests.Harness.TestRunner")
|
||||
local Suite = ____TestRunner.Suite
|
||||
local Test = ____TestRunner.Test
|
||||
local AssertEqual = ____TestRunner.AssertEqual
|
||||
local AssertTrue = ____TestRunner.AssertTrue
|
||||
local AssertFalse = ____TestRunner.AssertFalse
|
||||
local AssertNil = ____TestRunner.AssertNil
|
||||
local BaseEntity = __TS__Class()
|
||||
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__Class()
|
||||
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__Class()
|
||||
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
|
||||
return ____exports
|
||||
@@ -0,0 +1,62 @@
|
||||
local UnLuaBaseActor = UnLua.Class()
|
||||
UnLuaBaseActor.DEFAULT_HP = 100
|
||||
function UnLuaBaseActor.ReceiveBeginPlay(self)
|
||||
self.Overridden.ReceiveBeginPlay(self)
|
||||
self.hp = UnLuaBaseActor.DEFAULT_HP
|
||||
self.callTrace = {"Base.ReceiveBeginPlay"}
|
||||
end
|
||||
function UnLuaBaseActor.ApplyConfig(self, ____param_0)
|
||||
local name = ____param_0.name
|
||||
local hp = ____param_0.hp
|
||||
if hp == nil then
|
||||
hp = UnLuaBaseActor.DEFAULT_HP
|
||||
end
|
||||
self.hp = hp
|
||||
return (name .. ":") .. tostring(self.hp)
|
||||
end
|
||||
function UnLuaBaseActor.SumScores(self, ____param_1)
|
||||
local first = ____param_1[1]
|
||||
local second = ____param_1[2]
|
||||
if second == nil then
|
||||
second = 5
|
||||
end
|
||||
local rest = {}
|
||||
table.move(
|
||||
____param_1,
|
||||
3,
|
||||
#____param_1,
|
||||
1,
|
||||
rest
|
||||
)
|
||||
local sum = first + second
|
||||
do
|
||||
local i = 0
|
||||
while i < #rest do
|
||||
sum = sum + rest[i + 1]
|
||||
i = i + 1
|
||||
end
|
||||
end
|
||||
return sum
|
||||
end
|
||||
function UnLuaBaseActor.GetTrace(self)
|
||||
return self.callTrace
|
||||
end
|
||||
function UnLuaBaseActor.GetHp(self)
|
||||
return self.hp
|
||||
end
|
||||
function UnLuaBaseActor.ComputeBonus(self, baseVal, multiplier)
|
||||
if multiplier == nil then
|
||||
multiplier = 2
|
||||
end
|
||||
return baseVal * multiplier
|
||||
end
|
||||
function UnLuaBaseActor.StaticWithSelf(self, delta)
|
||||
return self.DEFAULT_HP + delta
|
||||
end
|
||||
function UnLuaBaseActor.StaticNoSelf(a, b)
|
||||
return a + b
|
||||
end
|
||||
function UnLuaBaseActor.StaticDocNoSelf(x)
|
||||
return x * 3
|
||||
end
|
||||
return UnLuaBaseActor
|
||||
@@ -0,0 +1,8 @@
|
||||
local UnLuaExplicitWidget = UnLua.Class("Tests.Syntax.UnLuaBaseActor")
|
||||
function UnLuaExplicitWidget.Construct(self, model)
|
||||
self.title = model.title
|
||||
end
|
||||
function UnLuaExplicitWidget.GetTitle(self)
|
||||
return self.title
|
||||
end
|
||||
return UnLuaExplicitWidget
|
||||
@@ -0,0 +1,17 @@
|
||||
local UnLuaSubActor = require("Tests.Syntax.UnLuaSubActor")
|
||||
local UnLuaGrandChildActor = UnLua.Class("Tests.Syntax.UnLuaSubActor")
|
||||
function UnLuaGrandChildActor.ReceiveBeginPlay(self)
|
||||
UnLuaSubActor.ReceiveBeginPlay(self)
|
||||
self.tier = 3
|
||||
self.callTrace[#self.callTrace + 1] = "GrandChild.ReceiveBeginPlay"
|
||||
end
|
||||
function UnLuaGrandChildActor.ComputeBonus(self, baseVal, multiplier)
|
||||
if multiplier == nil then
|
||||
multiplier = 2
|
||||
end
|
||||
return UnLuaSubActor.ComputeBonus(self, baseVal, multiplier) + self.tier
|
||||
end
|
||||
function UnLuaGrandChildActor.GetTier(self)
|
||||
return self.tier
|
||||
end
|
||||
return UnLuaGrandChildActor
|
||||
@@ -0,0 +1,21 @@
|
||||
local UnLuaBaseActor = require("Tests.Syntax.UnLuaBaseActor")
|
||||
local UnLuaSubActor = UnLua.Class("Tests.Syntax.UnLuaBaseActor")
|
||||
function UnLuaSubActor.ReceiveBeginPlay(self)
|
||||
UnLuaBaseActor.ReceiveBeginPlay(self)
|
||||
self.faction = "Ally"
|
||||
self.callTrace[#self.callTrace + 1] = "Sub.ReceiveBeginPlay"
|
||||
end
|
||||
function UnLuaSubActor.ComputeBonus(self, baseVal, multiplier)
|
||||
if multiplier == nil then
|
||||
multiplier = 2
|
||||
end
|
||||
return UnLuaBaseActor.ComputeBonus(self, baseVal, multiplier) + 10
|
||||
end
|
||||
function UnLuaSubActor.SetupWithFaction(self, cfg, faction)
|
||||
self.faction = faction
|
||||
return ((UnLuaBaseActor.ApplyConfig(self, cfg) .. "[") .. self.faction) .. "]"
|
||||
end
|
||||
function UnLuaSubActor.GetFaction(self)
|
||||
return self.faction
|
||||
end
|
||||
return UnLuaSubActor
|
||||
@@ -0,0 +1,123 @@
|
||||
local ____exports = {}
|
||||
local ____TestRunner = require("Tests.Harness.TestRunner")
|
||||
local Suite = ____TestRunner.Suite
|
||||
local Test = ____TestRunner.Test
|
||||
local AssertEqual = ____TestRunner.AssertEqual
|
||||
local AssertTrue = ____TestRunner.AssertTrue
|
||||
local AssertArrayEqual = ____TestRunner.AssertArrayEqual
|
||||
local ____MockUnLuaRuntime = require("Tests.Harness.MockUnLuaRuntime")
|
||||
local CreateBoundInstance = ____MockUnLuaRuntime.CreateBoundInstance
|
||||
local GetMockState = ____MockUnLuaRuntime.GetMockState
|
||||
local ResetMockState = ____MockUnLuaRuntime.ResetMockState
|
||||
local UnLuaBaseActor = require("Tests.Syntax.UnLuaBaseActor")
|
||||
local UnLuaGrandChildActor = require("Tests.Syntax.UnLuaGrandChildActor")
|
||||
local UnLuaExplicitWidget = require("Tests.Syntax.UnLuaExplicitWidget")
|
||||
function ____exports.RunUnLuaSyntaxTests()
|
||||
Suite(
|
||||
"Syntax.UnLuaClassFeatures",
|
||||
function()
|
||||
Test(
|
||||
"@unlua base class: UnLua.Class(), self.Overridden, static methods and destructured params",
|
||||
function()
|
||||
ResetMockState()
|
||||
local actor = CreateBoundInstance(UnLuaBaseActor, "AActor")
|
||||
actor:ReceiveBeginPlay()
|
||||
AssertEqual(
|
||||
actor:GetHp(),
|
||||
100
|
||||
)
|
||||
AssertArrayEqual(
|
||||
actor:GetTrace(),
|
||||
{"Base.ReceiveBeginPlay"}
|
||||
)
|
||||
AssertArrayEqual(
|
||||
GetMockState().overriddenCalls,
|
||||
{"AActor.ReceiveBeginPlay"}
|
||||
)
|
||||
AssertEqual(
|
||||
actor:ApplyConfig({name = "Hero"}),
|
||||
"Hero:100"
|
||||
)
|
||||
AssertEqual(
|
||||
actor:ApplyConfig({name = "Boss", hp = 500}),
|
||||
"Boss:500"
|
||||
)
|
||||
AssertEqual(
|
||||
actor:SumScores({10}),
|
||||
15
|
||||
)
|
||||
AssertEqual(
|
||||
actor:SumScores({1, 2, 3, 4}),
|
||||
10
|
||||
)
|
||||
AssertEqual(
|
||||
UnLuaBaseActor:StaticWithSelf(25),
|
||||
125
|
||||
)
|
||||
AssertEqual(
|
||||
UnLuaBaseActor.StaticNoSelf(7, 8),
|
||||
15
|
||||
)
|
||||
AssertEqual(
|
||||
UnLuaBaseActor.StaticDocNoSelf(9),
|
||||
27
|
||||
)
|
||||
AssertTrue(actor:IsA(UE.AActor.StaticClass()))
|
||||
AssertTrue(UE.UKismetSystemLibrary.IsValid(actor))
|
||||
end
|
||||
)
|
||||
Test(
|
||||
"@unlua 3-level inheritance (GrandChild -> Sub -> Base) avoids self.Super infinite recursion",
|
||||
function()
|
||||
ResetMockState()
|
||||
local grand = CreateBoundInstance(UnLuaGrandChildActor, "AActor")
|
||||
grand:ReceiveBeginPlay()
|
||||
AssertArrayEqual(
|
||||
grand:GetTrace(),
|
||||
{"Base.ReceiveBeginPlay", "Sub.ReceiveBeginPlay", "GrandChild.ReceiveBeginPlay"}
|
||||
)
|
||||
AssertArrayEqual(
|
||||
GetMockState().overriddenCalls,
|
||||
{"AActor.ReceiveBeginPlay"}
|
||||
)
|
||||
AssertEqual(
|
||||
grand:GetFaction(),
|
||||
"Ally"
|
||||
)
|
||||
AssertEqual(
|
||||
grand:GetTier(),
|
||||
3
|
||||
)
|
||||
AssertEqual(
|
||||
grand:ComputeBonus(10),
|
||||
33
|
||||
)
|
||||
AssertEqual(
|
||||
grand:ComputeBonus(10, 4),
|
||||
53
|
||||
)
|
||||
local summary = grand:SetupWithFaction({name = "Knight", hp = 250}, "Enemy")
|
||||
AssertEqual(summary, "Knight:250[Enemy]")
|
||||
AssertEqual(
|
||||
grand:GetFaction(),
|
||||
"Enemy"
|
||||
)
|
||||
end
|
||||
)
|
||||
Test(
|
||||
"@unlua explicit base path class without extends",
|
||||
function()
|
||||
local widgetCls = UnLuaExplicitWidget
|
||||
AssertEqual(widgetCls.Super, UnLuaBaseActor)
|
||||
local widget = CreateBoundInstance(widgetCls, "UUserWidget")
|
||||
widget:Construct({title = "MainHUD"})
|
||||
AssertEqual(
|
||||
widget:GetTitle(),
|
||||
"MainHUD"
|
||||
)
|
||||
end
|
||||
)
|
||||
end
|
||||
)
|
||||
end
|
||||
return ____exports
|
||||
Reference in new issue
Block a user