Add unit test for scripting features
This commit is contained in:
40
Source/Engine/Tests/TestScripting.cpp
Normal file
40
Source/Engine/Tests/TestScripting.cpp
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
// Copyright (c) 2012-2022 Wojciech Figat. All rights reserved.
|
||||||
|
|
||||||
|
#include "TestScripting.h"
|
||||||
|
#include "Engine/Scripting/Scripting.h"
|
||||||
|
#include <ThirdParty/catch2/catch.hpp>
|
||||||
|
|
||||||
|
TestClassNative::TestClassNative(const SpawnParams& params)
|
||||||
|
: ScriptingObject(params)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Scripting")
|
||||||
|
{
|
||||||
|
SECTION("Test Class")
|
||||||
|
{
|
||||||
|
// Test native class
|
||||||
|
ScriptingTypeHandle type = Scripting::FindScriptingType("FlaxEngine.TestClassNative");
|
||||||
|
CHECK(type == TestClassNative::TypeInitializer);
|
||||||
|
ScriptingObject* object = Scripting::NewObject(type.GetType().ManagedClass);
|
||||||
|
CHECK(object);
|
||||||
|
CHECK(object->Is<TestClassNative>());
|
||||||
|
TestClassNative* testClass = (TestClassNative*)object;
|
||||||
|
CHECK(testClass->SimpleField == 1);
|
||||||
|
int32 methodResult = testClass->Test(TEXT("123"));
|
||||||
|
CHECK(methodResult == 3);
|
||||||
|
|
||||||
|
// Test managed class
|
||||||
|
type = Scripting::FindScriptingType("FlaxEngine.TestClassManaged");
|
||||||
|
CHECK(type);
|
||||||
|
object = Scripting::NewObject(type.GetType().ManagedClass);
|
||||||
|
CHECK(object);
|
||||||
|
CHECK(object->Is<TestClassNative>());
|
||||||
|
testClass = (TestClassNative*)object;
|
||||||
|
MObject* managed = testClass->GetOrCreateManagedInstance(); // Ensure to create C# object and run it's ctor
|
||||||
|
CHECK(managed);
|
||||||
|
CHECK(testClass->SimpleField == 2);
|
||||||
|
methodResult = testClass->Test(TEXT("123"));
|
||||||
|
CHECK(methodResult == 6);
|
||||||
|
}
|
||||||
|
}
|
||||||
23
Source/Engine/Tests/TestScripting.cs
Normal file
23
Source/Engine/Tests/TestScripting.cs
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
// Copyright (c) 2012-2022 Wojciech Figat. All rights reserved.
|
||||||
|
|
||||||
|
#if FLAX_TESTS
|
||||||
|
namespace FlaxEngine
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Test class.
|
||||||
|
/// </summary>
|
||||||
|
public class TestClassManaged : TestClassNative
|
||||||
|
{
|
||||||
|
TestClassManaged()
|
||||||
|
{
|
||||||
|
SimpleField = 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public override int Test(string str)
|
||||||
|
{
|
||||||
|
return str.Length + base.Test(str);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
23
Source/Engine/Tests/TestScripting.h
Normal file
23
Source/Engine/Tests/TestScripting.h
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
// Copyright (c) 2012-2022 Wojciech Figat. All rights reserved.
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "Engine/Core/ISerializable.h"
|
||||||
|
#include "Engine/Scripting/ScriptingObject.h"
|
||||||
|
|
||||||
|
// Test class.
|
||||||
|
API_CLASS() class TestClassNative : public ScriptingObject, public ISerializable
|
||||||
|
{
|
||||||
|
API_AUTO_SERIALIZATION();
|
||||||
|
DECLARE_SCRIPTING_TYPE(TestClassNative);
|
||||||
|
|
||||||
|
public:
|
||||||
|
// Test value
|
||||||
|
API_FIELD() int32 SimpleField = 1;
|
||||||
|
|
||||||
|
// Test virtual method
|
||||||
|
API_FUNCTION() virtual int32 Test(const String& str)
|
||||||
|
{
|
||||||
|
return str.Length();
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -45,6 +45,8 @@ public class FlaxTestsTarget : FlaxEditor
|
|||||||
{
|
{
|
||||||
base.SetupTargetEnvironment(options);
|
base.SetupTargetEnvironment(options);
|
||||||
|
|
||||||
|
options.ScriptingAPI.Defines.Add("FLAX_TESTS");
|
||||||
|
|
||||||
// Produce console program
|
// Produce console program
|
||||||
options.LinkEnv.LinkAsConsoleProgram = true;
|
options.LinkEnv.LinkAsConsoleProgram = true;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user