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,17 @@
|
||||
local Ex01_HelloWorld = UnLua.Class()
|
||||
function Ex01_HelloWorld.ReceiveBeginPlay(self)
|
||||
self.greeting = ("Hello UnLua from TypeScript (" .. _VERSION) .. ")!"
|
||||
UnLua.Log(self.greeting)
|
||||
UE.UKismetSystemLibrary.PrintString(
|
||||
self,
|
||||
self.greeting,
|
||||
true,
|
||||
true,
|
||||
UE.FLinearColor(0.1, 0.9, 0.4, 1),
|
||||
5
|
||||
)
|
||||
end
|
||||
function Ex01_HelloWorld.GetGreeting(self)
|
||||
return self.greeting
|
||||
end
|
||||
return Ex01_HelloWorld
|
||||
@@ -0,0 +1,54 @@
|
||||
local Ex02_OverrideBlueprintEvents = UnLua.Class()
|
||||
function Ex02_OverrideBlueprintEvents.Initialize(self, _Initializer)
|
||||
self.events = {"Initialize"}
|
||||
self.totalDamage = 0
|
||||
self.elapsed = 0
|
||||
self.overlapCount = 0
|
||||
end
|
||||
function Ex02_OverrideBlueprintEvents.UserConstructionScript(self)
|
||||
self.Overridden.UserConstructionScript(self)
|
||||
self.events[#self.events + 1] = "UserConstructionScript"
|
||||
end
|
||||
function Ex02_OverrideBlueprintEvents.ReceiveBeginPlay(self)
|
||||
self.Overridden.ReceiveBeginPlay(self)
|
||||
self.events[#self.events + 1] = "ReceiveBeginPlay"
|
||||
end
|
||||
function Ex02_OverrideBlueprintEvents.ReceiveTick(self, DeltaSeconds)
|
||||
self.Overridden.ReceiveTick(self, DeltaSeconds)
|
||||
self.elapsed = self.elapsed + DeltaSeconds
|
||||
end
|
||||
function Ex02_OverrideBlueprintEvents.ReceiveAnyDamage(self, Damage, _DamageType, _InstigatedBy, _DamageCauser)
|
||||
self.Overridden.ReceiveAnyDamage(
|
||||
self,
|
||||
Damage,
|
||||
_DamageType,
|
||||
_InstigatedBy,
|
||||
_DamageCauser
|
||||
)
|
||||
self.totalDamage = self.totalDamage + Damage
|
||||
self.events[#self.events + 1] = "Damage:" .. tostring(Damage)
|
||||
end
|
||||
function Ex02_OverrideBlueprintEvents.ReceiveActorBeginOverlap(self, OtherActor)
|
||||
self.Overridden.ReceiveActorBeginOverlap(self, OtherActor)
|
||||
if UE.UKismetSystemLibrary.IsValid(OtherActor) then
|
||||
self.overlapCount = self.overlapCount + 1
|
||||
self.events[#self.events + 1] = "Overlap:" .. OtherActor:GetName()
|
||||
end
|
||||
end
|
||||
function Ex02_OverrideBlueprintEvents.ReceiveEndPlay(self, EndPlayReason)
|
||||
self.Overridden.ReceiveEndPlay(self, EndPlayReason)
|
||||
self.events[#self.events + 1] = "EndPlay:" .. tostring(EndPlayReason)
|
||||
end
|
||||
function Ex02_OverrideBlueprintEvents.GetEvents(self)
|
||||
return self.events
|
||||
end
|
||||
function Ex02_OverrideBlueprintEvents.GetTotalDamage(self)
|
||||
return self.totalDamage
|
||||
end
|
||||
function Ex02_OverrideBlueprintEvents.GetElapsed(self)
|
||||
return self.elapsed
|
||||
end
|
||||
function Ex02_OverrideBlueprintEvents.GetOverlapCount(self)
|
||||
return self.overlapCount
|
||||
end
|
||||
return Ex02_OverrideBlueprintEvents
|
||||
@@ -0,0 +1,43 @@
|
||||
local Ex03_BindDelegates = UnLua.Class()
|
||||
function Ex03_BindDelegates.Construct(self)
|
||||
self.clickCount = 0
|
||||
self.timerTickCount = 0
|
||||
if self.ClickMeButton ~= nil then
|
||||
self.ClickMeButton.OnClicked:Add(self, self.OnButtonClicked)
|
||||
end
|
||||
if self.CustomSingleDelegate ~= nil then
|
||||
self.CustomSingleDelegate:Bind(self, self.OnCustomExecute)
|
||||
end
|
||||
self.timerHandle = UE.UKismetSystemLibrary.K2_SetTimerDelegate({self, self.OnTimerTick}, 0.5, true)
|
||||
end
|
||||
function Ex03_BindDelegates.OnButtonClicked(self)
|
||||
self.clickCount = self.clickCount + 1
|
||||
end
|
||||
function Ex03_BindDelegates.OnCustomExecute(self, value)
|
||||
return value * 10 + self.clickCount
|
||||
end
|
||||
function Ex03_BindDelegates.OnTimerTick(self)
|
||||
if not UE.UKismetSystemLibrary.IsValid(self) then
|
||||
return
|
||||
end
|
||||
self.timerTickCount = self.timerTickCount + 1
|
||||
end
|
||||
function Ex03_BindDelegates.Destruct(self)
|
||||
if self.ClickMeButton ~= nil then
|
||||
self.ClickMeButton.OnClicked:Remove(self, self.OnButtonClicked)
|
||||
end
|
||||
if self.CustomSingleDelegate ~= nil then
|
||||
self.CustomSingleDelegate:Unbind()
|
||||
end
|
||||
UE.UKismetSystemLibrary.K2_ClearAndInvalidateTimerHandle(self, self.timerHandle)
|
||||
end
|
||||
function Ex03_BindDelegates.GetClickCount(self)
|
||||
return self.clickCount
|
||||
end
|
||||
function Ex03_BindDelegates.GetTimerTickCount(self)
|
||||
return self.timerTickCount
|
||||
end
|
||||
function Ex03_BindDelegates.GetTimerHandle(self)
|
||||
return self.timerHandle
|
||||
end
|
||||
return Ex03_BindDelegates
|
||||
@@ -0,0 +1,59 @@
|
||||
local Ex04_DynamicBinding = UnLua.Class()
|
||||
function Ex04_DynamicBinding.SpawnDynamicActor(self, tag, power)
|
||||
local world = self:GetWorld()
|
||||
local actorClass = UE.UClass.Load("/Game/Blueprints/BP_DynamicDemo.BP_DynamicDemo_C")
|
||||
if not world or not actorClass then
|
||||
return nil
|
||||
end
|
||||
local transform = UE.FTransform(
|
||||
UE.FRotator(0, 90, 0):ToQuat(),
|
||||
UE.FVector(100, 200, 300),
|
||||
UE.FVector(1, 1, 1)
|
||||
)
|
||||
return world:SpawnActor(
|
||||
actorClass,
|
||||
transform,
|
||||
UE.ESpawnActorCollisionHandlingMethod.AlwaysSpawn,
|
||||
self,
|
||||
nil,
|
||||
"Tests.UnLuaExamples.Ex04_DynamicSpawnedActor",
|
||||
{InitTag = tag, InitPower = power}
|
||||
)
|
||||
end
|
||||
function Ex04_DynamicBinding.SpawnDynamicActorEx(self, tag)
|
||||
local world = self:GetWorld()
|
||||
local actorClass = UE.LoadClass("/Game/Blueprints/BP_DynamicDemoEx.BP_DynamicDemoEx_C")
|
||||
if not world or not actorClass then
|
||||
return nil
|
||||
end
|
||||
local params = UE.FActorSpawnParameters()
|
||||
params.Name = "ExSpawnedActor"
|
||||
params.bNoFail = true
|
||||
params.Owner = self
|
||||
params.SpawnCollisionHandlingOverride = UE.ESpawnActorCollisionHandlingMethod.AlwaysSpawn
|
||||
return world:SpawnActorEx(
|
||||
actorClass,
|
||||
UE.FTransform(
|
||||
UE.FRotator(0, 0, 0):ToQuat(),
|
||||
UE.FVector(10, 20, 30),
|
||||
UE.FVector(1, 1, 1)
|
||||
),
|
||||
{InitTag = tag, InitPower = 77},
|
||||
"Tests.UnLuaExamples.Ex04_DynamicSpawnedActor",
|
||||
params
|
||||
)
|
||||
end
|
||||
function Ex04_DynamicBinding.CreateDynamicObject(self, seed)
|
||||
local objClass = UE.UClass.Load("/Script/CoreUObject.Object")
|
||||
if not objClass then
|
||||
return nil
|
||||
end
|
||||
return UE.NewObject(
|
||||
objClass,
|
||||
self,
|
||||
"DynamicObjectInstance",
|
||||
"Tests.UnLuaExamples.Ex04_DynamicObject",
|
||||
{Seed = seed}
|
||||
)
|
||||
end
|
||||
return Ex04_DynamicBinding
|
||||
@@ -0,0 +1,12 @@
|
||||
local Ex04_DynamicObject = UnLua.Class()
|
||||
function Ex04_DynamicObject.Initialize(self, Initializer)
|
||||
self.seed = Initializer and Initializer.Seed or 1
|
||||
end
|
||||
function Ex04_DynamicObject.NextValue(self)
|
||||
self.seed = self.seed * 2 + 1
|
||||
return self.seed
|
||||
end
|
||||
function Ex04_DynamicObject.GetSeed(self)
|
||||
return self.seed
|
||||
end
|
||||
return Ex04_DynamicObject
|
||||
@@ -0,0 +1,20 @@
|
||||
local Ex04_DynamicSpawnedActor = UnLua.Class()
|
||||
function Ex04_DynamicSpawnedActor.Initialize(self, Initializer)
|
||||
self.initTag = Initializer and Initializer.InitTag or "Default"
|
||||
self.initPower = Initializer and Initializer.InitPower or 0
|
||||
self.beginPlayCalled = false
|
||||
end
|
||||
function Ex04_DynamicSpawnedActor.ReceiveBeginPlay(self)
|
||||
self.Overridden.ReceiveBeginPlay(self)
|
||||
self.beginPlayCalled = true
|
||||
end
|
||||
function Ex04_DynamicSpawnedActor.GetInitTag(self)
|
||||
return self.initTag
|
||||
end
|
||||
function Ex04_DynamicSpawnedActor.GetInitPower(self)
|
||||
return self.initPower
|
||||
end
|
||||
function Ex04_DynamicSpawnedActor.IsBeginPlayCalled(self)
|
||||
return self.beginPlayCalled
|
||||
end
|
||||
return Ex04_DynamicSpawnedActor
|
||||
@@ -0,0 +1,73 @@
|
||||
local Ex05_BindInputs = UnLua.Class()
|
||||
function Ex05_BindInputs.ReceiveBeginPlay(self)
|
||||
self.Overridden.ReceiveBeginPlay(self)
|
||||
self.lastAction = ""
|
||||
self.moveForwardValue = 0
|
||||
self.enhancedCount = 0
|
||||
end
|
||||
function Ex05_BindInputs.HandleSpacePressed(self)
|
||||
self.lastAction = "SpaceBar:Pressed"
|
||||
end
|
||||
function Ex05_BindInputs.HandleCtrlSave(self)
|
||||
self.lastAction = "Ctrl+S"
|
||||
end
|
||||
function Ex05_BindInputs.HandleJumpAction(self)
|
||||
self.lastAction = "Action:Jump"
|
||||
end
|
||||
function Ex05_BindInputs.HandleMoveForward(self, axisValue)
|
||||
self.moveForwardValue = axisValue
|
||||
end
|
||||
function Ex05_BindInputs.HandleEnhancedMove(self, actionValue)
|
||||
self.enhancedCount = self.enhancedCount + 1
|
||||
self.moveForwardValue = actionValue
|
||||
end
|
||||
function Ex05_BindInputs.GetLastAction(self)
|
||||
return self.lastAction
|
||||
end
|
||||
function Ex05_BindInputs.GetMoveForwardValue(self)
|
||||
return self.moveForwardValue
|
||||
end
|
||||
function Ex05_BindInputs.GetEnhancedCount(self)
|
||||
return self.enhancedCount
|
||||
end
|
||||
UnLua.Input.BindKey(
|
||||
Ex05_BindInputs,
|
||||
"SpaceBar",
|
||||
"Pressed",
|
||||
function(self)
|
||||
self:HandleSpacePressed()
|
||||
end
|
||||
)
|
||||
UnLua.Input.BindKey(
|
||||
Ex05_BindInputs,
|
||||
"S",
|
||||
"Pressed",
|
||||
function(self)
|
||||
self:HandleCtrlSave()
|
||||
end,
|
||||
{Ctrl = true}
|
||||
)
|
||||
UnLua.Input.BindAction(
|
||||
Ex05_BindInputs,
|
||||
"Jump",
|
||||
"Pressed",
|
||||
function(self)
|
||||
self:HandleJumpAction()
|
||||
end
|
||||
)
|
||||
UnLua.Input.BindAxis(
|
||||
Ex05_BindInputs,
|
||||
"MoveForward",
|
||||
function(self, axisValue)
|
||||
self:HandleMoveForward(axisValue)
|
||||
end
|
||||
)
|
||||
UnLua.EnhancedInput.BindAction(
|
||||
Ex05_BindInputs,
|
||||
"/Game/Input/IA_Move.IA_Move",
|
||||
"Triggered",
|
||||
function(self, actionValue)
|
||||
self:HandleEnhancedMove(actionValue)
|
||||
end
|
||||
)
|
||||
return Ex05_BindInputs
|
||||
@@ -0,0 +1,201 @@
|
||||
local ____exports = {}
|
||||
local ____TestRunner = require("Tests.Harness.TestRunner")
|
||||
local AssertEqual = ____TestRunner.AssertEqual
|
||||
local AssertTrue = ____TestRunner.AssertTrue
|
||||
local AssertFalse = ____TestRunner.AssertFalse
|
||||
local AssertNil = ____TestRunner.AssertNil
|
||||
local AssertArrayEqual = ____TestRunner.AssertArrayEqual
|
||||
--- 参考 UnLua 官方示例 06_NativeContainers:
|
||||
-- 演示 UE.TArray / UE.TMap / UE.TSet 的构造(使用 UE.int32 / UE.FString Token)、
|
||||
-- 1-based 索引访问、CRUD、ToTable 与原生 pairs() 遍历。
|
||||
function ____exports.VerifyTArrayOperations()
|
||||
local arr = UE.TArray(UE.int32)
|
||||
AssertTrue(arr:Reserve(8))
|
||||
AssertEqual(
|
||||
arr:Length(),
|
||||
0
|
||||
)
|
||||
AssertEqual(
|
||||
arr:Add(10),
|
||||
1
|
||||
)
|
||||
AssertEqual(
|
||||
arr:Add(20),
|
||||
2
|
||||
)
|
||||
AssertEqual(
|
||||
arr:Add(30),
|
||||
3
|
||||
)
|
||||
AssertEqual(
|
||||
arr:Length(),
|
||||
3
|
||||
)
|
||||
AssertEqual(
|
||||
arr:Num(),
|
||||
3
|
||||
)
|
||||
AssertEqual(
|
||||
arr:Get(1),
|
||||
10
|
||||
)
|
||||
AssertEqual(
|
||||
arr:Get(3),
|
||||
30
|
||||
)
|
||||
AssertEqual(
|
||||
arr:LastIndex(),
|
||||
3
|
||||
)
|
||||
AssertTrue(arr:IsValidIndex(1))
|
||||
AssertFalse(arr:IsValidIndex(0))
|
||||
AssertFalse(arr:IsValidIndex(4))
|
||||
arr:Set(2, 25)
|
||||
AssertEqual(
|
||||
arr:Get(2),
|
||||
25
|
||||
)
|
||||
AssertEqual(
|
||||
arr:Find(25),
|
||||
2
|
||||
)
|
||||
AssertTrue(arr:Contains(25))
|
||||
AssertFalse(arr:Contains(999))
|
||||
AssertEqual(
|
||||
arr:AddUnique(25),
|
||||
0
|
||||
)
|
||||
AssertEqual(
|
||||
arr:AddUnique(40),
|
||||
4
|
||||
)
|
||||
AssertEqual(
|
||||
arr:Length(),
|
||||
4
|
||||
)
|
||||
arr:Insert(15, 2)
|
||||
AssertEqual(
|
||||
arr:Get(2),
|
||||
15
|
||||
)
|
||||
arr:Swap(1, 5)
|
||||
AssertEqual(
|
||||
arr:Get(1),
|
||||
40
|
||||
)
|
||||
AssertEqual(
|
||||
arr:Get(5),
|
||||
10
|
||||
)
|
||||
arr:Add(15)
|
||||
local removedCount = arr:RemoveItem(15)
|
||||
AssertEqual(removedCount, 2)
|
||||
AssertFalse(arr:Contains(15))
|
||||
local other = UE.TArray(UE.int32)
|
||||
other:Add(100)
|
||||
other:Add(200)
|
||||
arr:Append(other)
|
||||
local tbl = arr:ToTable()
|
||||
AssertArrayEqual(tbl, {
|
||||
40,
|
||||
25,
|
||||
30,
|
||||
10,
|
||||
100,
|
||||
200
|
||||
})
|
||||
local sum = 0
|
||||
for idx, val in pairs(arr) do
|
||||
AssertTrue(idx >= 1)
|
||||
sum = sum + val
|
||||
end
|
||||
AssertEqual(sum, 405)
|
||||
arr:Clear()
|
||||
AssertEqual(
|
||||
arr:Length(),
|
||||
0
|
||||
)
|
||||
end
|
||||
function ____exports.VerifyTMapOperations()
|
||||
local map = UE.TMap(UE.FString, UE.int32)
|
||||
map:Add("HP", 100)
|
||||
map:Add("MP", 50)
|
||||
map:Add("ATK", 35)
|
||||
AssertEqual(
|
||||
map:Length(),
|
||||
3
|
||||
)
|
||||
AssertEqual(
|
||||
map:Find("HP"),
|
||||
100
|
||||
)
|
||||
AssertEqual(
|
||||
map:FindRef("MP"),
|
||||
50
|
||||
)
|
||||
AssertNil(map:Find("DEF"))
|
||||
map:Add("HP", 120)
|
||||
AssertEqual(
|
||||
map:Find("HP"),
|
||||
120
|
||||
)
|
||||
AssertEqual(
|
||||
map:Keys():Length(),
|
||||
3
|
||||
)
|
||||
AssertEqual(
|
||||
map:Values():Length(),
|
||||
3
|
||||
)
|
||||
local tbl = map:ToTable()
|
||||
AssertEqual(tbl.ATK, 35)
|
||||
local sum = 0
|
||||
for _k, v in pairs(map) do
|
||||
sum = sum + v
|
||||
end
|
||||
AssertEqual(sum, 205)
|
||||
AssertTrue(map:Remove("MP"))
|
||||
AssertFalse(map:Remove("NotExist"))
|
||||
AssertEqual(
|
||||
map:Length(),
|
||||
2
|
||||
)
|
||||
map:Clear()
|
||||
AssertEqual(
|
||||
map:Length(),
|
||||
0
|
||||
)
|
||||
end
|
||||
function ____exports.VerifyTSetOperations()
|
||||
local set = UE.TSet(UE.FString)
|
||||
set:Add("Fire")
|
||||
set:Add("Ice")
|
||||
set:Add("Fire")
|
||||
AssertEqual(
|
||||
set:Length(),
|
||||
2
|
||||
)
|
||||
AssertTrue(set:Contains("Fire"))
|
||||
AssertFalse(set:Contains("Poison"))
|
||||
local asArr = set:ToArray()
|
||||
AssertEqual(
|
||||
asArr:Length(),
|
||||
2
|
||||
)
|
||||
AssertEqual(
|
||||
#set:ToTable(),
|
||||
2
|
||||
)
|
||||
AssertTrue(set:Remove("Ice"))
|
||||
AssertFalse(set:Remove("Ice"))
|
||||
AssertEqual(
|
||||
set:Length(),
|
||||
1
|
||||
)
|
||||
set:Clear()
|
||||
AssertEqual(
|
||||
set:Length(),
|
||||
0
|
||||
)
|
||||
end
|
||||
return ____exports
|
||||
@@ -0,0 +1,37 @@
|
||||
local Ex07_CallLatentFunction = UnLua.Class()
|
||||
function Ex07_CallLatentFunction.ReceiveBeginPlay(self)
|
||||
self.Overridden.ReceiveBeginPlay(self)
|
||||
self.steps = {}
|
||||
self.completed = false
|
||||
local co = coroutine.create(function(actor)
|
||||
actor:RunSequence()
|
||||
end)
|
||||
local ok, err = coroutine.resume(co, self)
|
||||
if not ok then
|
||||
UnLua.LogError(
|
||||
"Ex07 coroutine failed:",
|
||||
tostring(err)
|
||||
)
|
||||
end
|
||||
end
|
||||
function Ex07_CallLatentFunction.RunSequence(self)
|
||||
self.steps[#self.steps + 1] = "Step1:Start"
|
||||
UE.UKismetSystemLibrary.Delay(self, 0.5)
|
||||
if not UE.UKismetSystemLibrary.IsValid(self) then
|
||||
return
|
||||
end
|
||||
self.steps[#self.steps + 1] = "Step2:After0.5s"
|
||||
UE.UKismetSystemLibrary.Delay(self, 1)
|
||||
if not UE.UKismetSystemLibrary.IsValid(self) then
|
||||
return
|
||||
end
|
||||
self.steps[#self.steps + 1] = "Step3:Done"
|
||||
self.completed = true
|
||||
end
|
||||
function Ex07_CallLatentFunction.GetSteps(self)
|
||||
return self.steps
|
||||
end
|
||||
function Ex07_CallLatentFunction.IsCompleted(self)
|
||||
return self.completed
|
||||
end
|
||||
return Ex07_CallLatentFunction
|
||||
@@ -0,0 +1,43 @@
|
||||
local Ex08_StructAndTrace = UnLua.Class()
|
||||
function Ex08_StructAndTrace.ReceiveBeginPlay(self)
|
||||
self.Overridden.ReceiveBeginPlay(self)
|
||||
self._tmpStart = UE.FVector(0, 0, 0)
|
||||
self._tmpEnd = UE.FVector(0, 0, 0)
|
||||
self._tmpHit = UE.FHitResult()
|
||||
self._ignoreActors = UE.TArray(UE.AActor.StaticClass())
|
||||
self._ignoreActors:Add(self)
|
||||
end
|
||||
function Ex08_StructAndTrace.ComputeVectorMath(self)
|
||||
local a = UE.FVector(3, 4, 0)
|
||||
local b = UE.FVector(0, 0, 12)
|
||||
local c = a:Add(b)
|
||||
local dot = a:Dot(b)
|
||||
local dist = UE.FVector.Dist(
|
||||
UE.FVector(0, 0, 0),
|
||||
c
|
||||
)
|
||||
return c:Size(), dot, dist
|
||||
end
|
||||
function Ex08_StructAndTrace.TraceForward(self, distance)
|
||||
local origin = self:K2_GetActorLocation()
|
||||
self._tmpStart:Set(origin.X, origin.Y, origin.Z)
|
||||
self._tmpEnd:Set(origin.X + distance, origin.Y, origin.Z)
|
||||
local bHit, hit = UE.UKismetSystemLibrary.LineTraceSingle(
|
||||
self,
|
||||
self._tmpStart,
|
||||
self._tmpEnd,
|
||||
UE.ETraceTypeQuery.TraceTypeQuery1,
|
||||
false,
|
||||
self._ignoreActors,
|
||||
UE.EDrawDebugTrace.None,
|
||||
self._tmpHit,
|
||||
true
|
||||
)
|
||||
return bHit, hit.Distance, hit.ImpactPoint
|
||||
end
|
||||
function Ex08_StructAndTrace.TeleportTo(self, x, y, z)
|
||||
local target = UE.FVector(x, y, z)
|
||||
local bMoved = self:K2_SetActorLocation(target, false, self._tmpHit, true)
|
||||
return bMoved
|
||||
end
|
||||
return Ex08_StructAndTrace
|
||||
@@ -0,0 +1,44 @@
|
||||
local Ex09_AssetRefAndUMG = UnLua.Class()
|
||||
function Ex09_AssetRefAndUMG.Initialize(self, _Initializer)
|
||||
self.isConstructed = false
|
||||
self.isDestructed = false
|
||||
self.weakRef = UE.FWeakObjectPtr()
|
||||
end
|
||||
function Ex09_AssetRefAndUMG.Construct(self)
|
||||
self.isConstructed = true
|
||||
self.loadedAsset = UE.UObject.Load("/Game/UI/Textures/T_Icon.T_Icon")
|
||||
if self.loadedAsset ~= nil then
|
||||
self.refProxy = UnLua.Ref(self.loadedAsset)
|
||||
self.weakRef:Set(self.loadedAsset)
|
||||
end
|
||||
end
|
||||
function Ex09_AssetRefAndUMG.ShowOnScreen(self)
|
||||
self:AddToViewport(10)
|
||||
end
|
||||
function Ex09_AssetRefAndUMG.CloseAndRelease(self)
|
||||
self:RemoveFromParent()
|
||||
self:Destruct()
|
||||
self:Release()
|
||||
end
|
||||
function Ex09_AssetRefAndUMG.Destruct(self)
|
||||
if self.isDestructed then
|
||||
return
|
||||
end
|
||||
self.isDestructed = true
|
||||
if self.loadedAsset ~= nil then
|
||||
UnLua.Unref(self.loadedAsset)
|
||||
self.refProxy = nil
|
||||
self.loadedAsset = nil
|
||||
end
|
||||
self.weakRef:Reset()
|
||||
end
|
||||
function Ex09_AssetRefAndUMG.HasValidWeakAsset(self)
|
||||
return self.weakRef:IsValid()
|
||||
end
|
||||
function Ex09_AssetRefAndUMG.IsConstructed(self)
|
||||
return self.isConstructed
|
||||
end
|
||||
function Ex09_AssetRefAndUMG.IsDestructed(self)
|
||||
return self.isDestructed
|
||||
end
|
||||
return Ex09_AssetRefAndUMG
|
||||
@@ -0,0 +1,315 @@
|
||||
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 AssertNotNil = ____TestRunner.AssertNotNil
|
||||
local AssertArrayEqual = ____TestRunner.AssertArrayEqual
|
||||
local ____MockUnLuaRuntime = require("Tests.Harness.MockUnLuaRuntime")
|
||||
local AdvanceMockTime = ____MockUnLuaRuntime.AdvanceMockTime
|
||||
local CreateBoundInstance = ____MockUnLuaRuntime.CreateBoundInstance
|
||||
local CreateMockMulticastDelegate = ____MockUnLuaRuntime.CreateMockMulticastDelegate
|
||||
local CreateMockScriptDelegate = ____MockUnLuaRuntime.CreateMockScriptDelegate
|
||||
local GetMockState = ____MockUnLuaRuntime.GetMockState
|
||||
local ResetMockState = ____MockUnLuaRuntime.ResetMockState
|
||||
local ____Ex06_NativeContainers = require("Tests.UnLuaExamples.Ex06_NativeContainers")
|
||||
local VerifyTArrayOperations = ____Ex06_NativeContainers.VerifyTArrayOperations
|
||||
local VerifyTMapOperations = ____Ex06_NativeContainers.VerifyTMapOperations
|
||||
local VerifyTSetOperations = ____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
|
||||
return ____exports
|
||||
Reference in new issue
Block a user