feat(test): add real UE 5.8 RunLuaTests commandlet, Lua.RunTests command, and fix UnLua .d.ts math/UObject bugs

This commit is contained in:
szy73711 committed 2026-11-29 15:05:35 +08:00
1 parent 3dd61c32a9
commit 96e3f0d009
29 files changed
+1417 -134

No files matched your search

+44
View File
@@ -2,7 +2,12 @@
#include "UnrealLuaDemo.h"
#include "HotUpdate/LuaHotUpdateSubsystem.h"
#include "Engine/Engine.h"
#include "Engine/World.h"
#include "HAL/IConsoleManager.h"
#include "UnLua.h"
#include "LuaEnv.h"
#include "lua.hpp"
DEFINE_LOG_CATEGORY(LogUnrealLuaDemo);
@@ -34,12 +39,51 @@ void FUnrealLuaDemoModule::StartupModule()
}),
ECVF_Default);
RunTestsCmd = IConsoleManager::Get().RegisterConsoleCommand(
TEXT("Lua.RunTests"),
TEXT("Run compiled TypeScript -> UnLua runtime test suite in the current active game/PIE UWorld."),
FConsoleCommandWithWorldDelegate::CreateLambda([](UWorld* World)
{
TMap<lua_State*, UnLua::FLuaEnv*>& AllEnvs = UnLua::FLuaEnv::GetAll();
if (AllEnvs.Num() == 0)
{
UE_LOG(LogUnrealLuaDemo, Warning, TEXT("Lua.RunTests: No active UnLua environment (start PIE first)."));
return;
}
for (const TPair<lua_State*, UnLua::FLuaEnv*>& Pair : AllEnvs)
{
if (UnLua::FLuaEnv* Env = Pair.Value)
{
lua_State* L = Env->GetMainState();
if (L && World)
{
UnLua::PushUObject(L, World);
lua_setglobal(L, "__UE_TEST_WORLD");
}
Env->DoString(TEXT("require('Tests.UE.UERuntimeSuite').RunAllInUE(_G.__UE_TEST_WORLD)"), TEXT("Lua.RunTests"));
if (L)
{
lua_pushnil(L);
lua_setglobal(L, "__UE_TEST_WORLD");
}
break;
}
}
}),
ECVF_Default);
UE_LOG(LogUnrealLuaDemo, Log, TEXT("UnrealLuaDemo module started. Hot-update script dir: %s"),
*ULuaHotUpdateSubsystem::GetHotUpdateScriptDir());
}
void FUnrealLuaDemoModule::ShutdownModule()
{
if (RunTestsCmd)
{
IConsoleManager::Get().UnregisterConsoleObject(RunTestsCmd);
RunTestsCmd = nullptr;
}
if (HotReloadCmd)
{
IConsoleManager::Get().UnregisterConsoleObject(HotReloadCmd);
+1
View File
@@ -16,4 +16,5 @@ public:
private:
IConsoleObject* HotReloadCmd = nullptr;
IConsoleObject* ReloadModuleCmd = nullptr;
IConsoleObject* RunTestsCmd = nullptr;
};
@@ -0,0 +1,172 @@
// Copyright Epic Games, Inc. All Rights Reserved.
#include "RunLuaTestsCommandlet.h"
#include "UnrealLuaDemoEditor.h"
#include "Engine/Engine.h"
#include "Engine/World.h"
#include "GameFramework/WorldSettings.h"
#include "UnLua.h"
#include "UnLuaModule.h"
#include "LuaEnv.h"
#include "lua.hpp"
namespace
{
UWorld* GCurrentLuaTestWorld = nullptr;
int LuaTestWorldTick(lua_State* L)
{
const float DeltaSeconds = lua_gettop(L) >= 1 ? static_cast<float>(luaL_checknumber(L, 1)) : 0.016f;
if (GCurrentLuaTestWorld && IsValid(GCurrentLuaTestWorld))
{
// Advance GFrameCounter so FTimerManager::HasBeenTickedThisFrame() treats each call as a new engine frame.
++GFrameCounter;
GCurrentLuaTestWorld->Tick(LEVELTICK_All, DeltaSeconds);
}
return 0;
}
}
URunLuaTestsCommandlet::URunLuaTestsCommandlet()
{
IsClient = true;
IsEditor = true;
IsServer = false;
LogToConsole = true;
ShowErrorCount = true;
}
int32 URunLuaTestsCommandlet::Main(const FString& Params)
{
UE_LOG(LogUnrealLuaDemoEditor, Display, TEXT("[RunLuaTestsCommandlet] Starting real UE + UnLua test runner..."));
IUnLuaModule& UnLuaModule = IUnLuaModule::Get();
const bool bWasActive = UnLuaModule.IsActive();
if (!bWasActive)
{
UnLuaModule.SetActive(true);
}
UWorld* TestWorld = UWorld::CreateWorld(EWorldType::Game, false, TEXT("LuaTestWorld"));
if (!TestWorld)
{
UE_LOG(LogUnrealLuaDemoEditor, Error, TEXT("[RunLuaTestsCommandlet] Failed to create UWorld."));
if (!bWasActive)
{
UnLuaModule.SetActive(false);
}
return 1;
}
FWorldContext& WorldContext = GEngine->CreateNewWorldContext(EWorldType::Game);
WorldContext.SetCurrentWorld(TestWorld);
TestWorld->InitializeActorsForPlay(FURL());
if (AWorldSettings* WorldSettings = TestWorld->GetWorldSettings())
{
WorldSettings->NotifyBeginPlay();
WorldSettings->NotifyMatchStarted();
}
TestWorld->BeginPlay();
GCurrentLuaTestWorld = TestWorld;
int32 ExitCode = 1;
UnLua::FLuaEnv* Env = UnLuaModule.GetEnv(TestWorld);
if (!Env)
{
UE_LOG(LogUnrealLuaDemoEditor, Error, TEXT("[RunLuaTestsCommandlet] Failed to obtain UnLua FLuaEnv."));
}
else
{
lua_State* L = Env->GetMainState();
if (L)
{
UnLua::PushUObject(L, TestWorld);
lua_setglobal(L, "__UE_TEST_WORLD");
lua_pushcfunction(L, &LuaTestWorldTick);
lua_setglobal(L, "__UE_TEST_TICK");
const FString RunChunk = TEXT(
"local suite = require('Tests.UE.UERuntimeSuite')\n"
"local runner = require('Tests.Harness.TestRunner')\n"
"local summary = suite.RunAllInUE(_G.__UE_TEST_WORLD)\n"
"_G.__UE_TEST_TOTAL = summary.total or 0\n"
"_G.__UE_TEST_PASSED = summary.passed or 0\n"
"_G.__UE_TEST_FAILED = summary.failed or 0\n"
"_G.__UE_TEST_REPORT = runner.FormatReport(summary)\n"
);
const bool bExecOk = Env->DoString(RunChunk, TEXT("RunLuaTestsCommandlet"));
lua_getglobal(L, "__UE_TEST_REPORT");
const char* ReportUtf8 = lua_tostring(L, -1);
const FString ReportText = ReportUtf8 ? UTF8_TO_TCHAR(ReportUtf8) : FString();
lua_pop(L, 1);
if (!ReportText.IsEmpty())
{
TArray<FString> Lines;
ReportText.ParseIntoArrayLines(Lines, false);
for (const FString& Line : Lines)
{
UE_LOG(LogUnrealLuaDemoEditor, Display, TEXT("%s"), *Line);
}
}
lua_getglobal(L, "__UE_TEST_TOTAL");
const int32 Total = static_cast<int32>(lua_tointeger(L, -1));
lua_pop(L, 1);
lua_getglobal(L, "__UE_TEST_PASSED");
const int32 Passed = static_cast<int32>(lua_tointeger(L, -1));
lua_pop(L, 1);
lua_getglobal(L, "__UE_TEST_FAILED");
const int32 Failed = static_cast<int32>(lua_tointeger(L, -1));
lua_pop(L, 1);
// Clear test globals before teardown
lua_pushnil(L);
lua_setglobal(L, "__UE_TEST_WORLD");
lua_pushnil(L);
lua_setglobal(L, "__UE_TEST_TICK");
lua_pushnil(L);
lua_setglobal(L, "__UE_TEST_REPORT");
if (!bExecOk)
{
UE_LOG(LogUnrealLuaDemoEditor, Error, TEXT("[RunLuaTestsCommandlet] Lua execution error while running Tests.UE.UERuntimeSuite."));
ExitCode = 1;
}
else if (Total <= 0 || Failed > 0)
{
UE_LOG(LogUnrealLuaDemoEditor, Error,
TEXT("[RunLuaTestsCommandlet] UE Lua tests FAILED (Total=%d, Passed=%d, Failed=%d)."),
Total, Passed, Failed);
ExitCode = 1;
}
else
{
UE_LOG(LogUnrealLuaDemoEditor, Display,
TEXT("[RunLuaTestsCommandlet] All UE Lua tests PASSED (Total=%d, Passed=%d, Failed=%d)."),
Total, Passed, Failed);
ExitCode = 0;
}
}
}
GCurrentLuaTestWorld = nullptr;
TestWorld->EndPlay(EEndPlayReason::Quit);
GEngine->DestroyWorldContext(TestWorld);
TestWorld->DestroyWorld(false);
if (!bWasActive)
{
UnLuaModule.SetActive(false);
}
return ExitCode;
}
@@ -0,0 +1,26 @@
// Copyright Epic Games, Inc. All Rights Reserved.
#pragma once
#include "CoreMinimal.h"
#include "Commandlets/Commandlet.h"
#include "RunLuaTestsCommandlet.generated.h"
/**
* Headless commandlet that boots a real UWorld + UnLua 2.3.6 Lua 5.4 runtime
* and executes the compiled TypeScript -> UnLua test suite (`Tests.UE.UERuntimeSuite`).
*
* Usage:
* UnrealEditor-Cmd <Project.uproject> -run=RunLuaTests -unattended -nop4 -nosplash -NullRHI
* (Optional: pass -UseLuaBundle to run tests directly from /Game/Generated/DA_LuaBundle)
*/
UCLASS()
class UNREALLUADEMOEDITOR_API URunLuaTestsCommandlet : public UCommandlet
{
GENERATED_BODY()
public:
URunLuaTestsCommandlet();
virtual int32 Main(const FString& Params) override;
};
@@ -26,7 +26,9 @@ public class UnrealLuaDemoEditor : ModuleRules
"Projects",
"Slate",
"SlateCore",
"ToolMenus"
"ToolMenus",
"UnLua",
"Lua"
});
}
}