test(tests): brought testing framework for logging from SERiF
This commit is contained in:
@@ -24,6 +24,7 @@ project('liblogging', 'cpp', version: '0.0.1a', default_options: ['cpp_std=c++23
|
|||||||
add_project_arguments('-fvisibility=default', language: 'cpp')
|
add_project_arguments('-fvisibility=default', language: 'cpp')
|
||||||
subdir('build-config')
|
subdir('build-config')
|
||||||
subdir('src')
|
subdir('src')
|
||||||
|
subdir('tests')
|
||||||
|
|
||||||
pkg = import('pkgconfig')
|
pkg = import('pkgconfig')
|
||||||
pkg.generate(
|
pkg.generate(
|
||||||
|
|||||||
16
subprojects/gtest.wrap
Normal file
16
subprojects/gtest.wrap
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
[wrap-file]
|
||||||
|
directory = googletest-1.15.2
|
||||||
|
source_url = https://github.com/google/googletest/archive/refs/tags/v1.15.2.tar.gz
|
||||||
|
source_filename = gtest-1.15.2.tar.gz
|
||||||
|
source_hash = 7b42b4d6ed48810c5362c265a17faebe90dc2373c885e5216439d37927f02926
|
||||||
|
patch_filename = gtest_1.15.2-2_patch.zip
|
||||||
|
patch_url = https://wrapdb.mesonbuild.com/v2/gtest_1.15.2-2/get_patch
|
||||||
|
patch_hash = 641a16b33c96cd32a593537bc30eb7d853c5cc361fa1ee96884f0e2fca21e2d3
|
||||||
|
source_fallback_url = https://github.com/mesonbuild/wrapdb/releases/download/gtest_1.15.2-2/gtest-1.15.2.tar.gz
|
||||||
|
wrapdb_version = 1.15.2-2
|
||||||
|
|
||||||
|
[provide]
|
||||||
|
gtest = gtest_dep
|
||||||
|
gtest_main = gtest_main_dep
|
||||||
|
gmock = gmock_dep
|
||||||
|
gmock_main = gmock_main_dep
|
||||||
82
tests/logging/loggingTest.cpp
Normal file
82
tests/logging/loggingTest.cpp
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
#include <set>
|
||||||
|
#include <sstream>
|
||||||
|
#include <fstream>
|
||||||
|
#include <regex>
|
||||||
|
#include "quill/LogMacros.h"
|
||||||
|
|
||||||
|
#include "logging.h"
|
||||||
|
|
||||||
|
std::string getLastLine(const std::string& filename) {
|
||||||
|
std::ifstream file(filename);
|
||||||
|
std::string line, lastLine;
|
||||||
|
|
||||||
|
if (!file.is_open()) {
|
||||||
|
throw std::runtime_error("Could not open file");
|
||||||
|
}
|
||||||
|
|
||||||
|
while (std::getline(file, line)) {
|
||||||
|
lastLine = line;
|
||||||
|
}
|
||||||
|
|
||||||
|
return lastLine; // Returns the last non-empty line
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string stripTimestamps(const std::string& logLine) {
|
||||||
|
std::regex logPattern(R"(\d+:\d+:\d+\.\d+\s+\[\d+\]\s+loggingTest\.cpp:\d+\s+LOG_INFO\s+[A-Za-z]*\s+(.*))");
|
||||||
|
std::smatch match;
|
||||||
|
if (std::regex_match(logLine, match, logPattern) && match.size() > 1) {
|
||||||
|
return match[1].str(); // Extract log message after timestamp
|
||||||
|
}
|
||||||
|
return logLine; // Return as-is if pattern doesn't match
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class loggingTest : public ::testing::Test {};
|
||||||
|
|
||||||
|
TEST_F(loggingTest, DefaultConstructorTest) {
|
||||||
|
EXPECT_NO_THROW(fourdst::logging::LogManager::getInstance());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(loggingTest, getLoggerTest) {
|
||||||
|
fourdst::logging::LogManager& logManager = fourdst::logging::LogManager::getInstance();
|
||||||
|
const std::string loggerName = "testLog";
|
||||||
|
const std::string filename = "test.log";
|
||||||
|
quill::Logger* logger = logManager.newFileLogger(filename, loggerName);
|
||||||
|
EXPECT_NE(logger, nullptr);
|
||||||
|
LOG_INFO(logger, "This is a test message");
|
||||||
|
// Wait for the log to be written by calling getLastLine until it is non empty
|
||||||
|
std::string lastLine;
|
||||||
|
while (lastLine.empty()) {
|
||||||
|
lastLine = getLastLine("test.log");
|
||||||
|
}
|
||||||
|
EXPECT_EQ(stripTimestamps(lastLine), "This is a test message");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(loggingTest, newFileLoggerTest) {
|
||||||
|
fourdst::logging::LogManager& logManager = fourdst::logging::LogManager::getInstance();
|
||||||
|
const std::string loggerName = "newLog";
|
||||||
|
const std::string filename = "newLog.log";
|
||||||
|
quill::Logger* logger = logManager.newFileLogger(filename, loggerName);
|
||||||
|
EXPECT_NE(logger, nullptr);
|
||||||
|
LOG_INFO(logger, "This is a new test message");
|
||||||
|
// Wait for the log to be written by calling getLastLine until it is non empty
|
||||||
|
std::string lastLine;
|
||||||
|
while (lastLine.empty()) {
|
||||||
|
lastLine = getLastLine(filename);
|
||||||
|
}
|
||||||
|
EXPECT_EQ(stripTimestamps(lastLine), "This is a new test message");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(loggingTest, getLoggerNames) {
|
||||||
|
fourdst::logging::LogManager& logManager = fourdst::logging::LogManager::getInstance();
|
||||||
|
std::vector<std::string> loggerNames = logManager.getLoggerNames();
|
||||||
|
EXPECT_EQ(loggerNames.size(), 4);
|
||||||
|
EXPECT_EQ(loggerNames.at(0), "log");
|
||||||
|
EXPECT_EQ(loggerNames.at(1), "newLog");
|
||||||
|
EXPECT_EQ(loggerNames.at(2), "stdout");
|
||||||
|
EXPECT_EQ(loggerNames.at(3), "testLog");
|
||||||
|
}
|
||||||
23
tests/logging/meson.build
Normal file
23
tests/logging/meson.build
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
# Test files for dobj
|
||||||
|
test_sources = [
|
||||||
|
'loggingTest.cpp',
|
||||||
|
]
|
||||||
|
|
||||||
|
foreach test_file : test_sources
|
||||||
|
exe_name = test_file.split('.')[0]
|
||||||
|
message('Building test: ' + exe_name)
|
||||||
|
|
||||||
|
# Create an executable target for each test
|
||||||
|
test_exe = executable(
|
||||||
|
exe_name,
|
||||||
|
test_file,
|
||||||
|
dependencies: [gtest_dep, logging_dep, quill_dep, gtest_main],
|
||||||
|
install_rpath: '@loader_path/../../src' # Ensure runtime library path resolves correctly
|
||||||
|
)
|
||||||
|
|
||||||
|
# Add the executable as a test
|
||||||
|
test(
|
||||||
|
exe_name,
|
||||||
|
test_exe,
|
||||||
|
env: ['MESON_SOURCE_ROOT=' + meson.project_source_root(), 'MESON_BUILD_ROOT=' + meson.project_build_root()])
|
||||||
|
endforeach
|
||||||
7
tests/meson.build
Normal file
7
tests/meson.build
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
# Google Test dependency
|
||||||
|
gtest_dep = dependency('gtest', main: true, required : true)
|
||||||
|
gtest_main = dependency('gtest_main', required: true)
|
||||||
|
gtest_nomain_dep = dependency('gtest', main: false, required : true)
|
||||||
|
|
||||||
|
# Subdirectories for unit and integration tests
|
||||||
|
subdir('logging')
|
||||||
Reference in New Issue
Block a user