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 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 AssertArrayEqual = M__Tests_Harness_TestRunner.AssertArrayEqual local M__Tests_Harness_MockUnLuaRuntime = __MODULE__.Import("Tests.Harness.MockUnLuaRuntime", __module) local AdvanceMockTime = M__Tests_Harness_MockUnLuaRuntime.AdvanceMockTime local CreateBoundInstance = M__Tests_Harness_MockUnLuaRuntime.CreateBoundInstance local CreateMockMulticastDelegate = M__Tests_Harness_MockUnLuaRuntime.CreateMockMulticastDelegate local CreateMockScriptDelegate = M__Tests_Harness_MockUnLuaRuntime.CreateMockScriptDelegate local GetMockState = M__Tests_Harness_MockUnLuaRuntime.GetMockState local ResetMockState = M__Tests_Harness_MockUnLuaRuntime.ResetMockState 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 Ex01_HelloWorld = require("Tests.UnLuaExamples.Ex01_HelloWorld") local Ex02_OverrideBlueprintEvents = require("Tests.UnLuaExamples.Ex02_OverrideBlueprintEvents") local Ex03_BindDelegates = require("Tests.UnLuaExamples.Ex03_BindDelegates") local Ex04_DynamicBinding = require("Tests.UnLuaExamples.Ex04_DynamicBinding") local Ex05_BindInputs = require("Tests.UnLuaExamples.Ex05_BindInputs") local Ex07_CallLatentFunction = require("Tests.UnLuaExamples.Ex07_CallLatentFunction") local Ex08_StructAndTrace = require("Tests.UnLuaExamples.Ex08_StructAndTrace") local Ex09_AssetRefAndUMG = require("Tests.UnLuaExamples.Ex09_AssetRefAndUMG") function ____exports.RunUnLuaExamplesTests() Suite( "UnLuaExamples.OfficialPorts", function() Test( "Ex01_HelloWorld: ReceiveBeginPlay prints and logs greeting", function() ResetMockState() local actor = CreateBoundInstance(Ex01_HelloWorld, "AActor") actor:ReceiveBeginPlay() AssertTrue((string.find( actor:GetGreeting(), "Hello UnLua from TypeScript", 1, true )) ~= nil) AssertEqual( #GetMockState().printedStrings, 1 ) AssertEqual( #GetMockState().logs, 1 ) end ) Test( "Ex02_OverrideBlueprintEvents: lifecycle events and self.Overridden chain", function() ResetMockState() local actor = CreateBoundInstance(Ex02_OverrideBlueprintEvents, "AActor") local other = CreateBoundInstance({}, "AActor", {__name = "OtherActor_1"}) actor:Initialize() actor:UserConstructionScript() actor:ReceiveBeginPlay() actor:ReceiveTick(0.016) actor:ReceiveTick(0.034) actor:ReceiveAnyDamage(35, nil, nil, other) actor:ReceiveActorBeginOverlap(other) actor:ReceiveEndPlay(UE.EEndPlayReason.Destroyed) AssertNear( actor:GetElapsed(), 0.05 ) AssertEqual( actor:GetTotalDamage(), 35 ) AssertEqual( actor:GetOverlapCount(), 1 ) AssertArrayEqual( actor:GetEvents(), { "Initialize", "UserConstructionScript", "ReceiveBeginPlay", "Damage:35", "Overlap:OtherActor_1", "EndPlay:0" } ) AssertEqual( #GetMockState().overriddenCalls, 7 ) end ) Test( "Ex03_BindDelegates: Multicast, FScriptDelegate and K2_SetTimerDelegate via UnLua.Delegate", function() ResetMockState() local mockBtn = {OnClicked = CreateMockMulticastDelegate()} local singleDel = CreateMockScriptDelegate() local widget = CreateBoundInstance(Ex03_BindDelegates, "UUserWidget", {ClickMeButton = mockBtn, CustomSingleDelegate = singleDel}) widget:Construct() AssertTrue(mockBtn.OnClicked:IsBound()) mockBtn.OnClicked:Broadcast() mockBtn.OnClicked:Broadcast() AssertEqual( widget:GetClickCount(), 2 ) AssertEqual( singleDel:Execute(5), 52 ) AdvanceMockTime(1.6) AssertEqual( widget:GetTimerTickCount(), 3 ) AssertTrue(UE.UKismetSystemLibrary.K2_IsTimerActiveHandle( widget, widget:GetTimerHandle() )) widget:Destruct() AssertFalse(mockBtn.OnClicked:IsBound()) mockBtn.OnClicked:Broadcast() AssertEqual( widget:GetClickCount(), 2 ) AdvanceMockTime(2) AssertEqual( widget:GetTimerTickCount(), 3 ) end ) Test( "Ex04_DynamicBinding: World:SpawnActor, SpawnActorEx and UE.NewObject with Initializer", function() ResetMockState() local spawner = CreateBoundInstance(Ex04_DynamicBinding, "AActor") local spawned = spawner:SpawnDynamicActor("Fireball", 120) AssertNotNil(spawned) AssertEqual( spawned:GetInitTag(), "Fireball" ) AssertEqual( spawned:GetInitPower(), 120 ) AssertTrue(spawned:IsBeginPlayCalled()) local spawnedEx = spawner:SpawnDynamicActorEx("IceShard") AssertNotNil(spawnedEx) AssertEqual( spawnedEx:GetInitTag(), "IceShard" ) AssertEqual( spawnedEx:GetInitPower(), 77 ) local dynObj = spawner:CreateDynamicObject(10) AssertNotNil(dynObj) AssertEqual( dynObj:GetSeed(), 10 ) AssertEqual( dynObj:NextValue(), 21 ) AssertEqual( dynObj:NextValue(), 43 ) end ) Test( "Ex05_BindInputs: UnLua.Input and UnLua.EnhancedInput module registrations", function() local pc = CreateBoundInstance(Ex05_BindInputs, "APlayerController") pc:ReceiveBeginPlay() ResetMockState() local pkg = _G.package local modName = "Tests.UnLuaExamples.Ex05_BindInputs" local prevMod = pkg.loaded[modName] pkg.loaded[modName] = nil require(modName) pkg.loaded[modName] = prevMod local bindings = GetMockState().inputBindings AssertEqual(#bindings, 5) AssertEqual(bindings[1].kind, "Key") AssertEqual(bindings[1].name, "SpaceBar") local ____AssertEqual_2 = AssertEqual local ____opt_0 = bindings[2].args ____AssertEqual_2(____opt_0 and ____opt_0.Ctrl, true) AssertEqual(bindings[3].kind, "Action") AssertEqual(bindings[4].kind, "Axis") AssertEqual(bindings[5].kind, "EnhancedAction") bindings[1].handler(pc) AssertEqual( pc:GetLastAction(), "SpaceBar:Pressed" ) bindings[2].handler(pc) AssertEqual( pc:GetLastAction(), "Ctrl+S" ) bindings[3].handler(pc) AssertEqual( pc:GetLastAction(), "Action:Jump" ) bindings[4].handler(pc, 0.75) AssertNear( pc:GetMoveForwardValue(), 0.75 ) bindings[5].handler(pc, -1) AssertNear( pc:GetMoveForwardValue(), -1 ) AssertEqual( pc:GetEnhancedCount(), 1 ) end ) Test( "Ex06_NativeContainers: TArray, TMap and TSet full API & pairs()", function() VerifyTArrayOperations() VerifyTMapOperations() VerifyTSetOperations() end ) Test( "Ex07_CallLatentFunction: coroutine Delay sequence & destroy abort guard", function() ResetMockState() local actor = CreateBoundInstance(Ex07_CallLatentFunction, "AActor") actor:ReceiveBeginPlay() AssertArrayEqual( actor:GetSteps(), {"Step1:Start"} ) AssertFalse(actor:IsCompleted()) AdvanceMockTime(0.5) AssertArrayEqual( actor:GetSteps(), {"Step1:Start", "Step2:After0.5s"} ) AssertFalse(actor:IsCompleted()) AdvanceMockTime(1) AssertArrayEqual( actor:GetSteps(), {"Step1:Start", "Step2:After0.5s", "Step3:Done"} ) AssertTrue(actor:IsCompleted()) local doomed = CreateBoundInstance(Ex07_CallLatentFunction, "AActor") doomed:ReceiveBeginPlay() doomed:K2_DestroyActor() AdvanceMockTime(2) AssertArrayEqual( doomed:GetSteps(), {"Step1:Start"} ) AssertFalse(doomed:IsCompleted()) end ) Test( "Ex08_StructAndTrace: FVector math, LineTraceSingle multi-return and K2_SetActorLocation", function() ResetMockState() local actor = CreateBoundInstance(Ex08_StructAndTrace, "AActor") actor:ReceiveBeginPlay() local size, dot, dist = actor:ComputeVectorMath() AssertNear(size, 13) AssertNear(dot, 0) AssertNear(dist, 13) AssertTrue(actor:TeleportTo(100, 0, 50)) local bHit, hitDist, impact = actor:TraceForward(200) AssertTrue(bHit) AssertNear(hitDist, 100) AssertNear(impact.X, 200) AssertNear(impact.Z, 50) end ) Test( "Ex09_AssetRefAndUMG: UObject.Load, UnLua.Ref/Unref, WeakPtr and UMG Release", function() ResetMockState() local widget = CreateBoundInstance(Ex09_AssetRefAndUMG, "UUserWidget") widget:Initialize() widget:Construct() widget:ShowOnScreen() AssertTrue(widget:IsConstructed()) AssertFalse(widget:IsDestructed()) AssertTrue(widget:HasValidWeakAsset()) widget:CloseAndRelease() AssertTrue(widget:IsDestructed()) AssertFalse(widget:HasValidWeakAsset()) end ) end ) end function __module.UpdateImports() 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 AssertArrayEqual = M__Tests_Harness_TestRunner.AssertArrayEqual AdvanceMockTime = M__Tests_Harness_MockUnLuaRuntime.AdvanceMockTime CreateBoundInstance = M__Tests_Harness_MockUnLuaRuntime.CreateBoundInstance CreateMockMulticastDelegate = M__Tests_Harness_MockUnLuaRuntime.CreateMockMulticastDelegate CreateMockScriptDelegate = M__Tests_Harness_MockUnLuaRuntime.CreateMockScriptDelegate GetMockState = M__Tests_Harness_MockUnLuaRuntime.GetMockState ResetMockState = M__Tests_Harness_MockUnLuaRuntime.ResetMockState VerifyTArrayOperations = M__Tests_UnLuaExamples_Ex06_NativeContainers.VerifyTArrayOperations VerifyTMapOperations = M__Tests_UnLuaExamples_Ex06_NativeContainers.VerifyTMapOperations VerifyTSetOperations = M__Tests_UnLuaExamples_Ex06_NativeContainers.VerifyTSetOperations end