hyprlang/tests/main.cpp

165 lines
8.0 KiB
C++
Raw Normal View History

#include <iostream>
2023-12-29 13:43:36 +01:00
#include <filesystem>
#include <hyprlang.hpp>
2023-12-29 11:22:24 +01:00
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(expr, val) \
if (const auto RESULT = expr; RESULT != (val)) { \
2023-12-29 11:22:24 +01:00
std::cout << Colors::RED << "Failed: " << Colors::RESET << #expr << ", expected " << #val << " but got " << RESULT << "\n"; \
ret = 1; \
} else { \
2023-12-29 11:22:24 +01:00
std::cout << Colors::GREEN << "Passed " << Colors::RESET << #expr << ". Got " << val << "\n"; \
}
2023-12-28 23:25:43 +01:00
// globals for testing
2023-12-29 13:43:36 +01:00
bool barrelRoll = false;
std::string flagsFound = "";
Hyprlang::CConfig* pConfig = nullptr;
std::string currentPath = "";
2023-12-28 23:25:43 +01:00
static Hyprlang::CParseResult handleDoABarrelRoll(const char* COMMAND, const char* VALUE) {
if (std::string(VALUE) == "woohoo, some, params")
barrelRoll = true;
Hyprlang::CParseResult result;
return result;
}
static Hyprlang::CParseResult handleFlagsTest(const char* COMMAND, const char* VALUE) {
std::string cmd = COMMAND;
flagsFound = cmd.substr(5);
Hyprlang::CParseResult result;
return result;
}
2023-12-29 13:43:36 +01:00
static Hyprlang::CParseResult handleSource(const char* COMMAND, const char* VALUE) {
std::string PATH = std::filesystem::canonical(currentPath + "/" + VALUE);
return pConfig->parseFile(PATH);
}
int main(int argc, char** argv, char** envp) {
int ret = 0;
try {
std::cout << "Starting test\n";
Hyprlang::CConfig config("./config/config.conf");
2023-12-29 13:43:36 +01:00
pConfig = &config;
currentPath = std::filesystem::canonical("./config/");
// setup config
config.addConfigValue("testInt", 0L);
config.addConfigValue("testFloat", 0.F);
2023-12-28 21:04:51 +01:00
config.addConfigValue("testVec", Hyprlang::SVector2D{69, 420});
config.addConfigValue("testString", "");
2023-12-29 12:01:01 +01:00
config.addConfigValue("testEnv", "");
2023-12-29 11:40:08 +01:00
config.addConfigValue("testVar", 0L);
config.addConfigValue("testStringQuotes", "");
config.addConfigValue("testCategory:testValueInt", 0L);
config.addConfigValue("testCategory:testValueHex", 0xAL);
config.addConfigValue("testCategory:nested1:testValueNest", 0L);
config.addConfigValue("testCategory:nested1:nested2:testValueNest", 0L);
config.addConfigValue("testDefault", 123L);
2023-12-28 21:23:10 +01:00
config.addConfigValue("testCategory:testColor1", 0L);
config.addConfigValue("testCategory:testColor2", 0L);
config.addConfigValue("testCategory:testColor3", 0L);
2023-12-29 13:43:36 +01:00
config.addConfigValue("myColors:pink", 0L);
config.addConfigValue("myColors:green", 0L);
config.addConfigValue("myColors:random", 0L);
2023-12-28 23:25:43 +01:00
config.registerHandler(&handleDoABarrelRoll, "doABarrelRoll", {false});
config.registerHandler(&handleFlagsTest, "flags", {true});
2023-12-29 13:43:36 +01:00
config.registerHandler(&handleSource, "source", {false});
2023-12-28 23:25:43 +01:00
2023-12-29 13:26:21 +01:00
config.addSpecialCategory("special", {"key"});
config.addSpecialConfigValue("special", "value", 0L);
config.commence();
2023-12-29 13:26:21 +01:00
config.addSpecialCategory("specialGeneric:one", {nullptr, true});
config.addSpecialConfigValue("specialGeneric:one", "value", 0L);
config.addSpecialCategory("specialGeneric:two", {nullptr, true});
config.addSpecialConfigValue("specialGeneric:two", "value", 0L);
const auto PARSERESULT = config.parse();
if (PARSERESULT.error) {
std::cout << "Parse error: " << PARSERESULT.getError() << "\n";
return 1;
}
EXPECT(PARSERESULT.error, false);
// test values
2023-12-29 11:22:24 +01:00
std::cout << " → Testing values\n";
EXPECT(std::any_cast<int64_t>(config.getConfigValue("testInt")), 123);
EXPECT(std::any_cast<float>(config.getConfigValue("testFloat")), 123.456f);
2023-12-28 21:04:51 +01:00
auto EXP = Hyprlang::SVector2D{69, 420};
EXPECT(std::any_cast<Hyprlang::SVector2D>(config.getConfigValue("testVec")), EXP);
EXPECT(std::any_cast<const char*>(config.getConfigValue("testString")), std::string{"Hello World! ## This is not a comment!"});
EXPECT(std::any_cast<const char*>(config.getConfigValue("testStringQuotes")), std::string{"\"Hello World!\""});
EXPECT(std::any_cast<int64_t>(config.getConfigValue("testCategory:testValueInt")), 123456L);
EXPECT(std::any_cast<int64_t>(config.getConfigValue("testCategory:testValueHex")), 0xFFFFAABBL);
EXPECT(std::any_cast<int64_t>(config.getConfigValue("testCategory:nested1:testValueNest")), 1L);
EXPECT(std::any_cast<int64_t>(config.getConfigValue("testCategory:nested1:nested2:testValueNest")), 1L);
EXPECT(std::any_cast<int64_t>(config.getConfigValue("testDefault")), 123L);
2023-12-28 21:23:10 +01:00
EXPECT(std::any_cast<int64_t>(config.getConfigValue("testCategory:testColor1")), 0xFFFFFFFFL);
EXPECT(std::any_cast<int64_t>(config.getConfigValue("testCategory:testColor2")), 0xFF000000L);
EXPECT(std::any_cast<int64_t>(config.getConfigValue("testCategory:testColor3")), 0x22ffeeffL);
2023-12-28 23:25:43 +01:00
// test handlers
2023-12-29 11:22:24 +01:00
std::cout << " → Testing handlers\n";
2023-12-28 23:25:43 +01:00
EXPECT(barrelRoll, true);
EXPECT(flagsFound, std::string{"abc"});
2023-12-29 11:22:24 +01:00
// test dynamic
std::cout << " → Testing dynamic\n";
barrelRoll = false;
EXPECT(config.parseDynamic("doABarrelRoll = woohoo, some, params").error, false);
EXPECT(barrelRoll, true);
EXPECT(config.parseDynamic("testCategory:testValueHex", "0xaabbccdd").error, false);
EXPECT(std::any_cast<int64_t>(config.getConfigValue("testCategory:testValueHex")), 0xAABBCCDDL);
2023-12-29 11:40:08 +01:00
// test variables
std::cout << " → Testing variables\n";
EXPECT(std::any_cast<int64_t>(config.getConfigValue("testVar")), 13371337);
// test dynamic variables
std::cout << " → Testing dynamic variables\n";
EXPECT(config.parseDynamic("$MY_VAR_2 = 420").error, false);
EXPECT(std::any_cast<int64_t>(config.getConfigValue("testVar")), 1337420);
2023-12-29 12:01:01 +01:00
// test env variables
2023-12-29 13:26:21 +01:00
std::cout << " → Testing env variables\n";
2023-12-29 12:05:08 +01:00
EXPECT(std::any_cast<const char*>(config.getConfigValue("testEnv")), std::string{getenv("SHELL")});
2023-12-29 12:01:01 +01:00
2023-12-29 13:26:21 +01:00
// test special categories
std::cout << " → Testing special categories\n";
EXPECT(std::any_cast<int64_t>(config.getSpecialConfigValue("special", "value", "a")), 1);
EXPECT(std::any_cast<int64_t>(config.getSpecialConfigValue("special", "value", "b")), 2);
EXPECT(std::any_cast<int64_t>(config.getSpecialConfigValue("specialGeneric:one", "value")), 1);
EXPECT(std::any_cast<int64_t>(config.getSpecialConfigValue("specialGeneric:two", "value")), 2);
2023-12-29 13:43:36 +01:00
// test sourcing
std::cout << " → Testing sourcing\n";
EXPECT(std::any_cast<int64_t>(config.getConfigValue("myColors:pink")), 0xFFc800c8L);
EXPECT(std::any_cast<int64_t>(config.getConfigValue("myColors:green")), 0xFF14f014L);
EXPECT(std::any_cast<int64_t>(config.getConfigValue("myColors:random")), 0xFFFF1337L);
} catch (const char* e) {
2023-12-29 11:22:24 +01:00
std::cout << Colors::RED << "Error: " << Colors::RESET << e << "\n";
return 1;
}
return ret;
}