Step 171: theme engine core

This commit is contained in:
Bill
2026-02-09 21:41:58 -07:00
parent 6f62dccf04
commit b0f3a8f4e3
7 changed files with 240 additions and 3 deletions

View File

@@ -0,0 +1,34 @@
// Step 171: Theme engine integration checks.
#include <cassert>
#include <filesystem>
#include <fstream>
#include <string>
namespace fs = std::filesystem;
static std::string readFile(const std::string& path) {
std::ifstream f(path);
if (!f.is_open()) return {};
return std::string((std::istreambuf_iterator<char>(f)),
std::istreambuf_iterator<char>());
}
static void assertContains(const std::string& text, const std::string& needle) {
assert(text.find(needle) != std::string::npos);
}
int main() {
assert(fs::exists("src/ThemeEngine.h"));
const std::string themeEngine = readFile("src/ThemeEngine.h");
assertContains(themeEngine, "ThemeColor");
assertContains(themeEngine, "refreshWatchedThemes");
assertContains(themeEngine, "userThemeDirectory");
const std::string mainCpp = readFile("src/main.cpp");
assertContains(mainCpp, "userThemeDirectory");
assertContains(mainCpp, "refreshWatchedThemes");
printf("step171_integration_test: all assertions passed\n");
return 0;
}

View File

@@ -0,0 +1,68 @@
// Step 171: Theme engine core checks.
#include <cassert>
#include <filesystem>
#include <fstream>
#include <string>
#include <thread>
#include <chrono>
#include "ThemeEngine.h"
namespace fs = std::filesystem;
static void writeFile(const fs::path& path, const std::string& text) {
std::ofstream out(path.string(), std::ios::binary);
out << text;
}
int main() {
ImGui::CreateContext();
ThemeEngine& themes = ThemeEngine::instance();
themes.clear();
const std::string jsonText =
"{"
"\"name\":\"UnitTheme\","
"\"syntax\":{\"keyword\":\"#010203\"},"
"\"editor\":{\"editor_bg\":\"#040506\"}"
"}";
assert(themes.loadThemeFromJson(jsonText));
assert(themes.applyTheme("UnitTheme"));
ImU32 keyword = themes.getColor(ThemeColor::SyntaxKeyword, IM_COL32(0, 0, 0, 255));
ImU32 editorBg = themes.getColor(ThemeColor::EditorBg, IM_COL32(0, 0, 0, 255));
assert(keyword == IM_COL32(1, 2, 3, 255));
assert(editorBg == IM_COL32(4, 5, 6, 255));
fs::path tempDir = fs::current_path() / "test_theme_data";
fs::create_directories(tempDir);
fs::path themeFile = tempDir / "reload.json";
writeFile(themeFile,
"{"
"\"name\":\"ReloadTheme\","
"\"syntax\":{\"keyword\":\"#111111\"}"
"}");
assert(themes.loadThemesFromDirectory(tempDir.string()));
assert(themes.applyTheme("ReloadTheme"));
ImU32 original = themes.getColor(ThemeColor::SyntaxKeyword, IM_COL32(0, 0, 0, 255));
assert(original == IM_COL32(17, 17, 17, 255));
std::this_thread::sleep_for(std::chrono::milliseconds(10));
writeFile(themeFile,
"{"
"\"name\":\"ReloadTheme\","
"\"syntax\":{\"keyword\":\"#222222\"}"
"}");
assert(themes.refreshWatchedThemes());
ImU32 updated = themes.getColor(ThemeColor::SyntaxKeyword, IM_COL32(0, 0, 0, 255));
assert(updated == IM_COL32(34, 34, 34, 255));
std::error_code ec;
fs::remove(themeFile, ec);
fs::remove(tempDir, ec);
ImGui::DestroyContext();
printf("step171_test: all assertions passed\n");
return 0;
}