173 lines
4.6 KiB
C++
173 lines
4.6 KiB
C++
// 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 = __MODULE__.Import('Tests.UE.UERuntimeSuite')\n"
|
|
"local runner = __MODULE__.Import('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;
|
|
}
|