feat(test): some few lua testing
This commit is contained in:
parent
d086cf629b
commit
b117f3cd4d
6 changed files with 77 additions and 4 deletions
|
@ -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)
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
static std::ofstream logStream;
|
||||
static std::vector<Logger::LogListener> 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();
|
||||
}
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <ostream>
|
||||
#include <string>
|
||||
|
||||
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); }
|
||||
|
|
|
@ -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 ()
|
||||
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})
|
34
tests/src/luatest.cpp
Normal file
34
tests/src/luatest.cpp
Normal file
|
@ -0,0 +1,34 @@
|
|||
#include "testutil.h"
|
||||
|
||||
#include "logger.h"
|
||||
#include "objects/datamodel.h"
|
||||
#include "objects/script.h"
|
||||
#include <memory>
|
||||
#include <sstream>
|
||||
|
||||
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;
|
||||
}
|
19
tests/src/testutil.h
Normal file
19
tests/src/testutil.h
Normal file
|
@ -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<DataModel>
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
|
||||
int TEST_STATUS = 0;
|
Loading…
Add table
Reference in a new issue