local ____exports = __module.env local M__Tests_Harness_TestRunner = __MODULE__.Import("Tests.Harness.TestRunner", __module) local Reset = M__Tests_Harness_TestRunner.Reset local PrintReport = M__Tests_Harness_TestRunner.PrintReport local Suite = M__Tests_Harness_TestRunner.Suite local Test = M__Tests_Harness_TestRunner.Test local AssertEqual = M__Tests_Harness_TestRunner.AssertEqual local AssertNear = M__Tests_Harness_TestRunner.AssertNear local AssertTrue = M__Tests_Harness_TestRunner.AssertTrue local AssertFalse = M__Tests_Harness_TestRunner.AssertFalse local AssertNotNil = M__Tests_Harness_TestRunner.AssertNotNil local AssertNil = M__Tests_Harness_TestRunner.AssertNil local AssertArrayEqual = M__Tests_Harness_TestRunner.AssertArrayEqual local M__Tests_Syntax_BasicTypesAndControlFlow = __MODULE__.Import("Tests.Syntax.BasicTypesAndControlFlow", __module) local RunBasicTypesAndControlFlowTests = M__Tests_Syntax_BasicTypesAndControlFlow.RunBasicTypesAndControlFlowTests local M__Tests_Syntax_FunctionsClosuresAndTuples = __MODULE__.Import("Tests.Syntax.FunctionsClosuresAndTuples", __module) local RunFunctionsClosuresAndTuplesTests = M__Tests_Syntax_FunctionsClosuresAndTuples.RunFunctionsClosuresAndTuplesTests local M__Tests_Syntax_PureClassesAndGenerics = __MODULE__.Import("Tests.Syntax.PureClassesAndGenerics", __module) local RunPureClassesAndGenericsTests = M__Tests_Syntax_PureClassesAndGenerics.RunPureClassesAndGenericsTests local M__Tests_Gameplay_GameplaySuite = __MODULE__.Import("Tests.Gameplay.GameplaySuite", __module) local RunGameplayTests = M__Tests_Gameplay_GameplaySuite.RunGameplayTests local M__Tests_Extra_Lua54PitfallsTest = __MODULE__.Import("Tests.Extra.Lua54PitfallsTest", __module) local RunLua54PitfallsTests = M__Tests_Extra_Lua54PitfallsTest.RunLua54PitfallsTests local EventBus = __MODULE__.Import("Framework.EventBus", __module) local HotReload = __MODULE__.Import("Framework.HotReload", __module) local M__Tests_UnLuaExamples_Ex06_NativeContainers = __MODULE__.Import("Tests.UnLuaExamples.Ex06_NativeContainers", __module) local VerifyTArrayOperations = M__Tests_UnLuaExamples_Ex06_NativeContainers.VerifyTArrayOperations local VerifyTMapOperations = M__Tests_UnLuaExamples_Ex06_NativeContainers.VerifyTMapOperations local VerifyTSetOperations = M__Tests_UnLuaExamples_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) AssertEqual( type(bHit), "boolean" ) AssertEqual( type(hitDist), "number" ) AssertNotNil(impact) local bHit2, outHit2 = actor:TraceForwardMultiReturn(500) AssertEqual(bHit2, bHit) AssertNotNil(outHit2) if bHit then AssertNear(hitDist, outHit2.Distance, 0.01) end 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 (C++ callback -> pure TS Game.LuaDemoActor) + EventBus + Tick + EndPlay", function() 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, "Pure-TS Actor", 1, true)) ~= nil, "initText: " .. initText) 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() 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() local badOk = HotReload.ReloadModule("") AssertFalse(badOk) local ok = HotReload.ReloadModule("Framework.Log") AssertTrue(ok) end ) Test( "Real UE single-file __MODULE__.Reimport hot-reloads Game.LuaDemoActor spin speed and color live", function() local demoActor = world:SpawnActor( UE.ALuaDemoActor.StaticClass(), UE.FTransform( UE.FRotator(0, 0, 0):ToQuat(), UE.FVector(0, 0, 200), UE.FVector(1, 1, 1) ), UE.ESpawnActorCollisionHandlingMethod.AlwaysSpawn ) AssertNotNil(demoActor) local beforeText = demoActor:GetStatusTextString() AssertTrue((string.find(beforeText, "v1", 1, true)) ~= nil) AssertTrue((string.find(beforeText, "Speed: 60", 1, true)) ~= nil) AssertTrue((string.find(beforeText, "Color: Cyan", 1, true)) ~= nil) local actorMod = __MODULE__.Import("Game.LuaDemoActor") local activeBefore = actorMod:GetActiveControllerCount() AssertTrue(activeBefore >= 1) local defaultDir = UE.ULuaHotUpdateSubsystem.GetDefaultScriptDir() local overrideDir = UE.ULuaHotUpdateSubsystem.GetHotUpdateScriptDir() local srcPath = defaultDir .. "/Game/LuaDemoActor.lua" local dstGameDir = overrideDir .. "/Game" local dstPath = dstGameDir .. "/LuaDemoActor.lua" local srcFile = (io.open(srcPath, "rb")) AssertNotNil(srcFile) local origLua = srcFile:read("a") srcFile:close() local patchedLua = string.gsub(origLua, "\"v1\"", "\"v2-HOTFIX\"") patchedLua = string.gsub(patchedLua, "ACTOR_SPIN_SPEED = 60", "ACTOR_SPIN_SPEED = 240") patchedLua = string.gsub(patchedLua, "\"Cyan\"", "\"Crimson\"") patchedLua = string.gsub(patchedLua, "R = 0%.08", "R = 0.95") patchedLua = string.gsub(patchedLua, "G = 0%.78", "G = 0.12") patchedLua = string.gsub(patchedLua, "B = 0%.95", "B = 0.18") os.execute(string.format("mkdir -p \"%s\"", dstGameDir)) local dstFile = (io.open(dstPath, "wb")) AssertNotNil(dstFile) dstFile:write(patchedLua) dstFile:close() local okReload, errReload = HotReload.ReloadModule("Game.LuaDemoActor") os.remove(dstPath) AssertTrue( okReload, tostring(errReload) ) AssertEqual(actorMod.DEMO_VERSION, "v2-HOTFIX") AssertEqual(actorMod.ACTOR_SPIN_SPEED, 240) AssertEqual( actorMod:GetActiveControllerCount(), activeBefore ) local newColor = demoActor:GetCubeColor() AssertNear(newColor.R, 0.95, 0.01) AssertNear(newColor.G, 0.12, 0.01) local afterText = demoActor:GetStatusTextString() AssertTrue((string.find(afterText, "v2-HOTFIX", 1, true)) ~= nil, "afterText: " .. afterText) AssertTrue((string.find(afterText, "Speed: 240", 1, true)) ~= nil, "afterText: " .. afterText) AssertTrue((string.find(afterText, "Color: Crimson", 1, true)) ~= nil, "afterText: " .. afterText) local okRestore = HotReload.ReloadModule("Game.LuaDemoActor") AssertTrue(okRestore) AssertEqual(actorMod.DEMO_VERSION, "v1") AssertEqual(actorMod.ACTOR_SPIN_SPEED, 60) demoActor:K2_DestroyActor() 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 function __module.UpdateImports() Reset = M__Tests_Harness_TestRunner.Reset PrintReport = M__Tests_Harness_TestRunner.PrintReport Suite = M__Tests_Harness_TestRunner.Suite Test = M__Tests_Harness_TestRunner.Test AssertEqual = M__Tests_Harness_TestRunner.AssertEqual AssertNear = M__Tests_Harness_TestRunner.AssertNear AssertTrue = M__Tests_Harness_TestRunner.AssertTrue AssertFalse = M__Tests_Harness_TestRunner.AssertFalse AssertNotNil = M__Tests_Harness_TestRunner.AssertNotNil AssertNil = M__Tests_Harness_TestRunner.AssertNil AssertArrayEqual = M__Tests_Harness_TestRunner.AssertArrayEqual RunBasicTypesAndControlFlowTests = M__Tests_Syntax_BasicTypesAndControlFlow.RunBasicTypesAndControlFlowTests RunFunctionsClosuresAndTuplesTests = M__Tests_Syntax_FunctionsClosuresAndTuples.RunFunctionsClosuresAndTuplesTests RunPureClassesAndGenericsTests = M__Tests_Syntax_PureClassesAndGenerics.RunPureClassesAndGenericsTests RunGameplayTests = M__Tests_Gameplay_GameplaySuite.RunGameplayTests RunLua54PitfallsTests = M__Tests_Extra_Lua54PitfallsTest.RunLua54PitfallsTests VerifyTArrayOperations = M__Tests_UnLuaExamples_Ex06_NativeContainers.VerifyTArrayOperations VerifyTMapOperations = M__Tests_UnLuaExamples_Ex06_NativeContainers.VerifyTMapOperations VerifyTSetOperations = M__Tests_UnLuaExamples_Ex06_NativeContainers.VerifyTSetOperations end