From b117f3cd4d3c55a1fc18857ea37cb7cd59b2e92e Mon Sep 17 00:00:00 2001 From: maelstrom Date: Thu, 24 Jul 2025 22:57:00 +0200 Subject: [PATCH] feat(test): some few lua testing --- CMakeLists.txt | 6 +++--- core/src/logger.cpp | 12 ++++++++++++ core/src/logger.h | 3 +++ tests/CMakeLists.txt | 7 ++++++- tests/src/luatest.cpp | 34 ++++++++++++++++++++++++++++++++++ tests/src/testutil.h | 19 +++++++++++++++++++ 6 files changed, 77 insertions(+), 4 deletions(-) create mode 100644 tests/src/luatest.cpp create mode 100644 tests/src/testutil.h diff --git a/CMakeLists.txt b/CMakeLists.txt index d684f17..439a7f8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,15 +14,15 @@ set(USE_CCACHE ON) add_subdirectory(autogen) -enable_testing() -add_subdirectory(tests) - set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin ) set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib ) set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib ) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) +enable_testing() +add_subdirectory(tests) + add_subdirectory(core) add_subdirectory(client) add_subdirectory(editor) diff --git a/core/src/logger.cpp b/core/src/logger.cpp index 173860e..1cfe114 100644 --- a/core/src/logger.cpp +++ b/core/src/logger.cpp @@ -10,6 +10,7 @@ static std::ofstream logStream; static std::vector logListeners; std::string Logger::currentLogDir = "NULL"; +static std::stringstream* rawOutputBuffer = nullptr; void Logger::init() { initProgramLogsDir(); @@ -22,6 +23,12 @@ void Logger::init() { Logger::debug("Logger initialized"); } +// Initializes the logger in a "void" mode for testing. +// It is not necessary to call Logger::finish +void Logger::initTest(std::stringstream* outputBuffer) { + rawOutputBuffer = outputBuffer; +} + void Logger::finish() { Logger::debug("Closing logger..."); logStream.close(); @@ -41,6 +48,7 @@ void Logger::log(std::string message, Logger::LogLevel logLevel, ScriptSource so logStream << formattedLogLine << std::endl; printf("%s\n", formattedLogLine.c_str()); + if (rawOutputBuffer != nullptr) *rawOutputBuffer << logLevelStr << ": " << message << "\n"; for (Logger::LogListener listener : logListeners) { listener(logLevel, message, source); @@ -53,4 +61,8 @@ void Logger::log(std::string message, Logger::LogLevel logLevel, ScriptSource so void Logger::addLogListener(Logger::LogListener listener) { logListeners.push_back(listener); +} + +void Logger::resetLogListeners() { + logListeners.clear(); } \ No newline at end of file diff --git a/core/src/logger.h b/core/src/logger.h index dc19355..0833c9c 100644 --- a/core/src/logger.h +++ b/core/src/logger.h @@ -2,6 +2,7 @@ #include #include +#include #include class Script; @@ -26,8 +27,10 @@ namespace Logger { extern std::string currentLogDir; void init(); + void initTest(std::stringstream* out); // Testing only! void finish(); void addLogListener(LogListener); + void resetLogListeners(); // Testing only! void log(std::string message, LogLevel logLevel, ScriptSource source = {}); inline void info(std::string message) { log(message, LogLevel::INFO); } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 3dd3389..69f25ef 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -4,4 +4,9 @@ function (create_test TEST_NAME) target_link_libraries(${TARGET_NAME} PRIVATE openblocks) add_dependencies(${TARGET_NAME} openblocks) add_test(NAME ${TARGET_NAME} COMMAND ${TARGET_NAME}) -endfunction () \ No newline at end of file +endfunction () + +create_test(lua src/luatest.cpp) + +# https://stackoverflow.com/a/36729074/16255372 +add_custom_target(check ${CMAKE_CTEST_COMMAND} --output-on-failure WORKING_DIRECTORY ${CMAKE_BINARY_DIR}) \ No newline at end of file diff --git a/tests/src/luatest.cpp b/tests/src/luatest.cpp new file mode 100644 index 0000000..d6a7e27 --- /dev/null +++ b/tests/src/luatest.cpp @@ -0,0 +1,34 @@ +#include "testutil.h" + +#include "logger.h" +#include "objects/datamodel.h" +#include "objects/script.h" +#include +#include + +std::string luaEval(DATAMODEL_REF m, std::string source) { + std::stringstream out; + Logger::initTest(&out); + + auto s = Script::New(); + m->AddChild(s); + s->source = source; + s->Run(); + + return out.str(); +} + +void test_output(DATAMODEL_REF m) { + ASSERT_EQ("INFO: Hello, world!\n", luaEval(m, "print('Hello, world!')")); + // ASSERT_EQ("WARN: Some warning here.\n", luaEval(m, "warn('Some warning here.')")); + // ASSERT_EQ("ERROR: An error!.\n", luaEval(m, "error('An error!')")); +} + +int main() { + auto m = DataModel::New(); + m->Init(true); + + test_output(m); + + return TEST_STATUS; +} \ No newline at end of file diff --git a/tests/src/testutil.h b/tests/src/testutil.h new file mode 100644 index 0000000..1edd9eb --- /dev/null +++ b/tests/src/testutil.h @@ -0,0 +1,19 @@ +#pragma once + +// https://bastian.rieck.me/blog/2017/simple_unit_tests/ + +#ifdef __FUNCTION__ +#define ASSERT(x, msg) if (!(x)) { fprintf(stderr, "ASSERT FAILED : %s:%d : %s : '%s'\n", __FILE__, __LINE__, __FUNCTION__, msg); exit(1); TEST_STATUS = 1; } +#else +#define ASSERT(x, msg) if (!(x)) { fprintf(stderr, "ASSERT FAILED : %s:%d : ?? : '%s'\n", __FILE__, __LINE__, msg); TEST_STATUS = 1; } +#endif + +#define ASSERT_EQ(x, y) ASSERT(x == y, #x " != " #y) +#define ASSERT_EQSTR(x, y) ASSERT(strcmp(x, y) == 0, #x " != " #y) + +#define DATAMODEL_REF std::shared_ptr + +#include +#include + +int TEST_STATUS = 0; \ No newline at end of file