Add tests for interface reference marshaling

#2746
This commit is contained in:
2026-09-14 07:00:55 +02:00
parent e5a6b73caa
commit c43cfef0a9
3 changed files with 59 additions and 3 deletions
+12 -3
View File
@@ -37,9 +37,7 @@ TEST_CASE("Scripting")
MMethod* method = klass->GetMethod("TestLibraryImports");
CHECK(method);
MObject* result = method->Invoke(nullptr, nullptr, nullptr);
CHECK(result);
int32 resultValue = MUtils::Unbox<int32>(result);
CHECK(resultValue == 0);
CHECK(MUtils::Unbox<int32>(result) == 0);
}
SECTION("Test Class")
@@ -167,4 +165,15 @@ TEST_CASE("Scripting")
CHECK(interfaceObject);
CHECK(interfaceObject == object);
}
SECTION("Test Interface Reference")
{
// Test native interface implementation
MClass* klass = Scripting::FindClass("FlaxEngine.Tests.TestScripting");
CHECK(klass);
MMethod* method = klass->GetMethod("TestInterfaceReference");
CHECK(method);
MObject* result = method->Invoke(nullptr, nullptr, nullptr);
CHECK(MUtils::Unbox<int32>(result) == 0);
}
}
+28
View File
@@ -2,6 +2,7 @@
#if FLAX_TESTS
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.InteropServices;
@@ -42,6 +43,33 @@ namespace FlaxEngine.Tests
NativeLibrary.Free(library);
return result;
}
/// <summary>
/// Tests <see cref="ScriptingObjectInterfaceReference{T}"/> usage with marshalling.
/// </summary>
public static int TestInterfaceReference()
{
var native = new TestClassNative();
native.InterfaceRef = native;
var returned = native.InterfaceRef;
if (returned != native)
return 1;
returned = native.TestPassInterface(native);
if (returned != native)
return 2;
returned = native.TestPassInterfaceArray(new ScriptingObjectInterfaceReference<ITestInterface>[1] { native })[0];
if (returned != native)
return 3;
var dic = new Dictionary<string, ScriptingObjectInterfaceReference<ITestInterface>>();
dic.Add("key", native);
returned = native.TestPassInterfaceDictionary(dic)["key"];
if (returned != native)
return 4;
var res = returned.Interface.TestInterfaceMethod("123");
if (res != 3)
return 5;
return 0;
}
}
}
+19
View File
@@ -5,6 +5,7 @@
#include "Engine/Core/ISerializable.h"
#include "Engine/Core/Math/Vector3.h"
#include "Engine/Core/Collections/Array.h"
#include "Engine/Core/Collections/Dictionary.h"
#include "Engine/Scripting/ScriptingObject.h"
#include "Engine/Scripting/ScriptingObjectInterfaceReference.h"
#include "Engine/Scripting/SerializableScriptingObject.h"
@@ -204,6 +205,24 @@ public:
// Test nameless arguments
API_FUNCTION() void TestNamelessArguments(int32, float, bool){}
// Test pass interface ref in function
API_FUNCTION() ScriptingObjectInterfaceReference<ITestInterface> TestPassInterface(ScriptingObjectInterfaceReference<ITestInterface> param1) const
{
return param1;
}
// Test pass interface ref array in function
API_FUNCTION() Array<ScriptingObjectInterfaceReference<ITestInterface>> TestPassInterfaceArray(Array<ScriptingObjectInterfaceReference<ITestInterface>> param1) const
{
return param1;
}
// Test pass interface ref dictionary in function
API_FUNCTION() Dictionary<String, ScriptingObjectInterfaceReference<ITestInterface>> TestPassInterfaceDictionary(Dictionary<String, ScriptingObjectInterfaceReference<ITestInterface>> param1) const
{
return param1;
}
int32 TestInterfaceMethod(const String& str) override
{
return str.Length();