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,483 @@
|
||||
local ____lualib = require("lualib_bundle")
|
||||
local __TS__New = ____lualib.__TS__New
|
||||
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 ____MockUnLuaRuntime = require("Tests.Harness.MockUnLuaRuntime")
|
||||
local CreateBoundInstance = ____MockUnLuaRuntime.CreateBoundInstance
|
||||
local ____FSM = require("Tests.Gameplay.FSM")
|
||||
local StateMachine = ____FSM.StateMachine
|
||||
local ____ObjectPool = require("Tests.Gameplay.ObjectPool")
|
||||
local ObjectPool = ____ObjectPool.ObjectPool
|
||||
local ____TimerScheduler = require("Tests.Gameplay.TimerScheduler")
|
||||
local TimerScheduler = ____TimerScheduler.TimerScheduler
|
||||
local CooldownTracker = ____TimerScheduler.CooldownTracker
|
||||
local ____BuffAndAttributeSystem = require("Tests.Gameplay.BuffAndAttributeSystem")
|
||||
local AttributeAndBuffComponent = ____BuffAndAttributeSystem.AttributeAndBuffComponent
|
||||
local ____InventorySystem = require("Tests.Gameplay.InventorySystem")
|
||||
local InventorySystem = ____InventorySystem.InventorySystem
|
||||
local ____CoroutineTaskRunner = require("Tests.Gameplay.CoroutineTaskRunner")
|
||||
local CoroutineTaskRunner = ____CoroutineTaskRunner.CoroutineTaskRunner
|
||||
local ____BehaviorTree = require("Tests.Gameplay.BehaviorTree")
|
||||
local BTAction = ____BehaviorTree.BTAction
|
||||
local BTCondition = ____BehaviorTree.BTCondition
|
||||
local BTCooldown = ____BehaviorTree.BTCooldown
|
||||
local BTInverter = ____BehaviorTree.BTInverter
|
||||
local BTSelector = ____BehaviorTree.BTSelector
|
||||
local BTSequence = ____BehaviorTree.BTSequence
|
||||
function ____exports.RunGameplayTests()
|
||||
Suite(
|
||||
"Gameplay.Systems",
|
||||
function()
|
||||
Test(
|
||||
"FSM: state lifecycle, conditional transitions, pause and history",
|
||||
function()
|
||||
local ctx = {speed = 0, hp = 100, log = {}}
|
||||
local fsm = __TS__New(StateMachine, ctx)
|
||||
fsm:RegisterState(
|
||||
"Idle",
|
||||
{
|
||||
OnEnter = function(c)
|
||||
c.log[#c.log + 1] = "Enter:Idle"
|
||||
end,
|
||||
OnExit = function(c)
|
||||
c.log[#c.log + 1] = "Exit:Idle"
|
||||
end
|
||||
}
|
||||
)
|
||||
fsm:RegisterState(
|
||||
"Run",
|
||||
{
|
||||
OnEnter = function(c)
|
||||
c.log[#c.log + 1] = "Enter:Run"
|
||||
end,
|
||||
OnUpdate = function(c, dt)
|
||||
c.log[#c.log + 1] = "Run:" .. tostring(math.floor(dt * 100))
|
||||
end
|
||||
}
|
||||
)
|
||||
fsm:RegisterState(
|
||||
"Dead",
|
||||
{OnEnter = function(c)
|
||||
c.log[#c.log + 1] = "Enter:Dead"
|
||||
end}
|
||||
)
|
||||
fsm:AddTransition(
|
||||
"Idle",
|
||||
"Run",
|
||||
function(c) return c.speed > 0 end
|
||||
)
|
||||
fsm:AddTransition(
|
||||
"Run",
|
||||
"Idle",
|
||||
function(c) return c.speed == 0 end
|
||||
)
|
||||
fsm:AddTransition(
|
||||
"*",
|
||||
"Dead",
|
||||
function(c) return c.hp <= 0 end
|
||||
)
|
||||
AssertTrue(fsm:Start("Idle"))
|
||||
AssertEqual(
|
||||
fsm:GetCurrentState(),
|
||||
"Idle"
|
||||
)
|
||||
ctx.speed = 300
|
||||
fsm:Update(0.1)
|
||||
AssertEqual(
|
||||
fsm:GetCurrentState(),
|
||||
"Run"
|
||||
)
|
||||
fsm:SetPaused(true)
|
||||
ctx.hp = 0
|
||||
fsm:Update(0.1)
|
||||
AssertEqual(
|
||||
fsm:GetCurrentState(),
|
||||
"Run"
|
||||
)
|
||||
fsm:SetPaused(false)
|
||||
fsm:Update(0.1)
|
||||
AssertEqual(
|
||||
fsm:GetCurrentState(),
|
||||
"Dead"
|
||||
)
|
||||
AssertArrayEqual(
|
||||
fsm:GetHistory(),
|
||||
{"Idle", "Run", "Dead"}
|
||||
)
|
||||
end
|
||||
)
|
||||
Test(
|
||||
"ObjectPool: prewarm, acquire/reuse, maxCapacity cap and reset hooks",
|
||||
function()
|
||||
local pool = __TS__New(
|
||||
ObjectPool,
|
||||
{
|
||||
factory = function() return {active = false, damage = 0} end,
|
||||
onAcquire = function(b)
|
||||
b.active = true
|
||||
b.damage = 10
|
||||
end,
|
||||
onRelease = function(b)
|
||||
b.active = false
|
||||
b.damage = 0
|
||||
end,
|
||||
maxCapacity = 2
|
||||
}
|
||||
)
|
||||
pool:Prewarm(2)
|
||||
AssertEqual(
|
||||
pool:GetIdleCount(),
|
||||
2
|
||||
)
|
||||
AssertEqual(
|
||||
pool:GetTotalCreated(),
|
||||
2
|
||||
)
|
||||
local b1 = pool:Acquire()
|
||||
local b2 = pool:Acquire()
|
||||
local b3 = pool:Acquire()
|
||||
AssertTrue(b1.active)
|
||||
AssertEqual(b1.damage, 10)
|
||||
AssertEqual(
|
||||
pool:GetActiveCount(),
|
||||
3
|
||||
)
|
||||
AssertEqual(
|
||||
pool:GetTotalCreated(),
|
||||
3
|
||||
)
|
||||
pool:Release(b1)
|
||||
pool:Release(b2)
|
||||
pool:Release(b3)
|
||||
AssertFalse(b1.active)
|
||||
AssertEqual(
|
||||
pool:GetActiveCount(),
|
||||
0
|
||||
)
|
||||
AssertEqual(
|
||||
pool:GetIdleCount(),
|
||||
2
|
||||
)
|
||||
local reused = pool:Acquire()
|
||||
AssertEqual(
|
||||
pool:GetTotalCreated(),
|
||||
3
|
||||
)
|
||||
AssertTrue(reused.active)
|
||||
end
|
||||
)
|
||||
Test(
|
||||
"TimerScheduler & CooldownTracker: one-shot, repeat, self-cancel, timeScale and cooldowns",
|
||||
function()
|
||||
local scheduler = __TS__New(TimerScheduler)
|
||||
local events = {}
|
||||
scheduler:ScheduleOnce(
|
||||
0.5,
|
||||
function()
|
||||
events[#events + 1] = "once"
|
||||
end
|
||||
)
|
||||
local repId = scheduler:ScheduleRepeat(
|
||||
0.25,
|
||||
function(id)
|
||||
events[#events + 1] = "rep#" .. tostring(id)
|
||||
end,
|
||||
3
|
||||
)
|
||||
local selfCancelCount = 0
|
||||
scheduler:ScheduleRepeat(
|
||||
0.2,
|
||||
function(id)
|
||||
selfCancelCount = selfCancelCount + 1
|
||||
if selfCancelCount == 2 then
|
||||
scheduler:Cancel(id)
|
||||
end
|
||||
end
|
||||
)
|
||||
scheduler:Tick(0.5)
|
||||
AssertEqual(selfCancelCount, 2)
|
||||
AssertEqual(#events, 3)
|
||||
scheduler:SetPaused(repId, true)
|
||||
scheduler:Tick(1)
|
||||
AssertEqual(#events, 3)
|
||||
scheduler:SetPaused(repId, false)
|
||||
scheduler:SetTimeScale(0.5)
|
||||
scheduler:Tick(0.5)
|
||||
AssertEqual(#events, 4)
|
||||
AssertEqual(
|
||||
scheduler:GetActiveCount(),
|
||||
0
|
||||
)
|
||||
local cd = __TS__New(CooldownTracker)
|
||||
AssertTrue(cd:IsReady("Fireball"))
|
||||
cd:StartCooldown("Fireball", 5)
|
||||
AssertFalse(cd:IsReady("Fireball"))
|
||||
cd:Tick(2)
|
||||
AssertNear(
|
||||
cd:GetRemaining("Fireball"),
|
||||
3
|
||||
)
|
||||
cd:ReduceCooldown("Fireball", 3.5)
|
||||
AssertTrue(cd:IsReady("Fireball"))
|
||||
AssertEqual(
|
||||
cd:GetRemaining("Fireball"),
|
||||
0
|
||||
)
|
||||
end
|
||||
)
|
||||
Test(
|
||||
"BuffAndAttributeSystem: flat/percent modifiers, stacking, DoT tick and damage mitigation",
|
||||
function()
|
||||
local hero = __TS__New(AttributeAndBuffComponent, {MaxHP = 200, HP = 200, Attack = 50, Defense = 0})
|
||||
local boss = __TS__New(AttributeAndBuffComponent, {MaxHP = 500, HP = 500, Attack = 40, Defense = 100})
|
||||
local furyBuff = {
|
||||
id = "Fury",
|
||||
duration = 5,
|
||||
maxStacks = 3,
|
||||
stackPolicy = 1,
|
||||
modifiers = {{attr = "Attack", op = 0, value = 10}, {attr = "Attack", op = 1, value = 0.2}}
|
||||
}
|
||||
hero:ApplyBuff(furyBuff)
|
||||
hero:ApplyBuff(furyBuff)
|
||||
AssertEqual(
|
||||
hero:GetBuffStacks("Fury"),
|
||||
2
|
||||
)
|
||||
AssertNear(
|
||||
hero:GetFinal("Attack"),
|
||||
98
|
||||
)
|
||||
local dmg = boss:ApplyDamageFrom(hero, 1, true, 1.5)
|
||||
AssertEqual(dmg.mitigatedDamage, 74)
|
||||
AssertEqual(dmg.targetHpAfter, 426)
|
||||
AssertFalse(dmg.targetKilled)
|
||||
boss:ApplyBuff({id = "Poison", duration = 3.1, tickInterval = 1, hpPerTick = -15})
|
||||
boss:Tick(2.5)
|
||||
AssertEqual(
|
||||
boss:GetBase("HP"),
|
||||
396
|
||||
)
|
||||
boss:Tick(1)
|
||||
AssertEqual(
|
||||
boss:GetBase("HP"),
|
||||
381
|
||||
)
|
||||
AssertEqual(
|
||||
boss:GetBuffStacks("Poison"),
|
||||
0
|
||||
)
|
||||
end
|
||||
)
|
||||
Test(
|
||||
"InventorySystem: stack merge, split, swap, consume, filter and serialize/deserialize",
|
||||
function()
|
||||
local inv = __TS__New(InventorySystem, 4)
|
||||
inv:RegisterItemDef({
|
||||
itemId = "potion_hp",
|
||||
name = "Health Potion",
|
||||
category = "Consumable",
|
||||
rarity = 0,
|
||||
maxStack = 10
|
||||
})
|
||||
inv:RegisterItemDef({
|
||||
itemId = "sword_excalibur",
|
||||
name = "Excalibur",
|
||||
category = "Weapon",
|
||||
rarity = 3,
|
||||
maxStack = 1
|
||||
})
|
||||
AssertEqual(
|
||||
inv:AddItem("potion_hp", 25),
|
||||
0
|
||||
)
|
||||
AssertEqual(
|
||||
inv:GetItemCount("potion_hp"),
|
||||
25
|
||||
)
|
||||
AssertEqual(
|
||||
inv:GetUsedSlotCount(),
|
||||
3
|
||||
)
|
||||
AssertEqual(
|
||||
inv:AddItem("sword_excalibur", 2),
|
||||
1
|
||||
)
|
||||
AssertEqual(
|
||||
inv:GetItemCount("sword_excalibur"),
|
||||
1
|
||||
)
|
||||
AssertTrue(inv:ConsumeItem("potion_hp", 12))
|
||||
AssertEqual(
|
||||
inv:GetItemCount("potion_hp"),
|
||||
13
|
||||
)
|
||||
AssertNil(inv:GetSlot(2))
|
||||
AssertTrue(inv:SplitSlot(0, 2, 4))
|
||||
AssertEqual(
|
||||
inv:GetSlot(0).count,
|
||||
6
|
||||
)
|
||||
AssertEqual(
|
||||
inv:GetSlot(2).count,
|
||||
4
|
||||
)
|
||||
AssertTrue(inv:SwapSlots(0, 3))
|
||||
AssertEqual(
|
||||
inv:GetSlot(0).itemId,
|
||||
"sword_excalibur"
|
||||
)
|
||||
AssertEqual(
|
||||
#inv:FilterByCategory("Weapon"),
|
||||
1
|
||||
)
|
||||
AssertEqual(
|
||||
#inv:FilterByCategory("Consumable"),
|
||||
3
|
||||
)
|
||||
local saved = inv:Serialize()
|
||||
local restored = __TS__New(InventorySystem, 1)
|
||||
restored:Deserialize(saved)
|
||||
AssertEqual(
|
||||
restored:GetItemCount("potion_hp"),
|
||||
13
|
||||
)
|
||||
AssertEqual(
|
||||
restored:GetSlot(0).itemId,
|
||||
"sword_excalibur"
|
||||
)
|
||||
end
|
||||
)
|
||||
Test(
|
||||
"CoroutineTaskRunner: WaitSeconds, WaitUntil and UObject destroy auto-cancellation",
|
||||
function()
|
||||
local runner = __TS__New(CoroutineTaskRunner)
|
||||
local trace = {}
|
||||
local doorOpened = false
|
||||
runner:Start(function()
|
||||
trace[#trace + 1] = "start"
|
||||
CoroutineTaskRunner.WaitSeconds(0.5)
|
||||
trace[#trace + 1] = "after_delay"
|
||||
CoroutineTaskRunner.WaitUntil(function() return doorOpened end)
|
||||
trace[#trace + 1] = "entered_room"
|
||||
end)
|
||||
AssertArrayEqual(trace, {"start"})
|
||||
runner:Tick(0.3)
|
||||
AssertArrayEqual(trace, {"start"})
|
||||
runner:Tick(0.3)
|
||||
AssertArrayEqual(trace, {"start", "after_delay"})
|
||||
runner:Tick(1)
|
||||
AssertArrayEqual(trace, {"start", "after_delay"})
|
||||
doorOpened = true
|
||||
runner:Tick(0.016)
|
||||
AssertArrayEqual(trace, {"start", "after_delay", "entered_room"})
|
||||
AssertEqual(
|
||||
runner:GetActiveTaskCount(),
|
||||
0
|
||||
)
|
||||
local ownerActor = CreateBoundInstance({}, "AActor")
|
||||
local postDestroyExecuted = false
|
||||
runner:Start(
|
||||
function()
|
||||
CoroutineTaskRunner.WaitSeconds(1)
|
||||
postDestroyExecuted = true
|
||||
end,
|
||||
ownerActor
|
||||
)
|
||||
ownerActor:K2_DestroyActor()
|
||||
runner:Tick(2)
|
||||
AssertFalse(postDestroyExecuted)
|
||||
AssertEqual(
|
||||
runner:GetActiveTaskCount(),
|
||||
0
|
||||
)
|
||||
end
|
||||
)
|
||||
Test(
|
||||
"BehaviorTree: Selector/Sequence/Cooldown/Inverter with Running resumption",
|
||||
function()
|
||||
local log = {}
|
||||
local castProgress = 0
|
||||
local castUltimate = __TS__New(
|
||||
BTCooldown,
|
||||
__TS__New(
|
||||
BTSequence,
|
||||
{
|
||||
__TS__New(
|
||||
BTCondition,
|
||||
function(ctx) return ctx.bb.mana >= 50 end
|
||||
),
|
||||
__TS__New(
|
||||
BTAction,
|
||||
function(ctx)
|
||||
castProgress = castProgress + ctx.dt
|
||||
if castProgress < 1 then
|
||||
log[#log + 1] = "casting"
|
||||
return 2
|
||||
end
|
||||
castProgress = 0
|
||||
ctx.bb.mana = ctx.bb.mana - 50
|
||||
log[#log + 1] = "ult_fired"
|
||||
return 0
|
||||
end
|
||||
)
|
||||
}
|
||||
),
|
||||
5
|
||||
)
|
||||
local basicAttack = __TS__New(
|
||||
BTSequence,
|
||||
{
|
||||
__TS__New(
|
||||
BTInverter,
|
||||
__TS__New(
|
||||
BTCondition,
|
||||
function(ctx) return ctx.bb.stunned == true end
|
||||
)
|
||||
),
|
||||
__TS__New(
|
||||
BTAction,
|
||||
function()
|
||||
log[#log + 1] = "basic_attack"
|
||||
return 0
|
||||
end
|
||||
)
|
||||
}
|
||||
)
|
||||
local root = __TS__New(BTSelector, {castUltimate, basicAttack})
|
||||
local ctx = {dt = 0.5, time = 0, bb = {mana = 60, stunned = false}}
|
||||
AssertEqual(
|
||||
root:Tick(ctx),
|
||||
2
|
||||
)
|
||||
ctx.time = 0.5
|
||||
AssertEqual(
|
||||
root:Tick(ctx),
|
||||
0
|
||||
)
|
||||
AssertEqual(ctx.bb.mana, 10)
|
||||
ctx.time = 1
|
||||
AssertEqual(
|
||||
root:Tick(ctx),
|
||||
0
|
||||
)
|
||||
ctx.time = 1.5
|
||||
ctx.bb.stunned = true
|
||||
AssertEqual(
|
||||
root:Tick(ctx),
|
||||
1
|
||||
)
|
||||
AssertArrayEqual(log, {"casting", "ult_fired", "basic_attack"})
|
||||
end
|
||||
)
|
||||
end
|
||||
)
|
||||
end
|
||||
return ____exports
|
||||
Reference in new issue
Block a user