Files
dl/project/dl_unit_test/test_main.cpp
T
2026-09-16 14:07:40 +08:00

119 lines
3.5 KiB
C++

/**
* @file test_main.cpp
* @brief dl 单元测试入口(Main 函数,由 dl_main 调用)
*
*
* @version 1.0
* @author CodeBuddy
* @date 26-09-03
*
* @note 复用 dl_main 的 main 入口,这里提供 Main() 跑 GoogleTest
*/
#include <gtest/gtest.h>
#include "system/dl_engine.h"
#include "system/dl_system.h"
#include "io/dl_log.h"
#include "io/dl_file.h"
#include "render/dl_factory.h"
//! 用 dl 日志打印 gtest 结果(dl 日志会同时写入日志文件,控制台滚动后仍可查完整结果)
class DlLogListener : public ::testing::EmptyTestEventListener
{
//! 用 dl::Output 直接输出(纯字符串,同时写入 stdout/VS/日志文件)
static void Print(std::string_view str)
{
dl::Output(str);
dl::Output("\n");
}
static std::string FileName(const char* path)
{
return dl::File::GetFileName(path);
}
public:
void OnTestProgramStart(const ::testing::UnitTest& unit_test) override
{
Print(fmt::format("[==========] 运行 {} 个测试", unit_test.test_to_run_count()));
}
void OnTestStart(const ::testing::TestInfo& test_info) override
{
Print(fmt::format("[ RUN ] {}.{} ({}:{})",
test_info.test_suite_name(), test_info.name(),
FileName(test_info.file()), test_info.line()));
}
void OnTestPartResult(const ::testing::TestPartResult& result) override
{
if (result.failed())
Print(fmt::format("{}:{}: {}", result.file_name(), result.line_number(), result.message()));
}
void OnTestEnd(const ::testing::TestInfo& test_info) override
{
if (test_info.result()->Passed())
Print(fmt::format("[ OK ] {}.{}", test_info.test_suite_name(), test_info.name()));
else
Print(fmt::format("[ FAILED ] {}.{} ({}:{})",
test_info.test_suite_name(), test_info.name(),
FileName(test_info.file()), test_info.line()));
}
void OnTestProgramEnd(const ::testing::UnitTest& unit_test) override
{
Print(fmt::format("[==========] {} 通过,{} 失败,{} 跳过",
unit_test.successful_test_count(),
unit_test.failed_test_count(),
unit_test.skipped_test_count()));
}
};
int Main()
{
// 测试结果写入 dl 日志文件
dl::g_log->SetLogFile("log/", "unit_test.txt");
// 按正常使用方式初始化引擎(log_err/log_warn 依赖的 g_log 等全局对象在此初始化)
dl::Engine::GetParamRef()._test = false; // 关闭引擎内置测试
dl::Engine::GetParamRef()._showInfo = false; // 关闭引擎信息输出
dl::Engine::Init();
// 初始化 g_factory 路径(正常在 System::Start 时调用 _init_path,测试环境手动设置)
dl::g_factory->SetPath();
dl::File::CreateFolder(dl::g_factory->GetPathTemp());
// 关闭 err 日志的 MessageBox 弹窗(单元测试触发 log_err 不应中断等待点击)
dl::g_log->SetLevelMsgBox(dl::LogSystem::Level::DAMAGE);
// gtest 命令行参数(dl_main_init 已通过 Engine::_init_cmd 保存)
const auto& cmd = dl::Engine::GetCmd();
std::vector<char*> argv;
if (cmd.empty())
{
static std::string exe = "dl_unit_test";
argv.push_back(const_cast<char*>(exe.c_str()));
}
else
{
for (auto& c : cmd)
argv.push_back(const_cast<char*>(c.c_str()));
}
int argc = (int)argv.size();
::testing::InitGoogleTest(&argc, argv.data());
// 用 dl 日志打印测试结果(替换 gtest 默认输出,避免控制台刷屏)
auto& listeners = ::testing::UnitTest::GetInstance()->listeners();
delete listeners.Release(listeners.default_result_printer());
listeners.Append(new DlLogListener);
int ret = RUN_ALL_TESTS();
// 测试完成后暂停,方便观察结果
dl::g_system->Pause();
dl::Engine::Release();
return ret;
}