summaryrefslogtreecommitdiff
path: root/hyprtester/src/shared.hpp
blob: 1090aa9aecd4cdef1badef8308216a11ab30ddcb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Stolen from hyprutils

#pragma once
#include <iostream>

inline std::string HIS          = "";
inline std::string WLDISPLAY    = "";
inline int         TESTS_PASSED = 0;
inline int         TESTS_FAILED = 0;

namespace Colors {
    constexpr const char* RED     = "\x1b[31m";
    constexpr const char* GREEN   = "\x1b[32m";
    constexpr const char* YELLOW  = "\x1b[33m";
    constexpr const char* BLUE    = "\x1b[34m";
    constexpr const char* MAGENTA = "\x1b[35m";
    constexpr const char* CYAN    = "\x1b[36m";
    constexpr const char* RESET   = "\x1b[0m";
};

#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++;                                                                                                                                                            \
    } else {                                                                                                                                                                       \
        NLog::log("{}Passed: {}{}. Got {}", Colors::GREEN, Colors::RESET, #expr, (RESULT - (desired)));                                                                            \
        TESTS_PASSED++;                                                                                                                                                            \
    }

#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++;                                                                                                                                                            \
    }

#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++;                                                                                                                                                        \
        } else {                                                                                                                                                                   \
            NLog::log("{}Passed: {}{}. Got [{}, {}].", Colors::GREEN, Colors::RESET, #expr, RESULT.x, RESULT.y);                                                                   \
            TESTS_PASSED++;                                                                                                                                                        \
        }                                                                                                                                                                          \
    } while (0)

#define EXPECT_CONTAINS(haystack, needle)                                                                                                                                          \
    if (const auto EXPECTED = needle; !std::string{haystack}.contains(EXPECTED)) {                                                                                                 \
        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++;                                                                                                                                                            \
    } else {                                                                                                                                                                       \
        NLog::log("{}Passed: {}{} contains {}.", Colors::GREEN, Colors::RESET, #haystack, EXPECTED);                                                                               \
        TESTS_PASSED++;                                                                                                                                                            \
    }

#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++;                                                                                                                                                            \
    } else {                                                                                                                                                                       \
        NLog::log("{}Passed: {}{} doesn't contain {}.", Colors::GREEN, Colors::RESET, #haystack, #needle);                                                                         \
        TESTS_PASSED++;                                                                                                                                                            \
    }

#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++;                                                                                                                                                            \
    } else {                                                                                                                                                                       \
        NLog::log("{}Passed: {}{} starts with {}.", Colors::GREEN, Colors::RESET, #str, #what);                                                                                    \
        TESTS_PASSED++;                                                                                                                                                            \
    }

#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++;                                                                                                                                                            \
    } else {                                                                                                                                                                       \
        NLog::log("{}Passed: {}{} contains {} {} times.", Colors::GREEN, Colors::RESET, #str, #what, no);                                                                          \
        TESTS_PASSED++;                                                                                                                                                            \
    }

#define OK(x) EXPECT(x, "ok")