Files
msime-windows/ui/tests/includes/test_framework.h
T
Evan Hu b8ac8e0b00 test: cover the layout core with unit tests
本仓库此前没有任何测试,CI 只能证明它编译得过。布局系统是这个框架里被所有控件依赖、也最容易在重构中被改坏的部分,而且是纯几何计算,不需要 D2D 设备即可测。

新增 13 个用例,覆盖 Visual 的测量与排列语义(margin 收缩可用尺寸并回填外框、显式尺寸覆盖测量值、min/max 夹取、可用空间封顶、测量缓存的命中与失效、Stretch/Center/Trailing 对齐定位),以及 StackPanel 与 HorizontalStackPanel 的堆叠、间距和 padding 行为。

断言值全部按 src/Layout.cpp 的现有实现推导,用于锁定当前行为,不是按我认为应该如何来写。测试框架沿用 MSIME-Server 的极简写法,但去掉了 fmt 依赖,本仓库不引入任何新依赖。

Grid、WrapPanel 和 ScrollViewer 本次未覆盖,留作后续。
2026-09-05 17:21:10 +09:00

50 lines
2.8 KiB
C++

#pragma once
#include <functional>
#include <stdexcept>
#include <string>
#include <vector>
namespace test
{
struct TestCase
{
std::string name;
std::function<void()> fn;
};
std::vector<TestCase> &registry();
struct Registrar
{
Registrar(const char *name, std::function<void()> fn);
};
} // namespace test
#define TEST_CASE(name) \
static void name(); \
static test::Registrar name##_registrar(#name, name); \
static void name()
#define REQUIRE(expr) \
do \
{ \
if (!(expr)) \
{ \
throw std::runtime_error("Requirement failed: " #expr); \
} \
} while (false)
// Layout maths runs in floats, so exact comparison would make the tests fragile
// for reasons that have nothing to do with the behaviour under test.
#define REQUIRE_NEAR(actual, expected) \
do \
{ \
const float _a = static_cast<float>(actual); \
const float _e = static_cast<float>(expected); \
if (!(std::fabs(_a - _e) <= 0.01f)) \
{ \
throw std::runtime_error("Requirement failed: " #actual " ~= " #expected); \
} \
} while (false)