/** * @file harness.h * @brief Minimal headless test harness for the T-Deck emulator shims. * * Must be included BEFORE the sketch (it only depends on the shims, never on * sketch internals). Sketch-level helpers (stepSim, units, ...) live in the * test suites themselves, which are included AFTER the sketch. * * Framework: TEST(name) registers a case; EXPECT/EXPECT_EQ assert; the runner * in test_main.cpp / e2e_tests.cpp reports failures and exits non-zero. */ #pragma once #include "Arduino.h" #include "TFT_eSPI.h" // rgb565to888 for pixel assertions #include "emu_gfx.h" #include #include #include #include namespace test { struct Case { const char *name; void (*fn)(); }; inline std::vector &cases() { static std::vector c; return c; } struct Registrar { Registrar(const char *name, void (*fn)()) { cases().push_back({name, fn}); } }; inline int g_failures = 0; inline std::string g_current; // full case name, e.g. "unit/map_queries" inline std::string g_group; // prefix before the first '/' inline void fail(const char *file, int line, const std::string &what) { std::printf(" FAIL [%s/%s] %s:%d %s\n", g_group.c_str(), g_current.c_str(), file, line, what.c_str()); g_failures++; } // --- serial transcript helpers (Serial.sink is set by captureSerial()) --- inline std::string serialJoined() { std::string all; if (Serial.sink) for (const auto &l : *Serial.sink) all += l; return all; } inline bool serialHas(const std::string &pat) { return serialJoined().find(pat) != std::string::npos; } inline void serialClear() { if (Serial.sink) Serial.sink->clear(); } inline void captureSerial() { static std::deque ring; Serial.sink = ˚ } // --- input pipeline helpers (the same state the SDL host writes) --- inline void resetInput() { for (auto &p : emu::pinLevels) p = 1; // everything pulled up while (!emu::keyboard.empty()) emu::keyboard.pop_front(); emu::touchDown = false; emu::touchX = emu::touchY = 0; } // queue a key on the fake 0x55 keyboard device (consumed by pollKeyboard()) inline void typeKey(char c) { emu::keyboard.push_back((uint8_t)c); } // --- framebuffer helpers (E2E pixel assertions) --- inline uint32_t pixel(int x, int y) { if (x < 0 || y < 0 || x >= emuGfx::W || y >= emuGfx::H) return 0; return emuGfx::fb[y * emuGfx::W + x]; } inline bool pixelIs565(int x, int y, uint16_t c565) { return pixel(x, y) == rgb565to888(c565); } } // namespace test // The display name may contain '/' (e.g. "unit/map_queries") — identifiers // are generated from __LINE__, so the name string itself is never pasted. #define TEST_CAT2(a, b) a##b #define TEST_CAT(a, b) TEST_CAT2(a, b) #define TEST(name) \ static void TEST_CAT(test_case_, __LINE__)(); \ static test::Registrar TEST_CAT(reg_case_, __LINE__)( \ #name, TEST_CAT(test_case_, __LINE__)); \ static void TEST_CAT(test_case_, __LINE__)() #define EXPECT(cond) \ do { \ if (!(cond)) test::fail(__FILE__, __LINE__, "expected " #cond); \ } while (0) #define EXPECT_EQ(a, b) \ do { \ if (!((a) == (b))) \ test::fail(__FILE__, __LINE__, \ std::string(#a " == " #b) + " (got: " + \ std::to_string((long long)(a)) + " vs " + \ std::to_string((long long)(b)) + ")"); \ } while (0)