462 lines
20 KiB
Lua
462 lines
20 KiB
Lua
local ____exports = {}
|
|||
|
|
local ____TestRunner = require("Tests.Harness.TestRunner")
|
||
|
|
local Reset = ____TestRunner.Reset
|
||
|
|
local PrintReport = ____TestRunner.PrintReport
|
||
|
|
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 AssertNotNil = ____TestRunner.AssertNotNil
|
||
|
|
local AssertNil = ____TestRunner.AssertNil
|
||
|
|
local AssertArrayEqual = ____TestRunner.AssertArrayEqual
|
||
|
|
local ____BasicTypesAndControlFlow = require("Tests.Syntax.BasicTypesAndControlFlow")
|
||
|
|
local RunBasicTypesAndControlFlowTests = ____BasicTypesAndControlFlow.RunBasicTypesAndControlFlowTests
|
||
|
|
local ____FunctionsClosuresAndTuples = require("Tests.Syntax.FunctionsClosuresAndTuples")
|
||
|
|
local RunFunctionsClosuresAndTuplesTests = ____FunctionsClosuresAndTuples.RunFunctionsClosuresAndTuplesTests
|
||
|
|
local ____PureClassesAndGenerics = require("Tests.Syntax.PureClassesAndGenerics")
|
||
|
|
local RunPureClassesAndGenericsTests = ____PureClassesAndGenerics.RunPureClassesAndGenericsTests
|
||
|
|
local ____GameplaySuite = require("Tests.Gameplay.GameplaySuite")
|
||
|
|
local RunGameplayTests = ____GameplaySuite.RunGameplayTests
|
||
|
|
local ____Lua54PitfallsTest = require("Tests.Extra.Lua54PitfallsTest")
|
||
|
|
local RunLua54PitfallsTests = ____Lua54PitfallsTest.RunLua54PitfallsTests
|
||
|
|
local EventBus = require("Framework.EventBus")
|
||
|
|
local HotReload = require("Framework.HotReload")
|
||
|
|
local ____Ex06_NativeContainers = require("Tests.UnLuaExamples.Ex06_NativeContainers")
|
||
|
|
local VerifyTArrayOperations = ____Ex06_NativeContainers.VerifyTArrayOperations
|
||
|
|
local VerifyTMapOperations = ____Ex06_NativeContainers.VerifyTMapOperations
|
||
|
|
local VerifyTSetOperations = ____Ex06_NativeContainers.VerifyTSetOperations
|
||
|
|
local UnLuaBaseActorClass = require("Tests.Syntax.UnLuaBaseActor")
|
||
|
|
local function TickUEWorld(steps, deltaSeconds)
|
||
|
|
local tickFn = _G.__UE_TEST_TICK
|
||
|
|
if tickFn == nil then
|
||
|
|
return
|
||
|
|
end
|
||
|
|
do
|
||
|
|
local i = 0
|
||
|
|
while i < steps do
|
||
|
|
tickFn(deltaSeconds)
|
||
|
|
i = i + 1
|
||
|
|
end
|
||
|
|
end
|
||
|
|
end
|
||
|
|
function ____exports.RunUERuntimeSuite(world)
|
||
|
|
local canTickWorld = _G.__UE_TEST_TICK ~= nil
|
||
|
|
Suite(
|
||
|
|
"UE.RealEngineIntegration",
|
||
|
|
function()
|
||
|
|
Test(
|
||
|
|
"Real UE C++ TArray / TMap / TSet containers & native __pairs iteration",
|
||
|
|
function()
|
||
|
|
VerifyTArrayOperations()
|
||
|
|
VerifyTMapOperations()
|
||
|
|
VerifyTSetOperations()
|
||
|
|
end
|
||
|
|
)
|
||
|
|
Test(
|
||
|
|
"Real UE SpawnActor + @unlua UnLuaBaseActor (Overridden, static methods, destructuring)",
|
||
|
|
function()
|
||
|
|
local actor = world:SpawnActor(
|
||
|
|
UE.ATargetPoint.StaticClass(),
|
||
|
|
UE.FTransform(
|
||
|
|
UE.FRotator(0, 0, 0):ToQuat(),
|
||
|
|
UE.FVector(0, 0, 0),
|
||
|
|
UE.FVector(1, 1, 1)
|
||
|
|
),
|
||
|
|
UE.ESpawnActorCollisionHandlingMethod.AlwaysSpawn,
|
||
|
|
nil,
|
||
|
|
nil,
|
||
|
|
"Tests.Syntax.UnLuaBaseActor"
|
||
|
|
)
|
||
|
|
AssertNotNil(actor)
|
||
|
|
AssertTrue(UE.UKismetSystemLibrary.IsValid(actor))
|
||
|
|
AssertTrue(actor:IsA(UE.AActor.StaticClass()))
|
||
|
|
AssertEqual(
|
||
|
|
actor:GetHp(),
|
||
|
|
100
|
||
|
|
)
|
||
|
|
AssertArrayEqual(
|
||
|
|
actor:GetTrace(),
|
||
|
|
{"Base.ReceiveBeginPlay"}
|
||
|
|
)
|
||
|
|
AssertEqual(
|
||
|
|
actor:ApplyConfig({name = "UEHero"}),
|
||
|
|
"UEHero:100"
|
||
|
|
)
|
||
|
|
AssertEqual(
|
||
|
|
actor:ApplyConfig({name = "UEBoss", hp = 888}),
|
||
|
|
"UEBoss:888"
|
||
|
|
)
|
||
|
|
AssertEqual(
|
||
|
|
actor:SumScores({10}),
|
||
|
|
15
|
||
|
|
)
|
||
|
|
AssertEqual(
|
||
|
|
actor:SumScores({1, 2, 3, 4}),
|
||
|
|
10
|
||
|
|
)
|
||
|
|
AssertEqual(
|
||
|
|
UnLuaBaseActorClass:StaticWithSelf(25),
|
||
|
|
125
|
||
|
|
)
|
||
|
|
AssertEqual(
|
||
|
|
UnLuaBaseActorClass.StaticNoSelf(7, 8),
|
||
|
|
15
|
||
|
|
)
|
||
|
|
AssertEqual(
|
||
|
|
UnLuaBaseActorClass.StaticDocNoSelf(9),
|
||
|
|
27
|
||
|
|
)
|
||
|
|
actor:K2_DestroyActor()
|
||
|
|
end
|
||
|
|
)
|
||
|
|
Test(
|
||
|
|
"Real UE SpawnActor + 3-level @unlua inheritance (GrandChild -> Sub -> Base -> AActor)",
|
||
|
|
function()
|
||
|
|
local grand = world:SpawnActor(
|
||
|
|
UE.ATriggerBox.StaticClass(),
|
||
|
|
UE.FTransform(
|
||
|
|
UE.FRotator(0, 0, 0):ToQuat(),
|
||
|
|
UE.FVector(0, 0, 0),
|
||
|
|
UE.FVector(1, 1, 1)
|
||
|
|
),
|
||
|
|
UE.ESpawnActorCollisionHandlingMethod.AlwaysSpawn,
|
||
|
|
nil,
|
||
|
|
nil,
|
||
|
|
"Tests.Syntax.UnLuaGrandChildActor"
|
||
|
|
)
|
||
|
|
AssertNotNil(grand)
|
||
|
|
AssertArrayEqual(
|
||
|
|
grand:GetTrace(),
|
||
|
|
{"Base.ReceiveBeginPlay", "Sub.ReceiveBeginPlay", "GrandChild.ReceiveBeginPlay"}
|
||
|
|
)
|
||
|
|
AssertEqual(
|
||
|
|
grand:GetFaction(),
|
||
|
|
"Ally"
|
||
|
|
)
|
||
|
|
AssertEqual(
|
||
|
|
grand:GetTier(),
|
||
|
|
3
|
||
|
|
)
|
||
|
|
AssertEqual(
|
||
|
|
grand:ComputeBonus(10),
|
||
|
|
33
|
||
|
|
)
|
||
|
|
AssertEqual(
|
||
|
|
grand:SetupWithFaction({name = "Knight", hp = 250}, "Enemy"),
|
||
|
|
"Knight:250[Enemy]"
|
||
|
|
)
|
||
|
|
grand:K2_DestroyActor()
|
||
|
|
end
|
||
|
|
)
|
||
|
|
Test(
|
||
|
|
"Real UE Ex01_HelloWorld & Ex02_OverrideBlueprintEvents lifecycle",
|
||
|
|
function()
|
||
|
|
local hello = world:SpawnActor(
|
||
|
|
UE.ATriggerSphere.StaticClass(),
|
||
|
|
UE.FTransform(
|
||
|
|
UE.FRotator(0, 0, 0):ToQuat(),
|
||
|
|
UE.FVector(0, 0, 0),
|
||
|
|
UE.FVector(1, 1, 1)
|
||
|
|
),
|
||
|
|
UE.ESpawnActorCollisionHandlingMethod.AlwaysSpawn,
|
||
|
|
nil,
|
||
|
|
nil,
|
||
|
|
"Tests.UnLuaExamples.Ex01_HelloWorld"
|
||
|
|
)
|
||
|
|
AssertNotNil(hello)
|
||
|
|
AssertTrue((string.find(
|
||
|
|
hello:GetGreeting(),
|
||
|
|
"Hello UnLua from TypeScript",
|
||
|
|
1,
|
||
|
|
true
|
||
|
|
)) ~= nil)
|
||
|
|
hello:K2_DestroyActor()
|
||
|
|
local eventsActor = world:SpawnActor(
|
||
|
|
UE.ATriggerCapsule.StaticClass(),
|
||
|
|
UE.FTransform(
|
||
|
|
UE.FRotator(0, 0, 0):ToQuat(),
|
||
|
|
UE.FVector(0, 0, 0),
|
||
|
|
UE.FVector(1, 1, 1)
|
||
|
|
),
|
||
|
|
UE.ESpawnActorCollisionHandlingMethod.AlwaysSpawn,
|
||
|
|
nil,
|
||
|
|
nil,
|
||
|
|
"Tests.UnLuaExamples.Ex02_OverrideBlueprintEvents"
|
||
|
|
)
|
||
|
|
AssertNotNil(eventsActor)
|
||
|
|
local events = eventsActor:GetEvents()
|
||
|
|
AssertTrue(#events >= 2)
|
||
|
|
AssertEqual(events[1], "Initialize")
|
||
|
|
eventsActor:K2_DestroyActor()
|
||
|
|
end
|
||
|
|
)
|
||
|
|
Test(
|
||
|
|
"Real UE Ex04 DynamicBinding: SpawnActor, SpawnActorEx & UE.NewObject with Initializer",
|
||
|
|
function()
|
||
|
|
local spawned = world:SpawnActor(
|
||
|
|
UE.APlayerStart.StaticClass(),
|
||
|
|
UE.FTransform(
|
||
|
|
UE.FRotator(0, 90, 0):ToQuat(),
|
||
|
|
UE.FVector(100, 200, 300),
|
||
|
|
UE.FVector(1, 1, 1)
|
||
|
|
),
|
||
|
|
UE.ESpawnActorCollisionHandlingMethod.AlwaysSpawn,
|
||
|
|
nil,
|
||
|
|
nil,
|
||
|
|
"Tests.UnLuaExamples.Ex04_DynamicSpawnedActor",
|
||
|
|
{InitTag = "RealUESpawn", InitPower = 555}
|
||
|
|
)
|
||
|
|
AssertNotNil(spawned)
|
||
|
|
AssertEqual(
|
||
|
|
spawned:GetInitTag(),
|
||
|
|
"RealUESpawn"
|
||
|
|
)
|
||
|
|
AssertEqual(
|
||
|
|
spawned:GetInitPower(),
|
||
|
|
555
|
||
|
|
)
|
||
|
|
AssertTrue(spawned:IsBeginPlayCalled())
|
||
|
|
spawned:K2_DestroyActor()
|
||
|
|
local params = UE.FActorSpawnParameters()
|
||
|
|
params.bNoFail = true
|
||
|
|
params.SpawnCollisionHandlingOverride = UE.ESpawnActorCollisionHandlingMethod.AlwaysSpawn
|
||
|
|
local spawnedEx = world:SpawnActorEx(
|
||
|
|
UE.APlayerStart.StaticClass(),
|
||
|
|
UE.FTransform(
|
||
|
|
UE.FRotator(0, 0, 0):ToQuat(),
|
||
|
|
UE.FVector(10, 20, 30),
|
||
|
|
UE.FVector(1, 1, 1)
|
||
|
|
),
|
||
|
|
{InitTag = "RealUESpawnEx", InitPower = 777},
|
||
|
|
"Tests.UnLuaExamples.Ex04_DynamicSpawnedActor",
|
||
|
|
params
|
||
|
|
)
|
||
|
|
AssertNotNil(spawnedEx)
|
||
|
|
AssertEqual(
|
||
|
|
spawnedEx:GetInitTag(),
|
||
|
|
"RealUESpawnEx"
|
||
|
|
)
|
||
|
|
AssertEqual(
|
||
|
|
spawnedEx:GetInitPower(),
|
||
|
|
777
|
||
|
|
)
|
||
|
|
spawnedEx:K2_DestroyActor()
|
||
|
|
local dynObj = UE.NewObject(
|
||
|
|
UE.ULuaBundleAsset.StaticClass(),
|
||
|
|
world,
|
||
|
|
"RealUEDynObj",
|
||
|
|
"Tests.UnLuaExamples.Ex04_DynamicObject",
|
||
|
|
{Seed = 10}
|
||
|
|
)
|
||
|
|
AssertNotNil(dynObj)
|
||
|
|
AssertEqual(
|
||
|
|
dynObj:GetSeed(),
|
||
|
|
10
|
||
|
|
)
|
||
|
|
AssertEqual(
|
||
|
|
dynObj:NextValue(),
|
||
|
|
21
|
||
|
|
)
|
||
|
|
AssertEqual(
|
||
|
|
dynObj:NextValue(),
|
||
|
|
43
|
||
|
|
)
|
||
|
|
end
|
||
|
|
)
|
||
|
|
Test(
|
||
|
|
"Real UE Ex08 Struct math, K2_SetActorLocation & LineTraceSingle multi-return",
|
||
|
|
function()
|
||
|
|
local actor = world:SpawnActor(
|
||
|
|
UE.ATextRenderActor.StaticClass(),
|
||
|
|
UE.FTransform(
|
||
|
|
UE.FRotator(0, 0, 0):ToQuat(),
|
||
|
|
UE.FVector(0, 0, 0),
|
||
|
|
UE.FVector(1, 1, 1)
|
||
|
|
),
|
||
|
|
UE.ESpawnActorCollisionHandlingMethod.AlwaysSpawn,
|
||
|
|
nil,
|
||
|
|
nil,
|
||
|
|
"Tests.UnLuaExamples.Ex08_StructAndTrace"
|
||
|
|
)
|
||
|
|
AssertNotNil(actor)
|
||
|
|
local size, dot, dist = actor:ComputeVectorMath()
|
||
|
|
AssertNear(size, 13, 0.001)
|
||
|
|
AssertNear(dot, 0, 0.001)
|
||
|
|
AssertNear(dist, 13, 0.001)
|
||
|
|
local bHit, hitDist, impact = actor:TraceForward(500)
|
||
|
|
AssertFalse(bHit)
|
||
|
|
AssertEqual(
|
||
|
|
type(hitDist),
|
||
|
|
"number"
|
||
|
|
)
|
||
|
|
AssertNotNil(impact)
|
||
|
|
local bHit2, outHit2 = actor:TraceForwardMultiReturn(500)
|
||
|
|
AssertFalse(bHit2)
|
||
|
|
AssertNotNil(outHit2)
|
||
|
|
actor:K2_DestroyActor()
|
||
|
|
end
|
||
|
|
)
|
||
|
|
Test(
|
||
|
|
"Real UE UnLua.Ref / UnLua.Unref & FWeakObjectPtr validity",
|
||
|
|
function()
|
||
|
|
local obj = UE.NewObject(
|
||
|
|
UE.UObjectLibrary.StaticClass(),
|
||
|
|
world,
|
||
|
|
"WeakRefTarget"
|
||
|
|
)
|
||
|
|
AssertTrue(UE.UKismetSystemLibrary.IsValid(obj))
|
||
|
|
local refProxy = UnLua.Ref(obj)
|
||
|
|
AssertNotNil(refProxy)
|
||
|
|
local weak = UE.FWeakObjectPtr(obj)
|
||
|
|
AssertTrue(weak:IsValid())
|
||
|
|
AssertNotNil(weak:Get())
|
||
|
|
weak:Reset()
|
||
|
|
AssertFalse(weak:IsValid())
|
||
|
|
AssertNil(weak:Get())
|
||
|
|
UnLua.Unref(obj)
|
||
|
|
end
|
||
|
|
)
|
||
|
|
Test(
|
||
|
|
"Real UE ALuaDemoActor (IUnLuaInterface static binding) + EventBus + Tick + EndPlay",
|
||
|
|
function()
|
||
|
|
EventBus.Clear()
|
||
|
|
local demoActor = world:SpawnActor(
|
||
|
|
UE.ALuaDemoActor.StaticClass(),
|
||
|
|
UE.FTransform(
|
||
|
|
UE.FRotator(0, 0, 0):ToQuat(),
|
||
|
|
UE.FVector(0, 0, 100),
|
||
|
|
UE.FVector(1, 1, 1)
|
||
|
|
),
|
||
|
|
UE.ESpawnActorCollisionHandlingMethod.AlwaysSpawn
|
||
|
|
)
|
||
|
|
AssertNotNil(demoActor)
|
||
|
|
AssertTrue(demoActor:IsA(UE.ALuaDemoActor.StaticClass()))
|
||
|
|
local initText = demoActor:GetStatusTextString()
|
||
|
|
AssertTrue((string.find(initText, "UnLua Active", 1, true)) ~= nil)
|
||
|
|
AssertTrue((string.find(initText, "Pings: 0", 1, true)) ~= nil)
|
||
|
|
if canTickWorld then
|
||
|
|
TickUEWorld(5, 0.05)
|
||
|
|
end
|
||
|
|
EventBus.Emit("Demo.Ping", "UERealEngineTest")
|
||
|
|
local pingedText = demoActor:GetStatusTextString()
|
||
|
|
AssertTrue((string.find(pingedText, "Pings: 1", 1, true)) ~= nil)
|
||
|
|
AssertTrue((string.find(pingedText, "Color: Gold", 1, true)) ~= nil)
|
||
|
|
local bMoved = demoActor:K2_SetActorLocation(
|
||
|
|
UE.FVector(120, 240, 360),
|
||
|
|
false,
|
||
|
|
nil,
|
||
|
|
true
|
||
|
|
)
|
||
|
|
AssertTrue(bMoved)
|
||
|
|
local loc = demoActor:K2_GetActorLocation()
|
||
|
|
AssertNear(loc.X, 120, 0.01)
|
||
|
|
AssertNear(loc.Y, 240, 0.01)
|
||
|
|
AssertNear(loc.Z, 360, 0.01)
|
||
|
|
demoActor:K2_DestroyActor()
|
||
|
|
EventBus.Clear()
|
||
|
|
end
|
||
|
|
)
|
||
|
|
if canTickWorld then
|
||
|
|
Test(
|
||
|
|
"Real UE FTimerManager + K2_SetTimerDelegate(UnLua.Delegate) ticked by UWorld",
|
||
|
|
function()
|
||
|
|
local timerActor = world:SpawnActor(
|
||
|
|
UE.ACameraActor.StaticClass(),
|
||
|
|
UE.FTransform(
|
||
|
|
UE.FRotator(0, 0, 0):ToQuat(),
|
||
|
|
UE.FVector(0, 0, 0),
|
||
|
|
UE.FVector(1, 1, 1)
|
||
|
|
),
|
||
|
|
UE.ESpawnActorCollisionHandlingMethod.AlwaysSpawn,
|
||
|
|
nil,
|
||
|
|
nil,
|
||
|
|
"Tests.UE.UETimerTestActor"
|
||
|
|
)
|
||
|
|
AssertNotNil(timerActor)
|
||
|
|
AssertTrue(timerActor:IsTimerActive())
|
||
|
|
AssertEqual(
|
||
|
|
timerActor:GetTimerCount(),
|
||
|
|
0
|
||
|
|
)
|
||
|
|
TickUEWorld(10, 0.05)
|
||
|
|
AssertTrue(timerActor:GetTimerCount() >= 3)
|
||
|
|
local countBeforeStop = timerActor:GetTimerCount()
|
||
|
|
timerActor:StopTimer()
|
||
|
|
AssertFalse(timerActor:IsTimerActive())
|
||
|
|
TickUEWorld(6, 0.05)
|
||
|
|
AssertEqual(
|
||
|
|
timerActor:GetTimerCount(),
|
||
|
|
countBeforeStop
|
||
|
|
)
|
||
|
|
timerActor:K2_DestroyActor()
|
||
|
|
end
|
||
|
|
)
|
||
|
|
Test(
|
||
|
|
"Real UE FLatentActionManager + UKismetSystemLibrary.Delay coroutine (Ex07)",
|
||
|
|
function()
|
||
|
|
local latentActor = world:SpawnActor(
|
||
|
|
UE.ADecalActor.StaticClass(),
|
||
|
|
UE.FTransform(
|
||
|
|
UE.FRotator(0, 0, 0):ToQuat(),
|
||
|
|
UE.FVector(0, 0, 0),
|
||
|
|
UE.FVector(1, 1, 1)
|
||
|
|
),
|
||
|
|
UE.ESpawnActorCollisionHandlingMethod.AlwaysSpawn,
|
||
|
|
nil,
|
||
|
|
nil,
|
||
|
|
"Tests.UnLuaExamples.Ex07_CallLatentFunction"
|
||
|
|
)
|
||
|
|
AssertNotNil(latentActor)
|
||
|
|
AssertArrayEqual(
|
||
|
|
latentActor:GetSteps(),
|
||
|
|
{"Step1:Start"}
|
||
|
|
)
|
||
|
|
AssertFalse(latentActor:IsCompleted())
|
||
|
|
TickUEWorld(6, 0.1)
|
||
|
|
AssertArrayEqual(
|
||
|
|
latentActor:GetSteps(),
|
||
|
|
{"Step1:Start", "Step2:After0.5s"}
|
||
|
|
)
|
||
|
|
AssertFalse(latentActor:IsCompleted())
|
||
|
|
TickUEWorld(11, 0.1)
|
||
|
|
AssertArrayEqual(
|
||
|
|
latentActor:GetSteps(),
|
||
|
|
{"Step1:Start", "Step2:After0.5s", "Step3:Done"}
|
||
|
|
)
|
||
|
|
AssertTrue(latentActor:IsCompleted())
|
||
|
|
latentActor:K2_DestroyActor()
|
||
|
|
end
|
||
|
|
)
|
||
|
|
end
|
||
|
|
Test(
|
||
|
|
"Real UE HotReload.ReloadModule validates arguments and emits events",
|
||
|
|
function()
|
||
|
|
EventBus.Clear()
|
||
|
|
local badOk = HotReload.ReloadModule("")
|
||
|
|
AssertFalse(badOk)
|
||
|
|
local ok = HotReload.ReloadModule("Framework.Log")
|
||
|
|
AssertTrue(ok)
|
||
|
|
EventBus.Clear()
|
||
|
|
end
|
||
|
|
)
|
||
|
|
end
|
||
|
|
)
|
||
|
|
end
|
||
|
|
--- 在真实 UE 5.8 + UnLua 2.3.6 环境下运行的统一入口。
|
||
|
|
-- 可由 RunLuaTestsCommandlet 或编辑器控制台命令 `Lua.RunTests` 调用。
|
||
|
|
function ____exports.RunAllInUE(world)
|
||
|
|
local targetWorld = world or _G.__UE_TEST_WORLD
|
||
|
|
Reset()
|
||
|
|
RunBasicTypesAndControlFlowTests()
|
||
|
|
RunFunctionsClosuresAndTuplesTests()
|
||
|
|
RunPureClassesAndGenericsTests()
|
||
|
|
RunGameplayTests()
|
||
|
|
RunLua54PitfallsTests()
|
||
|
|
if targetWorld ~= nil then
|
||
|
|
____exports.RunUERuntimeSuite(targetWorld)
|
||
|
|
end
|
||
|
|
return PrintReport()
|
||
|
|
end
|
||
|
|
return ____exports
|