Skip to content

Commit

Permalink
Added tests for delegate inspection
Browse files Browse the repository at this point in the history
  • Loading branch information
thommcgrath committed Nov 12, 2023
1 parent f07b105 commit 504ac98
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
1 change: 1 addition & 0 deletions Project/Beacon.xojo_project
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,7 @@ Class=LootDropOverride;Modules/Game Support/ArkSA/LootDropOverride.xojo_code;&h0
Class=MutableLootDropOverride;Modules/Game Support/ArkSA/MutableLootDropOverride.xojo_code;&h00000000462EC7FF;&h000000004504AFFF;false
Interface=DisambiguationCandidate;Modules/Beacon/DisambiguationCandidate.xojo_code;&h0000000029F2FFFF;&h0000000055ADDFFF;false
Class=MutableLootDropOverride;Modules/Game Support/Ark/MutableLootDropOverride.xojo_code;&h00000000035FA7FF;&h0000000016B997FF;false
Class=DelegateClass;Modules/Tests/DelegateClass.xojo_code;&h000000005F3617FF;&h00000000656D4FFF;false
AppMenuBar=MainMenuBar
MajorVersion=2
MinorVersion=0
Expand Down
3 changes: 2 additions & 1 deletion Project/Modules/Beacon.xojo_code
Original file line number Diff line number Diff line change
Expand Up @@ -1302,7 +1302,8 @@ Protected Module Beacon

#tag Method, Flags = &h1
Protected Function SafeToInvoke(Callback As Variant) As Boolean
Return Callback.IsNull = False And (GetDelegateWeakMBS(Callback) = False Or (GetDelegateTargetMBS(Callback) Is Nil) = False)
// Module methods will have a Nil target, but can never be weak. Non-weak methods are always safe to invoke.
Return Callback.IsNull = False And (GetDelegateWeakMBS(Callback) = False Or GetDelegateTargetMBS(Callback).IsNull = False)
End Function
#tag EndMethod

Expand Down
37 changes: 37 additions & 0 deletions Project/Modules/Tests.xojo_code
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,22 @@ Protected Module Tests
TestCachingTimes()
TestXmlParsing()
TestSaveInfo()
TestDelegateDetection()
App.Log("Tests complete")
#endif
End Sub
#tag EndMethod

#tag DelegateDeclaration, Flags = &h21
Private Delegate Sub SampleDelegate()
#tag EndDelegateDeclaration

#tag Method, Flags = &h21
Private Sub SampleDelegateTarget()

End Sub
#tag EndMethod

#tag Method, Flags = &h21
Private Sub TestArkClassStrings()
Const NormalPath = "/Game/Aberration/CoreBlueprints/AB_DinoSpawnEntriesRockDrake.AB_DinoSpawnEntriesRockDrake_C"
Expand Down Expand Up @@ -167,6 +178,32 @@ Protected Module Tests
End Sub
#tag EndMethod

#tag Method, Flags = &h21
Private Sub TestDelegateDetection()
Var ModuleDelegate As SampleDelegate = AddressOf SampleDelegateTarget
Var ModuleTarget As Variant = GetDelegateTargetMBS(ModuleDelegate)
Var ModuleWeak As Boolean = GetDelegateWeakMBS(ModuleDelegate)

Var ClassInstance As New DelegateClass
Var ClassDelegate As SampleDelegate = WeakAddressOf ClassInstance.TriggerMethod
Var ClassTarget As Variant = GetDelegateTargetMBS(ClassDelegate)
Var ClassWeak As Boolean = GetDelegateWeakMBS(ClassDelegate)

ClassInstance = Nil
Var UnsafeClassTarget As Variant = GetDelegateTargetMBS(ClassDelegate)
Var UnsafeClassWeak As Boolean = GetDelegateWeakMBS(ClassDelegate)

Call Assert(ModuleTarget.IsNull = True, "Module method delegate has a target when it should not.")
Call Assert(ModuleWeak = False, "Module method delegate is weak but that should not be possible.")

Call Assert(ClassTarget.IsNull = False, "Instance method delegate does not have a target.")
Call Assert(ClassWeak = True, "Instance method delegate is not weak.")

Call Assert(UnsafeClassTarget.IsNull = True, "Unsafe instance method delegate has a target.")
Call Assert(UnsafeClassWeak = True, "Unsafe instance method delegate is not weak.")
End Sub
#tag EndMethod

#tag Method, Flags = &h21
Private Sub TestEncryption()
Var PublicKey, PrivateKey As String
Expand Down
11 changes: 11 additions & 0 deletions Project/Modules/Tests/DelegateClass.xojo_code
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#tag Class
Private Class DelegateClass
#tag Method, Flags = &h0
Sub TriggerMethod()

End Sub
#tag EndMethod


End Class
#tag EndClass

0 comments on commit 504ac98

Please sign in to comment.