diff options
| author | Nikolai Nechaev <nikolay_nechaev@mail.ru> | 2026-04-28 19:17:12 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-04-28 11:17:12 +0100 |
| commit | d4c6ff434c5c7aa66bc7c28e5ce336d2a694bfcc (patch) | |
| tree | 3813063b81dc435177baba35ffdb655d4e3bd154 /hyprtester/src/shared.hpp | |
| parent | e1bda229bdf070f1eed01858f862ca975783df71 (diff) | |
hyprtester: minor refactoring/restructure (#14154)
* hyprtester: Minor misc fixes
- Use `std::filesystem::path` instead of `std::string` in some places;
- Use `std::ranges::sort`, as suggested by clang-tidy;
- Reset colors at the end of every log line;
- Give more specific error messages in main.
* hyprtester: Fix a bug in `hyprlandAlive()`
Successful standard library calls are permitted to (and do) modify the
value of `errno` arbitrarily. Checking the value of `errno` only makes
sense when the library function returns an error. So, the correct
condition of a successful signal delivery is that the call to `kill`
returns 0.
Btw, given the possible other error values of `kill(2)`, should we even
consider `errno` at all?
* hyprtester: Restructure main
Split main's `main()` into multiple functions, separate responsibilities
more cleanly.
Add default values for cmd-line options to the help message.
* hyprtester: Remove redundant path canonicalization
* hyprtester: Run unified clean up code before every test
* tests: Use RAII for clients in client tests
Turn clients in client tests into classes. Constructors
initialize, destructors deinitialize.
Not only is it better design when entities (clients) are
modeled as classes, keeping all the methods connected,
this also ensures the tests perform their custom cleanup.
This will be especially important when we introduce early
termination on failure into the tests.
* hyprtester: Rewrite the testing framework!
- Implement a new test definition system used via the `TEST_CASE` and
`SUBTEST` macros.
- Adds `ASSERT*` and reimplements `EXPECT*` macros. The former now
terminate the test immediately. Also add some new macros, such as
`FAIL_TEST`.
- Move existing tests to the new system. For trivial cases, break
existing test functions down to multiple test cases.
- For non-trivial cases and a few other possible improvements, leave
TODOs.
* tests: Use `EXPECT*` instead of `ASSERT*` in some places
`ASSERT*` macros terminate the test as soon as they fail. They are
appropriate to use when, if something fails, the rest of the test
does not make sense and more error messages from failing checks will
just pollute the output.
`EXPECT*` macros mark the test as failed but its execution continues.
It makes sense to use them when error messages from the following checks
in the test may be useful for someone who will be reading the log.
I changed some uses of `ASSERT*` in main tests to `EXPECT*`. But I was
not reading tests very carefully, so maybe I was wrong in some places.
My rule of thumb was:
- Checks that ensure that a kitty opens / a certain set/number of
windows exists are fatal;
- A series of checks, especially on the same string returned by
`Hyprland`, are non-fatal;
- Checks in tests with tightly coupled steps (i.e., do this, check,
do this with the result of the previous step, check, etc) are fatal.
Diffstat (limited to 'hyprtester/src/shared.hpp')
| -rw-r--r-- | hyprtester/src/shared.hpp | 235 |
1 files changed, 185 insertions, 50 deletions
diff --git a/hyprtester/src/shared.hpp b/hyprtester/src/shared.hpp index 941788fd..d40b2f1f 100644 --- a/hyprtester/src/shared.hpp +++ b/hyprtester/src/shared.hpp @@ -1,12 +1,12 @@ // Stolen from hyprutils #pragma once -#include <iostream> +#include <cmath> +#include <string> -inline std::string HIS = ""; -inline std::string WLDISPLAY = ""; -inline int TESTS_PASSED = 0; -inline int TESTS_FAILED = 0; +// TODO: localize these global variables +inline std::string HIS = ""; +inline std::string WLDISPLAY = ""; namespace Colors { constexpr const char* RED = "\x1b[31m"; @@ -18,94 +18,229 @@ namespace Colors { constexpr const char* RESET = "\x1b[0m"; }; +// ================================= +// TEST CASES DEFINITION +// ================================= + +class CTestCase { + public: + CTestCase() = default; + CTestCase(const CTestCase&) = delete; // Test cases probably should not be copied + bool failed = false; + virtual ~CTestCase() = default; + + // TODO: `test` will be protected + virtual void test() = 0; +}; + +#define TEST_CASE(name) \ + namespace { \ + class TestCase_##name : public CTestCase { \ + public: \ + void test() override; \ + }; \ + } \ + \ + static TestCase_##name test_case_##name{}; \ + static auto register_test_case_##name = [] { \ + /* `TEST_CASES_STORAGE` must be defined by the caller */ \ + TEST_CASES_STORAGE.emplace(#name, test_case_##name); \ + return 1; \ + }(); \ + \ + void TestCase_##name::test() + +#define SUBTEST(name, ...) \ + namespace { \ + class Subtest_##name { \ + public: \ + bool failed = false; \ + \ + void main(__VA_ARGS__); \ + }; \ + } \ + \ + void Subtest_##name::main(__VA_ARGS__) + +#define CALL_SUBTEST(name, ...) \ + do { \ + auto subtest_##name = Subtest_##name{}; \ + subtest_##name.main(__VA_ARGS__); \ + if (subtest_##name.failed) { \ + NLog::log("{}Subtest {}({}) failed", Colors::RED, #name, #__VA_ARGS__); \ + this->failed = true; \ + return; \ + } \ + } while (0) + +// ================================= +// IN-TEST MACROS +// ================================= + +/// Marks the test as failed without terminating it +#define MARK_TEST_FAILED_SILENT() this->failed = true + +/// Prints a failure message and makrs the test as failed without terminating it +#define MARK_TEST_FAILED(fmt, ...) \ + do { \ + NLog::log("{}Failed:{} " fmt ". Source: {}@{}.", Colors::RED, Colors::RESET __VA_OPT__(, ) __VA_ARGS__, __FILE__, __LINE__); \ + MARK_TEST_FAILED_SILENT(); \ + } while (0) + +/// Terminates the test execution and marks it as failed +#define FAIL_TEST_SILENT() \ + do { \ + MARK_TEST_FAILED_SILENT(); \ + return; \ + } while (0) + +/// Prints a failure message, terminates the test execution, and marks it as failed +#define FAIL_TEST(fmt, ...) \ + do { \ + MARK_TEST_FAILED(fmt, __VA_ARGS__); \ + return; \ + } while (0) + +#define LOG_OK(fmt, ...) \ + do { \ + NLog::log("{}OK:{} " fmt ". Source: {}@{}", Colors::GREEN, Colors::RESET, __VA_ARGS__, __FILE__, __LINE__); \ + } while (0) + +// In case of failure: +// - All the `EXPECT*` macros will print a message and mark the test as failed without terminating its execution; +// - All the `ASSERT*` macros will print a message, mark the test as failed, and terminate it. +// +// `OK` acts like `ASSERT_OK`. + #define EXPECT_MAX_DELTA(expr, desired, delta) \ if (const auto RESULT = expr; std::abs(RESULT - (desired)) > delta) { \ - NLog::log("{}Failed: {}{}, expected max delta of {}, got delta {} ({} - {}). Source: {}@{}.", Colors::RED, Colors::RESET, #expr, delta, (RESULT - (desired)), RESULT, \ - desired, __FILE__, __LINE__); \ - ret = 1; \ - TESTS_FAILED++; \ + MARK_TEST_FAILED("{}, expected max delta of {}, got delta {} ({} - {})", #expr, delta, (RESULT - (desired)), RESULT, desired); \ } else { \ - NLog::log("{}Passed: {}{}. Got {}", Colors::GREEN, Colors::RESET, #expr, (RESULT - (desired))); \ - TESTS_PASSED++; \ + LOG_OK("{}. Got {}", #expr, (RESULT - (desired))); \ } #define EXPECT(expr, val) \ - if (const auto RESULT = expr; RESULT != (val)) { \ - NLog::log("{}Failed: {}{}, expected {}, got {}. Source: {}@{}.", Colors::RED, Colors::RESET, #expr, val, RESULT, __FILE__, __LINE__); \ - ret = 1; \ - TESTS_FAILED++; \ - } else { \ - NLog::log("{}Passed: {}{}. Got {}", Colors::GREEN, Colors::RESET, #expr, val); \ - TESTS_PASSED++; \ - } + do { \ + if (const auto RESULT = expr; RESULT != (val)) { \ + MARK_TEST_FAILED("{}, expected {}, got {}", #expr, val, RESULT); \ + } else { \ + LOG_OK("{}. Got {}", #expr, val); \ + } \ + } while (0) #define EXPECT_NOT(expr, val) \ if (const auto RESULT = expr; RESULT == (val)) { \ - NLog::log("{}Failed: {}{}, expected not {}, got {}. Source: {}@{}.", Colors::RED, Colors::RESET, #expr, val, RESULT, __FILE__, __LINE__); \ - ret = 1; \ - TESTS_FAILED++; \ + MARK_TEST_FAILED("{}, expected not {}, got {}", #expr, val, RESULT); \ } else { \ - NLog::log("{}Passed: {}{}. Got {}", Colors::GREEN, Colors::RESET, #expr, val); \ - TESTS_PASSED++; \ + LOG_OK("{}. Got {}", #expr, val); \ } #define EXPECT_VECTOR2D(expr, val) \ do { \ const auto& RESULT = expr; \ - const auto& EXPECTED = val; \ - if (!(std::abs(RESULT.x - EXPECTED.x) < 1e-6 && std::abs(RESULT.y - EXPECTED.y) < 1e-6)) { \ - NLog::log("{}Failed: {}{}, expected [{}, {}], got [{}, {}]. Source: {}@{}.", Colors::RED, Colors::RESET, #expr, EXPECTED.x, EXPECTED.y, RESULT.x, RESULT.y, __FILE__, \ - __LINE__); \ - ret = 1; \ - TESTS_FAILED++; \ + const auto& ASSERTED = val; \ + if (!(std::abs(RESULT.x - ASSERTED.x) < 1e-6 && std::abs(RESULT.y - ASSERTED.y) < 1e-6)) { \ + MARK_TEST_FAILED("{}, expected [{}, {}], got [{}, {}]", #expr, ASSERTED.x, ASSERTED.y, RESULT.x, RESULT.y); \ } else { \ - NLog::log("{}Passed: {}{}. Got [{}, {}].", Colors::GREEN, Colors::RESET, #expr, RESULT.x, RESULT.y); \ - TESTS_PASSED++; \ + LOG_OK("{}. Got [{}, {}].", #expr, RESULT.x, RESULT.y); \ } \ } while (0) +// String check macros below use a special error message layout, putting the Source reference before `haystack`, +// since `haystack` may be a large multi-line string. Thus, they use bare `NLog::log` + `MARK_TEST_FAILED_SILENT`. + #define EXPECT_CONTAINS(haystack, needle) \ - if (const auto EXPECTED = needle; !std::string{haystack}.contains(EXPECTED)) { \ + if (const auto ASSERTED = needle; !std::string{haystack}.contains(ASSERTED)) { \ NLog::log("{}Failed: {}{} should contain {} but doesn't. Source: {}@{}. Haystack is:\n{}", Colors::RED, Colors::RESET, #haystack, #needle, __FILE__, __LINE__, \ std::string{haystack}); \ - ret = 1; \ - TESTS_FAILED++; \ + MARK_TEST_FAILED_SILENT(); \ } else { \ - NLog::log("{}Passed: {}{} contains {}.", Colors::GREEN, Colors::RESET, #haystack, EXPECTED); \ - TESTS_PASSED++; \ + LOG_OK("{} contains {}.", #haystack, ASSERTED); \ } #define EXPECT_NOT_CONTAINS(haystack, needle) \ if (std::string{haystack}.contains(needle)) { \ NLog::log("{}Failed: {}{} shouldn't contain {} but does. Source: {}@{}. Haystack is:\n{}", Colors::RED, Colors::RESET, #haystack, #needle, __FILE__, __LINE__, \ std::string{haystack}); \ - ret = 1; \ - TESTS_FAILED++; \ + MARK_TEST_FAILED_SILENT(); \ } else { \ - NLog::log("{}Passed: {}{} doesn't contain {}.", Colors::GREEN, Colors::RESET, #haystack, #needle); \ - TESTS_PASSED++; \ + LOG_OK("{} doesn't contain {}.", #haystack, #needle); \ } #define EXPECT_STARTS_WITH(str, what) \ if (!std::string{str}.starts_with(what)) { \ NLog::log("{}Failed: {}{} should start with {} but doesn't. Source: {}@{}. String is:\n{}", Colors::RED, Colors::RESET, #str, #what, __FILE__, __LINE__, \ std::string{str}); \ - ret = 1; \ - TESTS_FAILED++; \ + MARK_TEST_FAILED_SILENT(); \ } else { \ - NLog::log("{}Passed: {}{} starts with {}.", Colors::GREEN, Colors::RESET, #str, #what); \ - TESTS_PASSED++; \ + LOG_OK("{} starts with {}.", #str, #what); \ } #define EXPECT_COUNT_STRING(str, what, no) \ if (Tests::countOccurrences(str, what) != no) { \ NLog::log("{}Failed: {}{} should contain {} {} times, but doesn't. Source: {}@{}. String is:\n{}", Colors::RED, Colors::RESET, #str, #what, no, __FILE__, __LINE__, \ std::string{str}); \ - ret = 1; \ - TESTS_FAILED++; \ + MARK_TEST_FAILED_SILENT(); \ } else { \ - NLog::log("{}Passed: {}{} contains {} {} times.", Colors::GREEN, Colors::RESET, #str, #what, no); \ - TESTS_PASSED++; \ + LOG_OK("{} contains {} {} times.", #str, #what, no); \ } -#define OK(x) EXPECT(x, "ok") +#define EXPECT_OK(x) EXPECT(x, "ok") + +#define ASSERT_MAX_DELTA(expr, desired, delta) \ + do { \ + EXPECT_MAX_DELTA(expr, desired, delta); \ + if (this->failed) \ + return; \ + } while (0) + +#define ASSERT(expr, val) \ + do { \ + EXPECT(expr, val); \ + if (this->failed) \ + return; \ + } while (0) + +#define ASSERT_NOT(expr, val) \ + do { \ + EXPECT_NOT(expr, val); \ + if (this->failed) \ + return; \ + } while (0) + +#define ASSERT_VECTOR2D(expr, val) \ + do { \ + EXPECT_VECTOR2D(expr, val); \ + if (this->failed) \ + return; \ + } while (0) + +#define ASSERT_CONTAINS(haystack, needle) \ + do { \ + EXPECT_CONTAINS(haystack, needle); \ + if (this->failed) \ + return; \ + } while (0) + +#define ASSERT_NOT_CONTAINS(haystack, needle) \ + do { \ + EXPECT_NOT_CONTAINS(haystack, needle); \ + if (this->failed) \ + return; \ + } while (0) + +#define ASSERT_STARTS_WITH(str, what) \ + do { \ + EXPECT_STARTS_WITH(str, what); \ + if (this->failed) \ + return; \ + } while (0) + +#define ASSERT_COUNT_STRING(str, what, no) \ + do { \ + EXPECT_COUNT_STRING(str, what, no); \ + if (this->failed) \ + return; \ + } while (0) + +#define OK(x) ASSERT(x, "ok") |
