summaryrefslogtreecommitdiff
path: root/hyprtester/src/shared.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'hyprtester/src/shared.hpp')
-rw-r--r--hyprtester/src/shared.hpp235
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")